/********************************************************\

ADDING FRACTIONS

This program adds fractions together.  It could be shorter,
but this is very complete and easily extended to do
further fraction operations.  It demonstrates how to use
a CLASS to store a fraction as a single Object, with
a numerator and denominator.

\********************************************************/



   class Fraction
   {
      int top;
      int bottom;

      Fraction(int t, int b)
      {
         top = t;
         bottom = b;
      }
   }

public class Fractions
{
   public Fractions()
   {
      Fraction fa = inputFraction("First fraction:");
      Fraction fb = inputFraction("Second fraction:");
      Fraction fc = addFractions(fa,fb);
      reduce(fc);
      output("Sum = " + fc.top + "/" + fc.bottom);
   }

   public Fraction inputFraction(String prompt)
   {
      output(prompt);
      return new Fraction(inputInt(" Top = "), inputInt(" Bottom = ") );
   }

   public Fraction addFractions(Fraction fa, Fraction fb)
   {
      int top = fa.top * fb.bottom + fa.bottom * fb.top;
      int bottom = fa.bottom * fb.bottom;
      return new Fraction( top, bottom );
   }

   public void reduce(Fraction frac)
   { 
      if (frac.top != 0)
      {  int f = frac.top;
         while ( (frac.top % f != 0) || (frac.bottom % f != 0) )
         {
            f = f - 1;
         }
         frac.top = frac.top / f ;
         frac.bottom = frac.bottom / f ;
      }  
   }
  
   public static void main(String[] args)
   {  new Fractions(); }
  
   public void output(String message)
   {  javax.swing.JOptionPane.showMessageDialog(null,message);  }
  
   public String input(String prompt)
   {  return javax.swing.JOptionPane.showInputDialog(null,prompt);  }
     
   public int inputInt(String prompt)
   {  int result=0;
      try{result=Integer.valueOf(
      input(prompt).trim()).intValue();}
      catch (Exception e){result = 0;}
      return result;
   }
}
  
/************************************************************\

(1) Add a new method that multiplies two fractions together.
    It should multiply the numerators, multiply the denominators,
    and then use the "reduce" method to simplify the result.
    This will be very similar to the "addFractions" method.
    Then the program should display two answers, for
    adding and multiplying.

(2) Change this into a QUIZ program. It should choose 4 random
    numbers, to make 2 fractions.  It displays the first fraction
    and the second fraction, and then asks the user to input
    two numbers for the top and bottom of the correct answer.
   
    (a) In the first version, require the user to type exactly
        the correct numbers for the reduced answer.
       
    (b) In the second version, input the users answer,
        turn that into a fraction and REDUCE the fraction,
        and then compare this to the correct reduced answer.
   
(3) Make a method that adds up the following fractions:
    1/2 + 1/4 + 1/8 + ... + 1/1024.
    This should use a loop to automatically generate the numbers,
    create fractions and add them up.
   
\************************************************************/