Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Modify the BankAccount class (given in textbook 7th edition on page 373 or see the attachment) as follows: 1) Add an appropriate toString method; 2)

Modify the BankAccount class (given in textbook 7th edition on page 373 or see the attachment) as follows: 1) Add an appropriate toString method; 2) Add an appropriate equals method; 3) Add a copy constructor; 4) Add an integer objectID field to each BankAccount object---this field should contain a sequential unique numeric ID that is assigned to each object when it is created (the very first object should have an ID of 1001; 5) Add a static method named objectsInMemory() that returns the number of BankAccount objects that are currently in memory; 6) Use a finalize() method to keep track of objects that are reclaimed by the garbage collector so that such reclaimed objects are not counted as being objectsInMemory. Write a small demo class that demonstrates your modified BankAccount class.

/** The BankAccount class simulates a bank account. */

public class BankAccount { private double balance; // Account balance

/** This constructor sets the starting balance at 0.0. */

public BankAccount() { balance = 0.0; } /** This constructor sets the starting balance to the value passed as an argument. @param startBalance The starting balance. */

public BankAccount(double startBalance) { balance = startBalance; }

/** This constructor sets the starting balance to the value in the String argument. @param str The starting balance, as a String. */

public BankAccount(String str) { balance = Double.parseDouble(str); }

/** The deposit method makes a deposit into the account. @param amount The amount to add to the balance field. */

public void deposit(double amount) { balance += amount; }

/** The deposit method makes a deposit into the account. @param str The amount to add to the balance field, as a String. */

public void deposit(String str) { balance += Double.parseDouble(str); }

/** The withdraw method withdraws an amount from the account. @param amount The amount to subtract from the balance field. */

public void withdraw(double amount) { balance -= amount; }

/** The withdraw method withdraws an amount from the account. @param str The amount to subtract from the balance field, as a String. */

public void withdraw(String str) { balance -= Double.parseDouble(str); }

/** The setBalance method sets the account balance. @param b The value to store in the balance field. */

public void setBalance(double b) { balance = b; }

/** The setBalance method sets the account balance. @param str The value, as a String, to store in the balance field. */

public void setBalance(String str) { balance = Double.parseDouble(str); } /** The getBalance method returns the account balance. @return The value in the balance field. */

public double getBalance() { return balance; } }

The Demo can look like the following

public static void main(String[] args) { ArrayList accounts = new ArrayList(); accounts.add (new BankAccount(0.0)); accounts.set(0,null); accounts.add(new BankAccount(100.00)); accounts.add(new BankAccount(accounts.get(1))); accounts.add(new BankAccount("500.00")); accounts.add(3,null); accounts.add(new BankAccount(1000.00)); for(BankAccount acct: accounts) System.out.println(acct); System.out.println("The total number of account objects in memory is: " + BankAccount.objectsInMemory()); if(accounts.get(1).equals(accounts.get(2))) System.out.println("The account 1 is equal to account 2"); }

Then the output would look like similar to the following: null objectID = 1002, balance = 100.0 objectID = 1003, balance = 100.0 null objectID = 1004, balance = 500.0 objectID = 1005, balance = 1000.0 The total number of account objects in memory is: 5 /* why 5 not 4? */ The account 1 is equal to account 2 Press any key to continue . . .

This program can be done in Java

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

SQL Database Programming

Authors: Chris Fehily

1st Edition

1937842312, 978-1937842314

More Books

Students also viewed these Databases questions