Question

In: Computer Science

IN JAVA Write a program that calculates the occupancy rate for each floor of a hotel....

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.

Solutions

Expert Solution

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!


Related Solutions

Write a program that calculates the occupancy rate for each floor of a hotel. (Use a...
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...
Hotel Occupancy C++ Only Program Description Write a program that calculates the occupancy rate for a...
Hotel Occupancy C++ Only Program Description Write a program that calculates the occupancy rate for a hotel. The program should read its data from a file named "hotel.dat". The first line of this file will be a single integer specifying the number of floors in the hotel. Each of the remaining (N) lines will have two integers; the number of rooms on that floor and the number of occupied rooms on that floor. Your program should display the following: How...
Using Loops for the Hotel Occupancy calculator. You will write a program that calculates the occupancy...
Using Loops for the Hotel Occupancy calculator. You will write a program that calculates the occupancy of a hotel. Rules: 1# The hotel must have more than 2 floors and less than or equal 5 floors. 2# Each floor in the hotel can have a different number of rooms on the floor. 3# You must set the number of occupied rooms. Again, there must less rooms occupied than the number of rooms. 4# Using the total number of rooms and...
Write a program that calculates the occupancy rate for a hotel. The program should start by asking the user how many floors the hotel has.
Use JAVAProgram #1: Hotel Occupancy: Write a program that calculates the occupancy rate for a hotel. The program should start by asking the user how many floors the hotel has. A for loop should then iterate once for each floor. In each iteration of the for loop, the program should ask the user for the number of rooms of the floor and how many of them are occupied. After all of the iterations are complete the program should display how...
Write a program that calculates the floor of a decimal number as defined here. Basically the...
Write a program that calculates the floor of a decimal number as defined here. Basically the Floor of any decimal number x, is the nearest whole number less than or equal to x. ( Code Should Be In C++) Requirements 1) You must implement a function to calculate the floor (you cannot use C++'s floor function). Name it floorAndError). It will have a return value and a single parameter 2) The program will ask the user to input a number...
Write a java program that calculates a speeding fine. The user is prompted for the speed...
Write a java program that calculates a speeding fine. The user is prompted for the speed of the vehicle, the posted speed limit and if the offence took place in the construction zone. The fine is calculated at $75 + 3$/ km over the speed limit for the first 20km over + $6 / km for the next 20 and $9/km after that. If the posted limit is 40km, the fine is doubled. If the offence took place in a...
Write a Java program that calculates a random number 1 through 100. The program then asks...
Write a Java program that calculates a random number 1 through 100. The program then asks the user to guess the number.If the user guesses too high or too low then the program should output "too high" or "too low" accordingly.The program must let the user continue to guess until the user correctly guesses the number. ★Modify the program to output how many guesses it took the user to correctly guess the right number
Following are the number of victories for the Blue Sox and the hotel occupancy rate for...
Following are the number of victories for the Blue Sox and the hotel occupancy rate for the past eight years. You have been asked to test three forecasting methods to see which method provides a better forecast for the Number of Blue Sox Wins. Year Number of Blue Sox Wins Occupancy Rate 1 70 78% 2 67 83 3 75 86 4 87 85 5 87 89 6 91 92 7 89 91 8 85 94 For the following, you...
Write a C++ program that calculates mileage reimbursement for a salesperson at a rate of $...
Write a C++ program that calculates mileage reimbursement for a salesperson at a rate of $ 0.35 per mile. Your program should interact with the user in this manner: MILEAGE REIMBURSEMENT CALCULATOR Enter beginning odometer reading: 13505.2 Enter ending odometer reading     : 13810.6 You traveled 305.40 miles. At $ 0.35 per mile, your reimbursement is $ 106.89. The values in red color are inputs for the program. The values in blue color are results of expressions.
Question 2: Write a java program that calculates the weighted average of three grades using the...
Question 2: Write a java program that calculates the weighted average of three grades using the method with header public static double CalculateWeightedAverage (double grade1, double grade2, double grade3), such that: [4 Marks] Weighted average = grade1 * 0.5 + grade2 * 0.25 + grade3 * 0.25 The program in the main method will: o ask the user to enter the three grades o and then call the method that will calculate the weighted average o and finally display the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT