Bank Account Class Additional Information The constructor initializes the accoun
ID: 3713704 • Letter: B
Question
Bank Account Class Additional Information
The constructor initializes the account with a balance, an interest rate, and monthly service charges all other instance variables are set to 0
The deposit method accepts an amount, adds the amount to the balance and increases the numDeposits by 1.
The withdraw method accepts an amount, subtracts the amount from the balance and increases the numWithdrawals by 1
The calcInterest method calculates the monthly interest and adds it to the account balance.
Declare local variables named monthlyInterestRate. monthlyInterest.
Calculate the monthlyInterestRate using the following formula interestRate /12
Calculate the monthlyInterest using the formula balance *monthlyInterestRate.
Add the monthlyInterest to the balance
The monthlyProcess method subtracts the monthly service charge from the account balance , calls the calcInterest Method and sets the number of deposits and number of withdrawals to 0.
Savings Account Class Additional Information
The constructor initializes the account with a balance, an interest rate, and monthly service charges. If the balance is less than $25 the account is set as inactive. Otherwise it is set as active.
The withdraw method accepts an amount. If the account is active account if the account will override the superclass method by calling the withdraw method from the super class. Syntax: super.withdrawaI(amount). If the getBalance is <25 then status will set to false
The deposit method makes a deposit into the account by overriding the deposit method from the superclass. Syntax super.deposit(amount). If the status is false, call the getBalance and test to see if it greater than or equal to 25. If true set the status to true
The monthlyProcess method calls the super class's monthlyProcess method. If the number of withdrawals is greater than 4, the monthly service charges are increased.
In the monthlyProcess method declare a local variable named serviceCharge, double. This variable represents the monthly service charge
Using an if statement call the getNumWithdrawals method and test to see if it is greater than 4
If true you need to get the monthly service charge by calling the getMonthlyServiceCharges method and assigning it to the variable serviceCharge
Increase the monthly service charges by calling the setMonthlyServiceCharges method and sending over the serviceCharge and the getNumWithdrawals method -4
Do the monthly processing by calling the monthlyProcess method in the super
Set the monthly charges back by calling the setMonthlyServiceCharges method sending the serviceCharge
If false call the monthlyProcess method from the super class.
Main Class
Create a SavingsAccount object with a $100 balance, 3% interest rate, and a monthly service charge of $2.50.
Print out the balance
Print out the number of deposits
Print out the number of withdrawals
Make a deposit of 25
Make a deposit of 10
Make a deposit of 35
Print out the balance
Print out the number of deposits
Print out the number of withdrawals
Make a withdrawal of 100
Make a withdrawal of 50
Make a withdrawal of 10
Make a withdrawal of 1
Make a withdrawal of 1
Print out the balance
Print out the number of deposits
Print out the number of withdrawals
Call the monthly process method
Print out the balance
Print out the number of deposits
Print out the number of withdrawals
Explanation / Answer
class BankAccount
{
private double balance,interestRate,monthlyCharges;
private int numDeposits ;
private int numWithdrawals;
public BankAccount(double balance,double interestRate,double monthlyCharges)
{
this.balance = balance;
this.interestRate = interestRate;
this.monthlyCharges = monthlyCharges;
numDeposits = 0;
numWithdrawals = 0;
}
public void deposit(double amount)
{
balance = balance +amount;
numDeposits++;
}
public void withdraw(double amount)
{
balance = balance -amount;
numWithdrawals++;
}
public void calcInterest()
{
double monthlyInterestRate = interestRate/12;
double monthlyInterest = balance *monthlyInterestRate;
balance = balance + monthlyInterest;
}
public void monthlyProcess()
{
balance = balance - monthlyCharges;
calcInterest();
numDeposits = 0;
numWithdrawals = 0;
}
public double getBalance()
{
return balance;
}
public int getNumDeposits()
{
return numDeposits;
}
public int getNumWithdrawals()
{
return numWithdrawals;
}
public double getMonthlyServiceCharges()
{
return monthlyCharges;
}
public void setMonthlyServiceCharges(double monthlyCharges)
{
this.monthlyCharges = monthlyCharges;
}
}
class SavingsAccount extends BankAccount
{
private boolean active;
public SavingsAccount(double balance,double interestRate,double monthlyCharges)
{
super(balance,interestRate,monthlyCharges);
if(balance <25)
active = false;
else
active = true;
}
public void withdraw(double amount)
{
if(active == true)
super.withdraw(amount);
if(getBalance() < 25)
active = false;
}
public void deposit(double amount)
{
if(active == true)
super.deposit(amount);
if(active == false && getBalance() >= 25)
active = true;
}
public void monthlyProcess()
{
double serviceCharge = 0;
if(getNumWithdrawals() > 4)
serviceCharge = getMonthlyServiceCharges()*(getNumWithdrawals() -4);
setMonthlyServiceCharges(serviceCharge);
super.monthlyProcess();
}
}
class Test
{
public static void main (String[] args)
{
//Create a SavingsAccount object with a $100 balance, 3% interest rate, and a monthly service charge of $2.50.
SavingsAccount s = new SavingsAccount(100,0.03,2.5);
//Print out the balance
System.out.println("Balance = "+s.getBalance());
//Print out the number of deposits
System.out.println("Number of deposits "+s.getNumDeposits());
//Print out the number of withdrawals
System.out.println("Number of withdrawals "+s.getNumWithdrawals());
//Make a deposit of 25
s.deposit(25);
//Make a deposit of 10
s.deposit(10);
//Make a deposit of 35
s.deposit(35);
//Print out the balance
System.out.println("Balance = "+s.getBalance());
//Print out the number of deposits
System.out.println("Number of deposits "+s.getNumDeposits());
//Print out the number of withdrawals
System.out.println("Number of withdrawals "+s.getNumWithdrawals());
//Make a withdrawal of 100
s.withdraw(100);
//Make a withdrawal of 50
s.withdraw(50);
//Make a withdrawal of 10
s.withdraw(10);
//Make a withdrawal of 1
s.withdraw(1);
//Make a withdrawal of 1
s.withdraw(1);
//Print out the balance
System.out.println("Balance = "+s.getBalance());
//Print out the number of deposits
System.out.println("Number of deposits "+s.getNumDeposits());
//Print out the number of withdrawals
System.out.println("Number of withdrawals "+s.getNumWithdrawals());
//Call the monthly process method
s.monthlyProcess();
//Print out the balance
System.out.println("Balance = "+s.getBalance());
//Print out the number of deposits
System.out.println("Number of deposits "+s.getNumDeposits());
//Print out the number of withdrawals
System.out.println("Number of withdrawals "+s.getNumWithdrawals());
}
}
output:
Balance = 100.0
Number of deposits 0
Number of withdrawals 0
Balance = 170.0
Number of deposits 3
Number of withdrawals 0
Balance = 20.0
Number of deposits 3
Number of withdrawals 2
Balance = 20.05
Number of deposits 0
Number of withdrawals 0
Do ask if any doubt. Please upvote
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.