 
public class Rational
{
   private int myNumerator;
   private int myDenominator;
 
   /**
    *  Sets up the rational number by ensuring a nonzero denominator
    *  making only the numerator signed, and reducing.
    *  If the denominator is 0, set it to 1.
    */
   public Rational (int numer, int denom)
   { 
      if (denom == 0)
         denom = 1;
 
      // Make the numerator "store" the sign
      if (denom < 0)
      { 
         numer = numer * -1;
         denom = denom * -1;
      }
      myNumerator = numer;
      myDenominator = denom;
      reduce();
   }
 
   /**
    *  Returns a new rational number that is the sum of
    *  the rationals this and op2.
    */
   public Rational add (Rational op2) {
      int commonDenominator = myDenominator * op2.getDenominator();
      int numerator1 = myNumerator * op2.getDenominator();
      int numerator2 = op2.getNumerator() * myDenominator;
      int sum = numerator1 + numerator2;
      return new Rational (sum, commonDenominator);
   }
 

   /**
    *  Returns a new rational number that is the difference between
    *  the rationals this and op2.
    */
   public Rational subtract (Rational op2) {
		
   }

   /**
    *  Returns a new rational number that is the product of
    *  the rationals this and op2.
    */
   public Rational multiply (Rational op2) {
      // code goes here
   }

   /**
    *  Returns a new rational number that is the quotient of
    *  the rationals this and op2.
    */
   public Rational divide (Rational op2) {
      // code goes here
   }

   /**
    *  Returns a new rational number that is the reciprocal
    *  of this rational.
    */
   public Rational reciprocal () {
      // code goes here
   }
 
   /**
    *  Returns the numerator of this rational number.
    */
   public int getNumerator () {
      return myNumerator;
   }

   /**
    *  Returns the denominator of this rational number.
    */
   public int getDenominator () {
      return myDenominator;
   }

   /**
    *  Determines if this rational number is equal to the one passed
    *  as a parameter.  Assumes they are both reduced.
    */
   public boolean equals (Rational op2) {
      return ( myNumerator == op2.getNumerator() &&
               myDenominator == op2.getDenominator() );
   }

   /**
    *  Returns this rational number as a string.
    */
   public String toString () {
      String result;

      if (myNumerator == 0)
         result = "0";
      else if (myDenominator == 1)
            result = myNumerator + "";
      else
            result = myNumerator + "/" + myDenominator;
      return result;
   }

   /**
    *  Reduces this rational number by dividing both the numerator
    *  and the denominator by their greatest common divisor.
    */
   private void reduce () {
      int common;
      if (myNumerator != 0) {
         common = gcd (Math.abs(myNumerator), myDenominator);
         myNumerator = myNumerator / common;
         myDenominator = myDenominator / common;
      }
   }   

   /**
    *  Computes and returns the greatest common divisor of the two
    *  positive parameters. Uses Euclid's algorithm.
    */
   private int gcd (int num1, int num2)
   {
      while (num1 != num2)
         if (num1 > num2)
            num1 = num1 - num2;
         else
            num2 = num2 - num1;
      return num1;
   }
}


