Skip to the content.

Collegeboard 2015 Practice Exam FRQ 4 • 9 min read

Interfaces (Includes Classes and some Methods and Control Structures)

  • This FRQ made me think about how to use Java’s object-oriented features effectively. It made me consider how to organize classes and use concepts like interfaces. By creating a NumberGroup interface and implementing it in multiple ways, I was able to build a system that checks if numbers fall within certain ranges. This shows how Classes and Methods and Control Structures were used in this FRQ.

Part A

// NumberGroup interface
interface NumberGroup {
    boolean contains(int number);
}

// Simple implementation of NumberGroup interface
class SimpleNumberGroup implements NumberGroup {
    private int[] numbers;

    // Constructor
    public SimpleNumberGroup(int[] numbers) {
        this.numbers = numbers;
    }

    // Method to check if a number is in the group
    public boolean contains(int number) {
        for (int num : numbers) {
            if (num == number) {
                return true;
            }
        }
        return false;
    }
}

// Main class for testing
public class MainA {
    public static void main(String[] args) {
        // Example usage
        int[] group1Numbers = {-5, 3};
        NumberGroup group1 = new SimpleNumberGroup(group1Numbers);

        System.out.println("Group1 contains -5: " + group1.contains(-5)); // Should print true
        System.out.println("Group1 contains 3: " + group1.contains(3));   // Should print true
        System.out.println("Group1 contains 2: " + group1.contains(2));   // Should print false

    }
}

MainA.main(null);
Group1 contains -5: true
Group1 contains 3: true
Group1 contains 2: false

Part B

// Range class implementing NumberGroup
class Range implements NumberGroup {
    private int min;
    private int max;

    // Constructor
    public Range(int min, int max) {
        this.min = min;
        this.max = max;
    }

    // Method to check if a number is within the range
    public boolean contains(int number) {
        return number >= min && number <= max;
    }
}

// Main class for testing
public class MainB {
    public static void main(String[] args) {
        // Example usage
        NumberGroup range1 = new Range(-3, 2);
        NumberGroup range2 = new Range(17, 21);
        NumberGroup range3 = new Range(-100, -52);

        System.out.println("Range1 contains -5: " + range1.contains(-5)); // Should print false
        System.out.println("Range1 contains 0: " + range1.contains(0));   // Should print true

        System.out.println("Range2 contains 3: " + range2.contains(3)); // Should print false
        System.out.println("Range2 contains 21: " + range2.contains(21));   // Should print true

        System.out.println("Range3 contains -101: " + range3.contains(-101)); // Should print false
        System.out.println("Range3 contains -72: " + range3.contains(-72));   // Should print true

    }
}

MainB.main(null);
Range1 contains -5: false
Range1 contains 0: true
Range2 contains 3: false
Range2 contains 21: true
Range3 contains -101: false
Range3 contains -72: true

Part C

// MultipleGroups class representing a collection of NumberGroup objects
class MultipleGroups implements NumberGroup {
    private List<NumberGroup> groupList;

    // Constructor
    public MultipleGroups(List<NumberGroup> groupList) {
        this.groupList = groupList;
    }

    // Method to check if the integer is contained in one or more number groups
    public boolean contains(int number) {
        for (NumberGroup group : groupList) {
            if (group.contains(number)) {
                return true;
            }
        }
        return false;
    }
}

// Main class for testing
public class MainC {
    public static void main(String[] args) {
        // Example usage
        MultipleGroups multiple1 = new MultipleGroups(List.of(
                new Range(5, 8),
                new Range(10, 12),
                new Range(1, 6)
        ));

        System.out.println("Multiple1 contains 2: " + multiple1.contains(2)); // Should print true
        System.out.println("Multiple1 contains 9: " + multiple1.contains(9)); // Should print false
        System.out.println("Multiple1 contains 6: " + multiple1.contains(6)); // Should print true
    }
}

MainC.main(null); 
Multiple1 contains 2: true
Multiple1 contains 9: false
Multiple1 contains 6: true