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
  • Can someone tell me how to create a test for the following Hotel classes that test...

    Can someone tell me how to create a test for the following Hotel classes that test all of the methods? public class Bed { private String type; private static final String[] TYPES ={"Single", "Double", "King Size"}; public Bed(String type) { this.type = type; } public boolean isSingle() { return type.equals(TYPES[0]); } public boolean isDouble() { return type.equals(TYPES[1]); } public boolean isKingSize() { return type.equals(TYPES[2]); } } public class Guest { private String name; public Guest (String name) {     this.name...

  • 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...

  • I need the following merge-sort assignment completed using the "ArrayList<Comparable>" (the part I'm really stuck on)...

    I need the following merge-sort assignment completed using the "ArrayList<Comparable>" (the part I'm really stuck on) with comments: import java.util.ArrayList; public class Mergesort { /** * Sorts list given using the mergesort algorithm * @param list the list to be sorted * @param first the index of the first element of the list to be sorted * @param last the index of the last element of the list to be sorted */ public static void mergesort(ArrayList<Comparable> list, int first, int...

  • I need help with the last method listed in the problem: Implement a class Grid that...

    I need help with the last method listed in the problem: Implement a class Grid that stores measurements in a rectangular grid. The grid has a given number of rows and columns, and a description string can be added for any grid location. Supply the following constructor and methods: public Grid(int numRows, int numColumns) public void add(int row, int column, String description) public String getDescription(int row, int column) public ArrayList getDescribedLocations() Here, Location is an inner class that encapsulates the...

  • 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...

  • Inheritance and Polymorphism (use Pet.java) You are given a class named Pet.java.   Create two child classes...

    Inheritance and Polymorphism (use Pet.java) You are given a class named Pet.java.   Create two child classes named Cat.java and Dog.java. Cat.java should add attributes for color and breed. Dog.java should add attributes for breed and size (use String for small, medium, large). Add appropriate constructors, getter/setter methods and toString() methods.   In a separate file named PetDriver.java, create a main. In the main, create an ArrayList of Pet. Add an instance of Cat and an instance of Dog to the ArrayList....

  • 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       ...

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