gpt4 book ai didi

java - 多项式类中的错误

转载 作者:行者123 更新时间:2023-12-01 15:44:02 24 4
gpt4 key购买 nike

我为多项式类创建的一些方法遇到了一些问题。对于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;
}
}

编辑:哎呀。复制了错误的版本。已修复

最佳答案

在您的 CheckZero() 中方法,您从未调整数组的大小。另请注意 testArray引用与 coefficients 完全相同的变量这样从一开始就没有发生任何复制。

你要做的就是找到首项系数的阶数,创建一个新的float[]使用该大小,将非零系数复制到新数组,并设置 coefficients等于。

在您的 differentiate() 中方法,您尝试调用 setCoefficient()newResult ,初始化为 null 。因此出现空指针异常。

关于java - 多项式类中的错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7489384/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com