我试图获得利息和总余额,但我不明白我的代码有什么问题public class Account { private double bal; //The current balance private int accnum; //The account number public String owner; public int balance; public Account() { } public Account(int a) { bal=0.0; accnum=a; } public void deposit(double sum) { if (sum>0) bal+=sum; else System.err.println("Account.deposit(...): " +"cannot deposit negative amount."); } public void withdraw(double sum) { if (sum>0) bal-=sum; else System.err.println("Account.withdraw(...): " +"cannot withdraw negative amount."); } public double getBalance() { return bal; } public double getAccountNumber() { return accnum; } public String toString() { return "Acc: " + accnum + ": " + "Balance: " + bal; } public final void print() { //Don't override this, //override the toString method System.out.println( toString() ); } }班级 SavingsAccountpublic class SavingsAccount extends Account{ private double monthlyInterestRate; public SavingsAccount( int a, double mI) { super(a); monthlyInterestRate = mI; } public double getInterest() { return monthlyInterestRate*super.getBalance(); } public double totalBalance(){ return super.getBalance() + getInterest(); } public String toString() { return "Interest: " + getInterest() + "\n" + "Total balance: " + totalBalance(); } }
1 回答
慕桂英4014372
TA贡献1871条经验 获得超13个赞
在你的代码中
public SavingsAccount( int a, double mi) { super(a); 每月利率 = 毫;}
你在打电话
超级(一);
在 super 那是你拥有的帐户
公共账户(int a) {
bal=0.0; accnum=a; }
所以你的余额是 0,因此 public double getInterest() 返回 0 因为
monthlyInterestRate*super.getBalance();
等于monthlyInterestRate * 0
添加回答
举报
0/150
提交
取消