Modify this class by: 1. Suppose the bank wants to keep track of how many accounts exist. A. Do you need a static variable or a regular variable to save how many accounts have been created? Declare it and name the variable as numAccounts. It should be initialized to 0 (since no account exists initially). B. Update this variable in the constructor. (Ask the instructor if you do not know how) C. Add a method getNumAccounts that returns the total number of accounts. Decide if this method should be static – is the information related to a single account or is it for the entire class? Does this method manipulate a static variable? Complete this method.   2. Use the driver class TestAccounts1.java to test your modified Account class. Complete the code as instructed. 3. Add a method void close() to your Account class. This method should close the current account by appending “CLOSED” to the account name and setting the balance to 0. Also, decrement the total number of accounts. Should this method be static or non-static? 4. Modify the driver program to test this method. Choose one of the existing accounts to close. Then print this account as well as the total number of accounts existing, which should be one less after closing the account.

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

Modify this class by:

1. Suppose the bank wants to keep track of how many accounts exist.

A. Do you need a static variable or a regular variable to save how many accounts have been created? Declare it and name the variable as numAccounts. It should be initialized to 0 (since no account exists initially).

B. Update this variable in the constructor. (Ask the instructor if you do not know how)

C. Add a method getNumAccounts that returns the total number of accounts. Decide if this method should be static – is the information related to a single account or is it for the entire class? Does this method manipulate a static variable? Complete this method.

 

2. Use the driver class TestAccounts1.java to test your modified Account class. Complete the code as instructed.

3. Add a method void close() to your Account class. This method should close the current account by appending “CLOSED” to the account name and setting the balance to 0. Also, decrement the total number of accounts. Should this method be static or non-static?

4. Modify the driver program to test this method. Choose one of the existing accounts to close. Then print this account as well as the total number of accounts existing, which should be one less after closing the account.

 

Code:

//*******************************************************
// Account.java
//
// A bank account class with methods to deposit, withdraw, 
// and check the balance.
//*******************************************************

public class Account
{
    private double balance;
    private String name;
    private long acctNum;

  //----------------------------------------------
  //Constructor -- initializes balance, owner, and account number
  //----------------------------------------------
  public Account(double initBal, String owner, long number)
  {
    balance = initBal;
    name = owner;
    acctNum = number;
  }

  

  //----------------------------------------------
  // Checks to see if balance is sufficient for withdrawal.
  // If so decrements balance by amount; if not, prints message.
  //----------------------------------------------
  public void withdraw(double amount)
  {
    if (balance >= amount)
       balance -= amount;
    else
       System.out.println("Insufficient funds");

  }


  //----------------------------------------------
  // Adds deposit amount to balance.
  //----------------------------------------------
  public void deposit(double amount)
  {
    balance += amount;
  }

  //----------------------------------------------
  // Returns balance.
  //----------------------------------------------
  public double getBalance()
  {
    return balance;
  }

  //----------------------------------------------
  // Returns account number
  //----------------------------------------------
  public double getAcctNumber()
  {
    return acctNum;
  }


  //----------------------------------------------
  // Returns a string containing the name, acct number, and balance.
  //----------------------------------------------
    public String toString()
    {
    return "Name: " + name + 
        "\nAcct #: " + acctNum + 
        "\nBalance: " + balance;
    }
}

Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 2 steps

Blurred answer
Knowledge Booster
Function Calling
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education