1

I'm doing the following for a lab, and am getting everything to compile with the file they gave us apart from the last method named "divide". The point of it is to do different operations using Rational numbers. My This is my code for the divide method. I also included the name of the class:

public class Rational{

  public Rational divide (Rational newRational){
    int numer = (numerator / newRational.getNum());
    int denom = ( denominator / newRational.getDenom());
    return new Rational (numer, denom);
  }
}

The code we were given to have it compile with is the following. The rest of my code (not included here) compiles perfectly with this when I comment out the last two lines that have to deal with the divide method above.

public class RationalDemo{
    public static void main(String args[])
    {
        Rational r1= new Rational (3,4);

        Rational r2= new Rational (1,4);

        Rational addResult = r1.add(r2);
        System.out.println(addResult.toString()); //1

        Rational subtractResult = r1.subtract(r2);
        System.out.println (subtractResult.toString()); //1/2

        Rational multiplyResult = r1.multiply(r2);
        System.out.println(multiplyResult.toString());//3/16

        Rational.divideResult= r1.divide(r2);
        System.out.println(divideResult.toString());//3
     }
}

The error I get when trying to compile is:

2 errors found: [line: 17] Error: cannot find symbol symbol: variable divideResult location: class Rational [line: 18] Error: cannot find symbol symbol: variable divideResult location: class RationalDemo

I'm not sure what I am doing wrong here or why this error message is popping up.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Jack
  • 11
  • 4
  • 9
    `Rational.divideResult` remove the dot in the middle. – QBrute Oct 07 '16 at 20:57
  • 8
    That's not how you divide rational numbers. – biziclop Oct 07 '16 at 20:58
  • Fix your math first. – Robert Mikes Oct 07 '16 at 21:00
  • @QBrute , You mean in the second bit of code? That's what I'm using to see if my code compiles and I'm not supposed to mess with that. If that's not what you mean, could you elaborate? – Jack Oct 07 '16 at 21:18
  • @biziclop how would I then? Would I cross multiply ? I attempted to do so and am still getting the same error popping up – Jack Oct 07 '16 at 21:21
  • I mean this statement in your main `Rational.divideResult= r1.divide(r2);`. Remove the dot in `Rational.divideResult`. Fixing is not messing. Otherwise your code will never compile like that. – QBrute Oct 07 '16 at 21:22
  • 1
    @QBrute Thank you so much. This seems to have been my error in typing in the code! It seems to have been my issue and not the code they gave me. It compiles perfectly now! – Jack Oct 07 '16 at 21:26
  • 1
    Good you solved the problem you were asking about. You will get a more precise (more correct) result if you follow the rule: you divide by a fraction by multiplying by its inverse (3/5 divided by 4/7 equals 3/5 multiplied by 7/4 equals 21/20). – Ole V.V. Oct 07 '16 at 21:53
  • @biziclop et al.: (a/x)/b = a/(b\*x) ; a/(b/y) = (a\*y)/b ; (a/x)/(b/y) = (a\*y)/(b\*x) = (a/b)/(x/y) it's _exactly_ the same. – walen Oct 07 '16 at 22:51
  • 4
    @walen No, that's real division. Rational numbers have to have an integer denominator and numerator, and this is reflected by the code, which uses `int` values and operations. If you try to divide 1/1 by 1/2 with this method, you will get 1/0 (because 1/2 = 0), which is definitely not the correct result. (Unfortunately in the specific test case OP was given, the incorrect method will work. But in general it doesn't.) – biziclop Oct 07 '16 at 23:07

0 Answers0