QUESTION 4: METHODS AND CONTROL STRUCTURES:

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.

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

public class Main {
    public static void main(String[] args) {
        NumberGroup group1 = new IntegerList(Arrays.asList(66, 63));
        System.out.println(group1.contains(66)); // true
        System.out.println(group1.contains(2));  // 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);

true
false

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.

public class Main {
    public static void main(String[] args) {
        NumberGroup range1 = new Range(-6, 5);
        System.out.println(range1.contains(-3)); // true
        System.out.println(range1.contains(0));  // true
        System.out.println(range1.contains(7));  // false
    }

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

    public static class Range implements NumberGroup {
        // two int parameters min and max as requested in frq part b
        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;
        }
    }
}

Main.main(null);

true
true
false

PART 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.ArrayList;
import java.util.List;

public class Main {
    public static void main(String[] args) {
        List<NumberGroup> groups = new ArrayList<>();
        groups.add(new Range(5, 8));
        groups.add(new Range(10, 12));
        groups.add(new Range(1, 6));

        MultipleGroups multiple1 = new MultipleGroups(groups);
        
        System.out.println(multiple1.contains(2)); // true
        System.out.println(multiple1.contains(9)); // false
        System.out.println(multiple1.contains(6)); // 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);
true
false
true

Throughout this FRQ, knowledge of methods and control structures were necessary. First of all, through creating the NumberGroup interface, we had to create an interface which represented a group of integers and had a method to check if an integer was in that group. We had to write a “contains” method. In the Range class in part b, we had to implement the contains method, and had to use control structures through conditional statements to check whether or not an integer was within the range. Finally, with the MultipleGroups class, which implemented the NumberGroup interface, control structures were used to iterate over each NumberGroup and see if an integer was in any of them. The entire FRQ had a lot to do with implementing interfaces, classes, methods, and control structures in Java.

PROJECT IMPLEMENTATION OF METHODS AND CONTROL STRUCTURES:

for (int i = 0; i < numberOfSimulations; i++) {
    double[] fantasyPoints = simulateFantasyPoints(meanFantasyPoints, standardDeviation);
    projectedFantasyPointsA.add(fantasyPoints[0]);
    projectedFantasyPointsB.add(fantasyPoints[1]);
}

The above is just one example of implementation of methods and control structures. It is a control structure for iteration. Furthermore, each iteration calls the method (simulateFantasyPoints) in order to create the simulation and add the projections to the fantasyPoints list.