Questions & AnswersComputer Science

This question explores objects, classes, methods, selection and...

Question
Answered step-by-step
Asked by anectode1 on coursehero.com

This question explores objects, classes, methods, selection and...

This question explores objects, classes, methods, selection and iteration as well as use of a collection. You may want to look up methods in the Java API documentation. You should be able to answer this question once you have completed Chapters 1-4.

In all questions, assume that sensible arguments are received by methods, unless otherwise stated.

As you answer the question parts you should provide multiline comments for your class, constructor, and methods. Make sure that your comments explain the purpose of the code and identify which part of the TMA question you are answering in each case.

For example, if you were answering part (d)(ii) of a question, and this involved writing a setter for a field onLoan, a suitable comment would be

/**

  * (d)(ii) Set the onLoan field to the received value

  */

 

Scenario

A hotel allows interaction with a collection of rooms.

For example, a hotel has facilities to:

  • book a room
  • print a list of rooms
  • calculate the bill for a room
  • verify the format of a room's number.

a.Launch BlueJ or any Java  platforms. Note that this project does not contain any classes yet.

  • Begin by adding a new class called Room to the provided project.   
  • Retain but edit the class comment to include your own name and the date and a description of the class Room.

b. Now add the following private fields to the Room class:

  • guest, the person who booked the room, which is a String
  • number, an identifier for the room, which is a String
  • dailyRate, which is a double value giving the standard daily rate for the room.

c. Add a constructor to the class Room with the modifier and header

  • public Room(String aGuest, String aNumber, double aRate)
  • The constructor uses its three parameters to set the values of the corresponding fields.

d. Now add the following public methods to the Room class:

  • i.Getter methods for the three fields, with names getGuest, getNumber, and getDailyRate.
  • ii.A setter method for the guest field, called setGuest.
  • iii.A setter method for the dailyRate field, called setDailyRate.

e. Add a method to tell if the room is available or not, called isAvailable. 

  • The method takes no parameters and returns true if the room is available, and otherwise returns false.
  • A room is considered available if its guest is an empty string (i.e., a string of length 0).

f. Add a method verifyRoom() that returns true if the room has a valid number, and otherwise returns false.

  • A room number string must be 3 characters in length, otherwise the method returns false.
  • The first two characters should represent a room number from one to ninety-nine, which in string form are represented as "01" to "99", otherwise the room number is invalid.
  • The third character in the room number must be either 'A' or 'B' or 'C', otherwise the room number is invalid.
  • Hint: You will need to use the String method charAt in this part.  Note also that you can perform comparisons of characters using the < and > operators. Try experimenting with expressions such as "11B".charAt(0) > '0' and "19B".charAt(1) < '8' in BlueJ's Code Pad.

g. Add a method getType that returns a string describing the type of the room, based on the room number's third character. You may assume that the room number is valid.

  • If the room number's third character is 'A' the method returns "Single".
  • If the room type is 'B', the method returns "Double", otherwise the method returns "Family".

