i'm trying to write a class that defines a fraction, that will also implement a fraction, numerator and denominator and add or subtract the fractions. totally confused - any suggestions?

jesse johnson
Greenhorn

Joined: Jun 14, 2001
Posts: 6

posted

0

neglected to include what i've come up with so far

(edited by Cindy to format code) [This message has been edited by Cindy Glass (edited June 14, 2001).]

Good start. Now refactor and add the following methods: lcd() - return the true Least Common Denominator. LCD of 1/3 and 2/3 is 3, not 9. reduce() - reduces 2/6 to 1/3 equals() - (new Fraction(1, 3)).equals(new Fraction(2, 6)) should be true inverse() - System.out.println((new Fraction(2, 3)).inverse()); // "3/2" or to make it more interesting, make it printout "1-1/2" (Sorry, I've forgotten all the proper terms for these fractions) Have fun!

As Junilu said, Jesse, you've got a good start going here! I did the same thing as an exercise, and it turned out to be very instructive for me. One thing jumps out at me, though: you may want to do something in the event your user attempts to assign your denominator to zero, instead of letting Java throw its runtime ArithmeticException. Also, a method to add to Junilu's recommended list: public double toDouble() {...}. Good luck, Jess! Art