title: Polynomials in Java
body: <p>My add method works, however when I create a new <code>SparsePolynomial</code> object (at the bottom of the add method), the value of the <code>newSparePolynomial</code> changes when I debug it and I can't figure out where the extra information is coming from. Can someone help me?</p>
  
  <p>Here is a copy of my code:</p>
  
  <pre><code>import java.util.ArrayList;
  
  public class SparsePolynomial {
  
      private ArrayList&lt;Polynomial&gt; polynomialarraylist = new ArrayList&lt;Polynomial&gt;();
  
      /**
       * Constructor to get values of an arraylist of integers
       * @param arraylist that contains the  integer values used for the polynomials
       */
      public SparsePolynomial(ArrayList&lt;Integer&gt; arrayList)
      {
          //MODIFIDED: polynomialarraylist
          //EFFECT: constructs the arraylist of polynomials based off the arraylist of integers
          insertIntoPolynomialArray(arrayList);
      }
  
      /**
       * Converts the elements of the integer array into polynomials
       * @param arrayList that contains the polynomials contents
       */
      private void insertIntoPolynomialArray(ArrayList&lt;Integer&gt; arrayList)
      {
          //MODIFIED: polynomialarray
          //EFFECT: inputs the values of the arrayList into the polynomial array based on the position of the digits
          for(int i = 0; i &lt; arrayList.size(); i++)
          {
              Polynomial polynomial = new Polynomial(arrayList.get(i), arrayList.get(i+1));
              polynomialarraylist.add(polynomial);
              System.out.println("coef" + arrayList.get(i));
              System.out.println("degree" + arrayList.get(i+1));
              i++;
          }
      }
  
  
      /**
       * 
       */
      @Override
      public String toString() 
      {
          String result = "";
          sort();
          if (getDegree(0) ==  0) 
              return "" + getCoefficient(0);
          if (getDegree(0) ==  1) 
              return getCoefficient(0) + "x + " + getCoefficient(0);
          result =  getCoefficient(0) + "x^" + getDegree(0);
  
          for (int j = 1; j &lt; polynomialarraylist.size(); j++) 
          {
              if(j &gt; polynomialarraylist.size())
              {
                  break;
              }
              if      
              (getCoefficient(j) == 0) continue;
              else if 
              (getCoefficient(j)  &gt; 0) result = result+ " + " + ( getCoefficient(j));
              else if 
              (getCoefficient(j)  &lt; 0) result = result+ " - " + (-getCoefficient(j));
              if(getDegree(j) == 1) result = result + "x";
              else if (getDegree(j) &gt;  1) result = result + "x^" + getDegree(j);
          }
          return result;
  
  
      }
  
      /**
       * Sorts array 
       * @param array to sort
       */
      private void sort()
      {
          ArrayList&lt;Polynomial&gt; temp = polynomialarraylist;
          ArrayList&lt;Polynomial&gt; temp2 = new ArrayList&lt;Polynomial&gt;();
          int polydegreemain = polynomialarraylist.get(0).degree();
          temp2.add(polynomialarraylist.get(0));
          for(int i = 1; i &lt; polynomialarraylist.size(); i++)
          {
              if(i &gt; polynomialarraylist.size())
              {
                  break;
              }
              int polydegreesecondary = polynomialarraylist.get(i).degree();
  
              if(polydegreemain &lt; polydegreesecondary)
              {
                  temp.set(i-1, polynomialarraylist.get(i));
                  temp.set(i, temp2.get(0));
              }
  
          }
  
          polynomialarraylist = temp;
      }
      /**
       * Makes object hashable
       */
      @Override
      public int hashCode() {
          final int prime = 31;
          int result = 1;
          result = prime
                  * result
                  + ((polynomialarraylist == null) ? 0 : polynomialarraylist
                          .hashCode());
          return result;
      }
  
      /**
       * Checks for equality of two objects
       */
      @Override
      public boolean equals(Object obj) {
          if (this == obj)
              return true;
          if (obj == null)
              return false;
          if (getClass() != obj.getClass())
              return false;
          SparsePolynomial other = (SparsePolynomial) obj;
          if (polynomialarraylist == null) {
              if (other.polynomialarraylist != null)
                  return false;
          } else if (!polynomialarraylist.equals(other.polynomialarraylist))
              return false;
          return true;
      }
  
      public boolean equals(SparsePolynomial Sparse)
      {
          if(this == Sparse)
          {
          return true;
          }
          else
          {
          return false;
          }
      }
  
      public SparsePolynomial add(SparsePolynomial other)
      {
  
          ArrayList&lt;Polynomial&gt; thisPolynomial = createPolynomial();
          SparsePolynomial newSparsePolynomial;
          ArrayList&lt;Polynomial&gt; otherPolynomial = other.createPolynomial();
          Polynomial oldsum = new Polynomial();
          Polynomial newsum = new Polynomial();
          for(int i = 0; i &lt; thisPolynomial.size();i++)
          {
              if(thisPolynomial.size() == 1)
              {
                  newsum = thisPolynomial.get(i);
                  oldsum = newsum;
                  break;
              }
              if(i == 0)
              {
              newsum = thisPolynomial.get(i).add(thisPolynomial.get(i+1));
              oldsum = newsum;
              i++;
              }
              else
              { 
                  newsum = oldsum.add(thisPolynomial.get(i));
                  oldsum = newsum;
              }
          }
          for(int i = 0; i &lt; otherPolynomial.size(); i++)
          {       
              newsum = oldsum.add(otherPolynomial.get(i));
              oldsum = newsum;
          }
  
          ArrayList&lt;Integer&gt; ints = new ArrayList&lt;Integer&gt;();
  
          for(int i = 0; i &lt; oldsum.degree()+1; i++)
          {
              ints.add(oldsum.coefficient(i));
              ints.add(i);
          }
          newSparsePolynomial = new SparsePolynomial(ints);
  
          return newSparsePolynomial;
      }
  
      public SparsePolynomial subtract(SparsePolynomial other)
      {
          ArrayList&lt;Polynomial&gt; thisPolynomial = createPolynomial();
  
          ArrayList&lt;Polynomial&gt; otherPolynomial = other.createPolynomial();
  
          Polynomial olddifference = new Polynomial();
          Polynomial newdifference = new Polynomial();
          for(int i = 0; i &lt; thisPolynomial.size()+1;i++)
          {
              if(i == 0)
              {
                  newdifference = thisPolynomial.get(i).subtract(thisPolynomial.get(i+1));
                  olddifference = newdifference;
                  i++;
              }
              else
              {
                  newdifference = olddifference.subtract(thisPolynomial.get(i));
                  olddifference = newdifference;
              }
          }
          for(int i = 0; i &lt; otherPolynomial.size(); i++)
          {
  
              newdifference = olddifference.add(otherPolynomial.get(i));
              olddifference = newdifference;
  
          }
  
          ArrayList&lt;Polynomial&gt; polyarray = createArrayListOfPolynomialsFromPolynomials(olddifference);
  
          ArrayList&lt;Integer&gt; ints = new ArrayList&lt;Integer&gt;();
  
          for(int i = 0; i &lt; polyarray.size(); i++)
          {
          ints.add(polyarray.get(i).coefficient(polyarray.get(i).degree()));
          ints.add(polyarray.get(i).degree());
          }
  
          SparsePolynomial newSparsePolynomial = new SparsePolynomial(ints);
  
          return newSparsePolynomial;
      }
  
  
      private int getDegree(int index)
      {
          int degree;
  
          degree = polynomialarraylist.get(index).degree();
  
          return degree;
      }
  
      private int getCoefficient(int index)
      {
          int coefficient;
  
          coefficient = polynomialarraylist.get(index).coefficient(polynomialarraylist.get(index).degree());
  
          return coefficient;
      }
  
      private ArrayList&lt;Polynomial&gt; createPolynomial()
      {
          Polynomial polynomial = null;
          ArrayList&lt;Polynomial&gt; polynomialArray = new ArrayList&lt;Polynomial&gt;();
          for(int i = 0; i &lt; polynomialarraylist.size(); i++)
          {
              polynomial = new Polynomial(getCoefficient(i), getDegree(i));
              polynomialArray.add(polynomial);        
          }
          return polynomialArray;
      }
  Polynomial class
  
  public class Polynomial {
      // Overview: ...
      private int[] terms;
      private int degree;
  
      // Constructors
      public Polynomial() {
          // Effects: Initializes this to be the zero polynomial
          terms = new int[1];
          degree = 0;
      }
  
      public Polynomial(int constant, int power) {
          // Effects: if n &lt; 0 throws IllegalArgumentException else
          //  initializes this to be the polynomial c*x^n
          if(power &lt; 0){
              throw new IllegalArgumentException("Polynomial(int, int) constructor");
          }
  
          if(constant == 0) {
              terms = new int[1];
              degree = 0;
              return;
          }
  
          terms = new int[power+1];
  
          for(int i=0; i&lt;power; i++) {
              terms[i] = 0;
          }
  
          terms[power] = constant;
          degree = power;
      }
  
      private Polynomial(int power) {
          terms = new int[power+1];
          degree = power;
      }
  
      // Methods
      public int degree() {
          // Effects: Returns the degree of this, i.e., the largest exponent
          //  with a non-zero coefficient.  Returns 0 is this is the zero polynomial
          return degree;
      }
  
      public int coefficient(int degree) {
          // Effects: Returns the coefficient of the term of this whose exponent is degree
          if(degree &lt; 0 || degree &gt; this.degree) {
              return 0;
          }
          else {
              return terms[degree];
          }
      }
  
      public Polynomial subtract(Polynomial other) throws NullPointerException {
          // Effects: if other is null throws a NullPointerException else
          //  returns the Polynomial this - other
          return add(other.minus());
      }
  
      public Polynomial minus() {
          // Effects: Returns the polynomial - this
          Polynomial result = new Polynomial(degree);
          for(int i=0; i&lt;=degree; i++) {
              result.terms[i] = -this.terms[i];
          }
          return result;
      }
  
      public Polynomial add(Polynomial other) throws NullPointerException {
          // Effects: If other is null throws NullPointerException else
          //  returns the Polynomial this + other
          Polynomial larger, smaller;
          if (degree &gt; other.degree){
              larger = this;
              smaller = other;
          }
          else {
              larger = other;
              smaller = this;
          }
  
          int newDegree = larger.degree;
  
          if (degree == other.degree) {
              for(int k = degree; k &gt; 0 ; k--) {
                  if (this.terms[k] + other.terms[k] != 0) {
                      break;
                  }
                  else {
                      newDegree --;
                  }
              }
          }
  
  
          Polynomial newPoly = new Polynomial(newDegree);
          int i;
          for (i=0; i &lt;= smaller.degree &amp;&amp; i &lt;= newDegree; i++){
              newPoly.terms[i] = smaller.terms[i] + larger.terms[i];
          }
  
          for(int j=i; j &lt;= newDegree; j++) {
              newPoly.terms[j] = larger.terms[j];
          }
  
          return newPoly;
      }
  
      public Polynomial multiply(Polynomial other) throws NullPointerException {
          // Effects: If other is null throws NullPointerException else
          //  returns the Polynomial this * other
          if ((other.degree == 0 &amp;&amp; other.terms[0] == 0) ||
                  (this.degree==0 &amp;&amp; this.terms[0] == 0)) {
                      return new Polynomial();
          }
  
          Polynomial newPoly = new Polynomial(degree + other.degree);
  
          newPoly.terms[degree + other.degree] = 0;
  
          for(int i=0; i&lt;=degree; i++) {
              for (int j=0; j&lt;= other.degree; j++) {
                  newPoly.terms[i+j] = newPoly.terms[i+j] + this.terms[i] * other.terms[j];
              }
          }
          return newPoly;
      }
  </code></pre>