My homework is write a prgram for hotel occupancy rate. I figured out everything
ID: 3591279 • Letter: M
Question
My homework is write a prgram for hotel occupancy rate. I figured out everything expect occupacy rate of the floor. How to calculate occupancy rate of each floor?
import java.util.Scanner;
public class Hwk6 {
public static void main(String[] args) {
int floors = 0;
int rooms = 0;
int roomsTotals = 0;
double roomsOccupied = 0;
double roomsOccupiedTotals = 0;
double occupancyRate = 0;
Scanner stdin = new Scanner(System.in);
System.out.println(" Hotel room occupacy rate calculator ");
System.out.print("Enter the number of floors: ");
floors = stdin.nextInt();
while(floors < 1) {
System.out.print("The number of floors must greater than 0. ");
floors = stdin.nextInt();
}
for(int i=0; i<floors; i++){
System.out.print("Enter the number of rooms [Floor " + (int)(i + 1) + "]: ");
rooms = stdin.nextInt();
while(rooms < 8){
System.out.print("The minimum number of rooms are 8. [Floor " + (int)(i + 1) + "]: ");
rooms = stdin.nextInt();
}
System.out.print("Enter the number of rooms occupied [Floor " + (int)(i + 1) + "]: ");
roomsOccupied = stdin.nextInt();
roomsOccupiedTotals += roomsOccupied;
roomsTotals += rooms;
occupancyRate = (roomsOccupiedTotals/roomsTotals);
}
System.out.println("Total Rooms: " + roomsTotals);
System.out.println("Occupacy rate for entire hotel: " + occupancyRate);
}
}
Explanation / Answer
Added comments for added code:
Hwk6.java
import java.text.DecimalFormat;
import java.util.Scanner;
public class Hwk6 {
public static void main(String[] args) {
int floors = 0;
int rooms = 0;
int roomsTotals = 0;
double roomsOccupied = 0;
double roomsOccupiedTotals = 0;
double occupancyRate = 0;
//To round off up to two decimals for the occupancy rate
DecimalFormat format1 = new DecimalFormat("0.00");
Scanner stdin = new Scanner(System.in);
System.out.println(" Hotel room occupacy rate calculator ");
System.out.print("Enter the number of floors: ");
floors = stdin.nextInt();
while (floors < 1) {
System.out.print("The number of floors must greater than 0. ");
floors = stdin.nextInt();
}
//create array to store each floor occupancy
double[] floorOccupancy=new double[floors];
for (int i = 0; i < floors; i++) {
System.out.print("Enter the number of rooms [Floor " + (int) (i + 1) + "]: ");
rooms = stdin.nextInt();
while (rooms < 8) {
System.out.print("The minimum number of rooms are 8. [Floor " + (int) (i + 1) + "]: ");
rooms = stdin.nextInt();
}
System.out.print("Enter the number of rooms occupied [Floor " + (int) (i + 1) + "]: ");
roomsOccupied = stdin.nextInt();
//store floor occupancy of each floor
floorOccupancy[i]=roomsOccupied/rooms;
roomsOccupiedTotals += roomsOccupied;
roomsTotals += rooms;
occupancyRate = (roomsOccupiedTotals / roomsTotals);
}
System.out.println("Total Rooms: " + roomsTotals);
System.out.println("Occupacy rate for entire hotel: " + format1.format(occupancyRate));
//Print occupancy rate for each floor with formatted values
for (int i = 0; i < floorOccupancy.length; i++) {
System.out.println("Floor Occupancy of [Floor "+(int)(i+1)+"]: "+format1.format(floorOccupancy[i]));
}
//close the scanner
stdin.close();
}
}
Sample output:
Hotel room occupacy rate calculator
Enter the number of floors: 2
Enter the number of rooms [Floor 1]: 10
Enter the number of rooms occupied [Floor 1]: 3
Enter the number of rooms [Floor 2]: 6
The minimum number of rooms are 8.
[Floor 2]: 10
Enter the number of rooms occupied [Floor 2]: 4
Total Rooms: 20
Occupacy rate for entire hotel: 0.35
Floor Occupancy of [Floor 1]: 0.30
Floor Occupancy of [Floor 2]: 0.40
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.