This program is written in Java. I\'m having a lot of issues getting it to work.
ID: 3664787 • Letter: T
Question
This program is written in Java. I'm having a lot of issues getting it to work. Any help would be greatly appreciated. Thanks!
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 number In Room-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 remove0neFromRoom - removes a person from the room, ensuring that DnumberInRoom does not go below zero, and decreases the value of tota!Number as needed getNumber - returns the number of people in the room D getTotal-a static method that returns the total number of people Please write a program to test the class RoomPeople Expected results //addOneToRoom () Add two to room a andd three to roomb Room a holds 2 Room b holds 3 Total in all rooms is 5 Remove two from both rooms 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 //removeOneFromRoom () //getTotal)Explanation / Answer
Please find the required solution:
class RoomPeople {
private int numberInRoom;
private static int totalNumber;
public int getNumberInRoom() {
return numberInRoom;
}
public static int getTotalNumber() {
return totalNumber;
}
void addOneToRoom() {
numberInRoom++;
totalNumber++;
}
// removes only if number in room >0
void removeOneFromRoom() {
if (numberInRoom > 0) {
numberInRoom--;
if (totalNumber > 0)
totalNumber--;
}
}
int getNumber() {
return numberInRoom;
}
int getTotal() {
return totalNumber;
}
}
public class TestRoomPeople {
public static void main(String[] args) {
// create room a
RoomPeople roomA = new RoomPeople();
// create room b
RoomPeople roomB = new RoomPeople();
// Add two to room A and three to room B
roomA.addOneToRoom();
roomA.addOneToRoom();
roomB.addOneToRoom();
roomB.addOneToRoom();
roomB.addOneToRoom();
System.out.println("room A holds:" + roomA.getNumber());
System.out.println("room B holds:" + roomB.getNumber());
System.out.println("Total in all rooms:" + RoomPeople.getTotalNumber());
// remove two from both rooms
roomA.removeOneFromRoom();
roomA.removeOneFromRoom();
roomB.removeOneFromRoom();
roomB.removeOneFromRoom();
System.out.println("room A holds:" + roomA.getNumber());
System.out.println("room B holds:" + roomB.getNumber());
System.out.println("Total in all rooms:" + RoomPeople.getTotalNumber());
// remove two from a
roomA.removeOneFromRoom();
roomA.removeOneFromRoom();
System.out.println("room A holds:" + roomA.getNumber());
System.out.println("room B holds:" + roomB.getNumber());
System.out.println("Total in all rooms:" + RoomPeople.getTotalNumber());
}
}
room B holds:3
Total in all rooms:5
room A holds:0
room B holds:1
Total in all rooms:1
room A holds:0
room B holds:1
Total in all rooms:1
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.