Question

FRQ: Hotel Rooms A & B

The management of a motel wishes to keep track of the occupancy of rooms in the motel. Each room is represented by an object of the following Room class.

public class Room
{
  private int roomNumber;
  private boolean occupied;
  private String bookingName;
 
 
  /** Attempts to check a new guest into the room. If the room is not occupied (the 
    * variable occupied is false), changes bookingName to name and sets
    * occupied to true.
    * Otherwise does not change the values of the member variables.
    * @param name the name of the guest checking in as a String
    * @return true if the check-in was successful and bookingName was changed
    * 		false otherwise
    */
  public boolean checkInNewGuest(String name)
  {  /* implementation not shown */  }
 
 
  /** @return the room number
	*/
  public int getNumber()
  {  return roomNumber;  }

  /** @return true if the room is occupied; false otherwise.
	*/
  public boolean isOccupied()
  {  return occupied;  }
 
  // There may be instance variables, constructors and methods that are not shown.
}

The following OccupancyInfo class creates and maintains a database of the rooms in the hotel. The rooms are not sorted in the database, but no two rooms have the same number.

public class OccupancyInfo
{
  private ArrayList rooms;
  // no two Room objects in rooms have the same room number
 
 
  /** Attempts to check-in a new guest into a given room number. If a room exists in
    * the database with the room number requested, and the room is unoccupied, a guest
    * with the name given is checked-in to that room: the room is marked as occupied,
    * and the bookingName variable in the Room object is set to the name given.
    * @param name the name of the guest attempting to check-in
    * @param number the room number into which the guest is to be checked-in
    * @return -1 if no room with the requested number exists;
    * 		0 if the room requested is occupied;
    * 		1 if the check-in is successful
    */
  public int checkIn(String name, int number)
  {  /* to be implemented in part (a) */  }


  /** Returns an ArrayList containing the number of every room which is not occupied
    * in the order in which these appear in the ArrayList rooms.
    */
  public ArrayList availableRoomNumbers()
  {  /* to be implemented in part (b) */  }

  // There may be instance variables, constructors and methods that are not shown.
}

(a) Write the OccupancyInfo method checkIn. This method attempts to check a new guest into a given room number. If no Room object exists in the list rooms with that number, no data is changed and the method returns -1. If the room with that number is occupied (the variable occupied is true), no data is changed and the method returns 0. If the room with that number is unoccupied, the guest is checked-in: the room is marked as occupied, and the bookingName variable in the Room object is set to the name given. If a guest is successfully checked-in the method returns 1. Complete method checkIn below.

/** Attempts to check-in a new guest into a given room number. If a room exists in
  * the database with the room number requested, and the room is unoccupied, a guest
  * with the name given is checked-in to that room: the room is marked as occupied,
  * and the bookingName variable in the Room object is set to the name given.
  * @param name the name of the guest attempting to check-in
  * @param number the room number into which the guest is to be checked-in
  * @return -1 if no room with the requested number exists;
  * 		0 if the room requested is occupied;
  * 		1 if the check-in is successful
  */
public int checkIn(String name, int number)

(b) Write the OccupancyInfo method availableRoomNumbers. This method returns an ArrayList containing the number of every room which is not occupied in the order in which these appear in the ArrayList rooms.

Complete method availableRoomNumbers below.

/** Returns an ArrayList containing the number of every room which is not occupied
  * in the order in which these appear in the ArrayList rooms.
  */
public ArrayList availableRoomNumbers()


1 0
Add a comment Improve this question Transcribed image text
Answer #1

For question 1, the method checkIn would be as follows:

public boolean checkIn(String name){

if(this.occupied==true){

return false;

}

else{

this.bookingName=name;

this.occupied=true;

return true;

}

}

As you can see if the value of the boolean variable "occupied" is true then the if statement is executed. If statement is executed when the room is already occupied and thus false is returned according to the question. If the value of occupied is false, the else statement is executed.

For Question 2, the following is the code for the checkIn method:

public int checkIn(String name,int number){

boolean RoomPresent=false;

int index=0;

for(Room i : rooms){

if(i.roomNumber=number){

RoomPresent=true;

break;

}

index++;

}

if(RoomPresent==false){

return -1;

}

else{

if(rooms.get(index).checkIn(name)){

return 1;

}

else{

return 0;

}

}

}

In the second question first we run a for loop for each object present in the arraylist rooms. We create two variables, one boolean named RoomPresent which is initialized to false. This variable is set to true when one of the rooms in the ArrayList rooms has the room to be booked. Along with that, we track the index of that room by incrementing a variable index, which is initialized to 0. After every iteration of the loop, the value of the variable index gets incremented by 1 and thus it gets the index of the room we're looking for. Otherwise, the value of this variable is not important. If RoomPresent is false at the end of the loop, that means that the room is not present in the ArrayList rooms and thus -1 is returned. Otherwise, if RoomPresent is true, we call the checkIn function of the room object with index "index" in the ArrayList rooms, which returns true if the room is allotted, otherwise, it returns false. If true is returned, it means that the room is successfully allotted and we return 1. If not 0 is returned.


answered by: ANURANJAN SARSAM
Add a comment
Know the answer?
Add Answer to:
FRQ: Hotel Rooms A & B
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Language is Java, any help is appreciated. Thank you! WHERE TO START FROM: import java.util.ArrayList; //PartTest.java...

    Language is Java, any help is appreciated. Thank you! WHERE TO START FROM: import java.util.ArrayList; //PartTest.java //package simple; public class PartTest { public static void main(String[] args) { ArrayList<ExpendablePart> system=new ArrayList<ExpendablePart>();   // creating Part Object Part part1 = new ExpendablePart("Last, First"); part1.setNumber("AX-34R"); part1.setNcage("MN34R"); part1.setNiin("ABCD-RF-WDE-KLJM"); // printing information System.out.println(part1.toString());       //Create a part2 object of class Part Part part2=new ConsumablePart("Widget, purple"); part2.setNumber("12345"); part2.setNcage("OU812"); part2.setNiin("1234-12-123-1234");    // printing information System.out.println(part2.toString());    //checking equality of two Part class objects if(part1.equals(part2)) System.out.println("part1 and...

  • Please answer the following question with Java Modify the Employee and ProductionWorker classes provided below so...

    Please answer the following question with Java Modify the Employee and ProductionWorker classes provided below so they throw exceptions when the following errors occur. - The Employee class should throw an exception named InvalidEmployeeNumber when it receives an employee number that is less than 0 or greater than 9999. - The ProductionWorker class should throw an exception named InvalidShift when it receives an invalid shift. - The ProductionWorker class should thrown anexception named InvalidPayRate when it receives a negative number...

  • Given attached you will find a file from Programming Challenge 5 of chapter 6 with required...

    Given attached you will find a file from Programming Challenge 5 of chapter 6 with required you to write a Payroll class that calculates an employee’s payroll. Write exception classes for the following error conditions: An empty string is given for the employee’s name. An invalid value is given to the employee’s ID number. If you implemented this field as a string, then an empty string could be invalid. If you implemented this field as a numeric variable, then a...

  • Please fill in the remaining code necessary and follow the instructions below this is an abstract...

    Please fill in the remaining code necessary and follow the instructions below this is an abstract class; it represents an department in the company. This class cannot be instantiated (objects created), but it serves as the parent for other classes. Department – This class represents a department within the company. 1. Provide the implementation (the body) for the following methods: public int getDepartmentID()) public String getDepartmentName()) public Manager getManager()) 2. Fix the implementation for the following method– substitute “blah” with...

  • In this assignment you will implement software for your local library. The user of the software...

    In this assignment you will implement software for your local library. The user of the software will be the librarian, who uses your program to issue library cards, check books out to patrons, check books back in, send out overdue notices, and open and close the library. class Calendar We need to deal with the passage of time, so we need to keep track of Java. Declare this variable as private int date within the class itself, not within any...

  • Ive got this started but im really struggling its a c# program, I have to modify...

    Ive got this started but im really struggling its a c# program, I have to modify the class  Purse  to implement the interface ICloneable . create a main program that demonstrates that the Purse method Clone works. using System; using System.Collections; namespace TestingProject {    /// <summary>    /// A coin with a monetary value.    /// </summary>    public class Coin   {        ///   Constructs a coin.        ///   @param aValue the monetary value of the coin       ...

  • Hi, I am having trouble solving the constructor, it requires to ArrayList marks to be a...

    Hi, I am having trouble solving the constructor, it requires to ArrayList marks to be a deep copy of the instance variable. I have also provided the J-Unit test I have been provided. import java.util.*; public class GradebookEntry { private String name; private int id; private ArrayList<Double> marks; /** * DO NOT MODIFY */ public String toString() { String result = name+" (ID "+id+")\t"; for(int i=0; i < marks.size(); i++) { /** * display every mark rounded off to two...

  • please answer question (5 points) Below is the skeleton for a simplified StringBuilder class as done...

    please answer question (5 points) Below is the skeleton for a simplified StringBuilder class as done in the Closed Lab this semester. As in the lab, the class has an ArrayList of Character to hold the individual characters of the StringBuilder object. Write the instance method indexOf as given below that returns the index of the first occurrence of the character c in the SimplestringBuilder. If there is no index for that character (i.e. the character is not in the...

  • I need code in java The Student class: CODE IN JAVA: Student.java file: public class Student...

    I need code in java The Student class: CODE IN JAVA: Student.java file: public class Student {    private String name;    private double gpa;    private int idNumber;    public Student() {        this.name = "";        this.gpa = 0;        this.idNumber = 0;    }    public Student(String name, double gpa, int idNumber) {        this.name = name;        this.gpa = gpa;        this.idNumber = idNumber;    }    public Student(Student s)...

  • I need to output Number of rooms : , Occupied rooms:, Vacant rooms:, and Occupancy rate:,...

    I need to output Number of rooms : , Occupied rooms:, Vacant rooms:, and Occupancy rate:, I've written the java program but I can not get the output I need. What am I doing wrong here? This is a java program and I'm not to use anything advanced, just beginner java programming. Any help would be appreciated import java.util.Scanner; // Needed for the Scanner class /** * This program will read in the number of floors the resort * contains...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT