(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 num);
}

(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 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;
    }
}

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

public class MultipleGroups implements NumberGroup {
    private List<NumberGroup> groupList;

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

    @Override
    public boolean contains(int num) {
        for (NumberGroup group : groupList) {
            if (group.contains(num)) {
                return true;
            }
        }
        return false;
    }
}

Final Integration with User Input

import java.util.*;

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

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 num) {
        return num >= min && num <= max;
    }
}

public class MultipleGroups implements NumberGroup {
    private List<NumberGroup> groupList;

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

    @Override
    public boolean contains(int num) {
        for (NumberGroup group : groupList) {
            if (group.contains(num)) {
                return true;
            }
        }
        return false;
    }
}

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        // Create a list of NumberGroups (Ranges)
        List<NumberGroup> groups = new ArrayList<>();
        groups.add(new Range(5, 8));
        groups.add(new Range(10, 12));
        groups.add(new Range(1, 6));

        // Create a MultipleGroups object with the list of ranges
        MultipleGroups multiple1 = new MultipleGroups(groups);

        // Test the contains method
        System.out.println("Testing MultipleGroups:");
        System.out.print("Enter a number to check if it's contained in the groups: ");
        int num = scanner.nextInt();

        if (multiple1.contains(num)) {
            System.out.println(num + " is contained in the groups.");
        } else {
            System.out.println(num + " is not contained in the groups.");
        }

        scanner.close();
    }
}
Main.main(null)

Explanation

NumberGroup Interface:

  • Represents a group of integers with a single contains method.

Range Class:

  • Implements the NumberGroup interface.
  • Represents a range of integers from min to max, inclusive.
  • The contains method checks if a given number is within this range.

MultipleGroups Class:

  • Also implements the NumberGroup interface.
  • Represents a collection of NumberGroup objects.
  • The contains method checks if the input number is contained in any of the NumberGroup objects stored in groupList.

Main Class:

  • Creates a list of NumberGroup objects (Ranges).
  • Creates a MultipleGroups object with the list of ranges.
  • Uses a Scanner to get user input and tests the contains method of MultipleGroups.

Explanation of FRQ Type

Classes:

The program defines three classes:

  • NumberGroup (interface)
  • Range (implements NumberGroup)
  • MultipleGroups (also implements NumberGroup)

Each class has its own set of attributes and behaviors, adhering to the principles of object-oriented programming.