Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Java Programming Is this class diagram and program correct for the below prompt? Also does the code below meet the following standards? CODING STANDARDS All

Java Programming

Is this class diagram and program correct for the below prompt?

Also does the code below meet the following standards?

CODING STANDARDS

All classes should be public

Every class should have a print method

Every class must have a default constructor

Use packages for every lab staring lab 5

all properties (instance or static) should be declared private

class name must start with uppercase

must have public instance methods

main() should be in its own class and can have method for input or for automating testing

one class per .java file

must add a default constructor in each class that is used for creating objects

structure of class

o Instance and static variables

o Constructors

o Instance methods

o Static methods

_________________________________________________________________________________________________

PROMPT

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.

_______________________________________________________________________________________________

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

}

}

}

_____________________________________________________________________________________________________

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

}

//Overrides Savings Balance

public String toString ()

{

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

}

public void deposit(double balance)

{

}

public void withdraw(double balance)

{

}

}

__________________________________________________________________________________________________

//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

SavingsAccount saver1=new SpecialSavings (2000);

//creates savings account with initial balance of $3000

SavingsAccount 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

{

//prints formated column of month 1-12

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

//checks the balance of the account

SavingsAccount.modifyInterestRate(saver1.savingsBalance);

//assigns monthly interest rate depending on the balance

saver1.calculateMonthlyInterest ();

//checks the balance of the account

SavingsAccount.modifyInterestRate(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 ());

}

}

}

__________________________________________________________________________________________________

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

{

//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

{

//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 ());

}

}

}

image text in transcribed

SAVINGSACCOUNT account annuallnterestRate: double +savingsBalance: double SavingsAccount (double balance) +calculateMonthlyInterest0:void +modifyInterestRate(newRate): void +String toString O +deposit (double balance): void +withdraw (double balance): void DRIVER2 +main(StringD: void DRIVER1 SPECIALSAVINGS ~account +SpecialSavings(balance) +deposit (double balance): void +withdraw (double balance): void +checkInterestRate (double balance):void LEGEND Public Private Protected Package

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

More Books

Students also viewed these Databases questions

Question

5. Understand how cultural values influence conflict behavior.

Answered: 1 week ago

Question

e. What do you know about your ethnic background?

Answered: 1 week ago