Java问题中的多项式类

Java问题中的多项式类,java,arrays,Java,Arrays,这个多项式类,特别是checkZero和differention方法,我遇到了麻烦。checkZero类应该查看多项式中是否有任何前导系数,如果有,它应该调整系数数组的大小。微分法应该找到多项式的导数,但我一直得到ArrayIndexOutOfBounds错误 public class Polynomial { private float[] coefficients; public static void main (String[] args){ float

这个多项式类,特别是checkZero和differention方法,我遇到了麻烦。checkZero类应该查看多项式中是否有任何前导系数,如果有,它应该调整系数数组的大小。微分法应该找到多项式的导数,但我一直得到
ArrayIndexOutOfBounds
错误

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 <= degree; i++)
            coefficients[i] = 0;
    }


    public Polynomial(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 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 <= Polynomial.min(n, m); i++) 
                result.setCoefficient(i, coefficients[i] + p.getCoefficient(i));
            if (i <= n) {
                //we have to copy the remaining coefficients from this object
                for ( ; i <= n; i++) 
                    result.setCoefficient(i, coefficients[i]);
            } else {
                // we have to copy the remaining coefficients from p
                for ( ; i <= m; i++) 
                    result.setCoefficient(i, p.getCoefficient(i));
            }
        return result;
    }

    public void displayPolynomial () {
        for (int i=0; i < coefficients.length; i++)
            System.out.print(" "+coefficients[i]);
        System.out.println();
    }

    private static int max (int n, int m) {
        if (n > m)
            return n;
        return m;
    }

    private static int min (int n, int m) {
        if (n > m)
            return m;
        return n;
    }

    public Polynomial multiplyCon (double c){
        int n = getDegree();
        Polynomial results = new Polynomial(n);
        for (int i =0; i <= 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 <= 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>0; i--){
            if (coefficients[i] != 0){
                testArray[i] = coefficients[i];

            } 
            }
        for (int j = 0; j < 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<= n; i++){
            newResult.setCoefficient(i, coefficients[i+1] * (i+1));
        }
        return newResult;
    }
}
公共类多项式{
私有浮动系数;
公共静态void main(字符串[]args){
float[]fa={3,2,4};
多项式测试=新多项式(fa);
}
公共多项式(){
系数=新浮点数[1];
系数[0]=0;
}
公共多项式(整数次){
系数=新浮点数[度数+1];

对于(int i=0;i可能会有更多的问题,但您的区分方法有一个问题:

    int n = getDegree();
    ...
    Polynomial newResult = new Polynomial();
    ...
    for (int i = 0; i <= n; i++)
    {
        newResult.setCoefficient(i, coefficients[i + 1] * (i + 1));  //This line
    }
int n=getDegree();
...
多项式newResult=新多项式();
...

对于(int i=0;i首先,一些代码注释:

新数组在Java中自动初始化为0。这是不需要的

    coefficients = new float[degree+1];
    for (int i = 0; i <= degree; i++)
        coefficients[i] = 0;
系数=新浮点数[度数+1];

对于(int i=0;i)哪一行引发异常?您以前没有问过这个问题吗?您发布的代码太多了。异常应该确切地告诉您哪一行是问题所在,发布该部分并在必要时展开。更好的是,使用调试器并逐步完成代码。
int i;
        for (i = 0; i <= Polynomial.min(n, m); i++) 
            result.setCoefficient(i, coefficients[i] + p.getCoefficient(i));
        if (i <= n) {
            //we have to copy the remaining coefficients from this object
            for ( ; i <= n; i++) 
                result.setCoefficient(i, coefficients[i]);
        } else {
            // we have to copy the remaining coefficients from p
            for ( ; i <= m; i++) 
                result.setCoefficient(i, p.getCoefficient(i));
        }
        for (int i = 0; i <= result.getDegree(); i++) 
            result.setCoefficient(i, 
              i>n?0:coefficients[i] + 
              i>m?0:p.getCoefficient(i));
int n = getDegree();
....
for (int i =0; i<= n; i++){
        newResult.setCoefficient(i, coefficients[i+1] * (i+1));
    }