Question
Help writing a java program. I need help to complete part 2 of this prompt. I have completed part 1, which I have included, below.
Help writing a java program.
I need help to complete part 2 of this prompt. I have completed part 1, which I have included, below.
_____________________________________________________________________________________________________________________________________________________
Part 1 COMPLETED Code included below
Create a class SavingsAccount. Use a static class variable to store the annualInterestRate for each of the savers. 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 balance 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 driver program to test the class SavingsAccount. Instantiate two different 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 each of the savers. Then set the annualInterestRate to 5% and calculate the next months interest and print the new balances for each of the savers.
Part 2
Write another class SpecialSavings that extends SavingsAccount to pay interest of 10% on accounts that have balances that exceed 10K. Also provided methods to deposit and take money out of savings account. Write a driver program to test the class SpecialSavings. Instantiate two different savingsAccount objects, saver1 and saver2, with balances of $2000.00 and $3000.00, respectively. Make a few deposits and withdrawals using a scanner and show balance and interest earned for each account.
Some Tips for helping you with Lab 4:
You need to write deposit and withdrawal methods. Should you put this in parent class or child class? Try to answer this question so that you have most reusability in your class definition.
In this part - you have to learn how polymorphism works.
You will have accounts whose balance might be above or below 10K. By using methods in both classes try to change the interest earned to 10% if balance is above 10,000 or 4% if the balance is lower.
public class SpecialSavings extends SavingAcct{ //override - calcMonthlyInterest() //if balance is greater than 10000 then set interest rate to 10% and the call super.calcMonthlyInterest() //reset the rate back to what it should be. //keep in mind you are modifying static variable. } public class Driver2 { //test SpecialSavings }
______________________________________________________________________________________________________________________________________
import account.SavingsAccount;
public class Driver1
{
public static void main (String args [])
{
SavingsAccount saver1=new SavingsAccount (2000);
SavingsAccount saver2=new SavingsAccount (3000);
SavingsAccount.modifyInterestRate (0.04);
System.out.printf("*************************************** ");
System.out.printf("* Monthly Balances for one year at 4%% * ");
System.out.printf("*************************************** ");
System.out.println("Balances: ");
System.out.printf("%20s%10s ","saver1","saver2");
System.out.printf("%-10s%10s%10s ","Base",saver1.toString(),saver2.toString());
for (int month=1; month<=12; month++)
{
String monthLabel=String.format("Month %d:", month);
saver1.calculateMonthlyInterest ();
saver2.calculateMonthlyInterest ();
System.out.printf("%-10s%10s%10s ", monthLabel,saver1.toString (),saver2.toString ());
}
//SavingsAccount.modifyInterestRate (0.05);
//saver1.calculateMonthlyInterest();
//saver2.calculateMonthlyInterest();
System.out.printf(" ***************************************** ");
System.out.printf("* After setting the interest rate to 5%% * ");
System.out.printf("***************************************** ");
System.out.printf("Balances: ");
System.out.printf("%20s%10s ", "saver1", "saver2");
System.out.printf("%-10s%10s%10s ","Base",saver1.toString(),saver2.toString());
//applies 5 percent interest to last balance of accounts saver1 and saver2
for (int month=1; month<=12; month++)
{
String monthLabel=String.format("Month %d:", month);
SavingsAccount.modifyInterestRate (0.05);
saver1.calculateMonthlyInterest ();
saver2.calculateMonthlyInterest ();
System.out.printf("%-10s%10s%10s ",monthLabel,saver1.toString (),saver2.toString ());
}
}
}
_____________________________________________________________________________________________________________________
package account;
public class SavingsAccount
{
//interest rate for all accounts
private static double annualInterestRate=0;
private double savingsBalance;
//Constructor that creates a new account with the specified balance
public SavingsAccount (double balance)
{
savingsBalance=balance;
}
//get monthly interest
public void calculateMonthlyInterest ()
{
savingsBalance += savingsBalance*(annualInterestRate/12.0);
}
//modify the interest rate
public static void modifyInterestRate (double newRate)
{
annualInterestRate=(newRate>=0 && newRate<=1.0)?newRate:0.04;
}
//
public String toString ()
{
return String.format ("$%.2f", savingsBalance);
}
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started