title: Addition, Subtraction, Multiplication, & Division with Fractions in Java (Homework)
body: <p>I have written this code as my answer to an assignment I recieved in AP Computer Science, and my teacher is having my redo this because the answers don't work. I can only compile it on my machine, so I am relying on my teacher as to what results is produces now, which are below. This assignment is do by midnight, tonight, and the procrastinator I am waited till now to ask for help here, but still, it is VERY much appreciated. Also, it's my first time on here, so sorry that some of the code isn't formatted properly. Thanks for the help!</p>
  
  <p>Results ATM</p>
  
  <pre><code>if a = 3/4 and b = 5/6
  
  a.add(b) produces 35/24; This should be 19/12
  a.subtract(b) produces 1/0; This should be -1/12
  a.divide(b) produces 11/1; This should be 9/10
  a.multiply(b) produces 6/-5; This should be 5/8
  </code></pre>
  
  <p>Here's the code.</p>
  
  <pre><code>import java.awt.*;
  
  import java.awt.event.*;
  
  import javax.swing.*;
  
  import javax.swing.border.TitledBorder;
  
  class Fraction{
      private int numer;
      private int denom;
      private Fraction answer;
  
      //Definition
      public Fraction(int num, int den)
      {
          numer = num;
          denom = den;
          simplify();
      }
  
  
      //Simplify
      void simplify()
      {
          int gcd = findGCD(numer, denom);
          numer /= gcd;
          denom /= gcd;
      }
  
  
      //GCD Function
      int findGCD(int a, int b)
      { int temp;
          while(b != 0)
          {
              temp = b;
              b = a % b;
              a = temp;
          }
          return a;
      }
  
  
      //GetNumerator
      public int getNumer()
      {
          return numer;
      }
  
      //GetDenominator
      public int getDenom()
      {
          return denom;
      }
  
      //Fraction Add Method
      Fraction add(Fraction x)
      {
          Fraction answer;
  
          if(denom == x.denom)
          {
              answer = new Fraction(x.numer + numer, denom);
              }
          else
          {
            int den = denom * x.getDenom();
            int num = numer * x.getNumer();
            num += x.getNumer() * denom;
            answer = new Fraction(num, den);
  
          }
       return answer;
      }
  
  
      //Fraction Subtract Method
      Fraction subtract(Fraction x)
      {
          Fraction answer;
  
          if(x.denom == denom)
          {
              answer = new Fraction(numer - x.numer, denom);
          }
          else
          {
              int den = denom / x.getDenom();
              int num = numer / x.getNumer();
              num -= x.getNumer() * denom;
              answer = new Fraction(num, den);
          }
          return answer;
      }
  
      //Fraction Multiply Method
      Fraction multiply(Fraction x)
      {
          Fraction answer;
  
          if(x.denom == denom)
          {
              answer = new Fraction(numer + x.numer, denom);
          }
          else
          {
              int den = denom + x.getDenom();
              int num = numer + x.getNumer();
              num -= x.getNumer() * denom;
              answer = new Fraction(num, den);
          }
          return answer;
      }
  
      //Fraction Divide Method
      Fraction divide(Fraction x)
      {
          Fraction answer;
  
          if(x.denom == denom)
          {
              answer = new Fraction(numer - x.numer, denom);
          }
          else
          {
              int den = denom - x.getDenom();
              int num = numer - x.getNumer();
              num -= x.getNumer() * denom;
              answer = new Fraction(num, den);
          }
          return answer;
      }
  
  
      //@Override
      public boolean equals(Fraction x)  
      {
          boolean answer = false;
  
          if(numer == x.numer &amp;&amp; denom == x.denom)
        {
              answer = true;
          }
  
          return answer;
      }
  
  
      //ToString
      public String toString()
      {
      return (Integer.toString(numer) + "/" +
               Integer.toString(denom));
      }
  
  
      //Main Method
      public void main(String[]args)
      {
          Fraction a = new Fraction(3,4);
          Fraction b = new Fraction(5,6);
          System.out.println(a.toString());
          System.out.println(b.toString());
  
          Fraction c = a.add(b);
          System.out.println(c.toString());
  
          c = a.subtract(b);
          System.out.println(c.toString());
  
          c = a.divide(b);
          System.out.println(c.toString());
  
          c = a.multiply(b);
          System.out.println(c.toString());
  
  
  
  
  
      }
  
  }
  </code></pre>