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 -4/8. 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 write
code to do this but we do not know yet how. So for now we will not
simplify fractions.
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
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
public void setDenum(int a); // (setter/mutator) Set the _denom field to a
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
// We sum the fractions a/b and c/d as (a*d+b*c)/b*d
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.
public String toString(); // Applied to the fraction 4/8 will return "{4, 8}"
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 first asks the user to
// enter values to create two fractions.
// then prints them out using toString,
// says if they are equal using equals, and prints
// out their sum.
Notice that the methods readFraction and main are static. Try to think
about what makes them different from the other methods.
If you feel tough, instead of writing one file with the single class Fraction, write two files one called Fraction.java containing all the code as before, excluding the main method, and the other called FractionTester.java containg the class FractionTester with as only method the main method code that you took out of the Fraction class. In this case you will compile and run your code with the commands
javac Fraction.java javac FractionTester.java java FractionTester
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.