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.

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.

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

Question Type:

  • Method and Control Structures

Overall Objectives:

Create a interface that has:

  • A single method interface called NumberGroup
  • A Range class that represents and only contains the range values
  • Create variables and extend the NumberGroup into the Range class
  • A class called MultipleGroups that represents the collection of NumberGroup and stores many of these in the variable groupList
  • Create constructors and extend the NumberGroup into the MultipleGroups
import java.util.List;
import java.util.ArrayList;

public interface NumberGroup { // A (A single method interface called `NumberGroup`)
    boolean contains(int number);
}

public class Range implements NumberGroup { // B (A Range class that represents and only contains the range values)
    private int min;
    private int max;

    public Range(int min, int max) { // constructor class to store the ranges
        this.min = min;
        this.max = max;
    }

    @Override // overriding the inherited values from the NumberGroup and 
    public boolean contains(int number) {
        return number >= min && number <= max; // returns true if the value is in range
    }
}

public class MultipleGroups implements NumberGroup { // C (A class called `MultipleGroups` that represents the collection of `NumberGroup` and stores many of these in the variable `groupList`)
    private List<NumberGroup> groupList;

    public MultipleGroups() {
        this.groupList = new ArrayList<NumberGroup>(); // constructor to store the NumberGroup list made
    }

    public void addGroup(NumberGroup group) {
        groupList.add(group);
    }

    @Override // override the inherited values from number group
    public boolean contains(int number) {
        for (NumberGroup group : groupList) {
            if (group.contains(number)) {
                return true;
            }
        }
        return false;
    }
}

public class MultipleContained { // tester data
    public static void main(String[] args) {
        MultipleGroups multiple1 = new MultipleGroups();
        multiple1.addGroup(new Range(5, 8));
        multiple1.addGroup(new Range(10, 12));
        multiple1.addGroup(new Range(1, 6));

        System.out.println("Contains 2?: " + multiple1.contains(2));
        System.out.println("Contains 6?: " + multiple1.contains(6));
        System.out.println("Contains 9?: " + multiple1.contains(9));
    }
}

MultipleContained.main(null)
Contains 2?: true
Contains 6?: true
Contains 9?: false

Reflection:

  • This frq in particular was pretty easy to do and wasn’t too challenging to complete and honestly I give that all to my CSP experience, the interface uses a lot of inheritance and I still remember the things that I have taught during that student lesson
  • This frq uses a lot of constructors and variables and those are pretty easy to do so this frq wasn’t that bad and was really easy to understand