Home 2015 FRQ Question 4
Post
Cancel

2015 FRQ Question 4

2015 Collegeboard FRQ

Question 4

This question involves the design of an interface, writing a class that implements the interface, and writing a method that uses the interface.

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.

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

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
public 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){
        if (num >= min && num <= max){
            return true;
        }
        return false;
    }
    public static void main(String args[]) {
        Range range = new Range(-5, 7);
        System.out.println("Contains 3: " + range.contains(3));
        System.out.println("Contains 10: " + range.contains(10));
    }
}
Range.main(null);
1
2
Contains 3: true
Contains 10: false

c.

The MultipleGroups class (not shown) represents a collection of NumberGroup objects and is a 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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
public 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;
    }

    public static void main(String args[]) {
        ArrayList<NumberGroup> groupsList = new ArrayList<NumberGroup>();

        Range range1 = new Range(-5, 7);
        Range range2 = new Range(3, 9);
        Range range3 = new Range(12,14);
        groupsList.add(range1); 
        groupsList.add(range2); 
        groupsList.add(range3);

        MultipleGroups multipleGrp = new MultipleGroups(groupsList);
        System.out.println("Contains 3: " + multipleGrp.contains(3));
        System.out.println("Contains 10: " + multipleGrp.contains(10));
        System.out.println("Contains 13: " + multipleGrp.contains(13));
    }
}
MultipleGroups.main(null);
1
2
3
Contains 3: true
Contains 10: false
Contains 13: true

Blog

The specific type of FRQ 4 is methods and control structures. It implements polymorphism through the NumberGroup interface and classes, where each of these classes has to implement the contains method. The Range class implements the NumberGroup interface directly for a range with a constructor initlizaing a min and a max, and contains method checks if a number is in that range. The MultipleGroups class holds the list of NumberGroup Objects where a MultipleGroups instance can contain multiple different number groups. The contains method here iterates through each group to check if the specified number is within the range.

This post is licensed under CC BY 4.0 by the author.