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 and print the new balances for both savers. Then set the annualInterestRate to 5%, calculate the next month's interest and print the new balances for both savers.
Updated: Oct. 1, 2023 — Training Time: 2 minutes
Overseen by: Archangel Macsika
Difficulty: Advance.
Companies who previously asked this: -.
Objective: 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 and print the new balances for both savers. Then set the annualInterestRate
to 5%, calculate the next month's interest and print the new balances for both savers.
Input: Monthly interest.
Expected Output: Displays the balance based on the user input.
Sikademy Solution
SavingsAccount.java
package sikademy;
/**
*
* @author Archangel Macsika
*/
// This class holds the bank account details of a person.
public class SavingsAccount {
private static float annualInterestRate;
private float savingsBalance;
private float monthlyInterest;
// This parameterized constructor is used to initialize the value of savingsBalance
public SavingsAccount(float balance) {
this.savingsBalance = balance;
}
// This static method is used to set the annualInterestRate The interest is divided by 100 to change it to real value.
public static void modifyInterestRate(float interestRate) {
annualInterestRate = interestRate / 100;
}
// This method calculates the monthly interest on savings.
public void calculateMonthlyInterest() {
monthlyInterest = savingsBalance * annualInterestRate / 12;
System.out.println("The monthly interest is: $" + monthlyInterest);
}
// This method adds the monthly interest to the total savings.
private void calculateSavings() {
savingsBalance += monthlyInterest;
}
// This method displays the total savingsBalance
public void displaySavings() {
calculateSavings();
System.out.println("The total balance is : $ " + savingsBalance);
}
}
SavingsAccountTest.java
package sikademy;
/**
*
* @author Archangel Macsika
*/
// This class is used to test the SavingsAccount class
public class SavingsAccountTest {
public static void main(String[] args) {
SavingsAccount saver1=new SavingsAccount(2000);
SavingsAccount saver2=new SavingsAccount(4000);
SavingsAccount.modifyInterestRate(4);
saver1.calculateMonthlyInterest();
saver2.calculateMonthlyInterest();
saver1.displaySavings();
saver2.displaySavings();
SavingsAccount.modifyInterestRate(5);
saver1.calculateMonthlyInterest();
saver2.calculateMonthlyInterest();
saver1.displaySavings();
saver2.displaySavings();
}