In: Computer Science
IN JAVA
Write a program that calculates the occupancy rate for each floor of a hotel. (Use a sentinel value and please point out the sentinel in bold.) The program should start by asking for the number of floors in the hotel. 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 the number of them that are occupied. After all the iterations, the program should display the number of rooms the hotel has, the number of them 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.
I have uploaded the Images of the code, Typed code and Output of the Code. I have provided explanation using comments (read them for better understanding).
Images of the Code:
Note: If the below code is missing indentation please refer
code Images
Typed Code:
// importing required packages
import java.util.*;
// Main class
public class Main
{
public static void main(String[] args)
{
// Creating a Scanner Object
Scanner input = new Scanner(System.in);
// A variable to store number of floors
int floors = 0;
// Prompting to enter number of floors
System.out.print("Enter Number of floors in the Hotel
:");
// reading input from user
floors = input.nextInt();
// Input Validation
// A while loop to ask user to enter valid input
while(floors<1)
{
// printing error message
System.out.println("Invalid Input: Number of floors
must be 1 or more");
// Prompting to enter number of floors
System.out.print("Enter Number of floors in the Hotel
:");
// reading input from user
floors = input.nextInt();
}
// An array to store number of rooms in each
floor
int rooms[] = new int[floors];
// An array to store number of Occupied rooms in each
floor
int occupied_rooms[] = new int[floors];
// An array to store number of Vacant rooms in each
floor
int vacant_rooms[] = new int[floors];
// A for loop to ask user number of rooms in each
floor
for (int i=0; i < floors ;i++ )
{
// Prompting to enter number of rooms
System.out.print("\nEnter number of rooms in the floor
" +(i+1)+": ");
// reading user input
rooms[i] = input.nextInt();
// Input Validation
// A while loop to ask user to enter valid input
while(rooms[i]<10)
{
// printing error message
System.out.println("Invalid Input: Each floor must
consist of at least 10 rooms");
// Prompting to enter number of rooms
System.out.print("Enter number of rooms in the floor "
+ (i+1) +": ");
// reading user input
rooms[i] = input.nextInt();
}
// Prompting to enter rooms occupied
System.out.print("Enter number of rooms occupied on
the floor "+ (i+1) +": ");
// reading input from user
occupied_rooms[i] = input.nextInt();
// Input Validation
// A while loop to ask user to enter valid input
while(occupied_rooms[i] > rooms[i])
{
// printing error message
System.out.println("Invalid Input: Occupied rooms must
be less the total floor rooms");
// Prompting to enter rooms occupied
System.out.print("Enter number of rooms occupied on
the floor "+(i+1)+": ");
// reading input from user
occupied_rooms[i] = input.nextInt();
}
// Calculating vacant rooms
vacant_rooms[i] = rooms[i] - occupied_rooms[i];
}
// variables to store total_rooms ,occupied_rooms and
vacant_rooms
int total_rooms = 0, total_occupied_rooms = 0,
total_vacant_rooms = 0;
// Calculating the total_rooms, occupied_rooms and
vacant_rooms
for(int i=0;i< floors;i++)
{
// Calculating total_rooms
total_rooms = total_rooms + rooms[i];
// Calculating total_occupied_rooms
total_occupied_rooms = total_occupied_rooms +
occupied_rooms[i];
// Calculating total_vacant_rooms
total_vacant_rooms = total_vacant_rooms +
vacant_rooms[i];
}
// printing total rooms
System.out.println("\nThe Total Number of rooms in the
Hotel: " + total_rooms);
// printing occupied rooms
System.out.println("Number of Occupied Rooms: " +
total_occupied_rooms);
// printing vacant rooms
System.out.println("Number of Vacant Rooms: " +
total_vacant_rooms);
//printing occupancy rate
double Occupancy_rate =
(total_occupied_rooms*100.0/total_rooms);
System.out.println("The Occupancy rate of the Hotel is
" + Occupancy_rate + "%");
}
}
//code ended here
Output:
If You Have Any Doubts. Please Ask Using Comments.
Have A Great Day!