
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));
}
}

Re: Homework Assignment, Need some help
First, use [code] tags. They'll make your code far more readable.
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?

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)

Re: Homework Assignment, Need some help
Quote:
First, use [code] tags. They'll make your code far more readable.
Also,
Can you say what is wrong with the answers you get?

Re: Homework Assignment, Need some help
it shows the wrong print out ,
and what is [code] tags?

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

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.

Re: Homework Assignment, Need some help
So what is the difference between 9/9 and 1/1? Are they the same?

Re: Homework Assignment, Need some help
it supposed to print out 1/1 , but i guess 9/9 will do

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.

Re: Homework Assignment, Need some help
Why have you implemented that gcd( ... ) method in your class? You're not using it ...
kind regards,
Jos

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 ... ;)

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

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. ;)

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

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.

Re: Homework Assignment, Need some help
I wonder if that is/should be Euclid's algorithm...?

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