Create class SavingsAccount. Use a shared class varialble to store the annualInt
ID: 3546819 • Letter: C
Question
Create class SavingsAccount. Use a shared class varialble to store the annualInterestRate for all account holders. Each object of the class contains a Private instance varialble savingsBalance indicating the amount the saver currently has on deposit. Provide the CalculateMonthlyInterest method to calculate the monthly interest by multiplying the savingsBalance by the annualInterestRate divided by 12; this interest should be added to the savingsBalance and returned to the method caller. Provide a Shared method ModifyInterestRate that sets the annualInterestRate to a new value. Write a console application to test class SavingsAccount.
Instantiate two SavingsAccount objects, saver 1 and saver 2, with balances of $2,000.00 and $3,000.00 respectively. Set annualInterestRate to 4%, then calculate the monthly interest and print theamounts of interest iearned and the new balanace of each of the savers. Then set the annualInterestRate to 5% and calcuate the next month's interest and pring the amouns of the interest earned and the new balances of each of the savers.
Explanation / Answer
public class SavingsAccount { private static double annualInterestRate; private double savingsBalance; protected SavingsAccount() { savingsBalance = 0; annualInterestRate = 0; } protected SavingsAccount(double balance) { savingsBalance = balance; annualInterestRate = 0; } protected void calculateMonthlyInterest() { System.out.println("Current savings balance: " + savingsBalance); double monthlyInterest; monthlyInterest = (savingsBalance * annualInterestRate)/12; savingsBalance += monthlyInterest; System.out.println("New savings balance: " + savingsBalance); } protected double getBalance() { return savingsBalance; } protected static void modifyInterestRate(double newInterestRate) { annualInterestRate = newInterestRate; } } class SpecialSavings extends SavingsAccount { protected static void modifyInterestRate() { if(SavingsAccount.getBalance() > 10000) { modifyInterestRate(.1); } } } class Driver { public static void main(String[] args) { SavingsAccount saver1 = new SavingsAccount(2000); SavingsAccount saver2 = new SavingsAccount(3000); saver1.modifyInterestRate(.04); saver1.calculateMonthlyInterest(); saver2.modifyInterestRate(.04); saver2.calculateMonthlyInterest(); saver1.modifyInterestRate(.05); saver1.calculateMonthlyInterest(); saver2.modifyInterestRate(.05); saver2.calculateMonthlyInterest(); } }
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.