java - 类不是抽象的,不会覆盖抽象方法错误
问题描述
我一直在研究一个程序,它可以为学校找到多项式的根,并执行各种其他多项式相关的事情,例如将它们加在一起或找到具有给定 x 的多项式的值。虽然我制作的其他两个类工作正常(它们找到了 sin 和 cos 函数的根),但我的 FuncPoly 类似乎与我的评估方法发生了某种冲突,并且不想继承它。这是我的确切错误信息:
.\FuncPoly.java:1: 错误:FuncPoly 不是抽象的,并且不会覆盖 Function public class FuncPoly extends Function{ 中的抽象方法 evaluate(double)
我已经尝试稍微摆弄一下评估方法并尝试添加一些覆盖,但它对我的帮助并不大。我需要帮助找出导致此错误的原因;也许它与我的构造函数有关?感谢您的阅读和帮助!代码如下;那里有很多,但我认为其中大部分与这个问题无关。
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;
}
}
解决方案
据我所知,您的value()
方法FuncPoly
需要重命名才能evaluate()
成功实现类中的抽象evaluate()
方法Function
。
推荐阅读
- javascript - 尽管使用了 setState(),但 React 组件在状态更改后不会重新渲染
- algorithm - 如何确定在较大网格内产生最大数量的正方形的布局
- javascript - 我想问一下行的总和
- azure-active-directory - 在 B2C 租户上创建用户:由 AzureAD 上的用户创建触发
- javascript - Ionic - 解析 JSON 内容并将其显示在模态控制器中
- javascript - 如何在组件中设置状态确实在调度动作后挂载
- wordpress - WordPress 默认裁剪我的一些图像
- git - gitignore:*.o 和 /**.o 之间的区别
- r - 改变ggplot上线图的颜色
- android - 如何使用 seekbar[i].setmax(a)