Create a Java project called Lab8-Q2. Inside this project create a class named \
ID: 3741474 • Letter: C
Question
Create a Java project called Lab8-Q2. Inside this project create a class named "Q2" with a
main function. Also create a class named "Customer".
The class Customer should have a static variable that counts how many Customer objects
have been created.
private static int numCustomers = 0;
The Customer constructor will increment the variable numCustomers every time a Customer is
created.
The main function should have the following lines of code
Customer Corey = new Customer("Corey Park");
Corey.CustomerInfo();
Customer Peyton = new Customer("Peyton Manning");
Peyton.CustomerInfo();
Customer Mario = new Customer("Mario Chalmers");
Mario.CustomerInfo();
And output the following
Corey Park has entered the store, there are now 1 people in the store
Peyton Manning has entered the store, there are now 2 people in the store
Mario Chalmers has entered the store, there are now 3 people in the store
The method CustomerInfo() will print the name of the customer and how many customers are
in the store.
Explanation / Answer
Q2.java
public class Q2 {
public static void main(String[] args) {
Customer Corey = new Customer("Corey Park");
Corey.CustomerInfo();
Customer Peyton = new Customer("Peyton Manning");
Peyton.CustomerInfo();
Customer Mario = new Customer("Mario Chalmers");
Mario.CustomerInfo();
}
}
Customer.java
public class Customer {
private String name;
private static int count = 0;
public Customer(String name) {
this.name = name;
count++;
}
public void CustomerInfo() {
System.out.println(name+" has entered the store, there are now "+count+" people in the store");
}
}
Output:
Corey Park has entered the store, there are now 1 people in the store
Peyton Manning has entered the store, there are now 2 people in the store
Mario Chalmers has entered the store, there are now 3 people in the store