You
are to write a simple Fraction class that implements simple fractions, with a numerator
and denominator. Denominators must be positive (not negative, not 0).
Numerators can be positive, negative or 0. Keep these in mind with your
constructors and other methods.
Your
Fraction class should provide the following
constructors
Fraction()
- create a new fraction with a reasonable default value (possibly
0 for the numerator and 1 for the denominator. Fraction(int num, int denom) - create a new fraction with
numerator num
and denominator denom. Fraction(int val)
- create a new fraction with value val
(that is, numerator val and
denominator 1).
Your
Fraction class should provide the following
methods:
void setValue(int num, int denom) -
set the value of the fraction. void setValue(Fraction f) - set the value of
the fraction. int getNumerator() - get the numerator. int getDenominator() - get the denominator. double toDouble() - convert the fraction to a
decimal value. String toString()
- convert the fraction to a string of the form num/denom.Fraction add(Fraction
f) - add a fraction to the current fraction. This should
not modify the value of the current fraction. Fraction multiply(Fraction
f) - multiply the current fraction by another fraction. This
should not modify the value of the current fraction. Fraction subtract(Fraction
f) - subtract a fraction from the current fraction. This
should not modify the value of the current fraction. Fraction divide(Fraction
f) - divide the value of the current fraction by f. This
should not modify the value of the current fraction. Fraction add(int i) - an an integer to the current fraction This should not
modify the value of the current fraction.