多项式Java

时间:2014-01-28 05:07:25

标签: java arrays

我正在尝试为Polynomial类制作两种方法,但我遇到了麻烦。 第一种方法checkZeros应该检查多项式的系数中是否存在任何前导零。如果存在前导零,则该方法应调整系数数组的大小。第二种方法应该找到多项式的导数,但我不断得到ArrayIndexOutOfBounds错误。

他们是:

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 void checkForZeros(){
   int newDegree = getDegree();
   int length = coefficients.length;
   double testArray[] = coefficients;

   for (int i = 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;
   }
}

3 个答案:

答案 0 :(得分:1)

我怀疑问题在这里

   for (int i =0; i<= n; i++){
      newResult.setCoefficient(i, coefficients[i+1] * (i+1));
   }

n = getDegree();开始,我们假设多项式是1度(例如1 + x)。然后n = 1我猜,系数的长度为2.但是你要检查系数[2](因为你有i + 1)超出界限。我猜你想要

   for (i=0; i<=newPolyDegree; i++){
      newResult.setCoefficient(i, coefficients[i] * (i+1));
   }

或者其他什么......很难说出你提供的代码量。

答案 1 :(得分:1)

你最有可能获得ArrayIndexOutofBounds,因为你以错误的方式实现了checkzeroes,因此getdegree()返回的粒度小于系数数组。考虑以下多项式:
f(x)= 2x ^ 3 + 5x + 1
系数数组将为
[2,0,5,1]
检查后,它变成了 [2,5,1](因为你删除所有零,而不仅仅是前导零。)
我认为度函数仍将返回3,并且你将在Differeate()

中用尽数组边界

答案 2 :(得分:0)

错误在循环中。

for (int i =0; i<= n; i++){
      newResult.setCoefficient(i, coefficients[i+1] * (i+1));

基于少量信息,我假设: 1. setCoefficient(x的幂,x的系数)

因此,在这种情况下,我们有一个n阶多项式用于区分,它具有n + 1项,从0-> n(x ^ 0 - > x ^ n)开始 看看循环,当i = n时,它必须获取不存在的x ^(n + 1)系数。 你应该这样做。

for (int i =0; i< n; i++){
      newResult.setCoefficient(i, coefficients[i+1] * (i+1));