【问题标题】:Abstract Methods In Java Method Not Woring ProperlyJava方法中的抽象方法无法正常工作
【发布时间】:2015-01-06 05:23:31
【问题描述】:

我在显示以下问题的复利和贷款时遇到问题。我必须有一个抽象超类和两个方法,一个用来存储本金金额,一个抽象集用来存储利率和年份。当我运行程序时,我对自己做错了什么一无所知,无论我做什么,化合物和贷款都保持在 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;
    }

【问题讨论】:

  • 看起来你还没有找到任务。在x = y;中,变量x获取变量y的值。所以你所有的任务都是错误的。 this.rate = rate3; 应该是 this.rate3 = rate;,以此类推。
  • 嗨!感谢您回答我的问题,但是,我只是将图像向后镜像。 Y = X 并且它为真,因为 x = y y 必须等于 x。我已经这样尝试过了,它仍然保持在 0.0
  • x = y 不是比较,而是分配。倒车是不明智的。
  • 是的,只是确认变量的分配不会影响任何事情。非常感谢您的回答!
  • 这段代码中还有很多其他的错误,但这是最初会给您带来最多问题的错误。先尝试一个更简单的问题;你第一次尝试就吃得太多了。

标签: java abstract-class abstract


【解决方案1】:
 @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;

}

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

【讨论】:

  • 好吧,但我最初让我的代码让所有类都这样做:this.rate = rate; this.years = 年。我知道在上面它必须是 this.rate2 = rate; this.rate3 = 速率;正确的?还是我错过了什么?但是仍然有所有这些变量分配,我已经翻转,放回,销毁,重新创建,制作更多,但我的复合和贷款保持在 0.0
猜你喜欢
  • 2012-02-20
  • 2013-11-08
  • 1970-01-01
  • 2019-04-01
  • 2019-04-04
  • 1970-01-01
  • 2019-06-02
  • 2021-12-29
  • 2014-09-29
相关资源
最近更新 更多