Question 4

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

Reflection

I felt like this was a pretty simple FRQ question as it was very easy to build off from part A and once part A was figured out, part B and C became very easy. I struggled the most with part A because implementing the number group was confusing to me.

import java.util.Arrays;
import java.util.List;

public class Main {
    public static void main(String[] args) {
        NumberGroup group1 = new IntegerList(Arrays.asList(23, 21));
        System.out.println(group1.contains(23));
        System.out.println(group1.contains(4));  
    }

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

    public static class IntegerList implements NumberGroup {
        private List<Integer> numbers;

        public IntegerList(List<Integer> numbers) {
            this.numbers = numbers;
        }

        @Override
        public boolean contains(int num) {
            return numbers.contains(num);
        }
    }
}

Main.main(null);
true
false

Explanation:

This Java code defines a NumberGroup interface with a method contains that checks if a given number is contained within the group. Additionally, it implements a concrete class IntegerList that represents a group of integers stored in a list and provides an implementation for the contains method by delegating to the contains method of the underlying list. In the main method, an instance of IntegerList is created with a list of integers, and then the contains method is invoked to check if specific numbers are contained within the group.

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

public class Main {
    public static void main(String[] args) {
        NumberGroup range1 = new Range(-13, 11);
        System.out.println(range1.contains(-23));
        System.out.println(range1.contains(2)); 
        System.out.println(range1.contains(10)); 
    }

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

    public static 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;
        }
    }
}

Main.main(null);
false
true
true

Explanation:

This Java code defines a NumberGroup interface with a method contains that checks if a given number falls within a specified range. It implements a concrete class Range representing a numerical range with minimum and maximum values, providing an implementation for the contains method to check if a number is within the range.

(c) The MultipleGroups class (not shown) represents a collection of NumberGroup objects and isa NumberGroup. The MultipleGroups class stores the number groups in the instance variable groupList (shown below), which is initialized in the constructor.

private List 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.

import java.util.ArrayList;
import java.util.List;

public class Main {
    public static void main(String[] args) {
        List<NumberGroup> groups = new ArrayList<>();
        groups.add(new Range(2, 8));
        groups.add(new Range(11, 14));
        groups.add(new Range(4, 9));

        MultipleGroups multiple1 = new MultipleGroups(groups);
        
        System.out.println(multiple1.contains(6)); 
        System.out.println(multiple1.contains(1));
        System.out.println(multiple1.contains(9));
    }

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

    public static 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 static class MultipleGroups implements NumberGroup {
        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;
        }
    }
}

Main.main(null);
true
false
true

Explanation:

This Java code defines a NumberGroup interface with a method contains that checks if a given number is contained within a group. It implements a concrete class MultipleGroups representing a collection of NumberGroup instances and provides an implementation for the contains method to check if the number is contained within any of the groups in the collection by iterating over them. In the main method, a list of NumberGroup instances, each representing a range of numbers, is created and then used to initialize an instance of MultipleGroups, which is then used to check for containment of specific numbers.