Java Programming Please include some comments in your own source code. The sourc
ID: 3818339 • Letter: J
Question
Java Programming
Please include some comments in your own source code. The source code cannot be copied and pasted from a book or website. As well as implementing the subclasses, provide a test main method in the main class SubclassesTest that demonstrates the use of your classes and methods.
Have all your classes inside one .java file, SubclassesTest.java. Do not have a separate file for each of your classes.
11.3 (Subclasses of Account) In Programming Exercise 9.7, the Account class was
defined to model a bank account. An account has the properties account number,
balance, annual interest rate, and date created, and methods to deposit and withdraw
funds. Create two subclasses for checking and saving accounts. A checking
account has an overdraft limit, but a savings account cannot be overdrawn.
Draw the UML diagram for the classes and then implement them. Write
a test program that creates objects of Account, SavingsAccount, and
CheckingAccount and invokes their toString() methods.
Exercise 9.7 (The Account class) Design a class named Account that contains:
A private int data field named id for the account (default 0 ).
A private double data field named balance for the account (default 0 ).
A private double data field named annualInterestRate that stores the cur-
rent interest rate (default 0 ). Assume all accounts have the same interest rate.
A private Date data field named dateCreated that stores the date when the
account was created.
A no-arg constructor that creates a default account.
A constructor that creates an account with the specified id and initial balance.
The accessor and mutator methods for id , balance , and annualInterestRate .
The accessor method for dateCreated .
A method named getMonthlyInterestRate() that returns the monthly
interest rate.
A method named getMonthlyInterest() that returns the monthly interest.
A method named withdraw that withdraws a specified amount from the
account.
A method named deposit that deposits a specified amount to the account.
Explanation / Answer
SubclassesTest.java
import java.util.Date;
class Account {
private int id;
private double balance;
private double annualInterestRate = 0;
private Date dateCreated;
public Account() {
dateCreated = new Date();
}
public Account(int id, double balance) {
super();
this.id = id;
this.balance = balance;
dateCreated = new Date();
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public double getBalance() {
return balance;
}
public void setBalance(double balance) {
this.balance = balance;
}
public double getAnnualInterestRate() {
return annualInterestRate;
}
public void setAnnualInterestRate(double annualInterestRate) {
this.annualInterestRate = annualInterestRate;
}
public Date getDateCreated() {
return dateCreated;
}
public double getMonthlyInterestRate() {
double monthlyInterestRate = getAnnualInterestRate() / 1200;
return monthlyInterestRate;
}
public double getMonthlyInterest() {
double monthlyInterest= getBalance() * getMonthlyInterestRate();
return monthlyInterest;
}
public void withdraw(double amount) {
balance = getBalance() - amount;
}
public void deposit(double amount) {
balance = getBalance() + amount;
}
@Override
public String toString() {
return "id=" + id + ", balance=" + balance
+ ", annualInterestRate=" + annualInterestRate
+ ", dateCreated=" + dateCreated ;
}
}
class Checking extends Account {
private double overDraft;
public Checking(int id, double balance,double overDraft) {
super(id, balance);
this.overDraft=overDraft;
}
public void withdraw(double amount)
{
if((getBalance()-amount)<overDraft)
{
System.out.println("*** Exceded Overgrat limit.Could'nt Withdraw **");
}
else
super.setBalance(getBalance()-amount);
}
@Override
public String toString() {
return super.toString()+" Checking overDraft=" + overDraft+" ";
}
}
class Savings extends Account {
public Savings(int id,double balance)
{
super(id, balance);
}
public void withdraw(double amount )
{
if(getBalance()-amount>0)
{
super.setBalance(getBalance()-amount);
}
else
{
System.out.println("** Account Do not have enough balance **");
}
}
@Override
public String toString() {
return super.toString()+" ";
}
}
public class SubclassesTest {
public static void main(String[] args) {
//Creating the Checking Account class Object
Checking check=new Checking(111,50000, -100);
//Displaying the Checking Account info
System.out.println("Checking Account Info# "+check.toString());
//Displaying the checking account balance
System.out.println("Checking Account balance before withdrawl:"+check.getBalance());
System.out.println("Performing Withdrawl of 5000");
//Performing withdraw operation
check.withdraw(5000);
//Displaying the checking account balance
System.out.println("Checking Account balance after withdrawl:"+check.getBalance());
System.out.println("Performing Withdrawl of 45100");
//Performing withdraw operation
check.withdraw(45100);
//Displaying the checking account balance
System.out.println("Checking Account balance after withdrawl:"+check.getBalance());
System.out.println("Performing Withdrawl of 1000");
check.withdraw(1000);
System.out.println("Checking Account balance :"+check.getBalance());
//Creating the Savings class Object
Savings saving=new Savings(1234,30000);
//Displaying the Savings Account info
System.out.println(" Savings Account Info # "+saving.toString());
//Displaying the Savings account balance
System.out.println("Savings Account balance before withdrawl:"+saving.getBalance());
System.out.println("Performing Withdrawl of 7000");
//Performing withdraw operation
saving.withdraw(7000);
//Displaying the checking account balance
System.out.println("Savings Account balance before withdrawl:"+saving.getBalance());
System.out.println("Performing Withdrawl of 31000");
//Performing withdraw operation
saving.withdraw(31000);
//Displaying the checking account balance
System.out.println("Savings Account balance:"+saving.getBalance());
}
}
______________________
Output:
Checking Account Info#
id=111, balance=50000.0, annualInterestRate=0.0, dateCreated=Sun Apr 16 02:22:03 IST 2017
Checking
overDraft=-100.0
Checking Account balance before withdrawl:50000.0
Performing Withdrawl of 5000
Checking Account balance after withdrawl:45000.0
Performing Withdrawl of 45100
Checking Account balance after withdrawl:-100.0
Performing Withdrawl of 1000
*** Exceded Overgrat limit.Could'nt Withdraw **
Checking Account balance :-100.0
Savings Account Info #
id=1234, balance=30000.0, annualInterestRate=0.0, dateCreated=Sun Apr 16 02:22:03 IST 2017
Savings Account balance before withdrawl:30000.0
Performing Withdrawl of 7000
Savings Account balance before withdrawl:23000.0
Performing Withdrawl of 31000
** Account Do not have enough balance **
Savings Account balance:23000.0
______________Thank You
Please rate me well.If you area satisfied.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.