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

Create a java class named HotelRoom that contains information about occupancy of

ID: 3812889 • Letter: C

Question

Create a java class named HotelRoom that contains information about occupancy of hotel rooms. The HotelRoom class has the following instance variables (a.k.a. fields or data):

Room number (int)
People in room (int)
totalOccupancy (int) - this is a static variable that keeps track of the total occupancy (number of people) for all rooms

The  HotelRoom class will have methods to:

Create a new HotelRoom (given a room number and number of people in room) [parameterized constructor]
addToRoom(int numberOfPeople) - adds the number of people to the room and increases the value of totalOccupancy by the same amount, only if the people in the room does not exceed 4.
removeFromRoom(int numberOfPeople)  - removes the number of people from the room and decrease the value of totalOccupancy by the same amount, only if the people in the room does not go below zero.
getNumberOfPeople - returns the number of people in the room
getTotalOccupancy - a static method that returns the total number of people in the entire hotel.
equals - method to check if one HotelRoom is the same as another by comparing all instance variables
toString - method to turn a HotelRoom into a string for display

After you complete the class, please create a driver class (called HotelRoomDemo) that tests the HotelRoom class just created.

Explanation / Answer

// The Driver Class
public class HotelRoomDemo
{
   public static void main(String args[])
   {
       // Here we are adding four rooms to the hotel with the help of parameterized constructor
       HotelRoom r1 = new HotelRoom(1, 2); // it add a room in the hotel with Room No. 1 and occupancy of 2
       HotelRoom r2 = new HotelRoom(2, 3);
       HotelRoom r3 = new HotelRoom(1, 2);
       HotelRoom r4 = new HotelRoom(1, 1);
       HotelRoom r5 = new HotelRoom(1, 3);
      
       r1.toString(); // It print the statement of calling room ,in this case it is r1
       r2.toString();
       r3.toString();
       r4.toString();
      
       r1.addToRoom(1); // It adds 1 more people in the room no 1
       r1.toString();
       r1.addToRoom(5);// It trues to add 5 more people in room no 1 but can't as room no already has occupancy of 3 people and number of people in one room can't exceed 4
       r1.toString();
       r1.removeFromRoom(2);// It removes 2 people in the room no 1
       r1.toString();
       r1.removeFromRoom(4);// It trues to remove 4 more people in room no 1 but can't as room no already has occupancy of 2 people only and number of people in one room can't decrease below to nagative value
       r1.toString();
       int totaloccupancy = HotelRoom.getTotalOccupancy(); //as its a static method which can be called by the name of class only.
       System.out.println("Total occupancy in hotel is "+totaloccupancy);//Gives the total occupancy in hotel
       r1.equals(r4); // Both rooms are equal as both Room No and number of people in both rooms are equal
       r1.equals(r5); // Both rooms are not equal even if room no are same ,but as number of people in r1 1 is 1 and in r5 is 3 is not same
   }
  
}

//The Hotel Class

class HotelRoom

{
   int room_number;
   int people_in_room;
   static int totaloccupancy;
  
   HotelRoom(int room_number, int people_in_room) {
       this.room_number = room_number;
       this.people_in_room = people_in_room;
       totaloccupancy += people_in_room;
   }
  
  
   public void addToRoom(int numberOfPeople)
   {
       this.people_in_room = this.people_in_room+numberOfPeople;
       if(this.people_in_room >4)
       {
           this.people_in_room = this.people_in_room-numberOfPeople;
           System.out.println("Can't add number of occupants as it will exceed the limit of 4");
       }
       else
       {
           totaloccupancy += numberOfPeople;
           System.out.println("Sucessfully added "+numberOfPeople +" more people in the room" );
       }
   }
  
   public void removeFromRoom(int numberOfPeople)
   {

       this.people_in_room = this.people_in_room-numberOfPeople;
       if(this.people_in_room <0)
       {
           this.people_in_room = this.people_in_room+numberOfPeople;
           System.out.println("Can't remove "+numberOfPeople +" number of occupants from the room as it will decrease the no of occupants below 0 ,thats not possible");
       }
       else
       {
          
           totaloccupancy -= numberOfPeople;
           System.out.println("Sucessfully removed "+numberOfPeople +" occupants from the room" );
       }
  
   }
  
   public int getNumberOfPeople()
   {
       return this.people_in_room;
   }
  
   public static int getTotalOccupancy()
   {
       return totaloccupancy;
   }
  
   void equals(HotelRoom r)
   {
       if(this.people_in_room == r.people_in_room && this.room_number == r.room_number)
       {
           System.out.println("Compared Rooms are same");
       }
       else
       {
           System.out.println("Compared Rooms are different");
          
       }
   }
  
  
   public String toString()
   {
       return "This is Hotel Room No."+this.room_number+"which has occupancy of"+this.people_in_room+"No.";
   }
  
}

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