多项式类中的错误

时间:2011-09-20 17:42:32

标签: java

我在为多项式类创建的一些方法上遇到了一些问题。对于CheckZero,我们应该检查多项式系数中是否有任何前导零。如果有,它应该重新调整系数数组的大小,但不应返回任何内容。我无法让它正常运行。对于Differentiate方法,我不断得到ArrayIndexOutOfBounds错误。

import java.util.ArrayList;
public class Poly {

    private float[] coefficients;
    public static void main (String[] args){
        float[] fa = {3, 2, 4};
        Poly test = new Poly(fa);

    }

    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) {
                //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 displayPoly () {
        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 Poly multiplyCon (double c){
        int n = getDegree();
        Poly results = new Poly(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 Poly multiplyPoly (Poly p){
        int n = getDegree();
        int m = p.getDegree();
        Poly result = null;
        for (int i = 0; i <= n; i++){
            Poly 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 Poly differentiate(){
        int n = getDegree();
        int newPolyDegree = n - 1;
        Poly newResult = new Poly();
        if (n == 0){
            newResult.setCoefficient(0, 0);
        }
        for (int i =0; i<= n; i++){
            newResult.setCoefficient(i, coefficients[i+1] * (i+1));
        }
        return newResult;
    }


    public Poly multiByConstantWithDegree(double c, int degree){ //used specifically for multiply poly
        int oldPolyDegree = this.getDegree();
        int newPolyDegree = oldPolyDegree + degree;
        Poly newResult = new Poly(newPolyDegree);
        //set all coeff to zero
        for (int i = 0; i<= newPolyDegree; i++){
            newResult.coefficients[i] = 0;
        }
        //shift by n degree
        for (int j = 0; j <= oldPolyDegree; j++){
            newResult.coefficients[j+degree] = coefficients[j] * (float)c;
        }

        return newResult;
    }
}

编辑:哎呀。复制了错误的版本。固定

2 个答案:

答案 0 :(得分:1)

CheckZero()方法中,您从未调整过数组的大小。另请注意,testArray引用与coefficients完全相同的变量,因此首先不会发生复制。

您需要做的是找到领先系数的程度,创建具有该大小的新float[],将非零系数复制到新数组,并将coefficients设置为等于

differentiate()方法中,您尝试在setCoefficient()上致电newResultnull初始化为{{1}}。因此空指针异常。

答案 1 :(得分:0)

public Poly differentiate(){
  int n = getDegree();
  int newPolyDegree = n - 1;
  Poly newResult = null;
  if (n == 0){
    newResult.setCoefficient(0, 0);
  }
  for (int i =0; i<= n; i++){
    newResult.setCoefficient(i, coefficients[i+1] * (i+1));
  }
  return newResult;
}

请注意 newResult = null 。然后 newResult.setCoefficient 。明显的NullPointerException。

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

在上面一个明显的问题是 float testArray [] =系数; 这是设置 testArray 指向与系数<相同的内存位置/ strong>即可。因此,当您更改 testArray 中的值时,您也会更改系数