Class) Create class SavingsAccount. Use a static vari Savings Account ro store t
ID: 3725591 • Letter: C
Question
Class) Create class SavingsAccount. Use a static vari Savings Account ro store the annual interest rate for all account holders. Each object 8.6 private insta Provide tance variable savingsBalance indicating the amount the saver curre method calculateMonthlyInterest to calculate the monthly interest t t holders. Each object of the class contains a ount the saver currently has on deposit. onthly interest by multiplying the sBalance by annualInterestRate divided by 12-this interest shou sav ance. Provide a static method modifyInterestRate that sets the annual In avingsBa be added to savings- sets the annualInterestRate to a new count objects, sav- tively. Set annualInterestRate to months and print the new balances for bot calculate the next month's interest and print the Write a program to test class SavingsAccount. Instantiate two savingsAc value. erl and saver2, wit eoh then calculate the monthly interest for each of 12 496, Next, set the annual InterestRate to 590, new balances for both savers. te the nex.Print theExplanation / Answer
SavingsAccount.java
public class SavingsAccount {
//Declaring static variable
static double annualInterestRate;
//Declaring instance variable
private double savingsBalance;
//parameterized constructor
public SavingsAccount(double savingsBalance) {
super();
this.savingsBalance = savingsBalance;
}
//This method will calculate the monthlyInterest and update savings balance
public void calculateMonthlyInterest() {
savingsBalance += savingsBalance * (annualInterestRate / 1200);
}
//This method will modify the interest rate based on supplied new interest rate as parameter
public static void modifyInterestRate(double newAnnualInterestRate) {
annualInterestRate = newAnnualInterestRate;
}
public double getSavingsBalance() {
return savingsBalance;
}
}
__________________
Test.java
public class Test {
public static void main(String[] args) {
SavingsAccount saver1=new SavingsAccount(2000.00);
SavingsAccount saver2=new SavingsAccount(3000.00);
SavingsAccount.annualInterestRate=4;
System.out.println("Saver 1#");
System.out.println("Month Balance");
for(int i=1;i<=12;i++)
{
saver1.calculateMonthlyInterest();
System.out.printf("%d %.2f ",i,saver1.getSavingsBalance());
}
System.out.println("Saver 2#");
System.out.println("Month Balance");
for(int i=1;i<=12;i++)
{
saver2.calculateMonthlyInterest();
System.out.printf("%d %.2f ",i,saver2.getSavingsBalance());
}
System.out.printf("After 12 months New Balance of saver#1 :$%.2f ",saver1.getSavingsBalance());
System.out.printf("After 12 months New Balance of saver#2 :$%.2f ",saver2.getSavingsBalance());
System.out.println("After changing Interest rate to 5%");
SavingsAccount.modifyInterestRate(5);
saver1.calculateMonthlyInterest();
System.out.printf("New Balance of save#1 :$%.2f ",saver1.getSavingsBalance());
saver1.calculateMonthlyInterest();
System.out.printf("New Balance of saver#2 :$%.2f ",saver2.getSavingsBalance());
}
}
____________________
Output:
Saver 1#
Month Balance
1 2006.67
2 2013.36
3 2020.07
4 2026.80
5 2033.56
6 2040.33
7 2047.14
8 2053.96
9 2060.81
10 2067.68
11 2074.57
12 2081.48
Saver 2#
Month Balance
1 3010.00
2 3020.03
3 3030.10
4 3040.20
5 3050.33
6 3060.50
7 3070.70
8 3080.94
9 3091.21
10 3101.51
11 3111.85
12 3122.22
After 12 months New Balance of saver#1 :$2081.48
After 12 months New Balance of saver#2 :$3122.22
After changing Interest rate to 5%
New Balance of save#1 :$2090.16
New Balance of saver#2 :$3122.22
______________Thank You