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.

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

class Test {
    public static void main(String[] args) {
        testNumberGroup();
    }
}

class NormalNumberGroup implements NumberGroup {
    private int[] numbers = {5, -3}; // array to hold the numbers in the group

    @Override
    public boolean contains(int number) {
        for (int n : numbers) { // iterate through the numbers in the group
            if (n == number) { // check if the current number matches the given number
                return true; // return true if the number is found in the group
            }
        }
        return false; // return false if the number is not found in the group
    }
}

public static void testNumberGroup() {
    NumberGroup group = new NormalNumberGroup();
    System.out.println("Testing number group:");
    System.out.println("Group contains 5: " + group.contains(5)); // contains 5, true
    System.out.println("Group contains -3: " + group.contains(-3)); // contains -3, true
    System.out.println("Group contains 100: " + group.contains(100)); // does not contain 100, false
    System.out.println("Group contains 23: " + group.contains(23)); // does not contain 23, false
}
Test.main(null);
Testing number group:
Group contains 5: true
Group contains -3: true
Group contains 100: false
Group contains 23: false

(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 Range implements NumberGroup {
    private int min; // minimum value of the range
    private int max; // maximum value of the range

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

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

class Test {
    public static void main(String[] args) {
        testNumberGroup(); // call the test method
    }

    public static void testNumberGroup() {
        NumberGroup range = new Range(1, 23); // create a range from 1 to 23
        System.out.println("Range contains 5: " + range.contains(5)); // true
        System.out.println("Range contains 2: " + range.contains(17)); // true
        System.out.println("Range contains 22: " + range.contains(24)); // false
    }
}

interface NumberGroup {
    boolean contains(int number);
}
Test.main(null);
Range contains 5: true
Range contains 2: true
Range contains 22: false

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

Image

Image

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

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

    @Override
    public boolean contains(int number) {
        for (NumberGroup group : groupList) {
            if (group.contains(number)) {
                return true; // return true if the number is found in any group
            }
        }
        return false; // return false if the number is not found in any group
    }
}
public class Test {
    public static void main(String[] args) {
        // ranges from College Board
        NumberGroup range1 = new Range(5, 8);
        NumberGroup range2 = new Range(10, 12);
        NumberGroup range3 = new Range(1, 6);
        
        // list of NumberGroup objects
        List<NumberGroup> groupList = new ArrayList<>();
        groupList.add(range1);
        groupList.add(range2);
        groupList.add(range3);
        
        MultipleGroups multiple1 = new MultipleGroups(groupList);
        
        System.out.println("Contains 2: " + multiple1.contains(2)); // true
        System.out.println("Contains 9: " + multiple1.contains(9)); // false, 9 is not included :(
        System.out.println("Contains 6: " + multiple1.contains(6)); // true
    }
}
Test.main(null);
Contains 2: true
Contains 9: false
Contains 6: true