简体   繁体   中英

inheritance and passing parameters through methods

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.

}

So far, I am having trouble with part 5. For starters, this is not stand alone code. It is a larger part of a bank account program. The 'teller' part is the code that is actually run.

My problem is I cannot get 'SavingsAccount' to clean compile. No process I try for part 5 works. It says it cannot read the symbol, but I'm not sure how exactly it can be wrong. I also compiled the 'teller' file, but it won't compile without quite a few errors. The biggest being that it can't find classes and symbols that I haven't touched in the first place. I'm not sure if that is due to the fact I haven't got SavingsAccount to compile cleanly or not.

COMPILER ERROR: Activity-10-1\\SavingsAccount.java:68: error: cannot find symbol deposit (getBalance() * (interestRate / 12.0)); ^ symbol: method getBalance() location: class SavingsAccount 1 error

Tool completed with exit code 1

You need to get step 1 done before step 5 will work. As it stands, SavingsAccount inherits from Object , not from BankAccount . Try changing

public class SavingsAccount

to

public class SavingsAccount extends BankAccount

At that point, the getBalance() method (which I'm guessing is defined in the BankAccount class) should become accessible.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM