Question 4

copied below

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.

(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.

(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.

Type of FRQ

This was a Methods and Control Structures question. I used inheritance to share properties of different classes between each other.

import java.util.ArrayList;
import java.util.List;

// part a

public interface NumberGroup {
    boolean contains(int value); // simple boolean value 
}

// part b

public class Range implements NumberGroup {
    private int minVal;
    private int maxVal;

    public Range(int minVal, int maxVal) {
        this.minVal = minVal; 
        this.maxVal = maxVal;
    }

    public boolean contains(int num) {
        if(num >= minVal && num <= maxVal) { // if the number is contained within the range 
            return true;
        }
        return false;
    }

    public static void main(String[] args) {
        NumberGroup range = new Range(-3, 2);

        boolean output1 = range.contains(-5);
        boolean output2 = range.contains(0);
        boolean output3 = range.contains(2);

        System.out.println("Part B:");
        System.out.println(output1);
        System.out.println(output2);
        System.out.println(output3);
    }
}
Range.main(null);

// part c

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

    public MultipleGroups() {
        groupList = new ArrayList<>();
    }

    public void addGroup(NumberGroup group) {
        groupList.add(group);
    }

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

    public static void main(String[] args) {
        MultipleGroups multiple = new MultipleGroups();

        multiple.addGroup(new Range(5, 8));
        multiple.addGroup(new Range(10, 12));
        multiple.addGroup(new Range(1, 6));

        boolean output1 = multiple.contains(6);
        boolean output2 = multiple.contains(9);
        boolean output3 = multiple.contains(12);

        System.out.println("Part C");
        System.out.println(output1);
        System.out.println(output2);
        System.out.println(output3);
    }
}

MultipleGroups.main(null);

Part B:
false
true
true
Part C
true
false
true

How I solved each problem:

a) This was a very simple boolean that simply checked whether or not the value exists within the number group

b) This part used the boolean from part a and conditionals to check whether or not a value given as a parameter fell within certain boundaries

c) MultipleGroups used a list to store the group objects. The method addGroup(NumberGroup group) allows adding new groups to the list. The method contains(int value) iterates through the list of groups and checks if any group contains the value.

Reflection

I think this question was the one I had to use the most outside help for. However, when I actually solved the question with the help of tutorials through YouTube, I saw that it was not difficult to solve. The problem is only testing inheritance. This showed me that the reason I was having trouble was because I didn’t understand the problem itself. Something I may work on is brushing up on vocabulary, finding key words/phrases that indicate what concepts CollegeBoard is testing, and overall improving my comprehension. Although they aren’t related to coding, these skills can be the differnce between whether I get a question on the actual test or not.