Skip to the content.

2015 Practice FRQ Question 4 • 15 min read

Description

Analysis and breakdown of Question 4 from the 2015 AP Exam

Question 4: METHODS AND CONTROL STRUCTURES

image

Question breakdown and approach:

This question involves designing an interface called NumberGroup to represent a group of integers with a method named contains. The interface is intended to check if a given integer is part of the group. Following this, a class named Range is to be implemented, which represents a number group of integers within a specified range. The Range class, being a implementation of NumberGroup, includes instance variables, methods, and a constructor to handle the range of values. Lastly, a class called MultipleGroups is mentioned, which represents a collection of NumberGroup objects and itself is a NumberGroup. The task requires writing the contains method for MultipleGroups, checking if a given integer is present in one or more of the number groups stored in the instance variable groupList. To approach this, start by defining the NumberGroup interface with the specified contains method. Then, implement the Range class ensuring the proper handling of ranges. Lastly, for the MultipleGroups class, write the contains method by iterating through the groupList and checking each group for the given integer.

Part A:

A number group represents a group of integers defined in some way. It could be empty, or it could contain one or more integers. Write an interface named NumberGroup that represents a group of integers. The interface should have a single contains method that determines if a given integer is in the group. For example, if group1 is of type NumberGroup, and it contains only the two numbers -5 and 3, then group1.contains(-5) would return true, and group1.contains(2) would return false. Write the complete NumberGroup interface. It must have exactly one method.

import java.util.Arrays;
import java.util.List;

public class Main {
    public static void main(String[] args) {
        NumberGroup group1 = new IntegerList(Arrays.asList(66, 63));
        System.out.println(group1.contains(66)); // true
        System.out.println(group1.contains(2));  // false
    }

    // Modified NumberGroup interface
    public interface NumberGroup {
        boolean contains(int num);
    }

    // Modified IntegerList class
    public static class IntegerList implements NumberGroup {
        private List<Integer> numbers;

        public IntegerList(List<Integer> numbers) {
            this.numbers = numbers;
        }

        @Override
        public boolean contains(int num) {
            return numbers.contains(num);
        }
    }
}

Main.main(null);
true
false

Part B:

A range represents a number group that contains all (and only) the integers between a minimum value and a maximum value, inclusive. Write the Range class, which is a NumberGroup. The Range class represents the group of int values that range from a given minimum value up through a given maximum value, inclusive. For example, the declaration:

NumberGroup range1 = new Range(-3, 2);

represents the group of integer values -3, -2, -1, 0, 1, 2. Write the complete Range class. Include all necessary instance variables and methods as well as a constructor that takes two int parameters. The first parameter represents the minimum value, and the second parameter represents the maximum value of the range. You may assume that the minimum is less than or equal to the maximum.

public class Main {
    public static void main(String[] args) {
        NumberGroup range1 = new CustomRange(-6, 5);
        System.out.println(range1.contains(-3)); // true
        System.out.println(range1.contains(0));  // true
        System.out.println(range1.contains(7));  // false
    }

    public interface NumberGroup {
        boolean contains(int num);
    }

    public static class CustomRange implements NumberGroup {
        private int min;
        private int max;

        public CustomRange(int min, int max) {
            this.min = min;
            this.max = max;
        }

        @Override
        public boolean contains(int num) {
            return num >= min && num <= max;
        }
    }
}

Main.main(null);
true
true
false

PART C:

The MultipleGroups class (not shown) represents a collection of NumberGroup objects and isa NumberGroup. The MultipleGroups class stores the number groups in the instance variable groupList (shown below), which is initialized in the constructor.

private List groupList;

Write the MultipleGroups method contains. The method takes an integer and returns true if and only if the integer is contained in one or more of the number groups in groupList. For example, suppose multiple1 has been declared as an instance of MultipleGroups and consists of the three ranges created by the calls new Range(5, 8), new Range(10, 12), and new Range(1, 6). The following table shows the results of several calls to contains.

import java.util.List;

public class Main {
    public static void main(String[] args) {
        List<NumberGroup> groups = List.of(new Range(5, 8), new Range(10, 12), new Range(1, 6));
        MultipleGroups multiple1 = new MultipleGroups(groups);
        
        System.out.println(multiple1.contains(2)); // true
        System.out.println(multiple1.contains(9)); // false
        System.out.println(multiple1.contains(6)); // true
    }

    // from part A
    public interface NumberGroup {
        boolean contains(int num);
    }

    // from part B
    public static class Range implements NumberGroup {
        private int min;
        private int max;

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

        @Override
        public boolean contains(int num) {
            return num >= min && num <= max;
        }
    }

    public static class MultipleGroups implements NumberGroup {
        private List<NumberGroup> groupList;

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

        @Override
        public boolean contains(int num) {
            for (NumberGroup group : groupList) {
                if (group.contains(num)) {
                    return true;
                }
            }
            return false;
        }
    }
}

Main.main(null);
true
false
true

Summary:

For this question, I had to create a class called MultipleGroups, which represents a collection of NumberGroup objects and implements the NumberGroup interface. The class stores its number groups in groupList and required a contains method that checks if a given integer is present in any of these groups. To achieve this, I iterated through each NumberGroup in groupList and used the contains method of each group to determine if the provided integer is contained within. The provided code also included a Main class with a main method showcasing the usage of the implemented classes: Range, MultipleGroups, and the NumberGroup interface. To come up with this solution, I had to look back on concepts from previous lessons, focusing on interface implementation, class structure, and list manipulation in Java.