This week's book giveaway is in the Cloud/Virtualizaton forum.
We're giving away four copies of Mesos in Action and have Roger Ignazio on-line!
See this thread for details.
Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

no clue

 
jesse johnson
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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"
Sheriff
Posts: 8521
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
5/6
Then you can start adding more stuff to main.
 
Junilu Lacar
Bartender
Posts: 7466
50
Android Eclipse IDE IntelliJ IDE Java Linux Mac Scala Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
Art Metzer
Ranch Hand
Posts: 241
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Conrad Kirby
Ranch Hand
Posts: 178
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When reducing, make sure you take into account negative numerator, negative denominator.
Good luck!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic