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.

(b) A range represents a number group that contains all (and only) the integers between a minimum value and a maximum value, inclusive. 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.

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.

Call Result
multiple1.contains(2) true
multiple1.contains(9) false
multiple1.contains(6) true
public interface NumberGroup {
    boolean contains(int integer);
}

public class Range implements NumberGroup {
    public int min;
    public int max;
    public Range (int min, int max) {
        this.min = min;
        this.max = max;
    }
    public boolean contains(int integer) {
        if (integer >= min && integer <= max) {
            return true;
        }
        return false;
    }
}

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

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

    public boolean contains(int integer) {
        for (NumberGroup num : grouplist) {
            if (num.contains(integer)) {
                return true;
            }
        }
        return false;
    }
}

public class Main {
    public static void main(String[] args) {
        NumberGroup range1 = new Range(-3,2);
        System.out.println("range1.contains(0): " + range1.contains(0));
        NumberGroup range2 = new Range(5, 8);
        NumberGroup range3 = new Range(10, 12);
        NumberGroup range4 = new Range(1, 6);
        List<NumberGroup> grouplist = new ArrayList<NumberGroup>();
        grouplist.add(range2);
        grouplist.add(range3);
        grouplist.add(range4);
        MultipleGroups multiple1 = new MultipleGroups(grouplist);
        System.out.println("multiple.contains(2): " + multiple1.contains(2));
        System.out.println("multiple.contains(9): " + multiple1.contains(9));
        System.out.println("multiple.contains(6): " + multiple1.contains(6));
    }
}
Main.main(null);
range1.contains(0): true
multiple.contains(2): true
multiple.contains(9): false
multiple.contains(6): true

Reflection

This FRQ centered around methods and control structures, and contained interfaces and implemented inheritance. The FRQ was mainly about the use of a contains method which was changed in each new class that implemented the interface. This FRQ wasn’t too bad although it was a bit time consuming as I took some time thinking of what my next steps should be. Overall, this FRQ was fairly simple and I was able to solve it without outside help.