Home > IB Computer Science 2017 > Fraction Class Task

Fraction Class Task

November 12, 2015 Leave a comment Go to comments

Create a Fraction class.

It should have encapsulated numerator and a denominator fields, along with the following methods:

  • getDouble, which returns the fraction in decimal format
  • isImproper, which returns true if the fraction is improper
  • isLowestTerms, which returns true if the fraction is in its lowest terms
  • getLowestTerms, which returns the fraction in its lowest terms
  • toString, which returns the String representation of the fraction
  • add, which takes a Fraction argument and returns the sum of this fraction and its argument

It should also have an appropriate constructor.

You should also provide an overloaded toString method which takes a boolean argument which, if true, causes the function to return the fraction as a mixed fraction and if false does not.

Does your IDE tell you something about the toString method? Find out and explain what it is telling you and why.

Create a main method to instantiate some fractions and play with them.

Advertisements
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s