8.6 (Savings Account Class) Create class SavingsAccount. Use a static variable a
ID: 3605949 • Letter: 8
Question
8.6 (Savings Account Class) Create class SavingsAccount. Use a static variable annualInter- estRate to store the annual interest rate for all account holders. Each object of the class contains a private instance variable savingsBalance indicating the amount the saver currently has on deposit. Provide method calculateMonthlyInterest to calculate the monthly interest by multiplying the savingsBalance by annualInterestRate divided by 12-this interest should be added to savings Balance. Provide a static method modifyInterestRate that sets the annualInterestRate to a new value. Write a program to test class SavingsAccount. Instantiate two savingsAccount objects, saverl and saver2, with balances of $2000.00 and $3000.00, respectively. Set annualInterestRate to 4%, then calculate the monthly interest for each of 12 months and print the new balances for both savers. Next, set the annual Intere stRate to 5%, calculate the next month's interest and print the new balances for both saversExplanation / 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
_____________Could you rate me well.Plz .Thank You
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.