Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Design a public class named Account that contains: A private int data field name

ID: 3762344 • Letter: D

Question

Design a public 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 1000).

A private double data field named annualInterestRate that stores the current interest rate (default 4.50).

A private java.util.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 a 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 (annualInterestRate/12).

A method named withDraw that withdraws a specified amount from the account. If there are not enough funds to withdraw an amount of money, print a statement saying that there are not enough money and leave the same balance in the account.

A method named deposit that deposits a specified amount to the account.

(Subclasses of Account) Create two default modified subclasses for CheckingAccount and SavingAccount accounts. A checking account has an overdraft limit, but a savings account cannot be overdrawn. The SavingsAccount and CheckingAccount subclasses must override the toString method of the superclass Account.

Write a test program that creates objects of Account, SavingsAccount, and CheckingAccount, and invokes their toString() methods.

Note: The point of this problem with accounts is to work with inheritance and see that properties (data fields) for superclasses are inherited by subclasses. I hope this will give you an insight on how inheritance and polymorphism works.

Explanation / Answer


public class Account {

   private int id;
   private double balance;
   private double annualInterestRate;
   private java.util.Date dateCreated;
  
   public Account() {
       this.id = 0;
       this.balance = 1000;
       this.annualInterestRate = 4.50;
   }
  
   public Account(int id, double balance) {
       this.id = id;
       this.balance = balance;
   }
  
   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 java.util.Date getDateCreated() {
       return dateCreated;
   }

   public double getMonthlyInterestRate() {
       return this.annualInterestRate/12;
   }
  
   //Withdraw method: deducts amount from current balance.
   public void withDraw(double amount)
   {
       if(balance - amount < 0)
           System.out.println("Not enough money");
       else
           balance = balance - amount;
   }
  
   //Deposit method: adds amount to current balance.
   public void deposit(double amount)
   {
       balance = balance + amount;
   }

   @Override
   public String toString() {
       return "Account [id=" + id + ", balance=" + balance
               + ", annualInterestRate=" + annualInterestRate
               + ", dateCreated=" + dateCreated + "]";
   }

}

class CheckingAccount extends Account
{
   double limit;
  
   public CheckingAccount() {
       this.limit = 1000;
   }
  
   public CheckingAccount(double limit) {
       this.limit = limit;
   }

   @Override
   public String toString() {
       return "CheckingAccount [limit=" + limit + ", id=" + getId()
               + ", balance=" + getBalance()
               + ", annualInterestRate=" + getAnnualInterestRate()
               + ", dateCreated=" + getDateCreated() + "]";
   }
}

class SavingsAccount extends Account
{
   public SavingsAccount() {
      
   }

   @Override
   public String toString() {
       return "SavingsAccount [id=" + getId() + ", balance="
               + getBalance() + ", annualInterestRate="
               + getAnnualInterestRate() + ", dateCreated="
               + getDateCreated() + "]";
   }
}

class TestAccount
{
   public static void main(String[] args) {
       Account acct = new Account();
       CheckingAccount cAcct = new CheckingAccount();
       SavingsAccount sAcct = new SavingsAccount();
       System.out.println(acct);
       System.out.println(cAcct);
       System.out.println(sAcct);
   }
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote