/* A Polynomial has all its coefficients in an array coeffs */
class Polynomial{
  
  // coeffs[i] is the coefficient of the term x^i
  protected double[] coeffs;
  
  /* This Polynomial has coefficients as specified in the array*/
  public Polynomial(double[] coefficients){
    coeffs = new double[coefficients.length];
    for(int i=0; i<coeffs.length; i++)
      coeffs[i] = coefficients[i];
  }
  
  /* =degree of this Polynomial */
  public int getDegree(){
    int d = coeffs.length-1;
    while((coeffs[d] == 0) && (d > 0))
      d--;
    return d;
  }
  
  /* ={This Polynomial is the same as Polynomial  p}, true or false */
  public boolean equals(Polynomial p){
    if(getDegree() != p.getDegree())
      return false;
    for(int i=0; i<=getDegree(); i++)
      if(coeffs[i] != p.coeffs[i])
        return false;
    return true;
  }

  public double evaluate(double x)
  {
    double sum = 0;
    double input = 1;
    for(int k=0; k<coeffs.length; k++)
    {
      sum += coeffs[k]*input;
      input *= x;
    }
    return sum;
  }

}
