Hi all, I just make a program and I get in trouble about set method.so anyone can help me?
import java.util.LinkedList;
public class Polynomial
{
private LinkedList<Integer> coeff = new LinkedList<Integer>();
private int deg = 0;
/* Constructs a new polynomial of degree zero */
public Polynomial()
{
}
/* Returns an integer representing the coefficient
* of the x^power term
*/
int getCoefficients(int power)
{
return coeff.get(power);
}
/* Sets the coefficient of the x^power term to coef.*/
void setCoefficients(int coef, int power)
{
}
/* Returns the String representation of the polynomial.
* For example, a polynomial can be represented as either
* 3 * x^2 + 2 * x + 1 or
* 3x^2 + 2x + 1
* Any term whose coefficient is zero should not appear
* in the string unless the polynomial has only a single
* constant term of zero.
*/
public String toString()
{
if(deg == 0)
return "" + coeff.get(0);
if(deg == 1)
return coeff.get(1) + "x^ " + coeff.get(0);
String p = coeff.get(deg) + "x^ " + deg;
for(int i = deg-1;i>=0;i--)
{
if(coeff.get(i) == 0) continue;
else if (coeff.get(i)> 0)
p = p + " + " +(coeff.get(i));
else if (coeff.get(i) < 0)
p = p + " + " +(-coeff.get(i));
if(i == 1)
p = p + "x ";
else if (i >= 1)
p = p + "x^ " + i;
}
return p;
}
/* Evaluates the polynomial for the value x and returns
* the result p(x).
*/
double evaluate(double x)
{
double n = 0;
for(int i = deg; i>=0; i--)
n = coeff.get(i) + (x * n);
return n;
}
/* Add to this polynomial the polynomial "other" and
* return the resulting polynomial.
*/
Polynomial add(Polynomial other)
{
Polynomial a = this;
Polynomial b = new Polynomial(0, Math.max(a.deg, other.deg));
for (int i = 0; i <= a.deg; i++)
b.coeff.set(i, b.coeff.get(i) + a.coeff.get(i));
for (int i = 0; i <= b.deg; i++)
b.coeff.set(i, b.coeff.get(i) + other.coeff.get(i));
return b;
}
/*
* This method is to get a * x^b.
*/
public Polynomial(int a ,int b)
{
setCoefficients(a, b);
}
/*
* This method is return the power of each coefficient.
*/
public int degree()
{
int d = 0;
for(int i = 0; i<=coeff.size();i++)
if(coeff.get(i)!=0)
d = i;
return d;
}
}