1

My Rational Class is supposed to add, subtract, multiply, and divide fractions so when called in the Main Class, it will perform these actions. However, I can't seem to reduce my fractions (gcd) properly to get the right answer and the division method isn't running at all (I get a error message):

Exception in thread "main" java.lang.ArithmeticException: / by zero at Rational.division(Project1.java:45) at Project1.main(Project1.java:12)

How can I fix these problems? Thanks

public class Project1 {

public static void main(String[] args) {

    Rational a = new Rational (6, 12);
    Rational b = new Rational (8, 9);

    System.out.println(a + " + " + b + " = " + a.add(b));
    System.out.println(a + " - " + b + " = " + a.subtraction(b));
    System.out.println(a + " * " + b + " = " + a.multiply(b));
    System.out.println(a + " / " + b + " = " + a.division(b));
}

}

class Rational {

private int numerator, denominator;

public Rational(int num, int denom) {
    numerator = num;
    denominator = denom;
}
public Rational gcd() {
    while (numerator != denominator)
    {
        if (numerator > denominator)
            numerator = numerator - denominator;
        else
            denominator = denominator - numerator;
    }
    return new Rational(numerator, denominator);
}

public Rational add(Rational o) {
    return new Rational (numerator + o.numerator, denominator +o.denominator);
}

public Rational conjugate() {
    return new Rational (numerator, - denominator);
}

public Rational division(Rational o) {
    return new Rational ((numerator/denominator) / (numerator/denominator), denominator);
}

public Rational multiply(Rational o) {
    return new Rational (numerator * o.numerator, denominator * o.denominator);
}

public Rational subtraction(Rational o) {
    return new Rational (numerator - o.numerator, denominator - o.denominator);
}

public String toString() {
    return (numerator + "/" + denominator);
}

}

solorzke
  • 43
  • 1
  • 4

3 Answers3

1

The problem you have is here:

public Rational division(Rational o) {
    return new Rational ((numerator/denominator) / (numerator/denominator), denominator);
}

When you run your code you do:

(6/12)/(6/12)

Both (6/12) 's in the brackets are downcasted to 0, and hence you end up diving by 0 which isn't allowed.

I'd recommend debugging code with System.out.println() to see where you go wrong. But the error is pretty clear, you're dividing by 0.

Brunaldo
  • 324
  • 3
  • 11
0

I would change the logic in your division method. You don't really even need to divide to calculate a fraction, which would avoid the situation where you are dividing by zero. Instead I would do this to divide two fractions:

return new Rational (numerator * o.denominator, o.numerator * denominator);

Also I don't really follow the logic in your GCD function, as I'm guessing this is causing you an infinite loop. Look up Euclid's algorithm if you need some help.

Alex S
  • 572
  • 7
  • 15
0

private int numerator, denominator;//this value is 0

the same you are using in

public Rational division(Rational o) {
    return new Rational ((numerator/denominator) / (numerator/denominator), denominator);
}

instead of object o