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);
}

b.

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;
    private int max;

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

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

Range range1 = new Range(-3, 2);
System.out.println("Does range1 contain -4: " + range1.contains(-4));
System.out.println("Does range1 contain 0: " + range1.contains(0));
Does range1 contain -4: false
Does range1 contain 0: true

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.

import java.util.List;

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

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

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

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

MultipleGroups multiple1 = new MultipleGroups();
multiple1.addGroup(new Range(5, 8));
multiple1.addGroup(new Range(10, 12));
multiple1.addGroup(new Range(1, 6));

System.out.println("contains 7: " + multiple1.contains(7));
System.out.println("contains 9: " + multiple1.contains(9));    
contains 7: true
contains 9: false