Java Programming Is this class diagram and program correct for the below prompt?
ID: 3722757 • Letter: J
Question
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<=1.0)?newRate:0.04;
}
//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<=12; 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<=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 ());
}
}
}
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 PackageExplanation / Answer
public class SavingsAccount
{
//Data fields
private double balance; //Account Balance
private double annualInterestRate; //Account annual interest rate
private double monthlyInterestRate;
private double totalDeposits;
private double totalWithdraws;
private double totalInterest;
/**
* Constructor
* @param startBalance The account's balance.
* @param annual_Interest_Rate The annual interest rate.
*/
public SavingsAccount(double startBalance, double annual_Interest_Rate)
{
balance = startBalance;
annualInterestRate = annual_Interest_Rate;
}
//end of Constructor
/**
* setAnnualInterestRate method sets the annual interest
* rate and calculates the monthly interest rate
* @param annual_Interest_Rate The annual interest rate.
*/
public void setAnnualInterestRate(double annual_Interest_Rate)
{
monthlyInterestRate = annualInterestRate / 12;
}
//end of setAnnualInterestRate method
/**
* The deposit method adds the amount to the balance
* and calculates the total deposit
* @param amount
*/
public void setDeposit(double amount)
{
balance += amount;
totalDeposits += amount;
}
//end of deposit method
/**
* The withdraw method subtracts the amount to the balance
* and calculates the total withdraws
* @param amount
*/
public void setWithdraw(double amount)
{
balance -= amount;
totalWithdraws += amount;
}
//end of withdraw method
/**
* The calculateMonthlyInterest method calculates the total
* interest and adds the monthly interest to the balance
*/
public void calculateMonthlyInterest()
{
totalInterest = totalInterest + balance * monthlyInterestRate;
balance = balance + balance * monthlyInterestRate;
}
//end of calculateMonthlyInterest method
/**
* The getBalance method returns the account's balance.
* @return The value of the balance field.
*/
public double getBalance()
{
return balance;
}
/**
* The getAnnualInterestRate method returns the annual interest rate.
* @return The value of the annual interest rate field.
*/
public double getAnnualInterestRate()
{
return annualInterestRate;
}
/**
* The getMonthlyInterestRate method returns the monthly interest rate.
* @return The value of the monthly interest rate field.
*/
public double getMonthlyInterestRate()
{
return monthlyInterestRate;
}
/**
* The getTotalDeposits method returns the total deposit amount.
* @return The value of the total deposits field.
*/
public double getTotalDeposits()
{
return totalDeposits;
}
/**
* The getTotalWithdraws method returns the total withdraws amount.
* @return The value of the total withdraws field.
*/
public double getTotalWithdraws()
{
return totalWithdraws;
}
/**
* The getTotalInterest method returns the total interest amount.
* @return The value of the total interest field.
*/
public double getTotalnterest()
{
return totalInterest;
}
/* displayData method displays the ending details of the savings account */
public void displayData()
{
balance = Math.round(balance * 100.0) / 100.0;
totalInterest = Math.round(totalInterest * 100.0) / 100.0;
System.out.println();
System.out.println("The ending balance is: $" + balance);
System.out.println("Total amount of deposits: $" + totalDeposits);
System.out.println("Total amount of withdraws: $" + totalWithdraws);
System.out.println("Total interest earned: $" + totalInterest);
}
//end of displayData method
}
//end of SavingsAccount class
Test class:
import java.util.Scanner;
public class SavingsAccountTest
{
public static void main(String[] args)
{
double startBalance;
double annual_Interest_Rate;
int months;
double deposit_Amount;
double withdraw_Amount;
//Create an object for Scanner class
Scanner input = new Scanner(System.in);
//Prompt user for starting balance
System.out.print("Enter starting balance: $");
startBalance = input.nextDouble();
//Prompt user for annual interest rate
System.out.print("Enter annual interest rate: ");
annual_Interest_Rate = input.nextDouble();
//Prompt user for number of months
System.out.print("Enter the number of months: ");
months = input.nextInt();
/* Create an object for SavingsAccount class */
SavingsAccount sa = new
SavingsAccount(startBalance, annual_Interest_Rate);
//Call to setAnnualInterestRate method
sa.setAnnualInterestRate(annual_Interest_Rate);
//Loop
for (int i = 0; i < months; i++)
{
/* Prompt user for deposit amount */
System.out.print("Enter amount to deposit for the month " + (i+1) + ":$");
deposit_Amount = input.nextDouble();
//Call to deposit method
sa.setDeposit(deposit_Amount);
/* Prompt user for amount to withdraw */
System.out.print("Enter amount to withdraw for the month " + (i+1) + ":$");
withdraw_Amount = input.nextDouble();
//Call to withdraw method
sa.setWithdraw(withdraw_Amount);
/* Call to calculateMonthlyInterest method */
sa.calculateMonthlyInterest();
}
//end of loop
//Call to displayData method
sa.displayData();
}
//end of main method
}
//end of SavingsAccountTest class
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.