FRQ #1 - Methods and Control Structures

This question involves the AppointmentBook class, which provides methods for students to schedule appointments with their teacher. Appointments can be scheduled during one of eight class periods during the school day, numbered 1 through 8. A requested appointment has a duration, which is the number of mins the appointment will last. The 60 minutes within a period are numbered 0 through 59. In order for an appointment to be scheduled, the teacher must have a block of consecutive, available minutes that contains at least the requested number of minutes in a requested period. Scheduled appointments must start and end within the same period.

Question

Part A

Part a

public int findFreeBlock(int period, int duration)
{
    int free = 0;
    
    for(int min = 0; min <= 59; min++)
    {
        if(isMinuteFree(period, minute))
            free++;
        else
            free = 0;
        
        if(free == duration)
            return min - free + 1;
    }
    
    return -1;
}

Part B

Part B_1

Part B_2

public boolean makeAppointment(int start, int end, int duration)
{
    for(int period = start; period <= end; period++)
    {
        int startMinute = findFreeBlock(period, duration);
        if(startMinute != -1)
        {
            reserveBlock(period, startMinute, duration);
            return true;
        }
    }
    return false;
}

Part 3

Array Experimentation

String Foods;
String[] Foods = {"Burrito", "Pasta", "Tacos", "Rice"};
System.out.println(Foods[0])
Burrito
System.out.println(Foods.length);
4

Now we will go with 2D Arrays, this is some experimentation

int[][] arr = { { 1, 2 }, { 3, 4 } };
System.out.println("Pos 0: " + arr[0][0] + ", " + arr[0][1]);
System.out.println("Pos 1: " + arr[1][0] + ", " + arr[1][1]);
Pos 0: 1, 2
Pos 1: 3, 4

Part 4 – Array Implimentation

Part A

There is only one variable here so it would not really make sense to put an array for a static variable that we are only changing to determine the time; ie we need no history of the variable as well.

public int findFreeBlock(int period, int duration)
{
    int free = 0;
    
    for(int min = 0; min <= 59; min++)
    {
        if(isMinuteFree(period, minute))
            free++;
        else
            free = 0;
        
        if(free == duration)
            return min - free + 1;
    }
    
    return -1;
}

Part B

Since we only use one variable here, pulling only one definate value from a function, it would be impractical to use an array here.

public boolean makeAppointment(int start, int end, int duration)
{
    for(int period = start; period <= end; period++)
    {
        int startMinute = findFreeBlock(period, duration);
        if(startMinute != -1)
        {
            reserveBlock(period, startMinute, duration);
            return true;
        }
    }
    return false;
}

Part A – Implimented

public int findFreeBlock(int period, int duration)
{
    int[] free = {0};
    
    for(int min = 0; min <= 59; min++)
    {
        if(isMinuteFree(period, minute))
            free[0]++;
        else
            free[0] = 0;
        
        if(free[0] == duration)
            return min - free + 1;
    }
    
    return -1;
}

Part 5: Project with Arrays with Methods and Control Structures

public int transform(int original)
{
    int[] transformers = {1, -1, 2, -3, 5, -8, 13, -21};

    for(int i = 0; i <= 7; i++)
    {
        // original = original + transformers[i];
        if(original < 0)
            original = (transformers[i] * original) - transformers[i];
        else
            original = (transformers[i] * original) + transformers[i];
    }
    return original;
}
transform(100)
6794718
transform(-1)
-308238
transform(0.5)
|   transform(0.5)

incompatible types: possible lossy conversion from double to int
transform(-300)
-19898718
transform(0)
242718

Complete Code Execution

public class AppointmentBook
{
    // package access for testing
    // [period - 1][minute]
    boolean[][] freeMinutes;
    
    public AppointmentBook()
    {
        freeMinutes = new boolean[8][60];
        
        for(int r = 0; r < freeMinutes.length; r++)
            for(int c = 0; c < freeMinutes[0].length; c++)
                freeMinutes[r][c] = true;
    }
    
    private boolean isMinuteFree(int period, int minute)
    {
        return freeMinutes[period - 1][minute];
    }
    
    // package access for testing
    void reserveBlock(int period, int startMinute, int duration)
    {
        for(int minute = startMinute; minute < startMinute + duration; minute++)
            freeMinutes[period - 1][minute] = false;
    }
    
    public int findFreeBlock(int period, int duration)
    {
        int freeInARow = 0;
        
        for(int minute = 0; minute <= 59; minute++)
        {
            if(isMinuteFree(period, minute))
                freeInARow++;
            else
                freeInARow = 0;
            
            if(freeInARow == duration)
                return minute - freeInARow + 1;
        }
        
        return -1;
    }
    
    public boolean makeAppointment(int startPeriod, int endPeriod,
                                   int duration)
    {
        for(int period = startPeriod; period <= endPeriod; period++)
        {
            int startMinute = findFreeBlock(period, duration);
            
            if(startMinute != -1)
            {
                reserveBlock(period, startMinute, duration);
                return true;
            }
        }
        
        return false;
    }
}
import java.util.Arrays;
//import org.junit.Test;
//import org.junit.Assert;

public class OneTest
{
    public void testFindFreeBlockAgainstExamples()
    {
        AppointmentBook book = new AppointmentBook();
        
        book.reserveBlock(2, 0, 10);
        book.reserveBlock(2, 15, 15);
        book.reserveBlock(2, 45, 5);
        
        System.out.println(book.findFreeBlock(2, 15) == 30);
        System.out.println(book.findFreeBlock(2, 9) == 30);
        System.out.println(book.findFreeBlock(2, 20) == -1);
    }
    
    public void testMakeAppointmentAgainstExamples()
    {
        AppointmentBook book = new AppointmentBook();
        
        book.reserveBlock(2, 0, 25);
        book.reserveBlock(2, 30, 30);
        book.reserveBlock(3, 15, 26);
        book.reserveBlock(4, 0, 5);
        book.reserveBlock(4, 30, 14);
        
        AppointmentBook expectedBook = new AppointmentBook();
        
        expectedBook.reserveBlock(2, 0, 25);
        expectedBook.reserveBlock(2, 30, 30);
        expectedBook.reserveBlock(3, 15, 26);
        expectedBook.reserveBlock(4, 0, 5);
        expectedBook.reserveBlock(4, 30, 14);
        
        expectedBook.reserveBlock(4, 5, 22);
        
        System.out.println(book.makeAppointment(2, 4, 22));
        System.out.println(Arrays.deepEquals(expectedBook.freeMinutes, book.freeMinutes));
        
        expectedBook.reserveBlock(3, 0, 3);
        
        System.out.println(book.makeAppointment(3, 4, 3));
        System.out.println(Arrays.deepEquals(expectedBook.freeMinutes, book.freeMinutes));
        
        System.out.println(!book.makeAppointment(2, 4, 30));
        System.out.println(Arrays.deepEquals(expectedBook.freeMinutes, book.freeMinutes));
    }
}

OneTest test = new OneTest();
test.testFindFreeBlockAgainstExamples();
test.testMakeAppointmentAgainstExamples();
true
true
true
true
true
true
true
true
true