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
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.
Working Code
public interface GroupOfNumbers {
boolean includes(int value);
}
public class Interval implements GroupOfNumbers {
private int lowerBound;
private int upperBound;
public Interval(int lowerBound, int upperBound) {
this.lowerBound = lowerBound;
this.upperBound = upperBound;
}
public boolean includes(int value) {
return value >= lowerBound && value <= upperBound;
}
}
public class CompositeGroup implements GroupOfNumbers {
private List<GroupOfNumbers> listOfGroups;
public CompositeGroup(List<GroupOfNumbers> listOfGroups) {
this.listOfGroups = listOfGroups;
}
public boolean includes(int value) {
for (GroupOfNumbers group : listOfGroups) {
if (group.includes(value)) {
return true;
}
}
return false;
}
}
public class TestGroups {
public static void main(String[] args) {
GroupOfNumbers group1 = new Interval(-3, 2);
System.out.println("group1.includes(-2): " + group1.includes(-2));
System.out.println("group1.includes(3): " + group1.includes(3));
GroupOfNumbers group2 = new Interval(4, 7);
GroupOfNumbers group3 = new Interval(10, 15);
List<GroupOfNumbers> groupList = new ArrayList<>();
groupList.add(group2);
groupList.add(group3);
GroupOfNumbers compositeGroup = new CompositeGroup(groupList);
System.out.println("compositeGroup.includes(4): " + compositeGroup.includes(4));
System.out.println("compositeGroup.includes(100): " + compositeGroup.includes(100));
System.out.println("compositeGroup.includes(11): " + compositeGroup.includes(11));
}
}
TestGroups.main(null);
group1.includes(-2): true
group1.includes(3): false
compositeGroup.includes(4): true
compositeGroup.includes(100): false
compositeGroup.includes(11): true