Question 4

// Define the NumberGroup interface with one method
interface NumberGroup {
    boolean contains(int number);
}

// Implement the interface with a simple class
class Range implements NumberGroup {
    private int min;
    private int max;

    public Range(int min, int max) {
        this.min = min;
        this.max = max;
    }

    // Implement the contains method to check if a number is in the range
    public boolean contains(int number) {
        return number >= min && number <= max;
    }
}

// Main class to run the code
public class NumberGroupDemo {
    public static void main(String[] args) {
        NumberGroup range = new Range(-5, 3);

        // Test the contains method
        System.out.println("Does the range contain -5? " + range.contains(-5)); // Expected: true
        System.out.println("Does the range contain 3? " + range.contains(3));   // Expected: true
        System.out.println("Does the range contain 2? " + range.contains(2));   // Expected: true
        System.out.println("Does the range contain 4? " + range.contains(4));   // Expected: false
    }
}

NumberGroupDemo.main(null);

Does the range contain -5? true
Does the range contain 3? true
Does the range contain 2? true
Does the range contain 4? false

4b

// NumberGroup interface as defined in part (a)
interface NumberGroup {
    boolean contains(int number);
}

// Complete Range class
class Range implements NumberGroup {
    private int min;
    private int max;

    public Range(int min, int max) {
        this.min = min;
        this.max = max;
    }

    // Implementation of the contains method from the NumberGroup interface
    public boolean contains(int number) {
        return number >= min && number <= max;
    }
}

// Main class to run the code
public class Main {
    public static void main(String[] args) {
        NumberGroup range1 = new Range(-3, 2);

        System.out.println("Does the range include -3? " + range1.contains(-3)); // Expected: true
        System.out.println("Does the range include 2? " + range1.contains(2));   // Expected: true
        System.out.println("Does the range include 3? " + range1.contains(3));   // Expected: false
    }
}

Main.main(null);

Does the range include -3? true
Does the range include 2? true
Does the range include 3? false

4c

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

// Assuming NumberGroup interface already defined
interface NumberGroup {
    boolean contains(int number);
}

class Range implements NumberGroup {
    private int min;
    private int max;

    public Range(int min, int max) {
        this.min = min;
        this.max = max;
    }

    public boolean contains(int number) {
        return number >= min && number <= max;
    }
}

class MultipleGroups {
    private List<NumberGroup> groupList;

    public MultipleGroups() {
        groupList = new ArrayList<>();
    }

    public void addNumberGroup(NumberGroup numberGroup) {
        groupList.add(numberGroup);
    }

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

public class NumberGroupDemo {
    public static void main(String[] args) {
        MultipleGroups multiple1 = new MultipleGroups();
        multiple1.addNumberGroup(new Range(5, 8));
        multiple1.addNumberGroup(new Range(10, 12));
        multiple1.addNumberGroup(new Range(1, 6));

        System.out.println("Multiple1 contains 2: " + multiple1.contains(2)); // Expected: true
        System.out.println("Multiple1 contains 9: " + multiple1.contains(9)); // Expected: false
        System.out.println("Multiple1 contains 6: " + multiple1.contains(6)); // Expected: true
    }
}

NumberGroupDemo.main(null);

Multiple1 contains 2: true
Multiple1 contains 9: false
Multiple1 contains 6: true