Question

In: Computer Science

Create a new class called Account with a main method that contains the following: • A...

Create a new class called Account with a main method that contains the following:

• A static variable called numAccounts, initialized to 0.

• A constructor method that will add 1 to the numAccounts variable each time a new Account object is created.

• A static method called getNumAccounts(). It should return numAccounts.

Test the functionality in the main method of Account by creating a few Account objects, then print out the number of accounts

Solutions

Expert Solution

SOLUTION

I am giving the implementation in 2 languages . The first one is C++.

Code:

#include <iostream>

using namespace std;

static int numAccounts = 0;

class Account

{

public:

Account()

{

numAccounts += 1;

}

};

static int getNumAccounts()

{

return numAccounts;

}

int main(int argc, char const *argv[])

{

Account a1;

Account a2;

cout << getNumAccounts()<<"\n";

return 0;

}

______________________________________________________________________________________________________

OUTPUT:

JAVA code :

class Account {

static int numAccounts = 0;

static int getNumAccounts(){

return numAccounts;

}

//constructor

Account(){

numAccounts += 1;

}

public static void main(String[] args) {

//testing the Class

Account a1 = new Account();

System.out.println(getNumAccounts());

Account a2 = new Account();

Account a3 = new Account();

System.out.println(getNumAccounts());

}

}

OUTPUT:

comment if any doubt


Related Solutions

Create a PoemDriver.java class with a main method. In the main method, create an ArrayList of...
Create a PoemDriver.java class with a main method. In the main method, create an ArrayList of Poem objects, read in the information from PoemInfo.txt and create Poem objects to populate the ArrayList. After all data from the file is read in and the Poem objects added to the ArrayList- print the contents of the ArrayList. Paste your PoemDriver.java text (CtrlC to copy, CtrlV to paste) into the open space before. You should not change Poem.java or PoemInfo.txt. Watch your time...
(In C++) Bank Account Program Create an Account Class Create a Menu Class Create a main()...
(In C++) Bank Account Program Create an Account Class Create a Menu Class Create a main() function to coordinate the execution of the program. We will need methods: Method for Depositing values into the account. What type of method will it be? Method for Withdrawing values from the account. What type of method will it be? Method to output the balance of the account. What type of method will it be? Method that will output all deposits made to the...
JAVA (1) Create two files to submit: Payroll.java - Class definition PayrollClient.java - Contains main() method...
JAVA (1) Create two files to submit: Payroll.java - Class definition PayrollClient.java - Contains main() method Build the Payroll class with the following specifications: 4 private fields String name - Initialized in default constructor to "John Doe" int ID - Initialized in default constructor to 9999 doulbe payRate - Initialized in default constructor to 15.0 doulbe hrWorked - Initialized in default constructor to 40 2 constructors (public) Default constructor A constructor that accepts the employee’s name, ID, and pay rate...
Create a new Class Payroll that contains our ClassEmployee.
JAVACreate a new Class Payroll that contains our ClassEmployee.we are adding a copy constructor to our Employee Classwe are adding a toString() method to our Employee Classour company has grown so we can no longer avoid deducting taxes from our workers, so we moved the PrintPayStub() method to our newPayroll Class.we will need 2 static members to our Payroll Class to help us compute State and Federal tax deductions from our Employee PaychecksDelving deeper into the concepts of Object Oriented...
Problem 1 Create a new project called Lab7 Create a class in that project called ListORama...
Problem 1 Create a new project called Lab7 Create a class in that project called ListORama Write a static method in that class called makeLists that takes no parameters and returns no value In makeLists, create an ArrayList object called avengers that can hold String objects. Problem 2 Add the following names to the avengers list, one at a time: Chris, Robert, Scarlett, Clark, Jeremy, Gwyneth, Mark Print the avengers object. You will notice that the contents are displayed in...
Write a class called Account that contains: • Three privateinstance variables: name (String), account (String),...
Write a class called Account that contains: • Three private instance variables: name (String), account (String), account balance (double). • One constructor, which constructs all instances with the values given. • Getters and setters for all the instance variables. • Debit function that takes an amount from the user and subtract the balance (make sure the balance will not drop below zero after the operation, if this was the case, the function should return false else it should return true)...
Create a Java project called Lab3B and a class named Lab3B. Create a second new class...
Create a Java project called Lab3B and a class named Lab3B. Create a second new class named Book. In the Book class: Add the following private instance variables: title (String) author (String) rating (int) Add a constructor that receives 3 parameters (one for each instance variable) and sets each instance variable equal to the corresponding variable. Add a second constructor that receives only 2 String parameters, inTitle and inAuthor. This constructor should only assign input parameter values to title and...
Create a Java project called Lab3A and a class named Lab3A. Create a second new class...
Create a Java project called Lab3A and a class named Lab3A. Create a second new class named Employee. In the Employee class: Add the following private instance variables: name (String) job (String) salary (double) Add a constructor that receives 3 parameters (one for each instance variable) and sets each instance variable equal to the corresponding variable. (Refer to the Tutorial3 program constructor if needed to remember how to do this.) Add a public String method named getName (no parameter) that...
Create a Java project called 5 and a class named 5 Create a second new class...
Create a Java project called 5 and a class named 5 Create a second new class named CoinFlipper Add 2 int instance variables named headsCount and tailsCount Add a constructor with no parameters that sets both instance variables to 0; Add a public int method named flipCoin (no parameters). It should generate a random number between 0 & 1 and return that number. (Important note: put the Random randomNumbers = new Random(); statement before all the methods, just under the...
Java Programming Create a class named Problem1, and create a main method, the program does the...
Java Programming Create a class named Problem1, and create a main method, the program does the following: - Prompt the user to enter a String named str. - Prompt the user to enter a character named ch. - The program finds the index of the first occurrence of the character ch in str and print it in the format shown below. - If the character ch is found in more than one index in the String str, the program prints...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT