Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

We want you to create a class RoomPeople that can be used to record the number o

ID: 3806057 • Letter: W

Question

We want you to create a class RoomPeople 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

Please write a java program to test the class RoomPeople.

Expected results:

Add two to room a and three to room b //addOneToRoom()

Room a holds 2 Room b holds 3

Total in all rooms is 5

Remove two from both rooms //removeOneFromRoom()

Room a holds 0 Room b holds 1

Total in all rooms is 1

Remove two from room a (should not change the values) //removeOneFromRoom()

Room a holds 0

Room b holds 1

Total in all rooms is 1   //getTotal()

Explanation / Answer

RoomPeople.java


public class RoomPeople {
static int totalNumber = 0;
private int numberInRoom;
  
public void addOneToRoom()
{
numberInRoom++;
totalNumber++;
}
  
public void removeOneFromRoom()
{
if (numberInRoom > 0)
{
numberInRoom--;
totalNumber--;
}
}
  
public int getNumber()
{
return numberInRoom;
}
  
public static int getTotal()
{
return totalNumber;
}
}

TestRoomPeople.java


public class TestRoomPeople {
public static void main(String[] args)
{
RoomPeople a = new RoomPeople();
RoomPeople b = new RoomPeople();
  
// Add two to room a
a.addOneToRoom();
a.addOneToRoom();
  
// Add three to room b
b.addOneToRoom();
b.addOneToRoom();
b.addOneToRoom();
  
System.out.println("Room a holds " + a.getNumber() + " Room b holds " + b.getNumber());
System.out.println("Total in all rooms is " + RoomPeople.getTotal());
// remove two from both rooms
a.removeOneFromRoom();
a.removeOneFromRoom();
  
b.removeOneFromRoom();
b.removeOneFromRoom();
  
System.out.println("Room a holds " + a.getNumber() + " Room b holds " + b.getNumber());
System.out.println("Total in all rooms is " + RoomPeople.getTotal());
  
a.removeOneFromRoom();
a.removeOneFromRoom();
  
System.out.println("Room a holds " + a.getNumber() + " Room b holds " + b.getNumber());
System.out.println("Total in all rooms is " + RoomPeople.getTotal());
  
}
}

Sample run

Room a holds 2 Room b holds 3
Total in all rooms is 5
Room a holds 0 Room b holds 1
Total in all rooms is 1
Room a holds 0
Room b holds 1
Total in all rooms is 1

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote