title: Simplifying a fraction, Java
body: <p>I am doing a school project in which I add 2 fractions and need to simplify the answer. I have a good bit of code so far but there are a few instances where certain fractions do not simplify properly. For instance 1/4+1/6 1ill give 5/6 instead of 5/12. Can you please check my code for any possible errors or for pointers?</p>
  
  <pre><code>public int num1, num2, denom1, denom2, numSum, denomAns, denomSimp, gcd;
  
  public Fraction(int a1, int a2, int a3, int a4, int a5, int a6) {
      this.num1 = a1;
      this.num2 = a2;
      this.denom1 = a3;
      this.denom2 = a4;
      this.numSum = a5;
      this.denomAns = a6;
  }
  
  public void simplify() {
  
      if (denom1 == denom2) {
          denomAns = denom1;
          numSum = num1 + num2;
      } else {
          denomAns = denom1 * denom2;
          num1 = num1 * denom2;
          num2 = num2 * denom1;
          numSum = num1 + num2;
      }
      int tempNum = numSum, tempDenom = denomAns;
      if (tempNum == 0) {
          gcd = tempDenom;
      } else {
          while (tempDenom != 0) {
              if (tempNum &gt; tempDenom) {
                  tempNum = tempNum - tempDenom;
              } else {
                  tempDenom = tempDenom - tempNum;
              }
          }
          gcd = tempNum;
      }
      denomSimp = denomAns / gcd;
      numSum = numSum / gcd;
  }
  </code></pre>