Full Code

// PART A
public interface NumberGroup{
    boolean contains(int val);
}

// PART B
public class Range implements NumberGroup{
    public int min;
    public int max;

    public Range(int min, int max){
        this.min = min;
        this.max = max;
    }

    @Override 
    public boolean contains(int val){
        if(val <= this.max && val >= this.min){
            return true;
        }
        return false;
    }
}
public class bMain {
    public static void main(String[] args) {
        NumberGroup range = new Range(5, 10);

        System.out.println("\nPart B Testing");
        System.out.println("-----------------");
        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
    }
}
bMain.main(null);


// PART C
public class multipleGroups{
    private List<NumberGroup> groupList;
    public multipleGroups(List<NumberGroup> groupList){
        this.groupList = groupList;
    }
    public boolean contains(int num){
        for(NumberGroup group : groupList){
            if(group.contains(num)){
                return true;
            }
        }
        return false;
    }
    public static void main(String[] args) {
        Range range1 = new Range(1, 5);
        Range range2 = new Range(10, 15);

        List<NumberGroup> groupList = new ArrayList<NumberGroup>();
        groupList.add(range1);
        groupList.add(range2);
        
        System.out.println("Part C Testing");
        System.out.println("-----------------");
        
        multipleGroups multiGroup = new multipleGroups(groupList);
        System.out.println("\nTest 1:");
        System.out.println(multiGroup.contains(5)); // should return true
        
        System.out.println("\nTest 2:");
        System.out.println(multiGroup.contains(7)); // should return false
    }
}

multipleGroups.main(null);
Part B Testing
-----------------
true
true
true
false
false


Part C Testing
-----------------

Test 1
true

Test 2
false

Part A

A number group represents a group of integers defined in some way. It could be empty, or it could contain one or more integers. Write an interface named NumberGroup that represents a group of integers. The interface should have a single contains method that determines if a given integer is in the group. For example, if group1 is of type NumberGroup, and it contains only the two numbers -5 and 3, then group1.contains(-5) would return true, and group1.contains(2) would return false. Write the complete NumberGroup interface. It must have exactly one method.

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

There’s not much to write home about in this part, as it was just defining an interface. I gave the contains() method a boolean output because the question requires the outputs to be either “true” or “false”.

Part B

A range represents a number group that contains all (and only) the integers between a minimum value and a maximum value, inclusive. Write the Range class, which is a NumberGroup. The Range class represents the group of int values that range from a given minimum value up through a given maximum value, inclusive. For example, the declaration NumberGroup range1 = new Range(-3, 2); represents the group of integer values -3, -2, -1, 0, 1, 2. Write the complete Range class. Include all necessary instance variables and methods as well as a constructor that takes two int parameters. The first parameter represents the minimum value, and the second parameter represents the maximum value of the range. You may assume that the minimum is less than or equal to the maximum.

// PART B
public class Range implements NumberGroup{
    public int min;
    public int max;

    public Range(int min, int max){
        this.min = min;
        this.max = max;
    }

    @Override 
    public boolean contains(int val){
        if(val <= this.max && val >= this.min){
            return true;
        }
        return false;
    }
}

This part needed me to add a class which implements the interface I made in part A, while also actually writing the logic for the contains() method. I wrote a constructor for range, which initialized a minumum value and a maximum value. These minimum and maximum values are the bounds for the range of numbers outputed. For example, if the minimum was 5 and the maximum was 10, the range of numbers outputted would be 5, 6, 7, 8, 9, and 10. The contains method just needs to check whether or not the value given via parameter is in between or equal to the minimum and maximum values.

Part C

The MultipleGroups class (not shown) represents a collection of NumberGroup objects and is a NumberGroup. The MultipleGroups class stores the number groups in the instance variable groupList (shown below), which is initialized in the constructor. private List<NumberGroup> groupList; Write the MultipleGroups method contains. The method takes an integer and returns true if and only if the integer is contained in one or more of the number groups in groupList. For example, suppose multiple1 has been declared as an instance of MultipleGroups and consists of the three ranges created by the calls new Range(5, 8), new Range(10, 12), and new Range(1, 6). The following table shows the results of several calls to contains.

public class multipleGroups{
    private List<NumberGroup> groupList;
    public multipleGroups(List<NumberGroup> groupList){
        this.groupList = groupList;
    }
    public boolean contains(int num){
        for(NumberGroup group : groupList){
            if(group.contains(num)){
                return true;
            }
        }
        return false;
    }
}

This part required me to add functionality for multiple groups. This was pretty easy, as I just needed to iterate through every group and check whether or not the groups contain the number provided by the parameter using the .contains() method.