Hello, Can anybody help me with this? its for java netbeans Create a class in ja
ID: 3798710 • Letter: H
Question
Hello, Can anybody help me with this? its for java netbeans
Create a class in java under the name "RoomOccupancy". The class can be used to record the number of people in the room of a building.
The class has the following attributes (fields):
numberInRoom—the number of people in a room
maxInRoom-- the maximum number of people a room can have.
totalNumber—the total number of people in all rooms as a static variable
The class has the following methods:
constructor that takes 1 parameter for the maximum room capacity.
addOneToRoom—adds a person to the room and increases the value of totalNumber. Make sure not to exceed the max number of room.
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 in the building
B. Test your class as follow:
create two RoomOccupancy objects r1 that can hold 12 persons an r2 that can hold 25 persons.
add 10 persons to r1 and 20 to r2. (hint, use loop)
remove 15 persons from r1 and 11 persons from r2.
print the total number of persons in each room and in the building.
Explanation / Answer
Here you go champ!
public class RoomOccupancy {
private int numberInRoom;
private int maxInRoom;
private static int totalNumber = 0;
public RoomOccupancy(int maxInRoom) {
this.maxInRoom = maxInRoom;
}
boolean addOneToRoom(){
if (numberInRoom == maxInRoom)
return false;
numberInRoom ++;
totalNumber ++;
return true;
}
boolean removeOneFromRoom() {
if (numberInRoom == 0)
return false;
numberInRoom --;
totalNumber --;
return true;
}
int getNumber() {
return numberInRoom;
}
static int getTotal() {
return totalNumber;
}
public static void main(String[] args) {
RoomOccupancy r1 = new RoomOccupancy(12);
RoomOccupancy r2 = new RoomOccupancy(25);
for (int i = 0; i < 10; i++)
r1.addOneToRoom();
for (int i = 0; i < 20; i++)
r2.addOneToRoom();
for (int i = 0; i < 15; i++)
r1.removeOneFromRoom();
for (int i = 0; i < 11; i++)
r2.removeOneFromRoom();
System.out.println("Number in room1: " + r1.getNumber()
+" Number in room2: " + r2.getNumber()
+" Total occupants: " + RoomOccupancy.getTotal());
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.