I have to do my first assignment for my java class for friday this week. So due in 3 days. I wrote the polynomial class but haven't done the driver for it yet as i don't know if my polynomial class is good. Please check and let me know if i have done correctly according to my assignment details (assignment pdf file is attached). I am missing the numberOfTerms as i can't seem to figure that out.
Please help me out.
Polynomial.java
public class Polynomial
{
private int[] coeff = new int[5];
public Polynomial()
{
coeff = null;
}
// constructor with 5 parameter
public Polynomial(int c0, int c1, int c2, int c3, int c4)
{
this.coeff[0] = c4;
this.coeff[1] = c3;
this.coeff[2] = c2;
this.coeff[3] = c1;
this.coeff[4] = c0;
}
// copy constructor
public Polynomial(Polynomial p)
{
coeff = p.coeff;
}
// Get Method
public int getCoeff(int i)
{
return this.coeff[i];
}
// Set Method
public boolean setCoef(int pos, int num)
{
if (pos <= 4 || pos >= 0 )
{
coeff[pos] = num;
return true;
}
else
return false;
}
// toString
public String toString()
{
return coeff[4]+"X4"+" + "+coeff[3]+"X3"+" + "+coeff[2]+"X2"+" + "+coeff[1]+"X"+" + "+coeff[0];
}
// Equals
public boolean equals(Polynomial p)
{
if ((p.coeff[4] != coeff[4]) ||(p.coeff[3] != coeff[3]) ||(p.coeff[2] != coeff[2]) ||(p.coeff[1] != coeff[1]) ||(p.coeff[0] != coeff[0]) )
return false;
return true;
}
// Add
public Polynomial add(Polynomial p)
{
return (new Polynomial(p.coeff[4]+coeff[4],p.coeff[3]+coeff[3],p.coeff[2]+coeff[2],p.coeff[1]+coeff[1],p.coeff[0]+coeff[0]));
}
// Derivative
public Polynomial derive()
{
return (new Polynomial(coeff[4]*4,coeff[3]*3,coeff[2]*2,coeff[1],0));
}
// Evaluate
public double evaluate(double x){
return coeff[4]*x*x*x*x+coeff[3]*x*x*x+coeff[2]*x*x+coeff[1]*x+coeff[0];
}
}