【发布时间】: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