August 2000
JP219 : JAVA PROGRAMMING

QUESTION 4

Total Marks: 15 Marks

Click here to access other questions

SUGGESTED SOLUTIONS
Solutions and allocated marks are indicated in green.
Return to
Question 4

You are required to implement a reservation system for meeting rooms. A room has a name, for example “Meeting Room”, and it has a number of slots. A slot is a time period in which the room may be booked. A slot has a time, and a day. The time can be any one of 9 am, 10 am, etc all the way to 4 pm, and a day can be any one of Monday to Friday. When a slot is booked,
it has filled in the name of the person booking the slot, and the reason for the meeting.
Consider the following enumerated class Day, which enumerates the days of the week:

public final class Day {
  public final static int Monday = 0;
  // ... etc ...
  public final static int Friday = 4;
}

public final class Time {
  public final static int am9 = 0;
  public final static int am10= 1;
  // ... etc ...
  public final static int pm4= 7;
};

(a) Define the class Slot, which has the attributes Day, as above, and Time. In addition to this, it should contain two strings, one for the name of the person booking the room and one for the reason they have for booking it. [3 marks]
(a) public class Slot {
  public Day d;
  public Time t;
  public String name;
  public String reason;
};
• Declaring the members public (default protection is not good enough); (1 mark)
• Two strings; (1 mark)
• Using Day and Time; (1 mark)
[3 marks]

(b) Define the class Room, such that it has a name, which is a string that cannot be overloaded by derived classes, and an array of 40 slots, one for each available slot in the week. This array should not be visible to other classes, but may need to be to any derived classes. [3 marks]
(b) public class Room {
  public final String name;
  protected Slot s[];
};
• Declaring name to be a final String; (1 mark)
• Declaring the array s[] (not s[40]); (1 mark)
• Declaring it to be protected; (1 mark)
[3 marks]

(c) Add to your class a constructor function, which initialises the name of the room, and the time and day in each slot, as well as setting the name and reason to empty strings to indicate that the room has not been booked. [5 marks]
(c) Room(String n) {
  s_counter= 0;
  s= new Slot[40];
  for(i= Day.Monday; i<= Day.Friday; i++) {
    for(j= Time.am9; j<= Time.pm4; j++) {
    s[s_counter].d= i;
    s[s_counter].t= j;
    s++;
    }
  }
}
• Allocating the array s to be 40 elements long; (1 mark)
• Declaring an outer loop bound from Day.Monday to Day.Friday; (1 mark)
• Declaring an inner loop bound from Time.am9 to Time.pm4; (1 mark)
• Initialising the members in the current s[] element; (1 mark)
• Incrementing a counter to iterate along s; (1 mark)
[5 marks]

(d) Add to your class a method Book, such that it takes an integer parameter which identifies a particular Slot, and two strings giving a name and a meeting, and books the corresponding slot in the room should it be free. The method should return true if the meeting has been successfully booked, and false otherwise. [4 marks]
(d) boolean Book(String n, String m, int i) {
  if(this.s[i].name== "" && this.s[i].meeting== "") {
    this.s[i].name= n;
    this.s[i].time= m;
    return true;
  }
    return false;
}
• Testing the correct element in s, indexed by i; (1 mark)
• For empty name and meeting strings; (1 mark)
• Assigning the values if they are empty; (1 mark)
• Returning the appropriate value; (1 mark)
[4 marks]