FRQ QUESTION 4

import java.util.Arrays;
import java.util.List;

public class Main {
    public static void main(String[] args) {
        NumberGroup group1 = new IntegerList(Arrays.asList(100, 200));
        System.out.println(group1.contains(100)); // true
        System.out.println(group1.contains(300));  // false
    }

    // specific interface it is asking for part a
    public interface NumberGroup {
        boolean contains(int num);
    }

    public static class IntegerList implements NumberGroup {
        private List<Integer> numbers;

        public IntegerList(List<Integer> numbers) {
            this.numbers = numbers;
        }

        @Override
        public boolean contains(int num) {
            return numbers.contains(num);
        }
    }
}
Main.main(null)

image

  • The provided code demonstrates the use of interfaces and classes to create a NumberGroup interface and an implementing class IntegerList. The NumberGroup interface has a method contains that checks if a given number is present in the group. The IntegerList class implements this interface for a group of integers stored in a List.
public class Main {    
    public static void main(String[] args) {
        NumberGroup range1 = new Range(100, 200);
        System.out.println(range1.contains(150));
        System.out.println(range1.contains(175));
        System.out.println(range1.contains(300));
    }

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

    public static 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 number) {
            return number >= min && number <= max;
        }
    }
}
Main.main(null)

image

  • The provided Java code defines a program with a NumberGroup interface and a Range class that implements this interface. The Range class represents a range of integers and checks if a given number is within that range. In the main method, an instance of Range is created with a minimum value of 100 and a maximum value of 200 (range1). The program then checks if certain numbers (150, 175, and 300) are contained within this range and prints the results.
import java.util.ArrayList;
import java.util.List;

public class Main {
    public static void main(String[] args) {
        List<NumberGroup> groups = new ArrayList<>();
        groups.add(new Range(100, 200));
        groups.add(new Range(300, 400));
        groups.add(new Range(500, 600));

        MultipleGroups multiple1 = new MultipleGroups(groups);

        System.out.println(multiple1.contains(150)); // true
        System.out.println(multiple1.contains(250)); // false
        System.out.println(multiple1.contains(550)); // true
    }

    // from part A
    public interface NumberGroup {
        boolean contains(int num);
    }

    // from part B
    public static 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 static class MultipleGroups implements NumberGroup {
        private List<NumberGroup> groupList;

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

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

Main.main(null);

image

  • The Java code extends the previous example by introducing a new class called MultipleGroups. This class implements the NumberGroup interface and represents a collection of NumberGroup objects. The contains method of MultipleGroups checks if the given number is contained in any of the groups within the collection. In the main method, an ArrayList of NumberGroup objects (instances of the Range class) is created. These ranges are [100, 200], [300, 400], and [500, 600]. Then, an instance of MultipleGroups (multiple1) is created with these groups.