Methods and Control Sturcutres

Methods

In Java, methods are blocks of code within classes that perform specific tasks or functions. They are defined with a name, optional parameters, and an optional return type, and they can be invoked (called) from other parts of the program to execute the code within them. Methods help organize and modularize code, making it easier to read, maintain, and reuse, and they are a fundamental building block of Java programs, allowing developers to encapsulate functionality and promote code reusability.

Control Structures

Control structures in Java are constructs that govern the flow of a program by allowing you to make decisions and control the execution of code. The primary control structures in Java include if-else statements, switch statements, loops (such as for, while, and do-while loops), and branching mechanisms like break and continue. These structures enable developers to create conditions, loops, and branching logic, empowering them to execute specific code blocks based on conditions, iterate through data, and control the program’s flow, ultimately enhancing the program’s logic and functionality.

FRQ HACKS

Using the classes type of questions

link the the frq https://apcentral.collegeboard.org/media/pdf/ap23-frq-comp-sci-a.pdf

Answering the FRQ

Solve the problem, including missing pieces in a Jupyter Notebook.

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 had a duration, which is the number of minutes the appointment will last. The 60 minutes within a period are numbered 0 through 59. In order for an appointemnt to be scheudled, the teacher must ave a block of consecutive, available minutes that contains at least the requested number of minutes in a requested period. Scheudled appointments must start and end within the same period.

The AppointmentBook class contains two helper methods, isMinuteFree and reserceBlock. You will write two additional methods of the AppointmentBook class.

PART A

Write the findFreeBlock method, which searches period for the first block of free minutes that is duration minutes long. If such bloack is found, findFreeBlock returns the first minute in the block. Otherwise, findFreeBlock returns -1. The findFreeBlock method uses the helper method isMinuteFree which returns true if a particular minute is available to be included in a new appointment and returns false if the minute is unavailable

Consider the following list of unavailable and available minutes in period 2

0-9 is not available

10-14 is available

15-29 is not available

30-44 is available

45-49 is not available

50-59 is available

import java.util.List;

public class FreeBlockFinder {
    // Function to find a free block of the specified duration within a period.
    public static int findFreeBlock(List<Boolean> period, int duration) {
        int periodSize = period.size();
        
        // Loop through each minute in the period.
        for (int minute = 0; minute <= periodSize - duration; minute++) {
            boolean isBlockFree = true;
            
            // Check if the block of the specified duration is free.
            for (int i = 0; i < duration; i++) {
                if (!isMinuteFree(period, minute + i)) {
                    isBlockFree = false;
                    break; // Break out of the inner loop as soon as a non-free minute is found.
                }
            }
            
            // If the block is free, return the starting minute of the free block.
            if (isBlockFree) {
                return minute;
            }
        }
        
        // If no free block of the specified duration is found, return -1.
        return -1;
    }
    
    // Function to check if a specific minute within the period is free.
    public static boolean isMinuteFree(List<Boolean> period, int minute) {
        if (minute < 0 || minute >= period.size()) {
            return false; // Minute is out of range, considered as not free.
        }
        
        return period.get(minute); // Return whether the minute is free or not.
    }
    
    // Main method for testing the FreeBlockFinder class.
    public static void main(String[] args) {
        // Example usage: Define a period of availability represented as a list of Boolean values.
        List<Boolean> period = List.of(
            false, false, false, false, false, false, false, false, false, false, // 0-9
            true, true, true, true, true, // 10-14
            false, false, false, false, false, false, false, false, false, false, // 15-29
            true, true, true, true, true, // 30-44
            false, false, false, false, false // 45-49
        );
        
        int duration = 3; // Specify the duration of the block to search for.
        int minute = findFreeBlock(period, duration); // Find a free block of the specified duration.
        
        // Output the result based on whether a free block was found or not.
        if (minute != -1) {
            System.out.println("Free block found starting at minute " + minute);
        } else {
            System.out.println("No free block of duration " + duration + " found.");
        }
    }
}
FreeBlockFinder.main(null)
Free block found starting at minute 10

PART B

