class a Person , a class RoomOccupancy , class called Counter

1. We created in class a Person class to represent a person.
The class has instance variables for a person’s name, which is a string, and an
integer age. These variables are name and age, respectively.

 

a. Write a default constructor for Person that sets name to
the string "No name yet" and age to zero.

 

b. Write a second constructor for Person that sets name to a
given string and age to a given age.

 

c. Write a static method createAdult() for Person that
returns a special instance of this class. The instance represents a generic
adult and has the name “An adult” and the age 21.

2. Create a class RoomOccupancy that can be used to record
the number of people in the rooms of a building. The class has the attributes

 

• numberInRoom—the number of people in a room

• totalNumber—the total number of people in all rooms as a
static variable

 

The class has the following methods:

 

• addOneToRoom—adds a person to the room and increases the
value of totalNumber

 

• removeOneFromRoom—removes a person from the room, ensuring
that numberInRoom does not go below zero, and decreases the value of
totalNumber as needed

 

• getNumber—returns the number of people in the room

 

• getTotal—a static method that returns the total number of
people

 

3.  Define a class
called Counter. An object of this class is used to count things, so it records
a count that is a nonnegative whole number. Include methods to set the counter
to 0, to increase the count by 1, and to decrease the count by 1. Be sure that no
method allows the value of the counter to become negative. Also include an
accessor method that returns the current count value, as well as a method that
displays the count on the screen. Do not define an input method. The only
method that can set the counter is the one that sets it to zero.
Powered by