Write a program that reads input for occupied hotel rooms and using the Java AP,
ID: 3541002 • Letter: W
Question
Write a program that reads input for occupied hotel rooms and using the Java AP, sorts the rooms, then search for a particular room to see if it is occupied or not. The program will utilize a loop to get input for occupied hotel rooms until a sentinel value of your choosing is entered.The program should utilize the Java API to sort the array of rooms. The program then asks for input for a particular room number. Utilize a binary search to search for a particular room and outputs Write a program that reads input for occupied hotel rooms and using the Java AP, sorts the rooms, then search for a particular room to see if it is occupied or not. The program will utilize a loop to get input for occupied hotel rooms until a sentinel value of your choosing is entered.The program should utilize the Java API to sort the array of rooms. The program then asks for input for a particular room number. Utilize a binary search to search for a particular room and outputs The program will utilize a loop to get input for occupied hotel rooms until a sentinel value of your choosing is entered.The program should utilize the Java API to sort the array of rooms. The program then asks for input for a particular room number. Utilize a binary search to search for a particular room and outputsExplanation / Answer
Not quite sure where you are stuck on this project, but perhaps I can give enough hints to get you past your roadblock.
First you will want to read input from the user and then stop reading it when the user gives you a 1. Your input loop could look something like this
ArrayList<Integer> bookedRooms = new ArrayList<Integer>();
Scanner scanner = new Scanner(System.in);
Integer userInput = 0;
while(userInput != 1)
{
String tempString = scanner.nextLine(); //User enters in a number
userInput = Integer.parseInt(tempString);
if(userInput != 1)
{
bookedRooms.add(userInput);
}
}
//You can then sort the user's input by simply calling
Collections.sort(bookedRooms);
For the next part, your question is a little vague, but hopefully the following is what you need.
If you are allowed to use the java API to do a binary search, then you simply ask the user for input and then call
int result = Collections.binarySearch(bookedRooms,userInput);
if(result != -1)
{
//The room was booked. Output message to user
}
else
{
//The room was not booked. Output message to user.
}
The binarySearch method returns -1 if the input is not in the list otherwise it returns the index of the item in the list.
If your teacher wants you to write your own binary search algorithm then I suggest reading about it online. There are plenty of good articles about binary searches (including wikipedia). Once you understand how binary search works its not too hard to write your own binary search methods.
p.s. If your code doesn't compile try make sure you have these imports at the top of your file:
import java.util.ArrayList;
import java.util.Scanner;
import java.util.Collections;
Otherwise the code I have given you will compile, I tried it.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.