Java Programming Please include some comments in your own source code. The sourc
ID: 3820572 • 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. The only file should be titled SubclassesTest.java.
//SubclassesTest.java
import java.util.Date;
class SubclassesTest {
//Code goes here...
}
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
import java.util.Date;
//Class definition
public class One
{
//Instance variable
private int accountId;
private double accountBalance;
private double annualInterestRate;
private Date dateCreated;
//Default constructor
One()
{
accountId = 0;
accountBalance = 0;
annualInterestRate = 0;
dateCreated = new Date();
}//End of default constructor
//Parameterized constructor
One(int id, double balance)
{
accountId = id;
accountBalance = balance;
}//End of constructor
//Sets the account id
void setaccountId(int id)
{
accountId = id;
}//End of method
//Sets the account balance
void setAccountBalance(double balance)
{
accountBalance = balance;
}//End of method
//Sets the annual interest rate
void setannualInterestRate(double interest)
{
annualInterestRate = interest;
}//End of method
//Returns account id
int getaccountId()
{
return accountId;
}//End of method
//Returns account balance
double getAccountBalance()
{
return accountBalance;
}//End of method
//Returns Annual Interest Rate
double getAnnualInterestRate()
{
return annualInterestRate;
}//End of method
//Returns account Created Date
Date getDateCreated()
{
return dateCreated;
}//End of method
//Returns Monthly Interest Rate
double getMonthlyInterestRate()
{
return (annualInterestRate/12.0);
}//End of method
//Returns Monthly Interest
double getMonthlyInterest()
{
return ((accountBalance* getMonthlyInterestRate())/100);
}//End of method
//Method to withdraw
void withdraw(double draw)
{
accountBalance -= draw;
}//End of method
//Method to deposit
void deposit(double depo)
{
accountBalance += depo;
}//End of method
}//End of class
//Class SavingsAccount derived from One class
class SavingsAccount extends One
{
//Parameterized constructor
SavingsAccount(int id, double balance)
{
//Calls the base class parameterized constructor
super(id, balance);
}//End of constructor
//Overrides toString() method
public String toString()
{
String mes = "";
mes += "Account Number " + getaccountId() + " Account Type: Saving Account " + " Balance = " + getAccountBalance() + " Annual Interest Rate = " + getAnnualInterestRate() +
" Monthly Interest Rate = " + getMonthlyInterestRate() + " Monthly Interest = " + getMonthlyInterest();
return mes;
}//End of method
}//End of class
//Class CheckingAccount derived from One class
class CheckingAccount extends One
{
//Parameterized constructor
CheckingAccount(int id, double balance)
{
//Calls the base class parameterized constructor
super(id, balance);
}//End of constructor
//Overrides base class method for overdrawn
void withdraw(double draw)
{
if(draw > getAccountBalance())
System.out.println("Exceeds overdraft limit");
else
setAccountBalance(getAccountBalance() - draw);
}//End of method
//Overrides toString() method
public String toString()
{
String mes = "";
mes += "Account Number " + getaccountId() + " Account Type: Checking Account " + " Balance = " + getAccountBalance() + " Annual Interest Rate = " + getAnnualInterestRate() +
" Monthly Interest Rate = " + getMonthlyInterestRate() + " Monthly Interest = " + getMonthlyInterest();
return mes;
}//End of method
}//End of class
package myProject;
//Driver class
public class SubclassesTest
{
//Main method
public static void main(String ss[])
{
//Creates two accounts
One account[] = new One[2];
//Initializes the Saving Account
account[0] = new SavingsAccount(111, 2000.0);
//Initializes the Checking Account
account[1] = new CheckingAccount(222, 2000.0);
//Sets the annual interest rate for Saving Account
account[0].setannualInterestRate(100.0);
//Sets the annual interest rate for Checking Account
account[1].setannualInterestRate(120.0);
//Deposit for Saving Account
account[0].deposit(150);
//Deposit for Checking Account
account[1].deposit(200);
//Withdraw Saving Account
account[0].withdraw(500);
//Withdraw Checking Account
account[1].withdraw(300);
//Displays Saving Account information
System.out.println(account[0]);
//Displays Checking Account information
System.out.println(account[1]);
//Exceeds overdraft limit error
account[1].withdraw(2000);
}//End of main method
}//End of class
Output:
Account Number 111
Account Type: Saving Account
Balance = 1650.0
Annual Interest Rate = 100.0
Monthly Interest Rate = 8.333333333333334
Monthly Interest = 137.50000000000003
Account Number 222
Account Type: Checking Account
Balance = 1900.0
Annual Interest Rate = 120.0
Monthly Interest Rate = 10.0
Monthly Interest = 190.0
Exceeds overdraft limit for Checking Account
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.