Question 4
Type: Classes
Blog This question made me think a lot about how to impliment classes and how the Java’s object-oriented structure could be put to use in different ways to make it a more beneficial programming language. This question explored the implementation of interfaces and the composite pattern through the NumberGroup interface and its implementations, while challenging me to apply these concepts to create a flexible system for checking if numbers belong to any of a collection of ranges, which demonstrates the power of abstraction and composition in object-oriented design.
Part A
Create an Interface - Make an interface : |
public interface NumberGroup
{
public boolean contains(int num);
}
// Testing Stuff
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;
}
}
public class Main {
public static void main(String[] args) {
NumberGroup range = new Range(5, 10);
System.out.println(range.contains(5)); // true
System.out.println(range.contains(10)); // true
System.out.println(range.contains(7)); // true
System.out.println(range.contains(4)); // false
System.out.println(range.contains(11)); // false
}
}
Main.main(null);
true
true
true
false
false
Part B
Implementing Interface - Implement the NumberGroup interface in a Range class to check if a number is within a range.
Whelp, I accedentally did that implimentation without realizing it! Here it is, reimplimented in a different way:
public class Range implements NumberGroup
{
private int[] list;
public Range(int min, int max)
{
list = new int[Math.abs(max-min+1)];
for(int i = 0; i < list.length; i++)
list[i] = min + i;
}
public boolean contains(int num)
{
for(int n: list)
if(num == n)
return true;
return false;
}
}
// Testing Stuff
public class Main {
public static void main(String[] args) {
NumberGroup range = new Range(5, 10);
System.out.println(range.contains(5)); // true
System.out.println(range.contains(10)); // true
System.out.println(range.contains(7)); // true
System.out.println(range.contains(4)); // false
System.out.println(range.contains(11)); // false
}
}
Main.main(null);
true
true
true
false
false
Part C
Composite Group - Create a MultipleGroups class that holds multiple NumberGroup objects and checks if a number is in any of the groups.
public boolean contains(int num)
{
for(NumberGroup n : groupList)
if(n.contains(num))
return true;
return false;
}
// Testing
public class MultipleGroups {
public static void main(String[] args) {
Range range1 = new Range(1, 5);
Range range2 = new Range(10, 15);
Range range3 = new Range(20, 25);
System.out.println(range1.contains(3)); // true (3 is in the range 1-5)
System.out.println(range2.contains(9)); // false (9 is not in the range 10-15)
System.out.println(range3.contains(12)); // false (12 is not in the range 20-25)
System.out.println(range1.contains(20)); // false (20 is not in the range 1-5)
System.out.println(range3.contains(30)); // false (30 is not in the range 20-25)
}
}
MultipleGroups.main(null);
true
false
false
false
false