CIS 67 - Homework 3

Handed out: 09/13/05
Due: by 10:00pm on 09/19/05

This program is an extension of homework 2. Now the Fraction class becomes more clever and powerful. We now (you may have already done so) represent fractions in normal form: the denominator must be positive, numerator and denominator must have no common factors (except 1). In the class log is a static method (a function) gcd that computes the greatest common divisor of two integers). We can use that method to make sure that numerator and denominator have no common factors. If two fractions are in normal form they are equal if they have the same numerator and same denominator. We introduce methods to subtract, multiply, and divide Fractions.
Here is the interface of the Fraction class:

public class Fraction {
   private int num;
   private int denom;
   // constructors
   public Fraction(){} // Default constructor, returns 0/1
   public Fraction(int num, int denom); // Constructor, for example new Fraction(14/-21)
			// returns a Fraction whose toString is "-2/3". If
			// the denominator is 0, this constructor returns 0/1.
   // Accessor methods 
   public int getNum(); // Return value of num
   public int getDenom(); // Return value of denom
   // Setter methods
   public void setNum(int n); // Setter method for the numerator. Remember 
			// that the fraction must remain in normal form
			// so some simplification may be needed
   public void setDenom(int d); // Setter method for the numerator. Remember 
			// that the fraction must remain in normal form
			// so some simplification may be needed
   // Standard methods
   public boolean equals(Fraction a); // Returns true if subject of method
			// and argument of call are math`ematically equal
   public String toString(); // Applied to the fraction 1/2 will return
                             // "1/2"
   public Fraction clone(); // Applied to the fraction1/2, returns a new 
			// Fraction with numerator 1 and denominator 2
   // Other methods
   public Fraction add(Fraction b); // Returns the fraction representing
			// the sum of the subject of the method with b
   public Fraction subtract(Fraction b); // Returns the fraction
			// representing the difference of the subject of
			// the method and b
   public Fraction multiply(Fraction b); // Returns the fraction
			// representing the product of the subject of the
			// method and b
   public Fraction divide(Fraction b); // Returns the fraction
			// representing the ratio of the subject of the
			// method and b
   public static Fraction readFraction(); // Prompts the user to enter 
 			// the numerator and denominator of a fraction
			// and returns the resulting fraction. It makes
			// sure that the denominator is not 0.
   public static void main(String[] args); // it first asks the user for
			// two fractions, then prints them out using toString,
			// says if they are equal using equals.
                        // Prints their sum, difference, product and
			// ratio. Then ask user to set the denominator 
			// of the first fraction and repeat computations 
			// and printouts.
}
You remember that we do not allow a fraction to have 0 as denominator. So be sure that if in a constructor, or in readFraction, or in setDenominator, or in division we have a fraction with 0 as denominator, then we replace it with the fraction 0/1.
Be sure to document your code. Develop your program on Unix or on Windows using the command window.

Your main program should test (i.e. use) all of your methods!

Email your program to the Teaching Assistant.