Skip to main content Link Menu Expand (external link) Document Search Copy Copied

FRQ 4 • 3 min read

Description

Final FRQ 4


4

a.

// This is the NumberGroup interface declaration.
public interface NumberGroup {

    // The contains method takes an integer as input and returns 
    // a boolean indicating whether or not the given integer is 
    // contained within the number group.
    boolean contains(int number);
}

b.

// The Range class implements the NumberGroup interface.
public class Range implements NumberGroup {
    // Instance variables to store the minimum and maximum values of the range.
    private int min;
    private int max;

    // Constructor that takes the minimum and maximum values of the range.
    public Range(int min, int max) {
        this.min = min;
        this.max = max;
    }

    // The contains method checks if a given integer is within the range.
    public boolean contains(int number) {
        return number >= min && number <= max;
    }
}

c.

public boolean contains(int num) {
    // Iterate over each NumberGroup in groupList
    for (NumberGroup group : groupList) {
        // Check if the number is contained within the current group
        if (group.contains(num)) {
            // If the number is found, return true immediately
            return true;
        }
    }
    // If the number is not found in any group, return false
    return false;
}