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.
public interface NumberGroup {
boolean contains(int n);
}
// test
class NumberGroupTest {
public static void main(String[] args) {
// using numbergroup
NumberGroup group = new NumberGroup() {
@Override
public boolean contains(int n) {
// defining group with only -5 and 3
return n == -5 || n == 3;
}
};
System.out.println("contains -5 : " + group.contains(-5)); // true
System.out.println("contains 2 : " + group.contains(2)); // false
}
}
NumberGroupTest.main(null);
contains -5 : true
contains 2 : 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 interface NumberGroup {
// checking if value is in group
boolean contains(int value);
}
public class Range implements NumberGroup {
// minimum value in range
private int min;
// maximum value in range
private int max;
// initializing range with the min and max values
public Range(int min, int max) {
this.min = min;
this.max = max;
}
//checking if values are within rnage
public boolean contains(int value) {
return value >= min && value <= max;
}
// returns a string representation of all values in the range
@Override
public String toString() {
StringBuilder builder = new StringBuilder();
builder.append("range: ");
for (int i = min; i <= max; i++) {
builder.append(i).append(" ");
}
return builder.toString().trim();
}
}
// test
class RangeTest {
public static void main(String[] args) {
// making an instance range of -3,2
Range range = new Range(-3, 2);
System.out.println(range);
System.out.println(range.contains(-3)); // true
System.out.println(range.contains(0)); // true
System.out.println(range.contains(3)); // false
}
}
RangeTest.main(null);
range: -3 -2 -1 0 1 2
true
true
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
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.
import java.util.List;
import java.util.ArrayList;
public class MultipleGroups implements NumberGroup {
private List<NumberGroup> groupList;
// initializes grouplist with range objects representing different ranges of numbs
public MultipleGroups() {
groupList = new ArrayList<>();
// add range object represetnign numbs 5-8
groupList.add(new Range(5, 8));
// add range object representing numbs 10-12
groupList.add(new Range(10, 12));
// add range object representing numbs 1-6
groupList.add(new Range(1, 6));
}
public boolean contains(int num) {
for (NumberGroup group : groupList) {
// if contains number return true
if (group.contains(num)) {
return true;
}
}
// grouo has the number it returns false
return false;
}
}
// testing
class MultipleGroupsTest {
public static void main(String[] args) {
MultipleGroups multiple = new MultipleGroups();
System.out.println(multiple.contains(3)); // true
System.out.println(multiple.contains(9)); // false
System.out.println(multiple.contains(5)); // true
}
}
MultipleGroupsTest.main(null);
true
false
true
Notes
- testing interfaces, no longer requirded on the AP test…