The method call findFreeBlock (2,15) would return 30 to indicate that a 15 minute block starting with minute 30 is available. No steps should be taken as a result of the call to findFreeBlock to mark those 15 minutes as unavailable.

The method call findFreeBlock (2,9) would also return 30. Whenever there are multiple blocks that satisfy the requirment, the earliest starting minute is returned.

The method findFreeBlock (2,20) would return -1, since no 20-minute block of available minutes exists in period 2.

Complete method findFreeBlock. You must use isMinuteFree appropriatley in order to receive full credit.

import java.util.List;

public class FreeBlockFinder {
    // Function to find a free block of the specified duration within a period.
    public static int findFreeBlock(List<Boolean> period, int duration) {
        int periodSize = period.size();
        
        // Loop through each minute in the period.
        for (int minute = 0; minute <= periodSize - duration; minute++) {
            boolean isBlockFree = true;
            
            // Check if the block of the specified duration is free.
            for (int i = 0; i < duration; i++) {
                if (!isMinuteFree(period, minute + i)) {
                    isBlockFree = false;
                    break; // Break out of the inner loop as soon as a non-free minute is found.
                }
            }
            
            // If the block is free, return the starting minute of the free block.
            if (isBlockFree) {
                return minute;
            }
        }
        
        // If no free block of the specified duration is found, return -1.
        return -1;
    }
    
    // Function to check if a specific minute within the period is free.
    public static boolean isMinuteFree(List<Boolean> period, int minute) {
        if (minute < 0 || minute >= period.size()) {
            return false; // Minute is out of range, considered as not free.
        }
        
        return period.get(minute); // Return whether the minute is free or not.
    }
    
    // Main method for testing the FreeBlockFinder class.
    public static void main(String[] args) {
        // Example usage: Define a period of availability represented as a list of Boolean values.
        List<Boolean> period = List.of(
            false, false, false, false, false, false, false, false, false, false, // 0-9
            true, true, true, true, true, // 10-14
            false, false, false, false, false, false, false, false, false, false, // 15-29
            true, true, true, true, true, // 30-44
            false, false, false, false, false // 45-49
        );
        
        // Test the findFreeBlock function with different durations.
        int duration1 = 15;
        int minute1 = findFreeBlock(period, duration1);
        
        int duration2 = 9;
        int minute2 = findFreeBlock(period, duration2);
        
        int duration3 = 20;
        int minute3 = findFreeBlock(period, duration3);
        
        // Output the results for each duration.
        System.out.println("For duration " + duration1 + ":");
        if (minute1 != -1) {
            System.out.println("Free block found starting at minute " + minute1);
        } else {
            System.out.println("No free block of duration " + duration1 + " found.");
        }
        
        System.out.println("For duration " + duration2 + ":");
        if (minute2 != -1) {
            System.out.println("Free block found starting at minute " + minute2);
        } else {
            System.out.println("No free block of duration " + duration2 + " found.");
        }
        
        System.out.println("For duration " + duration3 + ":");
        if (minute3 != -1) {
            System.out.println("Free block found starting at minute " + minute3);
        } else {
            System.out.println("No free block of duration " + duration3 + " found.");
        }
    }
}
FreeBlockFinder.main(null)
For duration 15:
No free block of duration 15 found.
For duration 9:
No free block of duration 9 found.
For duration 20:
No free block of duration 20 found.

IMG_0603603FAA0A-1 IMG_0232

thoughts/challenges while doing the FRQ

  • Planning on how to answer the prompt
  • time management, during the test this is crutial
  • realizing i’ll have to later wirte this by hand
  • optimizing my code
  • double checking code

future PBL questions ?

outside sources

Khan Academy Sources

https://www.khanacademy.org/computing/ap-computer-science-principles/algorithms-101/building-algorithms/a/the-building-blocks-of-algorithms

https://www.khanacademy.org/computing/ap-computer-science-principles/programming-101/lists/a/storing-lists-of-data

College Board Methods and COntrol Sturcutres

https://www.youtube.com/watch?v=hwXArSLHvMg