(a) NumberGroup Interface:
public interface NumberGroup {
boolean contains(int num);
}
- I have created an interface named NumberGroup.
- The interface has a single method called contains, which takes an integer as a parameter and returns a boolean indicating whether the integer is in the group or not.
(b) Range Class:
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 num) {
return num >= min && num <= max;
}
}
- I've created a class named Range that implements the NumberGroup interface.
- The class has two private instance variables min and max representing the range.
- The constructor initializes these variables with the provided minimum and maximum values.
- The contains method is implemented to check if the given number is within the specified range.
(c) MultipleGroups Class contains Method:
import java.util.List;
public class MultipleGroups implements NumberGroup {
private List<NumberGroup> groupList;
public MultipleGroups(List<NumberGroup> groupList) {
this.groupList = groupList;
}
@Override
public boolean contains(int num) {
for (NumberGroup group : groupList) {
if (group.contains(num)) {
return true;
}
}
return false;
}
}
- I've created a class named MultipleGroups that implements the NumberGroup interface.
- The class has a constructor that takes a list of NumberGroup objects and initializes the groupList instance variable.
- The contains method checks if the given number is contained in at least one of the number groups in groupList by iterating through them and using their contains method.
(a) NumberGroup Interface:
(b) Range Class:
(c) MultipleGroups Class contains Method: