SavingsAccount.java
/**
An account that earns interest at a fixed rate.
*/
public class SavingsAccount extends BankAccount
{
private double interestRate;
/**
Constructs a bank account with a given interest rate.
@balance initialBalance the initial balance
@param rate the interest rate
*/
public SavingsAccount(double initialBalance, double rate)
{
// your work here
}
/**
Adds the earned interest to the account balance.
*/
public void addInterest()
{
double interest = getBalance() * interestRate / 100;
deposit(interest);
}
// This method checks your work. Do not modify it.
public static double check(double initialBalance, double rate)
{
SavingsAccount account = new SavingsAccount(initialBalance, rate);
account.addInterest();
return account.getBalance();
}
}
Use the following file:
BankAccount.java
/**
A bank account has a balance that can be changed by
deposits and withdrawals.
*/
public class BankAccount
{
private double balance;
/**
Constructs a bank account with a zero balance.
*/
public BankAccount()
{
balance = 0;
}
/**
Constructs a bank account with a given balance.
@param initialBalance the initial balance
*/
public BankAccount(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;
}
}