首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >多项式类Java

多项式类Java
EN

Stack Overflow用户
提问于 2014-01-28 05:07:24
回答 3查看 9K关注 0票数 0

我试图为多项式类做两种方法,但我遇到了麻烦。第一种方法checkZeros应该检查多项式的系数中是否有前导零点。如果有前导零,该方法应该调整系数数组的大小。第二种方法应该找到多项式的导数,但我总是得到ArrayIndexOutOfBounds误差。

下面是它们:

代码语言:javascript
复制
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;
   }
}
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2014-01-28 05:19:24

我怀疑问题就在这里

代码语言:javascript
复制
   for (int i =0; i<= n; i++){
      newResult.setCoefficient(i, coefficients[i+1] * (i+1));
   }

既然是n = getDegree();,让我们假设多项式是1次的(例如1+x)。然后是n=1,我猜,系数的长度是2,但是你要检查系数2,它超出了界限。我猜你想

代码语言:javascript
复制
   for (i=0; i<=newPolyDegree; i++){
      newResult.setCoefficient(i, coefficients[i] * (i+1));
   }

或者别的什么..。很难用你给出的代码数量来判断。

票数 1
EN

Stack Overflow用户

发布于 2014-01-28 05:19:53

您很可能得到了ArrayIndexOutofBounds,因为您以错误的方式实现了校验零,因此get自由度()返回的大小小于系数数组。考虑以下多项式:

f(x) = 2x^3 + 5x +1

系数数组将是

2,0,5,1

在检查结束后,它变成

2,5,1

我想程度函数仍将返回3,并且在differentiate()中将超出数组边界。

票数 1
EN

Stack Overflow用户

发布于 2014-01-28 05:22:01

错误在循环中。

代码语言:javascript
复制
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)系数。你应该这么做。

代码语言:javascript
复制
for (int i =0; i< n; i++){
      newResult.setCoefficient(i, coefficients[i+1] * (i+1));
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21397295

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档