Enter Code Two Classes Bankaccountjava Program2java Shown Program2 Class Driver Class Use Q13140119
Enter the code for the two classes “BankAccount.java” and”Program2.java” shown below. The Program2 class is the driver classthat uses the BankAccount worker class to implement theapplication. Note that this version of the BankAccount classaccepts a monthly interest rate in decimal format thatmust be calculated by the user.
/**
* BankAccount class
* This class simulates a bank account.
*
* (Taken from “Starting Out with Java – Early Objects
* (Third Edition) by Tony Gaddis, 2008 by Pearson Educ.)
*
*/
public class BankAccount
{
private double balance; // Account balance
private double interestRate; // Interest rate
private double interest; // Interest earned
/**
* The constructor initializes the balance
* and interestRate fields with the values
* passed to startBalance and intRate. The
* interest field is assigned to 0.0.
*/
public BankAccount(double startBalance, double intRate)
{
balance = startBalance;
interestRate = intRate;
interest = 0.0;
}
/**
* The deposit method adds the parameter
* amount to the balance field.
*/
public void deposit(double amount)
{
balance += amount;
}
/**
* The withdraw method subtracts the
* parameter amount from the balance
* field.
*/
public void withdraw(double amount)
{
balance -= amount;
}
/**
* The addInterest method adds the interest
* for the month to the balance field.
*/
public void addInterest()
{
interest = balance * interestRate;
balance += interest;
}
/**
* The getBalance method returns the
* value in the balance field.
*/
public double getBalance()
{
return balance;
}
/**
* The getInterest method returns the
* value in the interest field.
*/
public double getInterest()
{
return interest;
}
}
/**
*
* Colorado State University – ITS-320 – Basic Programming
*
* This program demonstrates using the BankAccount class.
*
* (Taken from “Starting Out with Java – Early Objects
* (Third Edition) by Tony Gaddis, 2008 by Pearson Educ.)
*
* Programmed by: Reggie Haseltine, instructor
*
* Date: June 19, 2010
*
*/
import java.util.Scanner; // Needed for the Scannerclass
import java.text.DecimalFormat; // Needed for 2 decimal placeamounts
public class Program2
{
public static void main(String[] args)
{
BankAccount account; // To reference a BankAccount object
double balance, // The account’s starting balance
interestRate, // The annual interest rate
pay, // The user’s pay
cashNeeded; // The amount of cash to withdraw
// Create a Scanner object for keyboard input.
Scanner keyboard = new Scanner(System.in);
// Create an object for dollars and cents
DecimalFormat formatter = new DecimalFormat (“#0.00”);
// Get the starting balance.
System.out.print(“What is your account’s ” + “starting balance?”);
balance = keyboard.nextDouble();
// Get the monthly interest rate.
System.out.print(“What is your monthly interest rate? “);
interestRate = keyboard.nextDouble();
// Create a BankAccount object.
account = new BankAccount(balance, interestRate);
// Get the amount of pay for the month.
System.out.print(“How much were you paid this month? “);
pay = keyboard.nextDouble();
// Deposit the user’s pay into the account.
System.out.println(“We will deposit your pay ” + “into youraccount.”);
account.deposit(pay);
System.out.println(“Your current balance is %bodyquot; +formatter.format( account.getBalance() ));
// Withdraw some cash from the account.
System.out.print(“How much would you like ” + “to withdraw?”);
cashNeeded = keyboard.nextDouble();
account.withdraw(cashNeeded);
// Add the monthly interest to the account.
account.addInterest();
// Display the interest earned and the balance.
System.out.println(“This month you have earned %bodyquot; +formatter.format( account.getInterest() ) +
” in interest.”);
System.out.println(“Now your balance is %bodyquot; +formatter.format( account.getBalance() ) );
}
}
Compile the two test files (BankAccount.java first andProgram2.java second); then execute Program2 with the followinginputs:
starting balance – |
$500 |
(don’t enter the dollar sign) |
monthly interest rate – |
0.00125 |
(this is a 1.5% annual rate) |
monthly pay – |
$1000 |
(don’t enter the dollar sign) |
withdrawal amount – |
$900 |
(don’t enter the dollar sign) |
By observing the output of this program, you should be able toverify that you earned $0.75 in interest and have an ending balanceat the end of the month of $600.75.
Then modify the BankAccount class’s constructormethod to create a BankAccount object that stores a monthlyinterest when the user inputs an annual interest rate ofthe format “nnn.nn” (i.e., 1.5). Note that the BankAccountconstructor stored a monthly interest rate for the BankAccountobject’s instance field originally, but the user had to convert theannual rate to a monthly rate (i.e., 1.5 to 0.00125). Then modifythe Program2 driver class to prompt the user for an annualinterest rate. Recompile both classes and execute the modifiedProgram2 driver class again, this time with the followinginputs:
starting balance – |
$500 |
(don’t enter the dollar sign) |
annual interest rate – |
1.5 |
|
monthly pay – |
$1000 |
(don’t enter the dollar sign) |
withdrawal amount – |
$900 |
(don’t enter the dollar sign) |
Verify that you still earn $0.75 in interest and have an endingbalance at the end of the month of $600.75, as you did with theoriginal code.
Note that you must modify the BankAccount class’s constructor andnot the addInterest method or any other method, and do not add anyadditional methods. Failure to meet the requirements to modify theBankAccount class’s constructor and the Program2 prompt only willresult in lost points.