Part A
This is all the question asked for, to create an interface which contains a method that returns true or false on whether a number exists or not.
1
2
3
| public interface NumberGroup {
public boolean contains(int num); // single method
}
|
Part B
Here, I just created a class that checks to see if the numbers I am testing are within the range I defined. I do this by defining a minimum and maximum number within the instance and then I choose to test specific numbers within that range, using a contains method that I overrode in order to not include the previous interface I created. This new one is basically the same one, where it only returns true if the number is within the range (between min and max).
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
| public class Range implements NumberGroup {
private int min;
private int max;
public Range(int min, int max) {
this.min = min;
this.max = max;
}
@Override // overriding the method with a new one
public boolean contains(int num) {
return (num >= min && num <= max); // only returns true if num is greater than min or less than max (within range)
}
public static void main(String[] args) {
Range range = new Range(-3, 2);
System.out.println("Testing 1: " + range.contains(1));
System.out.println("Testing 10: " + range.contains(10));
}
}
Range.main(null);
|
1
2
| Testing 1: true
Testing 10: false
|
Part C
In this case, we use the contains()
method again, however this time we look through multiple group lists. We do this be creating a new contains()
method, which looks through the arraylist that contains all of the ranges we have defined. From there, we check to see if the number we have is within any of those ranges and return a true or false based on whether it is or isn’t.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
| public class MultipleGroups implements NumberGroup {
private List<NumberGroup> groupList;
public MultipleGroups(List<NumberGroup> groupList) {
this.groupList = groupList;
}
@Override
public boolean contains(int num) { // new method to new
for (int i = 0; i < this.groupList.size(); i++) { // looks through each of the group lists
if (this.groupList.get(i).contains(num)) { // if the number happens to be in one of the group lists, true is returned
return true;
}
}
return false;
}
public static void main(String[] args) {
// creating multiple groups
Range r1 = new Range(5, 8);
Range r2 = new Range(10, 12);
Range r3 = new Range(1, 6);
// adding all the groups to a group list
ArrayList<NumberGroup> groupList = new ArrayList<>();
groupList.add(r1);
groupList.add(r2);
groupList.add(r3);
// creating a new instance of the group list
MultipleGroups multiple1 = new MultipleGroups(groupList);
// checking different cases to see if they are within the ranges provided
System.out.println("Testing 2: " + multiple1.contains(2));
System.out.println("Testing 9: " + multiple1.contains(9));
System.out.println("Testing 6: " + multiple1.contains(6));
}
}
MultipleGroups.main(null);
|
1
2
3
| Testing 2: true
Testing 9: false
Testing 6: true
|