AP CSA 2023 Q1 - AppointmentBook Class


<-- Back to AP CSA Exam Solutions Next to Solution of Q2 (Sign) - FRQ - 2023 -->





Solution of Q1 (AppointmentBook) - Free Response Question - 2023


The original question can be found at: https://apcentral.collegeboard.org/media/pdf/ap23-frq-comp-sci-a.pdf


Part (a)- public int findFreeBlock(int period, int duration)


    public int findFreeBlock(int period, int duration)
    {
        int blockStart=-1;
        int blockEnd = -1;
        boolean available= true;

        for (int i=0; (i< 60); i++)
        {
            blockStart = i;
            blockEnd = blockStart + duration -1;
            
            // expected duration is larger than available time in the period
            if (blockEnd > 59) return -1;
            
            available= true;

                for (int j =i; (j<= blockEnd); j++)
                {
                    if (!isMinuteFree(period,j)) available = false;
                }
            if (available) return blockStart;
        }
        return -1;
    }


Part (b)- public boolean makeAppointment(int startPeriod, int endPeriod, int duration)


    public boolean makeAppointment(int startPeriod, int endPeriod, int duration)
    {
        int blockStartMinute =0;
        for (int i= startPeriod; i <= endPeriod; i++)
        {
            blockStartMinute = findFreeBlock(i, duration);
            
            if (blockStartMinute != -1)
            {   //reserve block
                reserveBlock(i, blockStartMinute, duration);
                return true;
            }
        }
        return false;
    }



Java project files (with Runner code):


<-- Back to AP CSA Exam Solutions Next to Solution of Q2 (Sign) - FRQ - 2023 -->