public class AppointmentBook
{
private boolean[][] freePeriod = new boolean[9][60];
public AppointmentBook()
{
for (int i = 0; i < 9; i++) // Only uses row 1 to 8
for (int j = 0; j < 60; j++)
freePeriod[i][j] = true;
}
/**
* Returns true if minute in period is available for an appointment and
* returns false otherwise
* Preconditions: 1 <= period <= 8; 0 <= minute <= 59
*/
private boolean isMinuteFree( int period, int minute )
{
return freePeriod[period][minute];
}
/**
* Marks the block of minutes that starts at startMinute in period and
* is duration minutes long as reserved for an appointment
* Preconditions: 1 <= period <= 8; 0 <= startMinute <= 59;
* 1 <= duration <= 60
*/
public void reserveBlock( int period, int startMinute, int duration )
{
for ( int j = startMinute; j < startMinute+duration; j++)
freePeriod[period][j] = false;
}
/**
* Searches for the first block of duration free minutes during period,
* as described in part (a).
* Returns the first minute in the block if such a block is found or
* returns -1 if no such block is found.
* Preconditions: 1 <= period <= 8; 1 <= duration <= 60
*/
public int findFreeBlock( int period, int duration )
{
/* to be implemented in part (a) */
return -1; // Change when writing answer
}
/**
* Searches periods from startPeriod to endPeriod, inclusive, for a block
* of duration free minutes, as described in part (b).
* If such a block is found, calls reserveBlock to reserve the block
* of minutes and returns true; otherwise returns false.
* Preconditions: 1 <= startPeriod <= endPeriod <= 8; 1 <= duration <= 60
*/
public boolean makeAppointment(int startPeriod, int endPeriod, int duration)
{
/* to be implemented in part (b) */
return false; // Change when writing answer
}
public String toString(int period)
{
boolean currState = freePeriod[period][0];
int initMin = 0;
int currMin = 1;
String out = "";
while ( currMin <= 59 )
{
if ( freePeriod[period][currMin] != currState )
{
out = out + period + "\t"
+ initMin + "-" + (currMin-1)
+ " (" + (currMin-initMin) + " minutes)\t"
+ (currState ? "Yes" : "No") + "\n";
initMin = currMin;
currState = freePeriod[period][currMin];
}
if ( currMin == 59 )
{
out = out + period + "\t"
+ initMin + "-" + (currMin)
+ " (" + (currMin-initMin+1) + " minutes)\t"
+ (currState ? "Yes" : "No") + "\n";
}
currMin++;
}
return out;
}
}
|
Use this Java program to test your answer to question A:
public class TestPartA
{
public static void main(String[] args)
{
AppointmentBook x = new AppointmentBook();
x.reserveBlock(2, 0, 10);
x.reserveBlock(2, 15, 15);
x.reserveBlock(2, 45, 5);
System.out.println("Period\tMinutes\t Available?");
System.out.println("======================================");
System.out.println( x.toString(2) );
System.out.println( x.findFreeBlock(2, 5));
System.out.println( x.findFreeBlock(2, 15));
System.out.println( x.findFreeBlock(2, 9));
System.out.println( x.findFreeBlock(2, 20));
}
}
|
The correct answer is:
Period Minutes Available? ====================================== 2 0-9 (10 minutes) No 2 10-14 (5 minutes) Yes 2 15-29 (15 minutes) No 2 30-44 (15 minutes) Yes 2 45-49 (5 minutes) No 2 50-59 (10 minutes) Yes 10 30 30 -1 |
Use this Java program to test your answer to question B:
public class TestPartB
{
public static void main(String[] args)
{
AppointmentBook x = new AppointmentBook();
x.reserveBlock(2, 0, 25);
x.reserveBlock(2, 30, 30);
x.reserveBlock(3, 15, 26);
x.reserveBlock(4, 0, 5);
x.reserveBlock(4, 30, 14);
System.out.println("Period\tMinutes\t Available?");
System.out.println("======================================");
System.out.println( x.toString(2) );
System.out.println( x.toString(3) );
System.out.println( x.toString(4) );
System.out.println( x.makeAppointment(2, 4, 22) );
System.out.println( x.makeAppointment(3, 4, 3) );
System.out.println( x.makeAppointment(2, 4, 30) );
System.out.println( );
System.out.println("Period\tMinutes\t Available?");
System.out.println("======================================");
System.out.println( x.toString(2) );
System.out.println( x.toString(3) );
System.out.println( x.toString(4) );
}
}
|
The correct answer is:
Period Minutes Available? ====================================== 2 0-24 (25 minutes) No 2 25-29 (5 minutes) Yes 2 30-59 (30 minutes) No 3 0-14 (15 minutes) Yes 3 15-40 (26 minutes) No 3 41-59 (19 minutes) Yes 4 0-4 (5 minutes) No 4 5-29 (25 minutes) Yes 4 30-43 (14 minutes) No 4 44-59 (16 minutes) Yes true true false Period Minutes Available? ====================================== 2 0-24 (25 minutes) No 2 25-29 (5 minutes) Yes 2 30-59 (30 minutes) No 3 0-2 (3 minutes) No 3 3-14 (12 minutes) Yes 3 15-40 (26 minutes) No 3 41-59 (19 minutes) Yes 4 0-26 (27 minutes) No 4 27-29 (3 minutes) Yes 4 30-43 (14 minutes) No 4 44-59 (16 minutes) Yes |