# Homework Assignment, Need some help

• 04-10-2012, 07:23 PM
xdrazkalnytex
Homework Assignment, Need some help
I fill in the parts, i got the subtraction and multiplication to work , but the addition and quotient doesn't work. For the quotient I don't know how to do reciprocals. I got the program to work, but 2 of the printout is wrong... Would someone help me?
Code:

```// Rational.java public class Rational {     private int n; // numerator     private int d; // denominator     // Return the greatest common divisor (gcd) of a and b.     private static int gcd(int a, int b)     {         // TBD       if( a < 0) a = -a;       if( b < 0) b = -b;       if( 0 ==b)         return a;       else         return gcd(b, a & b);     }     // Construct a rational number given its numerator and denominator.     public Rational(int n, int d)     {         // TBD       this.n = n;       this.d = d;              }     // Return the sum of this number and b.     public Rational plus(Rational b)     {         // TBD       int numerator = (this.n * b.d ) + (this.d * b.n);       int denominator = (this.d * b.d);       return new Rational(numerator, denominator);     }     // Return the difference of this number and b.     public Rational minus(Rational b)     {         // TBD       int numerator = (this.n * b.d ) - (this.d * b.n);       int denominator = this.d * b.d;       return new Rational(numerator, denominator);     }     // Return the product of this number and b.     public Rational times(Rational b)     {         // TBD       int numerator = (this.n * b.n );       int denominator = (this.d * b.d);       return new Rational(numerator,denominator);     }     // Return the quotient of this number and b.     public Rational over(Rational b)     {         // TBD        int numerator = (this.n * b.d );       int denominator = (this.d * b.n);       return new Rational(numerator,denominator);     }     // Return true if this number equals b, false otherwise.     public boolean equals(Rational b)     {         // TBD       boolean equals = this.n != this.d;       return (equals);     }     // Return a string representation of this number.     public String toString()     {         // TBD         String result;         if (n == 0)             result = "0";         else             if (d == 1)                 result = n + "";             else                 result = n + "/" + d;             return result;     }     // Test driver.     public static void main(String[] args)     {         Rational a = new Rational(1, 3);         Rational b = new Rational(2, 3);         System.out.println("Should print 1/1: " + a.plus(b));         System.out.println("Should print true: " + b.minus(a).equals(a));         System.out.println("Should print 2/9: " + a.times(b));         System.out.println("Should print 1/2: " + a.over(b));     } }```
• 04-10-2012, 08:04 PM
Diargg
Re: Homework Assignment, Need some help

Second, when you say " 2 of the printout is wrong" - Which 2? Can you show us what is wrong? What are they supposed to say?
• 04-10-2012, 08:11 PM
xdrazkalnytex
Re: Homework Assignment, Need some help
> run Rational
Should print 1/1: 9/9
Should print true: true
Should print 2/9: 2/9
Should print 1/2: 3/6
>

first printout which should be 1/1 (suppose to be addition)
last printout which should be 1/2 (suppose to be division)
• 04-10-2012, 08:13 PM
Diargg
Re: Homework Assignment, Need some help
Quote:

Also,
Can you say what is wrong with the answers you get?
• 04-10-2012, 08:29 PM
xdrazkalnytex
Re: Homework Assignment, Need some help
it shows the wrong print out ,

and what is [code] tags?
• 04-10-2012, 09:22 PM
Diargg
Re: Homework Assignment, Need some help
But what is wrong with it, beyond not being right? Should the numbers be different? Should they mean different things? (ie 3/4 means .75 )

Edit your initial post to put [code ] and [/code] around your Java code
• 04-10-2012, 09:55 PM
xdrazkalnytex
Re: Homework Assignment, Need some help
it should print out what what the print out said,
like 1/1 should be 1/1 and so on.
• 04-10-2012, 10:05 PM
Diargg
Re: Homework Assignment, Need some help
So what is the difference between 9/9 and 1/1? Are they the same?
• 04-10-2012, 11:49 PM
xdrazkalnytex
Re: Homework Assignment, Need some help
it supposed to print out 1/1 , but i guess 9/9 will do
• 04-11-2012, 03:24 PM
Diargg
Re: Homework Assignment, Need some help
One last try I guess.

Read this. Are you familiar with how fractions work? All of the code you need is present in the example you provided in the first post. The code isn't correct, and we can discuss that, but first you need to understand what you need to do to fix the problem.
• 04-11-2012, 03:50 PM
JosAH
Re: Homework Assignment, Need some help
Why have you implemented that gcd( ... ) method in your class? You're not using it ...

kind regards,

Jos
• 04-11-2012, 04:18 PM
Sierra
Re: Homework Assignment, Need some help
I have a general hint for you: Never use the same name for local and class attributes like you do... ;)

First reads as it does because it is correct. 9/9 == 1/1 - you just need to cut it down.
Last reads as it does because it is correct. 3/6 == 1/2 - you just need to cut it down.

Add a method that trims it down - you will maybe need gcd() for that ... ;)
• 04-11-2012, 04:29 PM
JosAH
Re: Homework Assignment, Need some help
Given the toString() method I don't think 1/1 is ever printed out; I also don't think that code was written by the OP ...

kind regards,

Jos
• 04-11-2012, 04:42 PM
Sierra
Re: Homework Assignment, Need some help
@JosAH: I agree to both and I think it is part of the homework to find a solution for the correct output before calling toString()... so he should give it a try. ;)
• 04-11-2012, 04:45 PM
JosAH
Re: Homework Assignment, Need some help
Quote:

Originally Posted by Sierra
@JosAH: I agree to both and I think it is part of the homework to find a solution for the correct output before calling toString()... so he should give it a try. ;)

Ah, ok, so all we can do (and have to do) is sit back and enjoy the show ;-)

kind regards,

Jos
• 04-11-2012, 04:47 PM
Diargg
Re: Homework Assignment, Need some help
Quote:

Originally Posted by JosAH
Ah, ok, so all we can do (and have to do) is sit back and enjoy the show ;-)

kind regards,

Jos

/bows

I'm personally a fan of the (a & b) integer being passed about.
• 04-11-2012, 04:59 PM
Sierra
Re: Homework Assignment, Need some help
I wonder if that is/should be Euclid's algorithm...?
• 04-11-2012, 05:56 PM
JosAH
Re: Homework Assignment, Need some help
Quote:

Originally Posted by Sierra
I wonder if that is/should be Euclid's algorithm...?

Nah, just a feeble attempt to it; it is utterly incorrect but the OP will find out ...

kind regards,

Jos