In: Computer Science
Use JAVA
Program #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 many rooms the hotel has, how many of them are occupied, and the percentage of rooms that are occupied.
It is traditional that most hotels do not have a 13thfloor. The for loop in this program should skip the entire thirteenth loop iteration.
Input validation: Do not accept a value of less than one for the number of floors. Do not accept a value of less than 10 for the number of rooms on a floor. Do not accept a value for occupied that is greater than rooms.
EXAMPLE INPUT TO USE FOR TESTING (just an example your program should work for all different inputs):
If the Number of Floors in the Hotel: 16
And the user to INPUT the following information for total rooms and occupied rooms:
Floor # | Total Rooms | Occupied Rooms |
1 | 200 | 180 |
2 | 170 | 100 |
3 | 50 | 20 |
4 | 125 | 125 |
5 | 100 | 80 |
6 | 200 | 80 |
7 | 80 | 40 |
8 | 90 | 45 |
9 | 95 | 0 |
10 | 115 | 103 |
11 | 125 | 17 |
12 | 140 | 128 |
14 | 130 | 129 |
15 | 100 | 99 |
16 | 60 | 24 |
The output of your program for the above example should be (do NOT print the above chart!):
The Hotel has 1780 Total Rooms.
1170 of the Rooms are Occupied.
66 % of the Rooms are Occupied.
Program #2: Pennies: For the first day worked a person earns $1.00 for the entire day. For the second day worked the person’s daily pay is doubled to $2.00. For the third day worked the person’s daily pay is doubled again to $4.00. For each consecutive day a person works their daily pay is doubled in this way.
Write a program that asks a user to enter the number of days they worked and then calculates the pay they earned for each day along with the total pay they earned for all of the days. The program output should be the pay for each day and the total pay earned for all the days.
Input Validation: Do not let the user enter a number of days that is less than 1. Use a loop to prompt them to enter another number for the number of days if they enter a number that is less than 1.
Make sure and format your output.
Example: This would be the output for a person who worked 4 days
Pay for Day 1: $1.00
Pay for Day 2: $2.00
Pay for Day 3: $4.00
Pay for Day 4: $8.00
TOTAL PAY FOR 4 DAYS: $15.00
1)Code - Main.java
import java.util.*;
public class Main
{
public static void main(String[] args) {
//variable declare
int noOfFloors;
//Scanner to read user input
Scanner sc = new Scanner(System.in);
System.out.println("Enter number of
floors hotel has : ");
//get no of floors from user
noOfFloors = sc.nextInt();
//number of floors check is greater then 1 or not
while(noOfFloors<1){
System.out.println("Enter number of
floors hotel should be greater than 1 : ");
noOfFloors = sc.nextInt();
}
//roomsPerFloor array and
ocuupiedRoomsPerFloor array initialized
int roomsPerFloor[] = new
int[noOfFloors];
int ocuupiedRoomsPerFloor[] = new
int[noOfFloors];
//use for loop to iterate
for(int i = 0 ;
i
if(i==12){
roomsPerFloor[i] = 0;
ocuupiedRoomsPerFloor[i]= 0;
continue;
}
//enter number of room in
floor
System.out.println("Enter number of
rooms on floor "+ (i+1)+":");
roomsPerFloor[i] =
sc.nextInt();
//check room are less then 10 then again ask to enter
while(roomsPerFloor[i]<10){
System.out.println("Enter number of
rooms on floor "+ (i+1)+" greater than 10 :");
roomsPerFloor[i] =
sc.nextInt();
}
//ask again number of room
occupied
System.out.println("Enter number of
occupied rooms on floor "+ (i+1)+":");
//store it
ocuupiedRoomsPerFloor[i] =
sc.nextInt();
}
//calculate total room and total
occupied rooms
int totalRooms=0,occupiedRooms =
0;
for(int i = 0 ;
i
totalRooms+=roomsPerFloor[i];
occupiedRooms+=ocuupiedRoomsPerFloor[i];
}
//print the details
System.out.println("The hotel has
"+totalRooms+" Total Rooms");
System.out.println(occupiedRooms+"
of Rooms are Occupied");
float percOccupied =
(float)occupiedRooms/totalRooms*100;
System.out.println(percOccupied+"%
of Rooms are Occupied");
}
}
Screenshots -