jQuery in Action, 3rd edition
The moose likes Beginning Java and the fly likes no clue Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "no clue" Watch "no clue" New topic

no clue

jesse johnson

Joined: Jun 14, 2001
Posts: 6
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

Joined: Jun 14, 2001
Posts: 6
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).]
Cindy Glass
"The Hood"

Joined: Sep 29, 2000
Posts: 8521
Very cool.
But no fun unless you can PLAY with it. Add in a main method so that you can execute the class.
Here is a sample that you might start with:

>java Fraction 5 6
should display
Then you can start adding more stuff to main.

"JavaRanch, where the deer and the Certified play" - David O'Meara
Junilu Lacar

Joined: Feb 26, 2001
Posts: 6319

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!

Junilu - [How to Ask Questions] [How to Answer Questions]
Art Metzer
Ranch Hand

Joined: Oct 31, 2000
Posts: 241
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!
Conrad Kirby
Ranch Hand

Joined: Jun 17, 2001
Posts: 178
When reducing, make sure you take into account negative numerator, negative denominator.
Good luck!
I agree. Here's the link: http://aspose.com/file-tools
subject: no clue
It's not a secret anymore!