Using Java: Create class SavingsAccount. Use a static variable annualInterestRat
ID: 3803348 • Letter: U
Question
Using Java:
Create class SavingsAccount. Use a static variable annualInterestRate 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 savingsBalance. Provide a static method modifyInterestRate that sets the annualInterestRate to a new value. Write a program to test class SavingsAccount. Instantiate two savingsAccount objects, saver1 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 annualInterestRate to 5%, calculate the next month’s interest and print the new balances for both savers.
Explanation / 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.println("After changing Interest rate to 5%");
SavingsAccount.modifyInterestRate(5);
System.out.println("Saver 1#");
saver1.calculateMonthlyInterest();
System.out.printf("New Balance of save 1 :%.2f ",saver1.getSavingsBalance());
System.out.println("Saver 2#");
saver1.calculateMonthlyInterest();
System.out.printf("New Balance of saver 2 :%.2f ",saver1.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
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
After changing Interest rate to 5%
Saver 1#
New Balance of save 1 :2083.21
Saver 2#
New Balance of saver 2 :2091.89
__________Thank You
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.