title: Trouble simplifying fractions in java
body: <p>I'm trying to make a program that takes two fractions and then adds, subtracts, multiplies, and divides them, then puts them in simplest form if necessary.</p>
  
  <p>The only problem I've run into is that the fractions are not simplifying and I'm not sure why.</p>
  
  <p>For example, right now when I run the fractions 3/4 and 1/4 through it gives me these answers:
  Addition: 4/4
  Subtraction: 2/4
  Multiplication: 3/16
  Division: 12/4</p>
  
  <p>I want addition to be 1/1, subtraction 1/2, and division 3/1. Below is my code.</p>
  
  <pre><code>import java.util.*;
  public class RationalNumber
  {
  private int numerator;
  private int denominator;//instance variables
  //constructor
  public RationalNumber()
  {
      numerator = 0;
      denominator = 1;
  }
  
  public RationalNumber(int num, int den)
  {
      setNumerator(num);
      setDenominator(den);
      int gcf= greatestCommonFactor(num, den);
  
      num = num/gcf;
      den = den/gcf;
      /*num = num/(greatestCommonFactor(num, den));
      den = den/(greatestCommonFactor(num, den));*/
  }
  
  //getters
  public int getDenominator()
  {
      return denominator;
  }
  
  public int getNumerator()
  {
      return numerator;
  }
  
  //setters
  public void setNumerator(int num)
  {
      numerator = num;
  }
  
  public void setDenominator(int den)
  {
      denominator = den;
  }
  
  //adds the rational numbers
  public RationalNumber add(RationalNumber two)
  {
      RationalNumber add = new RationalNumber();
      if(getDenominator() == two.getDenominator())
      {
          int newNum = numerator + two.numerator;//simply adds the numerators if the denominators are the same
          /*newNum = newNum/(greatestCommonFactor(newNum, denominator));
          int newDen = denominator/(greatestCommonFactor(newNum, denominator));//simplification*/
          return new RationalNumber(newNum, denominator);
      }
      else
      {
          int newDen = getDenominator() * two.getDenominator();
          int leftNum = getNumerator() * two.getDenominator();
          int rightNum = getDenominator() * two.getNumerator();//cross multiplication
  
          /*int newNum = (leftNum + rightNum)/(greatestCommonFactor((leftNum + rightNum), getDenominator()));//simplification
          newDen = denominator/(greatestCommonFactor(newNum, getDenominator()));*/
  
          return new RationalNumber(leftNum + rightNum, newDen);
      }
  }
  
  //subtracts the rational numbers, same math as the add method but with subtraction signs
  public RationalNumber sub(RationalNumber two)
  {
      RationalNumber sub = new RationalNumber();
  
      if(this.getDenominator() == two.getDenominator())
      {
          int newNum = this.numerator - two.numerator;
          return new RationalNumber(newNum, this.getDenominator());
      }
      else
      {
          int newDen = this.getDenominator() * two.getDenominator();
          int leftNum = this.getNumerator() * two.getDenominator();
          int rightNum = this.getDenominator() * two.getNumerator();
          return new RationalNumber(leftNum - rightNum, newDen);
      }
  
  }
  
  //multiplies the two rational numbers
  public RationalNumber mult(RationalNumber two)
  {
      RationalNumber mult = new RationalNumber();
      mult.denominator = denominator * two.denominator;
      mult.numerator = numerator * two.numerator;
  
      return mult;
  }
  
  //divides the rational numbers
  public RationalNumber div(RationalNumber two)
  {
      RationalNumber div = new RationalNumber();
      div.denominator = denominator * (two.numerator);
      div.numerator = numerator * two.denominator;//multiply by reciprocal in order to divide
  
      return div;
  }
  
  //returns the reciprocal of a rational number
  public RationalNumber reciprocal(RationalNumber two)
  {
      RationalNumber reciprocal = new RationalNumber();
      reciprocal.denominator = numerator;
      reciprocal.numerator = denominator;
      return reciprocal;
  }
  
  //returns the inverse of a rational number
  public RationalNumber inverse(RationalNumber two)
  {
      RationalNumber inverse = new RationalNumber();
      inverse.numerator = (numerator*-1);
      return inverse;
  }
  
  //finds the greatest common factor, used in simplifying
  public int greatestCommonFactor(int num, int den)
  {
      for(int i = (Math.max(num,den)/2)+1; i &gt; 0; i--)
      {
          if((num/i) == Math.round(num/i) &amp;&amp; (den/i) == Math.round(den/i))
              return i;//loop finds the GCF by finding if numerator &amp; denominator divided by i is an even number
      }
      return 1;
  }
  
  public String toString()
  {
      return numerator + "/" + denominator;//prints the numerator and denominator as a string
  }
  }
  </code></pre>