Results 1 to 18 of 18
 04102012, 07:23 PM #1Member
 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; 04102012 at 08:29 PM. Reason: added code tags
 04102012, 08:04 PM #2Senior Member
 Join Date
 Feb 2012
 Posts
 117
 Rep Power
 0
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?
 04102012, 08:11 PM #3Member
 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)
 04102012, 08:13 PM #4Senior Member
 Join Date
 Feb 2012
 Posts
 117
 Rep Power
 0
Re: Homework Assignment, Need some help
First, use [code] tags. They'll make your code far more readable.
Can you say what is wrong with the answers you get?Last edited by Diargg; 04102012 at 08:15 PM.
 04102012, 08:29 PM #5Member
 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?
 04102012, 09:22 PM #6Senior 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
 04102012, 09:55 PM #7Member
 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.
 04102012, 10:05 PM #8Senior 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?
 04102012, 11:49 PM #9Member
 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
 04112012, 03:24 PM #10Senior 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.
 04112012, 03:50 PM #11
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 13,878
 Blog Entries
 7
 Rep Power
 21
Re: Homework Assignment, Need some help
Why have you implemented that gcd( ... ) method in your class? You're not using it ...
kind regards,
Joscenosillicaphobia: the fear for an empty beer glass
 04112012, 04:18 PM #12AN21XX
 Join Date
 Mar 2012
 Location
 Munich
 Posts
 297
 Rep Power
 3
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 ... ;)
 04112012, 04:29 PM #13
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 13,878
 Blog Entries
 7
 Rep Power
 21
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,
Joscenosillicaphobia: the fear for an empty beer glass
 04112012, 04:42 PM #14AN21XX
 Join Date
 Mar 2012
 Location
 Munich
 Posts
 297
 Rep Power
 3
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. ;)
 04112012, 04:45 PM #15
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 13,878
 Blog Entries
 7
 Rep Power
 21
 04112012, 04:47 PM #16Senior Member
 Join Date
 Feb 2012
 Posts
 117
 Rep Power
 0
 04112012, 04:59 PM #17AN21XX
 Join Date
 Mar 2012
 Location
 Munich
 Posts
 297
 Rep Power
 3
Re: Homework Assignment, Need some help
I wonder if that is/should be Euclid's algorithm...?
 04112012, 05:56 PM #18
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 13,878
 Blog Entries
 7
 Rep Power
 21
Similar Threads

Need Help with Homework Lab Assignment on Loops and File Input/Output
By Terminus_Est in forum New To JavaReplies: 7Last Post: 02292012, 02:15 PM 
Need help with homework
By bkim33 in forum New To JavaReplies: 9Last Post: 02112011, 05:50 AM 
Homework help please
By chick in forum New To JavaReplies: 22Last Post: 03192010, 08:39 AM 
Calculator Program HELP NEEDED FAST! Homework assignment
By SteroidalPsycho in forum New To JavaReplies: 3Last Post: 03052009, 05:02 AM 
Homework help...
By robrobrob in forum New To JavaReplies: 4Last Post: 10172008, 04:24 AM
Bookmarks