Question 4

Type: Classes

Blog My thoughts on how to implement classes and how to take advantage of Java’s object-oriented structure to make it a more useful programming language were greatly influenced by this question. This question challenged me to use these ideas to develop a versatile system for determining whether numbers belong to any of a set of ranges, highlighting the power of abstraction and composition in object-oriented design. It also examined the implementation of interfaces and the composite pattern through the NumberGroup interface and its implementations.

Part A

Create an Interface

Question 4A

public interface NumberGroup
{
     public boolean contains(int num);
}
public interface NumberGroup {
    boolean contains(int num);
}

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 MainTesting {
    public static void main(String[] args) {
        NumberGroup range = new Range(5, 10);

        testContains(range, 5);  // true
        testContains(range, 10); // true
        testContains(range, 7);  // true
        testContains(range, 4);  // false
        testContains(range, 11); // false
    }

    private static void testContains(NumberGroup range, int num) {
        System.out.println("Number " + num + " is in range: " + range.contains(num));
    }
}

MainTesting.main(null)
Number 5 is in range: true
Number 10 is in range: true
Number 7 is in range: true
Number 4 is in range: false
Number 11 is in range: false

Part B

Implementing Interface - Implement the NumberGroup interface in a Range class to check if a number is within a range.

Question 4B

public class Range implements NumberGroup {
   private int[] numbers;

   // Constructor to initialize the Range with minimum and maximum values
   public Range(int min, int max) {
       // Calculate the size of the array based on the range
       int size = Math.abs(max - min) + 1;
       numbers = new int[size];

       // Populate the array with numbers within the specified range
       for (int i = 0; i < size; i++) {
           numbers[i] = min + i;
       }
   }

   // Method to check if a number is within the range
   @Override
   public boolean contains(int num) {
       // Iterate through the array to find if the number exists
       for (int n : numbers) {
           if (num == n) {
               return true; // Number found in the range
           }
       }
       return false; // Number not found in the range
   }
}

// Testing the Range class
public class MainTesting {
   public static void main(String[] args) {
       // Create a Range object with range 5 to 10
       NumberGroup range = new Range(5, 10);

       // Test cases to check if numbers are within the range
       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
   }
}

MainTesting.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.

Question 4C

public boolean contains(int num)
{
   for(NumberGroup n : groupList)
      if(n.contains(num))
         return true;
   return false;
}
public class MultipleGroups {
    public static void main(String[] args) {
        // Creating ranges
        Range range1 = new Range(1, 5);
        Range range2 = new Range(10, 15);
        Range range3 = new Range(20, 25);

        // Testing containment
        testContainment(range1, 3);
        testContainment(range2, 9);
        testContainment(range3, 12);
        testContainment(range1, 20);
        testContainment(range3, 30);
    }

    // Method to test containment and print result
    private static void testContainment(Range range, int value) {
        System.out.println("Range " + range + " contains " + value + ": " + range.contains(value));
    }
}

MultipleGroups.main(null)
Range REPL.$JShell$13C$Range@62a0b8a3 contains 3: true
Range REPL.$JShell$13C$Range@643ae4f contains 9: false
Range REPL.$JShell$13C$Range@46362ef2 contains 12: false
Range REPL.$JShell$13C$Range@62a0b8a3 contains 20: false
Range REPL.$JShell$13C$Range@46362ef2 contains 30: false