7)- Hotel Occupancy A hotel\'s occupancy rate is calculated as follows: Occupanc
ID: 3590184 • Letter: 7
Question
7)- 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 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 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 floorExplanation / Answer
import java.util.Scanner;
class Main{
public static void main(String args[]){
int floors,nrooms,totalr=0,totalo=0,noccupied;
float rate;
Scanner sc = new Scanner(System.in);
//floors input
System.out.print("Enter floors:");
floors = sc.nextInt();
//input validation
while(floors<1){
System.out.println("Floors cannot be less than 1.");
System.out.print("Enter floors:");
floors = sc.nextInt();
}
for(int floor=0;floor<floors;floor++){
// rooms input
System.out.print("Enter number of rooms in floor "+floor+" : ");
nrooms = sc.nextInt();
//input validation
while(nrooms<10){
System.out.println("Rooms cannot be less than 10");
System.out.print("Enter number of rooms in floor "+floor+" : ");
nrooms = sc.nextInt();
}
//add rooms to total
totalr += nrooms;
//occupied rooms input
System.out.print("Enter number of rooms occupied:");
noccupied = sc.nextInt();
//input validation
while(noccupied>nrooms){
System.out.println("occupied cannot be greater than available on floor. ");
System.out.print("Enter number of rooms occupied:");
noccupied = sc.nextInt();
}
//add rooms to occupied
totalo += noccupied;
}
//calculate rate
rate = (float)totalo/totalr;
//print output
System.out.println("Vacant rooms:"+(totalr-totalo));
System.out.println("Occupancy rate:"+rate);
}
}
/*Enter floors: 0
Floors cannot be less than 1.
Enter floors: 1
Enter number of rooms in floor 0 : 5
Rooms cannot be less than 10
Enter number of rooms in floor 0 : 10
Enter number of rooms occupied: 11
occupied cannot be greater than available on floor.
Enter number of rooms occupied: 5
Vacant rooms:5
Occupancy rate:0.5
*/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.