Question 4

This question involves the design of an interface, writing a class that implements the interface, and writing a method that uses the interface.

(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 { //The number Group interface
    boolean contains(int checkNum); //contains method which will be implemented by following classes
}

(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 interface NumberGroup { 
    boolean contains(int checkNum); 
}

public class Range implements NumberGroup {
    private List<Integer> numGroup = new ArrayList<Integer>();
    public Range(int start, int end) {
        for (int i=start; i<end+1; i++) {//adds all integers in the range to the array list
            numGroup.add(i);
        }
    }
    public boolean contains(int checkNum) {//checks the lst to see if the number inputted by the user is present
        for (int num : numGroup) {
            if (checkNum==num) {
                return true;
            }
        }
        return false;
    }
}

Range range1 = new Range(-3, 2);
System.out.println("Does this range contain 3? " + range1.contains(3));
System.out.println("Does this range contain 0? " + range1.contains(0));
Does this range contain 3? false
Does this range contain 0? true

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


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

    public class Range implements NumberGroup {
        private List<Integer> numGroup = new ArrayList<Integer>();
        public Range(int start, int end) {
            for (int i=start; i<end+1; i++) {
                numGroup.add(i);
            }
        }
        public boolean contains(int checkNum) {
            for (int num : numGroup) {
                if (checkNum==num) {
                    return true;
                }
            }
            return false;
        }
    }

    public class MultipleGroups implements NumberGroup {
        private List<NumberGroup> groupList = new ArrayList<>();
        public MultipleGroups(List<NumberGroup> groupList) {//adds the list of NumberGroups inputted by user to the array list
            this.groupList = groupList;
        }
        public boolean contains(int checkNum) {//checks if any of the NumberGroups contain the number inputted by user
            for(NumberGroup group : groupList) {
                boolean doesContain = group.contains(checkNum);
                if (doesContain==true) {
                    return true;
                }
            }
            return false;
        }
    }

List<NumberGroup> newList = new ArrayList<>();
newList.add(new Range(5,8));
newList.add(new Range(10,12));
newList.add(new Range(1,6));
MultipleGroups mult1 = new MultipleGroups(newList);
System.out.println("Do the multiple groups contain 2? " + mult1.contains(2));
System.out.println("Do the multiple groups contain 20? " + mult1.contains(20));
Do the multiple groups contain 2? true
Do the multiple groups contain 20? false
  • FRQ Type: Interfaces (but also Methods and Control Structures)
  • How main algorithm shows FRQ type:

This FRQ primarily involves interfaces, but it also involves methods and control structures. The NumberGroup class has a contains method, which is extended to the range and the multiple groups classes. In terms of control structures, it also uses for and if else loops. These are used to check whether the integers are in the number groups, through using for loops to iterate through the NumberGroups. In the MultipleGroups class, the contains method is overwritten so that it has a different implementation than the contains method in the NumberGroups class. The MultipleGroups class also uses for loops to check whether the integers are in any of the NumberGroups.