Computer Science 241
Fraction class assignment
Due: September 30, 2002

The following is an interface for a Fraction class, followed by the class itself.  Most of the code in the Fraction class has been omitted.  Fill it in, and create a test class to test all of the methods in the class.  This is a group assignment.  Each group should submit a single assignment.  This will consist of a disk with the three files, the interface, the Fraction class, and the test class.  Also hand in a printout of all the files.  Each file should be extensively commented explaining how each was designed and how they work.

package fractions;

public interface FractionsInterface
{
     public Fraction add (Fraction fraction);
     public Fraction subtract (Fraction fraction);
     public Fraction multiply (Fraction fraction);
     public Fraction divide (Fraction fraction);
     public boolean equal (Fraction fraction);
     public boolean lessThan (Fraction fraction);
}

___________________________________________________________________________

package fractions;

import java.io.*;

public class Fraction implements FractionsInterface
{
 private int numerator, denominator;

 Fraction ()
 {
      numerator = 0;
      denominator = 1;
 } // constructor

 Fraction (int top, int bottom)
 {
      numerator = top;
      denominator = bottom;
 } // constructor

 protected int getNumerator () {return numerator;}
 protected int getDenominator () {return denominator;}

 public Fraction add (Fraction fraction)
 {
      int top = fraction.getNumerator();
      int bottom = fraction.getDenominator ();
      int newTop = this.numerator * bottom + this.denominator * top;
      int newBottom = this.denominator * bottom;
      int d = gcd (newTop, newBottom);
      return new Fraction (newTop/d, newBottom/d);
 } // add

 public Fraction subtract (Fraction fraction)
 {
      return …;
 } // subtract

 public Fraction multiply (Fraction fraction)
 {
      return …;
 } // multiply

 public Fraction divide (Fraction fraction)
 {
      return …;
 } // divide

 public boolean equal (Fraction fraction)
 {
      int top = fraction.getNumerator ();
      int bottom = fraction.getDenominator ();
      if (this.numerator * bottom == this.denominator * top) return true;
      else return false;
 } // equal

 public boolean lessThan (Fraction fraction)
 {
      return …;
 } // lessThan

 protected void getFraction ()
 {
 } // getFraction

 public void displayFraction ()
 {
 } // displayFraction

 private int gcd (int a, int b)
 {
      if (a%b > 0) return gcd (b, a%b);
      else return b;
 } // gcd
} // Fraction