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.

_____________________________________________________________________________________________________________________________________________________

Exercise 1

Part 1

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 and show balance and interest earned for each account.

Some Tips for helping you with Lab 4:

For Lab 4 part 1 -

Pl. follow the instructions in the assignment and write the class definition. A driver program is the code you would write in main() to exercise the code of class definition.

You should declare private variable in your class definition. You should also write protected methods to set and get the value of private variables.

For Exercise 1 part 2 -

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 10K or 4% if the interest is lower.

______________________________________________________________________________________________________________________________________

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

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!