Java方法中的抽象方法不能正常工作

时间:2014-11-09 23:58:03

标签: java abstract-class abstract

我无法在以下问题上显示复利和贷款。我必须有一个抽象的超类和两个方法集合并获得存储原则数量,以及抽象集合并获得存储率和年份。当我运行程序时,我对我做错了什么一无所知,无论我做什么,化合物和贷款都保持在0.0。请帮忙!!谢谢!哦,我还必须创建我知道如何做的对象的引用。注意:我最初有公共双年和公共双倍率,但由于它不起作用我创建了year2 rate2等。

public static void main(String[] args) {
        //Instance of Scanner
        Scanner input = new Scanner(System.in);

        //Instances
        Guarantee guara = new Guarantee();
        CompoundInterest compI = new CompoundInterest();
        Loan borrow = new Loan();



        System.out.println("Please enter the principle amount: ");
        double principleAmount = input.nextDouble();

        System.out.println("Please enter the rate ");
        double rate = input.nextDouble();
        System.out.println("Please enter the years : ");
        double years = input.nextDouble();

        Funds ref = guara;
        ref.setPrincipleAmount(principleAmount);
        ref.setData(rate, years);
        System.out.printf("With a principle amount of $%.2f , an interest rate of %.2f, and %.2f years, you would have earned $%.2f\n", principleAmount, rate, years, guara.getData());


        ref = compI;
        ref.setData(rate, years);
        System.out.printf("With a principle amount of $%.2f, an interest rate of %.2f, and %.2f, you would have a compound interest of $%.2f\n", principleAmount, rate, years, ref.getData());




        //Instances



    }//End main

}//End class


//Super class
abstract class Funds{
    //Instance variables
    public double principleAmount;
    public double rate;
    public double years;
    public double rate2;
    public double rate3;
    public double year2;
    public double year3;
    public void setPrincipleAmount(double principleAmount){
        this.principleAmount = principleAmount;
    }//End setPrincipleAmount

    public double getPrincipleAmount(){
        return principleAmount;
    }//End getPrincipleAmount

    public abstract void setData(double rate, double years);
    public abstract  double getData();




}//End abstract class Funds


class Guarantee extends Funds{

    @Override
    public void setData(double rate, double years) {
        this.rate = rate;
        this.years = years;

    }

    @Override
    public double getData() {
        return (principleAmount * rate * years);


    }




}//End sub class Guarantee


class CompoundInterest extends Funds{

    @Override
    public void setData(double rate, double years) {
        // TODO Auto-generated method stub
        this.rate = rate2;
        this.years = year2;

    }

    @Override
    public double getData() {
        // TODO Auto-generated method stub
        return (principleAmount * Math.pow(1 + rate2, year2));
    }




}//End sub class CompundInterest

class Loan extends Funds{

    @Override
    public void setData(double rate, double years) {
        // TODO Auto-generated method stub
        this.rate = rate3;
        this.years = year3;
    }

    @Override
    public double getData() {
        // TODO Auto-generated method stub
        return (principleAmount * rate3 * year3) + principleAmount;
    }

1 个答案:

答案 0 :(得分:0)

 @Override
public void setData(double rate, double years) {
    // TODO Auto-generated method stub
    this.rate = rate2;
    this.years = year2;

}

应该是

 @Override
public void setData(double rate, double years) {
    // TODO Auto-generated method stub
    this.rate2 = rate;
    this.years2 = year;

}

请重新检查您的作业。它们似乎不正确。