title: Polynomial Class in Java problems
body: <p>I'm having trouble with this polynomial class, specifically the checkZero and differentiate methods. The checkZero class is supposed to see if there are any leading coefficients in the polynomial, and if so, it should resize the coefficient array. The differentiate method should find the derivative of a polynomial, but I keep getting <code>ArrayIndexOutOfBounds</code> errors.</p>
  
  <pre><code>public class Polynomial {
  
      private float[] coefficients;
      public static void main (String[] args){
          float[] fa = {3, 2, 4};
          Polynomial test = new Polynomial(fa);
  
      }
  
      public Polynomial() {
          coefficients = new float[1];
          coefficients[0] = 0;
      }
  
      public Polynomial(int degree) {
          coefficients = new float[degree+1];
          for (int i = 0; i &lt;= degree; i++)
              coefficients[i] = 0;
      }
  
  
      public Polynomial(float[] a) {
          coefficients = new float[a.length];
          for (int i = 0; i &lt; a.length; i++)
              coefficients[i] = a[i];
      }
  
      public int getDegree() {
          return coefficients.length-1;
      }
  
      public float getCoefficient(int i) {
          return coefficients[i];
      }
  
      public void setCoefficient(int i, float value) {
          coefficients[i] = value;
      }
  
      public Polynomial add(Polynomial p) {
          int n = getDegree();
          int m = p.getDegree();
          Polynomial result = new Polynomial(Polynomial.max(n, m));
          int i;
  
              for (i = 0; i &lt;= Polynomial.min(n, m); i++) 
                  result.setCoefficient(i, coefficients[i] + p.getCoefficient(i));
              if (i &lt;= n) {
                  //we have to copy the remaining coefficients from this object
                  for ( ; i &lt;= n; i++) 
                      result.setCoefficient(i, coefficients[i]);
              } else {
                  // we have to copy the remaining coefficients from p
                  for ( ; i &lt;= m; i++) 
                      result.setCoefficient(i, p.getCoefficient(i));
              }
          return result;
      }
  
      public void displayPolynomial () {
          for (int i=0; i &lt; coefficients.length; i++)
              System.out.print(" "+coefficients[i]);
          System.out.println();
      }
  
      private static int max (int n, int m) {
          if (n &gt; m)
              return n;
          return m;
      }
  
      private static int min (int n, int m) {
          if (n &gt; m)
              return m;
          return n;
      }
  
      public Polynomial multiplyCon (double c){
          int n = getDegree();
          Polynomial results = new Polynomial(n);
          for (int i =0; i &lt;= n; i++){ // can work when multiplying only 1 coefficient
              results.setCoefficient(i, (float)(coefficients[i] * c)); // errors ArrayIndexOutOfBounds for setCoefficient
             }
  
          return results;
         }
  
      public Polynomial multiplyPoly (Polynomial p){
          int n = getDegree();
          int m = p.getDegree();
          Polynomial result = null;
          for (int i = 0; i &lt;= n; i++){
              Polynomial tmpResult = p.multiByConstantWithDegree(coefficients[i], i); //Calls new method
              if (result == null){
                  result = tmpResult;
              } else {
                  result = result.add(tmpResult);
              }
          }
          return result;
      }
  
      public void checkZero(){
  
          int newDegree = getDegree();
          int length = coefficients.length;
          float testArray[] = coefficients;
  
          for (int i = coefficients.length-1; i&gt;0; i--){
              if (coefficients[i] != 0){
                  testArray[i] = coefficients[i];
  
              } 
              }
          for (int j = 0; j &lt; testArray.length; j++){
              coefficients[j] = testArray[j];
          }
      }    
  
      public Polynomial differentiate(){
  
          int n = getDegree();
          int newPolyDegree = n - 1;
          Polynomial newResult = new Polynomial();
  
          if (n == 0){
              newResult.setCoefficient(0, 0);
          }
  
          for (int i =0; i&lt;= n; i++){
              newResult.setCoefficient(i, coefficients[i+1] * (i+1));
          }
          return newResult;
      }
  }
  </code></pre>