通过方法继承和传递参数

时间:2016-01-23 23:15:43

标签: java inheritance

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部分的问题。对于初学者来说,这不是独立的代码。它是银行账户计划的一个重要组成部分。出纳员' part是实际运行的代码。

我的问题是我无法获得' SavingsAccount'清理编译。我没有尝试第5部分的工作。它说它无法读取符号,但我不确定它究竟是错误的。我还编辑了出纳员'文件,但它不会编译没有很多错误。最大的问题是,它无法找到我从未触及到的类别和符号。我不确定这是否是因为我没有得到SavingsAccount干净利落地编译。

COMPILER ERROR:Activity-10-1 \ SavingsAccount.java:68:错误:找不到符号       存款(getBalance()*(interestRate / 12.0));                ^   符号:方法getBalance()   location:类SavingsAccount 1错误

工具已完成,退出代码为1

1 个答案:

答案 0 :(得分:1)

您需要在第5步完成之前完成第1步。目前,SavingsAccount继承自Object,而不是BankAccount。尝试更改

public class SavingsAccount

public class SavingsAccount extends BankAccount

此时,getBalance()方法(我猜测在BankAccount类中定义)应该可以访问。