I am having trouble multiplying the polynomial by a constant (double). It works when there is only one coefficient, but when there is more than one, it gives an ArrayIndexOutOfBounds error and points to the setCoefficient method. Any help? Thanks
public class Poly { private float[] coefficients; public Poly() { coefficients = new float[1]; coefficients[0] = 0; } public Poly(int degree) { coefficients = new float[degree+1]; for (int i = 0; i <= degree; i++) coefficients[i] = 0; } public Poly(float[] a) { coefficients = new float[a.length]; for (int i = 0; i < 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 Poly add(Poly p) { int n = getDegree(); int m = p.getDegree(); Poly result = new Poly(Poly.max(n, m)); int i; for (i = 0; i <= Poly.min(n, m); i++) result.setCoefficient(i, coefficients[i] + p.getCoefficient(i)); if (i <= n) {
source share