1. Add a method public void addInterest(double rate) to the BankAccount class th
ID: 3638681 • Letter: 1
Question
1. Add a method
public void addInterest(double rate)
to the BankAccount class that adds interest at the given rate. For example, after the statements
BankAccount momsSavings = new BankAccount(1000);
momsSavings.addInterest(10); // 10% interest
the balance in momsSavings is $1,100.
Also supply a BankAccountTester class that prints the actual and expected balance.
/**
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)
{
double newBalance = balance + amount;
balance = newBalance;
}
/**
Withdraws money from the bank account.
@param amount the amount to withdraw
*/
public void withdraw(double amount)
{
double newBalance = balance - amount;
balance = newBalance;
}
/**
Gets the current balance of the bank account.
@return the current balance
*/
public double getBalance()
{
return balance;
}
/**
. . .
*/
public void addInterest(double rate)
{
. . .
}
}
/**
Tests the bank account class with interest.
*/
public class BankAccountTester
{
public static void main(String[] args)
{
BankAccount momsSavings = new BankAccount(1000);
momsSavings.addInterest(10);
System.out.print("Balance: ");
System.out.println(. . .);
System.out.println("Expected: . . .");
}
}
Explanation / Answer
/** 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) { double newBalance = balance + amount; balance = newBalance; } /** Withdraws money from the bank account. @param amount the amount to withdraw */ public void withdraw(double amount) { double newBalance = balance - amount; balance = newBalance; } /** Gets the current balance of the bank account. @return the current balance */ public double getBalance() { return balance; } /** . . . * @return */ public double addInterest(double rate) { balance = balance * rate + balance; return balance; } } /** Tests the bank account class with interest. */ public class BankAccountTester { public static void main(String[] args) { BankAccount momsSavings = new BankAccount(1000); System.out.println("old Balance: " +momsSavings.getBalance());// getBalanse momsSavings.addInterest(.1); System.out.println("new Balance: " +momsSavings.getBalance());// getBalanse System.out.println(momsSavings); System.out.println("Expected:1120"); } }
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.