The question involves the design of an interface, writing a class that implements the interface, and writing a method that uses the interface.
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
public interface NumberGroup
{
boolean contains(int number);
}
Testing this will be shown in part b, implementation of the interface.
A range represents a number group that contains all (and only) the integers between a minimum value and a maximum value, inclusive.
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.
public class Range implements NumberGroup{
private int min;
private int max;
public Range(int min, int max){
this.min = min;
this.max = max;
}
public boolean contains(int number){
if (num >= min && num <= max){
return True;
}
else{
return False;
}
}
}
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.
I slightly did this already in part B on accident but I will redo:
public boolean contains (int number) {
for (NumberGroup group : groupList)
if (group.contains(number)) {
return true;
}
return false;
}
Question: my original method for contains already contains the boolean return (true or false). Do I need the return statements a second time in part C?
Creating the NumberGroup interface was easy enough. Once I wrapped my head around what the questopn was actually asking , designing the contains method afterwards also felt relatively straightforward. Interface is just the groudwork.
Implementing the Range class was a bit more challenging. When ensuring that the range includes all integers within the specified bounds, I got the constructor and instance variables sorted out. Additionally I wrote the contains method here but I do not know if I should have.
Writing the contains method for MultipleGroups was easy as well. This was just the implementation of an enhanced for loop, and the method I wrote previously. Additionally this line is important to understand (within the for ) :
Overall, this FRQ went pretty well.