• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

no clue

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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).]
 
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Ranch Hand
Posts: 241
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 178
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When reducing, make sure you take into account negative numerator, negative denominator.
Good luck!
 
get schwifty. tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic