Java问题中的多项式类

时间:2014-01-29 21:48:37

标签: java arrays

我遇到了这个多项式类的问题,特别是checkZero和区分方法。 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;
    }
}

2 个答案:

答案 0 :(得分:0)

可能会有更多问题,但是你的差异化方法存在一个问题:

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

你的无参数构造函数初始化一个长度为1的数组,所以“newResult”只有1个索引,你试图把东西放到第i位,如果你所在的多项式有一个长度大于1的数组,则超过1 1。

答案 1 :(得分:0)

首先,一些代码注释:

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

    coefficients = new float[degree+1];
    for (int i = 0; i <= degree; i++)
        coefficients[i] = 0;

如果你使用三元运算符,我也会看到很多行可能会变得更具可读性和紧凑性,例如:

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));
    }

这将始终在最后一次迭代中调用系数[coefficients.length],这将始终失败。

运行此程序时异常的堆栈跟踪应该告诉您错误的确切位置。