import java.text.NumberFormat;
// 1. ***** indicate that SavingsAccount inherits
// from BankAccount
public class SavingsAccount
{
public final double DEFAULT_RATE = .03;
// 2. ****** define the private interestRate instance variable
// interestRate, a double, represents an annual rate
// Part 2 student code starts here:
private double interestRate;
// Part 2 student code ends here.
// 3 ***** write the default constructor
/** default constructor
* explicitly calls the BankAccount default constructor
* set interestRate to default value DEFAULT_RATE
* print a message to System.out indicating that
* constructor is called
*/
// Part 3 student code starts here:
public void BankAccount()
{
interestRate = DEFAULT_RATE;
System.out.println("Default constructor is called. ");
}
// Part 3 student code ends here.
// 4 ***** write the overloaded constructor
/** overloaded constructor
* explicitly call BankAccount overloaded constructor
* call setInterestRate method, passing startInterestRate
* print a message to System.out indicating that
* constructor is called
* @param startBalance starting balance
* @param startInterestRate starting interest rate
*/
// Part 4 student code starts here:
public void BankAccount(double startBalance,double startInterestRate)
{
setInterestRate(startInterestRate);
System.out.println("Overloaded constructor is called. ");
}
// Part 4 student code ends here.
// 5 ****** write this method:
/** applyInterest method, no parameters, void return value
* call deposit method, passing a month's worth of interest
* remember that interestRate instance variable is annual rate
*/
// Part 5 student code starts here:
public void applyInterest()
{
deposit ( super.getBalance() * (interestRate / 12.0));
}
// Part 5 student code ends here.
/** accessor method for interestRate
* @return interestRate
*/
public double getInterestRate()
{
return interestRate;
}
/** mutator method for interestRate
* @param newInterestRate new value for interestRate
* newInterestRate must be >= 0.0
* if not, print an error message
*/
public void setInterestRate(double newInterestRate)
{
if (newInterestRate >= 0.0)
{
interestRate = newInterestRate;
}
else
{
System.err.println("Interest rate cannot be negative");
}
}
// 6 ***** write this method
/* toString method
* @return String containing formatted balance and interestRate
* invokes superclass toString to format balance
* formats interestRate as percent using a NumberFormat object
* To create a NumberFormat object for formatting percentages
* use the getPercentInstance method in the NumberFormat class,
* which has this API:
* static NumberFormat getPercentInstance( )
*/
// Part 6 student code starts here:
public String toString()
{
NumberFormat percent = NumberFormat.getPercentInstance();
return super.toString()
+ "\n" + "interestRate is " + percent.format(interestRate);
}
// Part 6 student code ends here.
}
到目前为止,我在第5部分中遇到了麻烦。对于初学者来说,这并不是独立的代码。它是银行帐户计划的较大部分。 “讲述人”部分是实际运行的代码。
我的问题是我无法获取“ SavingsAccount”进行清理。对于第5部分,我没有尝试任何过程。它说它看不懂该符号,但是我不确定它到底有多错误。我还编译了“ teller”文件,但没有很多错误就不会编译。最大的优点是它找不到我一开始没有碰过的类和符号。我不确定这是否是由于我没有SavingsAccount可以干净编译的事实。
编译器错误:Activity-10-1 \ SavingsAccount.java:68:错误:找不到符号
存款(getBalance()*(interestRate / 12.0));
^
符号:方法getBalance()
位置:SavingsAccount类
1个错误
工具已完成,退出代码为1
最佳答案
您需要先完成步骤1,然后才能执行步骤5。就目前而言,SavingsAccount
继承自Object
,而不是BankAccount
。尝试改变
public class SavingsAccount
至
public class SavingsAccount extends BankAccount
那时,
getBalance()
方法(我猜是在BankAccount
类中定义的)应该可以访问了。