Java Bluej question.
question 1,
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.
(2 marks)
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.
(3 marks)
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.
(3 marks)
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).
(5 marks)
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. You will find this method documented in Section 1 of the M250 Java Reference, or for more detail see BlueJ's Help menu and under Java Class Libraries look for the String class in the left-hand panel, then for the charAt method in the right-hand panel. 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.
(5 marks)
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".
(5 marks)
h.
Add a method called description that takes no parameters and returns a string describing the room, using the following format:
type room number (availability) guest
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.
(5 marks)
i.
Go back and check that you have provided multiline comments for your class, constructor, and methods. We do not expect you to use all of the features of comments illustrated in the book at this stage of your study but be sure that your comments explain the purpose of the code and identify which part of the TMA question you are answering in each case.
Also check that you have used a consistent style across the whole class. See the M250 Code conventions and design document for more advice on coding style.
(10 marks)