我一直在开发一个程序,它可以找到学校多项式的根,并做各种其他多项式相关的事情,比如将它们相加或找到给定x的多项式的值。虽然我制作的另外两个类工作得很好(它们找到了sin和cos函数的根),但我的FuncPoly类似乎与我的评估方法发生了冲突,并且不想继承它。这是我确切的错误信息:
.\FuncPoly.java: 1:错误:FuncPoly不是抽象的,并且不会覆盖Function公共类FuncPoly扩展Function{中的抽象方法计算(双精度)
我试着稍微摆弄了一下评估方法,并试图添加一些覆盖,但它对我没有太大帮助。我需要帮助找到导致此错误的原因;也许它与我的构造函数有关?。感谢阅读和您的帮助!代码如下;里面有很多,但我认为大部分与问题无关。
public abstract double evaluate(double x); //Basically just a filler for SinFunc and CosFunc
public double findRoot(double a, double b, double epsilon){
double x = ( a + b ) / 2;
if (Math.abs( a - x) <= epsilon){
return x;
}else if (evaluate(x)*evaluate(a) >= 0){
return findRoot(x, b, epsilon);
}else{
return findRoot(a, x, epsilon);
}
}
public static void main(String[] args){
//Tests SinFunc and CosFunc
SinFunc q = new SinFunc();
CosFunc w = new CosFunc();
System.out.println("The root of sin(x) with the numbers entered with the given epsilon is: " + q.findRoot(3,4,.00000001));
System.out.println("The root of cos(x) with the numbers entered with the given epsilon is: " + w.findRoot(1,3,.00000001));
//Tests the FuncPoly stuff
int[] test1 = {1,0,-3};
int[] test2 = {1,-1,-2};
FuncPoly poly1 = new FuncPoly(test1);
FuncPoly poly2 = new FuncPoly(test2);
System.out.println("The root of x^2 + (-3) is" + poly1.findRoot(0,10,.00000001));
}
}
____________________________
public class FuncPoly extends Function{
public int coefficients[];
public FuncPoly(int[] coefficients){
//Constructor
this.coefficients = coefficients;
}
public int degree(){
//Finds the highest power by finding the location of the last number in the array.
return this.coefficients.length - 1;
}
public String toString(){
//Converts a polynomial to a string
StringBuilder poly = new StringBuilder();
for(int i = degree(); i >= 0; i--){
if (i == degree()){
poly.append(this.coefficients[i] + "x^" + degree());
}else{
if (this.coefficients[i] == 0){
System.out.println(i);
}else if (i == 0){
poly.append(" + " + this.coefficients[0]);
} else if ( i == 1){
poly.append(" + " + this.coefficients[1] + "x");
} else{
poly.append(" + " + this.coefficients[i] + "x^" + i);
}
}
}
return poly.toString();
}
public FuncPoly add(FuncPoly a){
//Adds the selected polynomial and the last called polynomial together and returns the array.
if (this.degree() > a.degree()){
int[] polyAdd = new int[this.degree() + 1];
for (int i = 0; i <= a.degree(); i++){
polyAdd[i] = a.coefficients[i] + this.coefficients[i];
}
for (int i = a.degree() + 1; i < this.degree() + 1; i++){
polyAdd[i] = this.coefficients[i];
}
FuncPoly polyResult = new FuncPoly(polyAdd);
return polyResult;
} else if (this.degree() < a.degree()){
int[] polyAdd = new int[a.degree() + 1];
for (int i = 0; i <= this.degree(); i++){
polyAdd[i] = a.coefficients[i] + this.coefficients[i];
}
for (int i = this.degree() + 1; i < degree() + 1; i++){
polyAdd[i] = a.coefficients[i];
}
FuncPoly polyResult = new FuncPoly(polyAdd);
return polyResult;
} else {
int[] polyAdd = new int[a.degree() + 1];
for (int i = 0; i < a.degree() + 1; i++){
polyAdd[i] = a.coefficients[i] + this.coefficients[i];
}
FuncPoly polyResult = new FuncPoly(polyAdd);
return polyResult;
}
}
public double value(double x){
//Finds the value of polynomial with a given x.
double sum = 0;
for(int i = 0; i < this.degree() + 1; i++){
sum += this.coefficients[i] * Math.pow(x,i);
}
return sum;
}
}
据我所知,您在FuncPoly
中的value()
方法需要重命名为评估()
才能成功实现Function
类中的抽象评估()
方法。