类不是抽象的,并且不会覆盖抽象方法错误

时间:2019-09-18 03:18:47

标签: java compiler-errors

我一直在研究一个程序,该程序可以找到学校的多项式的根,并且可以做其他与多项式有关的事情,例如将它们加在一起或找到具有给定x的多项式的值。尽管我使其他两个类工作正常(它们找到了sin和cos函数的根),但我的FuncPoly类似乎与我的评估方法有某种冲突,并且不想继承它。这是我的确切错误消息:

。\ FuncPoly.java:1:错误:FuncPoly不是抽象的,并且不覆盖Function中的抽象方法Evaluate(double) 公共类FuncPoly扩展了功能{

我尝试摆弄评估方法并尝试添加一些替代,但是它并没有太大帮助。我需要帮助来查找导致此错误的原因;也许与我的构造函数有关?感谢您的阅读和帮助!代码如下;里面有很多东西,但是我不认为这大部分与问题有关。


    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;
    }

}

1 个答案:

答案 0 :(得分:2)

据我所知,您需要将value()中的FuncPoly方法重命名为evaluate(),才能成功实现{{1} }类。