Help rewriting constructors!!
Can anyone help me rewrite these constructors to be less repetitive!?
public Q (String s){
String rational[] = s.split("/");
Z numz, denomz;
if(rational.length == 1){
numz =new Z(rational[0]);
denomz = Z.ONE;
}
else{
if(rational.length == 2){
numz =new Z(rational[0]);
denomz =new Z(rational[1]);
}
else{
thrownew RuntimeException("Cannot make a Q from " + s);
}
}
if(denomz.equals(Z.ZERO)){
this.num = Z.UNDEFINED;
this.denom = Z.UNDEFINED;
}
else{
if(numz.equals(Z.ZERO)){
this.num = Z.ZERO;
this.denom = Z.ONE;
}
else
{
Z gcd = numz.gcd(denomz);
numz = numz.dividedBy(gcd);
denomz = denomz.dividedBy(gcd);
if(!(denomz.sign() == numz.sign()))
{
numz =new Z(-1, numz.magnitude());
denomz =new Z(1, denomz.magnitude());
}
this.num = numz;
this.denom = denomz;
}
}
}
public Q(Z numz, Z denomz){
if(denomz.equals(Z.ZERO)){
this.num = Z.UNDEFINED;
this.denom = Z.UNDEFINED;
}
else{
if(numz.equals(Z.ZERO)){
this.num = Z.ZERO;
this.denom = Z.ONE;
}
else{
Z gcd = numz.gcd(denomz);
numz = numz.dividedBy(gcd);
denomz = denomz.dividedBy(gcd);
if(!(denomz.sign() == numz.sign())){
numz =new Z(-1, numz.magnitude());
denomz =new Z(1, denomz.magnitude());
}
this.num = numz;
this.denom = denomz;
}
}
}

