Question
The Driver class) Write a main program that instantiates a checking account object and savings account object. The checking account should be created with a
The Driver class)
Write a main program that instantiates a checking account object and savings account object. The checking account should be created with a balance of $100 and a savings account with a balance of $200 and an interest rate of 1.5. Print the resulting accounts using the toString() method. Then, proceed with withdrawing $50 from savings and depositing $50 into checking. Print the resulting accounts using the toString() method.
public class Account { // data properties private String id; private double balance, annualInterestRate; private java.util.Date dateCreated = new java.util.Date(); public Account( ){ } public Account( String id, double initialBalance ){ this.id = id; balance = initialBalance; } //accessors public String getId( ){ return id; } public double getBalance( ){ return balance; } public double getAnnualInterestRate( ){ return annualInterestRate; } public java.util.Date getDateCreated( ){ return dateCreated; } //mutator public void setId( String newId ){ id = newId; } public void setBalance( double newBalance ){ balance = newBalance; } public void setAnnualInterestRate( double newAnnualInterestRate ){ annualInterestRate = newAnnualInterestRate; } public double getMonthlyInterestRate( ){ return annualInterestRate/12; } public void withdraw( double amount ){ balance = balance - amount; } public void deposit( double amount ){ balance = balance + amount; } public String toString( ){ return "Account #" + this.getId() + ", Balance: " + this.getBalance(); } }
public class Checking extends Account { // data properties private String id;//changes ID attriute to a string private double balance, annualInterestRate; private java.util.Date dateCreated = new java.util.Date(); public Checking( ){ } public Checking( double initialBalance ){ balance = initialBalance; } //accessors public String getId( ){ return id; } public double getBalance( ){ return balance; } public double getAnnualInterestRate( ){ return annualInterestRate; } public java.util.Date getDateCreated( ){ return dateCreated; } //mutator public void setId( String newId ){ id = newId; } public void setBalance( double newBalance ){ balance = newBalance; } public void setAnnualInterestRate( double newAnnualInterestRate ){ annualInterestRate = newAnnualInterestRate; } public double getMonthlyInterestRate( ){ return annualInterestRate/12; } public void withdraw( double amount ){ balance = balance - amount; } public void deposit( double amount ){ balance = balance + amount; } public String toString( ){//modified tostring method as per requirement return "Account Type: Checking,Account #" + this.getId() + ", Balance: " + this.getBalance()+",Rate: "+this.getAnnualInterestRate(); } }
public class Savings extends Account{
//data properties
private double interestRate;
// default constructor
public Savings()
{
super();
this.interestRate = 0;
}
// parameterized constructor
public Savings(String id, double balance, double interestRate)
{
super(id, balance); // calling Account class constructor
this.interestRate = interestRate;
this.setAnnualInterestRate(interestRate);
}
// mutator
public void setInterestRate(double interestRate)
{
this.interestRate = interestRate;
this.setAnnualInterestRate(interestRate);
}
public String toString()
{
return "Account Type : Savings, "+super.toString() +", Rate : "+interestRate ;
}
public static void main(String[] args) {
// create object of Savings class
Savings savingsaAccount = new Savings("1243543",20000,6);
System.out.println(savingsaAccount); // display the Savings class object
}
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started