Right now, when running my Rational program I get a 1 when creating a new rational with the numerator being 2 and denominator being 0, and my teacher wants me to replace the denominator with a 1 instead of a 0 but I am unable to and get a stack overflow error whenever trying to add an if in the reduce method that is used to reduce the fractions to their simplest form. I have also tried adding an if statement to the constructor where the rational object containing the numerator and denominator are, but still to no avail. I also have a similar problem when a rational is created with a 0 numerator and 2 in the denominator. It is supposed to return 0/1, but 0 is returned. I believe these two are related, does anyone know what's going on here?
public Rational(int numerator, int denominator) {
int gcd = reduce(numerator, denominator);
num = numerator / gcd;
denom = denominator / gcd;
if(denom == 0)
{
denom = 1;
}
}
private static int reduce(int numerator, int denominator) {
if(denominator == 0) numerator = 1;
return reduce(denominator, numerator % denominator);
}