Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Java Programming Just completed the prompt. Would like someone to look over and correct me if needed. Programmed the best i could not sure i

Java Programming

Just completed the prompt. Would like someone to look over and correct me if needed.

Programmed the best i could not sure i interpreted the prompts coirrectly though.

Also would like a class diagram for the 4 classes.

________________________________________________________________________________________________________________________

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;

//tests Savings Account

public class Driver1

{

public static void main (String args [])

{

//creates an account with balance of $2000

SavingsAccount saver1=new SavingsAccount (2000);

//creates an account with balance of $3000

SavingsAccount saver2=new SavingsAccount (3000);

//sets interest rate to 4%

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++)

{

//Prints a column of Month 1-12

String monthLabel=String.format("Month %d:", month);

saver1.calculateMonthlyInterest ();

saver2.calculateMonthlyInterest ();

//Prints data in a column

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++)

{

//Prints a column of Month 1-12

String monthLabel=String.format("Month %d:", month);

//sets interest rate to 5%

SavingsAccount.modifyInterestRate (0.05);

saver1.calculateMonthlyInterest ();

saver2.calculateMonthlyInterest ();

//Prints data in a column

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;

public 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;

}

//Overrides Savings Balance

public String toString ()

{

return String.format ("$%.2f", savingsBalance);

}

}

_______________________________________________________________________________________________

package account;

public class SpecialSavings extends SavingsAccount

{

public SpecialSavings(double balance)

{

super(balance);

// TODO Auto-generated constructor stub

}

public void deposit(double balance)

{

super.savingsBalance+=balance;

}

public void withdraw(double balance)

{

if(super.savingsBalance>balance)

{

super.savingsBalance-=balance;

}

}

//checks the balance of the account and applies and interest rate depending on the balance

public void checkInterestRate(double balance)

{

if(balance>10000)

{

//if $10000 or more account receives a rate of 10%

super.modifyInterestRate(0.10);

}else

{

//if less than $10000 account receives a rate of 10%

super.modifyInterestRate(0.04);

}

}

}

_______________________________________________________________________________________________

//imports methods, variables etc from accounts package

import account.*;

//tests special savings

public class Driver2

{

public static void main (String args [])

{

//creates savings account with initial balance of $2000

SpecialSavings saver1=new SpecialSavings (2000);

//creates savings account with initial balance of $3000

SpecialSavings saver2=new SpecialSavings (3000);

//withdraws specified amount from saver1

saver1.withdraw(5000);

//withdraws specified amount from saver2

saver2.deposit(7000);

System.out.printf("******************************************* ");

System.out.printf("* Projected Monthly Balances for the year * ");

System.out.printf("******************************************* ");

System.out.printf("Note: If accounts balance is above $10,000 dollars will earn interest of 10%%, balances below $10,000 will earn 4%% ");

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++)

{

//prints formated column of month 1-12

String monthLabel=String.format("Month %d:", month);

//checks the balance of the account

saver1.checkInterestRate(saver1.savingsBalance);

//assigns monthly interest rate depending on the balance

saver1.calculateMonthlyInterest ();

//checks the balance of the account

saver2.checkInterestRate(saver2.savingsBalance);

//assigns monthly interest rate depending on the balance

saver2.calculateMonthlyInterest ();

//Prints data to columns under saver1 or saver2

System.out.printf("%-10s%10s%10s ", monthLabel,saver1.toString (),saver2.toString ());

}

}

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Concepts

Authors: David Kroenke, David Auer, Scott Vandenberg, Robert Yoder

10th Edition

0137916787, 978-0137916788

More Books

Students also viewed these Databases questions