The Problem & First Impressions

  • An interface named NumberGroup is designed to represent a group of integers, encapsulating a single method, contains, which determines if a given integer is part of the group. The Range class is then implemented as a NumberGroup, defining a range of integer values between a specified minimum and maximum, inclusively. The class incorporates instance variables and methods, including a constructor accepting two int parameters representing the range boundaries. Additionally, a MultipleGroups class, acting as both a NumberGroup and a container for a collection of NumberGroup objects, contains a method named contains that evaluates whether an integer is present in any of the encapsulated number groups. This solution showcases key Java concepts such as interfaces, class implementation, encapsulation, and the use of control structures for managing collections of sparse arrays efficiently.

Part A

  • Task: Create an interface named NumberGroup representing a group of integers.
  • Purpose: Define a single method, contains, within the interface to determine if a given integer is part of the group.
  • Example: If group1 is of type NumberGroup and contains the numbers -5 and 3, then group1.contains(-5) should return true, and group1.contains(2) should return false.
  • Outcome: The NumberGroup interface provides a blueprint for classes that represent different ways of defining and checking integer groups. The contains method is essential for verifying the inclusion of a specified integer in the group.

My Solution:

  • Use a simple interface with no method implementations.
public interface NumberGroup {
  public boolean contains(int number);
}

Part B

  • Task: Implement the Range class as a NumberGroup, defining a number group containing all integers within a specified inclusive range.
  • Purpose: The Range class encapsulates a group of int values within the defined range, adhering to the NumberGroup interface.
  • Example: Upon declaring NumberGroup range1 = new Range(-3, 2), the represented group includes integer values -3, -2, -1, 0, 1, 2.
  • Outcome: The complete Range class should incorporate necessary instance variables, methods, and a constructor with two int parameters representing the minimum and maximum values. This design ensures proper validation, with the minimum being less than or equal to the maximum, facilitating the creation and validation of specific integer groups within a defined range.

My Solution:

  • Validates if the input integer (number) is within the inclusive range defined by the instance variables min and max, returning true if it is, and false otherwise. Uses a ternary operator to make the code more concise and readable
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 number) {
        return (number >= min && number <= max) ? true : false;
    }
}

Range test = new Range(-3,5);
System.out.println("test.contains(1): " + test.contains(1));
System.out.println("test.contains(10): " + test.contains(10));

Part C

  • Task: Implement the MultipleGroups class, representing a collection of NumberGroup objects and itself being a NumberGroup.
  • Purpose: The MultipleGroups class manages a list of number groups stored in the instance variable groupList.
  • Example: If an instance multiple1 of MultipleGroups contains three ranges (new Range(5, 8), new Range(10, 12), and new Range(1, 6)), calling multiple1.contains(2) should return true, multiple1.contains(9) should return false, and multiple1.contains(6) should return true.
  • Outcome: The contains method in MultipleGroups takes an integer as input and returns true only if the integer is present in one or more of the number groups in groupList. This design allows effective checking for the inclusion of a specified integer across various number groups.

My Solution:

  • Utilizes a for loop to iterate through the elements of groupList.
  • Inside the loop, employs this.groupList.get(i).contains(number) to check if the current NumberGroup in the iteration contains the specified integer.
  • Returns true if any NumberGroup contains the integer; otherwise, returns false.
public class MultipleGroups implements NumberGroup {
  private List<NumberGroup> groupList;

  public MultipleGroups(List<NumberGroup> groupList) {
      this.groupList = groupList;
  }

  @Override
  public boolean contains(int number) {
      for (int i = 0; i < this.groupList.size(); i++) {
          if (this.groupList.get(i).contains(number)) {
              return true;
          }
      }
      return false;
  }
}

ArrayList<NumberGroup> groupsList = new ArrayList<NumberGroup>();
Range range1 = new Range(5,8);
Range range2 = new Range(10,12);
Range range3 = new Range(1,6);
groupsList.add(range1); groupsList.add(range2); groupsList.add(range3);
MultipleGroups multiple1 = new MultipleGroups(groupsList);
System.out.println("multiple1.contains(2): " + multiple1.contains(2));
System.out.println("multiple1.contains(9): " + multiple1.contains(9));
System.out.println("multiple1.contains(6): " + multiple1.contains(6));