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

5. Hotel Occupancy A hotel\'s occupancy rate is calculated as follows Occupancy

ID: 3731322 • Letter: 5

Question

5. Hotel Occupancy A hotel's occupancy rate is calculated as follows Occupancy rate number of rooms occupied ÷ total number of rooms Write a program that calculates the occupancy rate for each floor of a hotel. The program should start by asking for the number of floors the hotel has. A loop should then iterate once for each floor. During each iteration, the loop should ask the user for the number of rooms on the floor, and how many of them are occupied. After all the iterations, the pro- gram should display the number of rooms the hotel has, the number that are occupied, the number that are vacant, and the occupancy rate for the hotel. Input Validation: Do not accept a value less than 1 for the number of floors. Do not accept a number less than 10 for the number of rooms on a floor

Explanation / Answer

import java.util.*;

class RoomOccupancy
{
public static void main (String[] args)
{
  Scanner input = new Scanner(System.in);
  
  int noOfRoomsHotel,roomsOccupiedHotel,noOfRoomsFloor,roomsOccupiedFloor;
  noOfRoomsHotel =0;
  roomsOccupiedHotel = 0;
  
  System.out.println("Enter the number of floors the hotel has : ");
  int floors = input.nextInt();
  if(floors < 1)//validation
  {
  System.out.println("No of floors cannot be less than 1. try again : ");
  floors = input.nextInt();
  }
  
  // enter total numbe rof rooms and occupied rooms on each floor
  for(int i=1;i<=floors;i++)
  {
  System.out.println("Enter the total number of rooms on the floor : ");
  noOfRoomsFloor = input.nextInt();
  if(noOfRoomsFloor < 10)// validation
  {
  System.out.println("No of rooms cannot be less than 10. try again : ");
  noOfRoomsFloor = input.nextInt();
  }
  System.out.println("Enter the number of rooms occupied on the floor : ");
  roomsOccupiedFloor = input.nextInt();
  noOfRoomsHotel = noOfRoomsHotel + noOfRoomsFloor; // compute total rooms in the hotel
  roomsOccupiedHotel = roomsOccupiedHotel + roomsOccupiedFloor;// compute total rooms occupied in the hotel
  
  }
  
  double occupancyRate = (double)roomsOccupiedHotel / noOfRoomsHotel;
  
  //display total rooms,rooms occupied,rooms vacant and occupancyRate
  System.out.println("Total number of rooms the hotel has : "+noOfRoomsHotel);
  System.out.println("Total number of rooms occupied in the hotel : "+roomsOccupiedHotel);
  System.out.println("Total number of rooms vacant in the hotel :"+(noOfRoomsHotel - roomsOccupiedHotel));
  System.out.printf("Occupancy rate of the hotel : %.2f",occupancyRate);
  
  
}
}

output:

Enter the number of floors the hotel has :5
Enter the total number of rooms on the floor :100
Enter the number of rooms occupied on the floor :70
Enter the total number of rooms on the floor :110
Enter the number of rooms occupied on the floor :78
Enter the total number of rooms on the floor :100
Enter the number of rooms occupied on the floor :56
Enter the total number of rooms on the floor :90
Enter the number of rooms occupied on the floor :45
Enter the total number of rooms on the floor :50
Enter the number of rooms occupied on the floor :28
Total number of rooms the hotel has : 450
Total number of rooms occupied in the hotel : 277
Total number of rooms vacant in the hotel :173
Occupancy rate of the hotel : 0.62

Do ask if any query. Please upvote if the answer is useful

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