Hello! I had some a question about my Java program. I just wanted to make sure i
ID: 670792 • Letter: H
Question
Hello! I had some a question about my Java program. I just wanted to make sure it was correct. Here is the question:
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 iin 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 persom 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
The following is my program:
//This program will allow you to record the number of people in the rooms of a building
import java.io.*;
class RoomOccupancy
{
int[] numberInRoom = new int [10];
private static int totalNumber = 0;
public RoomOccupancy()
{
for (int a1 = 0; a1< numberInRoom.length;
a1++)
{
numberInRoom [a1] = 0;
}
}
public void addOneToRoom (int a1)
{
numberInRoom [a1] += 1;
totalNumber++;
}
public void removeOneFromRoom (int a1)
{
if (numberInRoom [a1] > 0)
{
numberInRoom [a1] -= 1;
totalNumber--;
}
else
System.out.println("The room is now empty!");
}
public int getNumber (int a1)
{
return numberInRoom [a1];
}
public static int getTotal ()
{
return totalNumber;
}
}
public class RoomOccupancy
{
public static void main (String [] args)
{
RoomOccupancy roomOne = new RoomOccupancy();
roomOne.addOneToRoom(1);
System.out.println("One person has just entered the room! "
+ "The room now has " + RoomOccupancy.getTotal() + " people total");
roomOne.addOneToRoom(1);
System.out.println("Another person has entered. "
+ "The room now has " + RoomOccupancy.getTotal() + " people total");
roomOne.removeOneFromRoom(1);
System.out.println("One person is leaving! "
+ "The room now has " + RoomOccupancy.getTotal() + " people total");
roomOne.removeOneFromRoom(1);
System.out.println("Another person has left the room "
+ "The room now has " + RoomOccupancy.getTotal() + " people total");
roomOne.removeOneFromRoom(1);
}
}
The question I need help with is:
Write a program that tests the RoomOccupancy described in the previous exercise.
Thanks! (-:
Explanation / Answer
Yes your answer is correct for Sure. The only change is Change the name of main class with another name It is best programming practice to distinguish the Main class and normal class
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.