Your program will consist of a single Java file, Fraction.java.
It contains a class Fraction used to represent
fractions, an integer divided by a positive integer, things like
8/3, or -1/2. Each Fraction instance will have two
integer data fields, a numerator _num, and a denominator _denom.
With fractions there is a problem: we know in mathematics that 6/4, 3/2,
-6/-4, -3/-2, 24/16 etc. all denote the same fraction. In mathematics
we can write each fraction in normal form: eliminate the common factors
from both numerator and denominator, make the denominator positive.
So -24/-16 would be represented by 3/2 (and 32/-48 by -2/3).
We can do the same in our program making sure that fractions are always kept in normal form.
[To normalize a fraction we divide numerator and
denominator by their greatest common divisor
(remember that we have already written the computation
of the gcd using the Euclidean algorithm but we assumed the numbers were positive) and
then we make sure that the denominator is positive
]
The class has at least the following constructors and methods:
public Fraction(); // Constructor that creates the instance 0/1 public Fraction(int num, int denom); // Constructor that creates the // instance num/denom; where denom is not 0 [check that it isn't] public Fraction(Fraction a); // Copy constructor that creates a fraction with // same content as a public int getNum(); // Accessor to _num field public int getDenom(); // Accessor to _denom field public void setNum(int a); // (setter/mutator) Set the _num field to a then normalize public void setDenum(int a); // (setter/mutator) Set the _denom field to a then normalize // Make sure that _denom is not 0. public Fraction add(Fraction a); // Returns the fraction that is the // sum of the subject of the method and a. For example // (new Fraction(3,4)).add(new Fraction(1,4)) is 16/16 i.e 1/1 // We sum the fractions a/b and c/d as (a*d+b*c)/b*d then normalize public boolean equals(Fraction a); // Returns true if subject of method // and argument of call are equal. Fractions // a/b and c/d are equal if a*d and b*c are equal or // since the fractions are normalized, if a==c and b==d. public String toString(); // Applied to the fraction 1/2 will return "{1, 2}" public int compareTo(Fraction a); // It returns -1 if subject of call // is less than a; 0, if equal, +1 if greater public Fraction max(Fraction a); // It returns the subject of the call // if greater or equal to a, otherwise returns a public static Fraction readFraction(); // Prompts the user to enter // the numerator and denominator of a fraction // and returns the resulting fraction public static void main(String[] args); // It is a driver, i.e. // it tests that the methods listed above work // correctly.Notice that the methods readFraction and main are static. Try to think about what makes them different from the other methods.
Develop your program on Unix or on Windows using the command window.
Email your program (just the .java file, not the .class file) to the Teaching Assistant.