h. Add a method called description that takes no parameters and returns a string describing the room, using the following format: 

  • type room number (availabilityguest
  • Here the type and number should be replaced by the actual type and number of the room. For example, the method might return the string
  • Single room 21A (available)
  • or
  • Single room 21A (reserved) Joe Bloggs
  • depending on whether the room is available or not, as determined by its isAvailable method.

 

i. Go back and check that you have provided multiline comments for your class, constructor, and methods.  

  

NEXT do the following, in the same project file

a. Make​​​​​ a public class called Hotel . 

  • You will develop this class in the following parts.
  • Edit the class comment so that it includes your own name and the date and a description of the class Hotel. Remove the sample field, constructor, and method.

 

b. In the class Hotel add two private fields:

  • rooms, which is an ArrayList of Room, used to keep a record of rooms in the hotel
  • name, a String representing the name of the hotel.
  • Remember that you will require an import statement above your class in order to use an ArrayList.

 

c. Add a public constructor with one String parameter called aName to the Hotel class.

  • The constructor should use its parameter to set the hotel's name.
  • The constructor should also initialize the hotel's field rooms to an empty collection of the appropriate type.

 

d. Add The method addRooms which has been provided for you below        

Use the following method

/**
* (d) Adds some unoccupied test rooms to the hotel
*/
public void addRooms()
{
int i = 10;
while (i < 19) {
String number = "" + i;
double rate;

if(i % 3 == 1) {
number += "A";
rate = 100;
}
else if (i % 3 == 2) {
number += "B";
rate = 180;
}
else
{
number += "C";
rate = 250;
}

Room r = new Room("", number, rate);

rooms.add(r);
i++;
}
}

Copy this method, which sets up a number of unoccupied rooms, to your Hotel class, so that you can use it to help you set up some rooms for testing purposes.

 

In your Solution Document, explain the following details of how the addRooms method works  outside of the class and under a separate entry for part (d):

  • i.The purpose of the local variable i and the while loop
  • ii.How the integer value of i is converted to a string
  • iii.How the modulo operator is used
  • iv.The relationship between the room type and the room price implied by this method.
  • v.Describe the contents of the rooms list after the addRooms method is run, and the state of the Room object accessed from index 2 of the rooms list.

 

In the following parts, add public methods to the Hotel class using the given signatures. You will need to choose your own sensible formal parameter names.

e. calculateBill(Room, int) will calculate a bill for a room that is being vacated. 

  • The method has a parameter for the Room and the number of days the room was occupied.
  • Calculate a bill for the room as its daily rate multiplied by the number of days the room was occupied.
  • If the room was occupied for at least four days, a 10% discount to the overall bill is applied.
  • The method should return the bill as a double value.
  • You may assume that the number of days is at least 1.

f. getMatchingRooms(String) receives the type of a Room as a parameter and returns an ArrayList of rooms in the hotel whose types match the parameter and which are unoccupied. 

  • For example, calling the method with the parameter "Single" will return a list of all free rooms whose type is single.
  • Hint: Use a local variable to create a new empty list of rooms, add any matching rooms that are free to that list, and then return the list when done.

 

g. vacancies() takes no parameters and returns the number of vacancies in the hotel rooms list as an int, based on 1 vacancy for every single room, 2 for every double room, and 4 for every family room that is unoccupied.

 

h. bookRoom(Room) takes a Room as an argument and searches the rooms list for a room with a matching number. If a match is found, the hotel room concerned has its guest field set to a dummy value "Guest". 

  • If no match is found the method does nothing. (Assume that room numbers are unique.)

 

i. removeRoom(String) is used to take a room out of service. 

  • It takes a room number as an argument and removes from the rooms collection the room with a matching number, if the number is found.
  • If the room is not found the method prints "Room <number> not found!" where <number> is the room number that was not found.

 

 

j. Go back and check that you have provided multiline comments for your class, methods and constructor.             

Answer & Explanation

Solved by verified expert
Answered by BaronJaguarMaster708 on coursehero.com

sec

sectetur adipiscing elit. Nam lacinia pulvinar tortor nec facilisis. Pellentesque dapibus efficitur laoreet. Nam risus ante, dapibus a molestie consequat, ultrices ac magna. Fusce dui lectus, congue vel laoreet ac, dictum vitae odio. Donec aliquet. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam lacinia pu
CliffsNotes Logo

Unlock access to this and over
10,000 step-by-step explanations

Unlock Explanation

Have an account? Log In

<p>sec</p>sectetur adipiscing elit. Nam lacinia pulvinar tortor nec facilisis. Pellentesque dapibus efficitur laoreet. Nam risus ante, dapibus a molestie consequat, ultrices ac magna. Fusce dui lectus, congue vel laoreet ac, dictum vitae odio. Donec aliquet. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam lacinia pu

Step-by-step explanation

sectetur adipiscing elit. Nam lacinia pulvinar tortor nec facilisis. Pellentesque dapibus efficitur laoreet. Nam risus ante, dapibus a molestie consequat, ultrices ac magna. Fusce dui lectus, congue vel laoreet ac, dictum vitae odio. Donec aliquet. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam lacinia pulvinar tortor nec facilisis. Pellentesque dapibus efficitur laoreet. Nam risus ante, dapibus a molestie consequat, ultrices ac magna. Fusce dui lectus, congue vel laoreet ac, dictum vitae odio. Donec aliquet. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam lacinia pulvinar tortor nec facilisis. Pellentesque dapibus efficitur laoreet. Nam risus ante, dapibus a molestie consequat, ultrices ac magna. Fusce dui lectus, congue vel laoreet ac, dictum vitae odio. Donec aliquet. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam lacinia pulvinar tortor nec facilisis. Pellentesque dapibus efficitur laoreet. Nam risus ante, dapibus a molestie consequat, ultrices ac magna. Fusce dui lectus, congue vel laoreet ac, dictum vitae odio. Donec aliquet. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam lacinia pulvinar tortor nec facilisis. Pellentesque dapibus efficitur laoreet. Nam risus ante, dapibus a molestie consequat, ultrices ac magna. Fusce dui lectus, congue vel laoreet ac, dictum vitae odio. Donec aliquet. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam lacinia pulvinar tortor nec facilisis. Pellentesque dapibus efficitur laoreet. Nam risus ante, dapibus a molestie consequat, ultrices ac magna. Fusce dui lectus, congue vel laoreet ac, dictum vitae odio. Donec aliquet. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam lacinia pulvinar tortor nec facilisis. Pellentesque dapibus efficitur laoreet. Nam risus ante, dapibus a molestie consequat, ultrices ac magna. Fusce dui lectus, congue vel laoreet ac, dictum vitae odio. Donec aliquet. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam lacinia pulvinar tortor nec facilisis. Pellentesque dapibus efficitur laoreet. Nam risus ante, dapibus a molestie consequat, ultrices ac magna. Fusce dui lectus, congue vel laoreet ac, dictum vitae odio. Donec aliquet. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam lacinia pulvinar tortor nec facilisis. Pellentesque dapibus efficitur laoreet. Nam risus ante, dapibus a molestie consequat, ultrices ac magna. Fusce dui lectus, congue vel laoreet ac, dictum vitae odio. Donec aliquet. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam lacinia pulvinar tortor nec facilisis. Pellentesque dapibus efficitur laoreet. Nam risus ante, dapibus a molestie consequat, ultrices ac magna. Fusce dui lectus, congue vel laoreet ac, dictum vitae odio. Donec aliquet. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam lacinia pulvinar tortor nec facilisis. Pellentesque dapibus efficitur laoreet. Nam risus ante, dapibus a molestie consequat, ultrices ac magna. Fusce dui lectus, congue vel laoreet ac, dictum vitae odio. Donec aliquet. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam lacinia pulvinar tortor nec facilisis. Pellentesque dapibus efficitur laoreet. Nam risus ante, dapibus a molestie consequat, ultrices ac magna. Fusce dui lectus, congue vel laoreet ac, dictum vitae odio. Donec aliquet. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam lacinia pulvinar tortor nec facilisis. Pellentesque dapibus efficitur laoreet. Nam risus ante, dapibus a molestie consequat, ultrices ac magna. Fusce dui lectus, congue vel laoreet ac, dictum vitae odio. Donec aliquet. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam lacinia pulvinar tortor nec facilisis. Pellentesque dapibus efficitur laoreet. Nam risus ante, dapibus a molestie consequat, ultrices ac magna. Fusce dui lectus, congue vel laoreet ac, dictum vitae odio. Donec aliquet. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam lacinia pulvinar tortor nec facilisis. Pellentesque dapibus efficitur laoreet. Nam risus ante, dapibus a molestie consequat, ultrices ac magna. Fusce dui lectus, congue vel laoreet ac, dictum vitae odio. Donec aliquet. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam lacinia pulvinar tortor nec facilisis. Pellentesque dapibus efficitur laoreet. Nam risus ante, dapibus a molestie consequat, ultrices ac magna. Fusce dui lectus, congue vel laoreet ac, dictum vitae odio. Donec aliquet. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam lacinia pulvinar tortor nec facilisis. Pellentesque dapibus efficitur laoreet. Nam risus ante, dapibus a molestie consequat, ultrices ac magna. Fusce dui lectus, congue vel laoreet ac, dictum vitae odio. Donec aliquet. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam lacinia pulvinar tortor nec facilisis. Pellentesque dapibus efficitur laoreet.

Get unstuck with a CliffsNotes subscription

Example CliffsNotes Question and Answer
Unlock every step-by-step explanation, download literature note PDFs, plus more.Get Access

Related Q&A