public interface NumberGroup {
boolean contains(int value);
}
(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 number) {
if (number >= min && number <= max) {
return true;
}
else {
return false;
}
}
public static void main(String args[]) {
Range range1 = new Range(-3, 2);
System.out.println("Does range1 contain -4: " + range1.contains(-4));
System.out.println("Does range1 contain 0: " + range1.contains(0));
}
}
Range.main(null);
Does range1 contain -4: false
Does range1 contain 0: true
(c) The MultipleGroups class (not shown) represents a collection of NumberGroup objects and is a NumberGroup. The MultipleGroups class stores the number groups in the instance variable groupList (shown below), which is initialized in the constructor.
private List<NumberGroup> 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.
public class MultipleGroups implements NumberGroup {
private List<NumberGroup> groupList;
public MultipleGroups () {
groupList = new ArrayList<NumberGroup>();
}
public void addGroup (NumberGroup group) {
groupList.add(group);
}
@Override
public boolean contains (int number) {
for (NumberGroup group : groupList) {
if (group.contains(number)) {
return true;
}
}
return false;
}
public static void main (String args[]) {
MultipleGroups multiple1 = new MultipleGroups();
multiple1.addGroup(new Range(5, 8));
multiple1.addGroup(new Range(10, 12));
multiple1.addGroup(new Range(1, 6));
System.out.println("Do any of the ranges in multiple1 contain 7: " + multiple1.contains(7));
System.out.println("Do any of the ranges in multiple1 contain 9: " + multiple1.contains(9));
}
}
MultipleGroups.main(null);
Do any of the ranges in multiple1 contain 7: true
Do any of the ranges in multiple1 contain 9: false
In this problem my task was to implement a system that can represent and manipulate groups of numbers. This involved creating a flexible structure that could handle various definitions of what constitutes a group of numbers.
In Part A, I started by defining the NumberGroup
interface with a single method contains
, which checks if a given integer is in the group. It set the groundwork for how groups of numbers would be interpreted and interacted with in the rest of the solution.
Moving to Part B, I implemented the Range class. The Range class defines a group of numbers as all integers within a specified range. In the contains
method of Range, I used basic control stuctures, primarily simple conditional statements, to determine if a number falls within the range. This part of the problem allowed me to apply basic control structures and demonstrate how they can be used to define specific behaviors in a class.
Part C introduced the MultipleGroups class, representing a collection of NumberGroup
objects. The challenging part was with the contains
method, which required iterating over a list of NumberGroup
objects. For each group in the list, I had to check if the given number was contained within it. It underscored the importance of understanding how to traverse collections and how control structures can be used to aggregate and evaluate data across multiple objects.
double sum = 0;
int totalPlayers = 0;
List<Player> allPlayers = new ArrayList<>(teamsData.getTeamA());
allPlayers.addAll(teamsData.getTeamB());
for (Player player : allPlayers) {
sum += calculateFantasyPoints(player);
totalPlayers++;
}
double mean = sum / totalPlayers;
double squaredDifferenceSum = 0;
for (Player player : allPlayers) {
double difference = calculateFantasyPoints(player) - mean;
squaredDifferenceSum += difference * difference;
}
double variance = squaredDifferenceSum / totalPlayers;
return Math.sqrt(variance);
The above excerpt is from the MonteCarloSimulator
class in our NBA Predictor project, which is designed for simulating and predicting fantasy points in NBA games. The calculateStandardDeviation
is crucial for accurately capturing the variability in player performances, thereby enabling us to generate more realistic and reliable fantasy point projections.
In developing this method, we employed enhanced for loops to iterate through lists of players, aggregating their fantasy points. This step was essential to calculate the mean fantasy points, laying the groundwork for further calculations. We then used another loop to compute the sum of squared differences from the mean, a critical step in determining the variance of the players’ performances. Finally, the standard deviation was derived by taking the square root of this variance.