Undeclared method in test class even though it's declared in original class

Viewed 30

This may be a dumb question to some of y'all, but I'm working on a problem where we need to create a bank account class and tester, and most of it works with the exception of the "adding interest" part of it. If you look at the code, the ".withdraw" and ".deposit" parts work on the test class, just not the ".addInterest". I tried "extend" on the class to help it to read the method "addInterest" that I tried defining, but it didn't help any. Any help from y'all is appreciated!

Here is the original class - I sectioned off the interest part of it that I'm having trouble with at the bottom:

public class BankAccountInterest {
private double balance;

 /**
     Constructs a bank account with a zero balance.
  */
public BankAccountInterest()
  {   
     balance = 0;
  }

  /**
     Constructs a bank account with a given balance.
     @param initialBalance the initial balance
  */
public BankAccountInterest(double initialBalance)
  {   
     balance = initialBalance;
  }

  /**
     Deposits money into the bank account.
     @param amount the amount to deposit
  */
public void deposit(double amount)
  {  
     balance = balance + amount;
  }

  /**
     Withdraws money from the bank account.
     @param amount the amount to withdraw
  */
public void withdraw(double amount)
  {   
     balance = balance - amount;
  }

  /**
     Gets the current balance of the bank account.
     @return the current balance
  */
public double getBalance()
  {   
     return balance;
  }

// Adds Interest to Bank Account

public void addInterest(double interestRate)
{
    balance = balance + (balance * interestRate);

        
}

}

-------- And here is the test class:

public class BankAccountWithInterestTester extends BankAccountInterest
{
    public static void main(String[] args)
    {
BankAccount pabloAccount = new BankAccount();
         pabloAccount.deposit(10);
         pabloAccount.withdraw(1);
         pabloAccount.addInterest(.1);
         System.out.println("Pablo's balance is: " + pabloAccount.getBalance());

}}
1 Answers

pabloAccount is declared to be of type BankAccount.

A BankAccountInterest is not a BankAccount, therefore methods of BankAccountInterest are of no (ahem) interest.

The only place that you're going to find relevant methods are in the BankAccount class, or its superclasses, if any.

You have shown no code for the BankAccount class so it's hard to say more about it.

Related