Assigned date: 3/29/2017 Due Date: 4/10/2017 Concepts: Classes and objects, inhe
ID: 3814436 • Letter: A
Question
Assigned date: 3/29/2017 Due Date: 4/10/2017 Concepts: Classes and objects, inheritance Point value: 100 points You will write 4 classes for Program 9. Please submit all classes in one file. The name of the physical program file submitted will be xxxx program9, where xxxx is your Kean email id. The assignment will be started in class but should be completed independently and submitted by the due date for full credit Assignment: The BankAccount class models an account of a customer. A BankAccount has the following instance variables: A unique account id sequentially assigned when the Bank Account is created. A balance which represents the amount of money in the account A date created which is the date on which the account is created. The following methods are defined in the BankAccount class: Withdraw subtract money from the balance Deposit add money to the balance Inquiry on Balance Account id o Date created The savingsAccount class models a bank account which is meant for long term savings and earns interest. It inherits all the fields and methods of the BankAccount class. The Saving class has an additional instance variable, interest rate. The interest rate is a decimal representing the rate at which the account earns interest. For example, an interest rate of .01 means that the account will earn 1% interest on its balance. The following additional methods are defined in the SavingsAccount class: Calculate interest returns the result of calculating the interest amount based on the current balance and interest rate. It does not update the balance Inquiry on interest rate Modifying the interest rate The CheckingAccount class models a bank account which is used to write checks and make frequent ATM deposits and withdrawals. This type of account does not earn interest. The Account class inherits all the fields and methods of the BankAccount class. It has an additional instance variable, monthly fee. The monthly fee is an amount of money charged by the bank for its services The following additional methods a defined in the CheckingAccount classExplanation / Answer
/**
* The BankAccount class that sets
* */
//BankAccount.java
import java.util.Date;
public class BankAccount
{
private static int id;
private double balance;
private Date date;
public BankAccount(double balance)
{
this.balance=balance;
date=new Date();
id=0;
}
public void incrementid()
{
id=id+1;
}
public int getId()
{
return id;
}
public double getBalance()
{
return balance;
}
public Date getDate()
{
return date;
}
public void withdraw(double amt)
{
balance=balance-amt;
}
public void deposit(double amt)
{
balance=balance+amt;
}
@Override
public String toString() {
return String.format("Account ID: %d Created on %s Balance :%.1f", id,date,balance);
}
}
------------------------------------------------------------------
//SavingsAccount.java
public class SavingsAccount extends BankAccount
{
private double rate;
public SavingsAccount(double balance,double rate) {
super(balance);
this.rate=rate;
super.incrementid();
}
public void calInterest()
{
double interest=super.getBalance()*(rate);
super.deposit(interest);
}
public void deposit(double amt) {
super.deposit(amt);
}
public void withdraw(double amt) {
super.withdraw(amt);
}
public void setrate(double rate)
{
this.rate=rate;
}
public double getrate()
{
return rate;
}
public String toString() {
return String.format("Savings account %s Interest Rate :%4.2f",
super.toString(),rate);
}
}
------------------------------------------------------------------
//CheckingAccount.java
public class CheckingAccount extends BankAccount
{
private double monthlyFee;
//constructor thta takes balance and monthly fee
public CheckingAccount(double balance,double monthlyFee) {
super(balance);
this.monthlyFee=monthlyFee;
super.incrementid();
}
public void deposit(double amt) {
super.deposit(amt);
}
public void withdraw(double amt) {
super.withdraw(amt);
}
public void deduct()
{
super.withdraw(monthlyFee);
}
public void setfee(double monthlyFee)
{
this.monthlyFee=monthlyFee;
}
public double getfee()
{
return monthlyFee;
}
public String toString() {
return String.format("Checking account %s Monthly Fee :%4.2f",
super.toString(),monthlyFee);
}
}
------------------------------------------------------------------
//Test java program
//xxxx_program9.java
import java.util.Scanner;
public class xxxx_program9
{
public static void main(String[] args)
{
Scanner scanner=new Scanner(System.in);
System.out.println
("Enter the amount of money to create a savings account");
double amt=Double.parseDouble(scanner.nextLine());
System.out.println
("Enter the interest rate of the savings account");
double rate=Double.parseDouble(scanner.nextLine());
SavingsAccount sact=new SavingsAccount(amt, rate);
System.out.println
("Enter the amount of money to create a checking account");
amt=Double.parseDouble(scanner.nextLine());
System.out.println
("Enter the montly fee of the checking account");
double fee=Double.parseDouble(scanner.nextLine());
CheckingAccount cact=new CheckingAccount(amt, fee);
System.out.println("Amount to deposit into the savings account?");
double deposit=Double.parseDouble(scanner.nextLine());
sact.deposit(deposit);
sact.calInterest();
System.out.println("Amount to withdraw into the checkings account?");
double withdraw=Double.parseDouble(scanner.nextLine());
cact.withdraw(withdraw);
cact.deduct();
System.out.println("Calculating and adding interest of savings account");
System.out.println("Deducting monnthly fee from checking account");
System.out.println(sact.toString());
System.out.println(cact.toString());
}
}
------------------------------------------------------------------
Sample output:
Enter the amount of money to create a savings account
1000
Enter the interest rate of the savings account
0.01
Enter the amount of money to create a checking account
2000
Enter the montly fee of the checking account
50
Amount to deposit into the savings account?
1000
Amount to withdraw into the checkings account?
100
Calculating and adding interest of savings account
Deducting monnthly fee from checking account
Savings account
Account ID: 1 Created on Mon Apr 10 08:15:19 IST 2017 Balance :2020.0 Interest Rate :0.01
Checking account
Account ID: 1 Created on Mon Apr 10 08:15:22 IST 2017 Balance :1850.0 Monthly Fee :50.00