# Thread: Homework Assignment, Need some help

1. Member Join Date
Mar 2012
Posts
34
Rep Power
0

## 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?
Java 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));
}
}```
Last edited by Norm; 04-10-2012 at 08:29 PM. Reason: added code tags  Reply With Quote

2. Senior Member Join Date
Feb 2012
Posts
117
Rep Power
0

## 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?  Reply With Quote

3. Member Join Date
Mar 2012
Posts
34
Rep Power
0

## 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)  Reply With Quote

4. Senior Member Join Date
Feb 2012
Posts
117
Rep Power
0

## Re: Homework Assignment, Need some help

Also,
Can you say what is wrong with the answers you get?
Last edited by Diargg; 04-10-2012 at 08:15 PM.  Reply With Quote

5. Member Join Date
Mar 2012
Posts
34
Rep Power
0

## Re: Homework Assignment, Need some help

it shows the wrong print out ,

and what is [code] tags?  Reply With Quote

6. Senior Member Join Date
Feb 2012
Posts
117
Rep Power
0

## 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  Reply With Quote

7. Member Join Date
Mar 2012
Posts
34
Rep Power
0

## 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.  Reply With Quote

8. Senior Member Join Date
Feb 2012
Posts
117
Rep Power
0

## Re: Homework Assignment, Need some help

So what is the difference between 9/9 and 1/1? Are they the same?  Reply With Quote

9. Member Join Date
Mar 2012
Posts
34
Rep Power
0

## Re: Homework Assignment, Need some help

it supposed to print out 1/1 , but i guess 9/9 will do  Reply With Quote

10. Senior Member Join Date
Feb 2012
Posts
117
Rep Power
0

## 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.  Reply With Quote

11. ## Re: Homework Assignment, Need some help

Why have you implemented that gcd( ... ) method in your class? You're not using it ...

kind regards,

Jos  Reply With Quote

12. AN21XX Join Date
Mar 2012
Location
Munich
Posts
297
Rep Power
9

## 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 ... ;)  Reply With Quote

13. ## 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  Reply With Quote

14. AN21XX Join Date
Mar 2012
Location
Munich
Posts
297
Rep Power
9

## 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. ;)  Reply With Quote

15. ## Re: Homework Assignment, Need some help 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  Reply With Quote

16. Senior Member Join Date
Feb 2012
Posts
117
Rep Power
0

## Re: Homework Assignment, Need some help 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.  Reply With Quote

17. AN21XX Join Date
Mar 2012
Location
Munich
Posts
297
Rep Power
9

## Re: Homework Assignment, Need some help

I wonder if that is/should be Euclid's algorithm...?  Reply With Quote

18. ## Re: Homework Assignment, Need some help 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  Reply With Quote

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•