2015 FRQ 4 (Methods and Control Structures)
2015 FRQ 4 Solution + Reflection
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.
public interface NumberGroup{
boolean contains(int value);
}
Reflection
This was a very simple question as it was only testing if we were able to declare an interface which is a very basic understanding of syntax in Java. Which we are relatively familiar with.
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 Range implements NumberGroup{
private int minVal;
private int maxVal;
public Range(int minVal, int maxVal){
this.minVal = minVal;
this.maxVal = maxVal;
}
@Override
public boolean contains(int value){
if (value >= this.minVal && value <= this.maxVal){
return true;
} else{
return false;
}
}
public static void main(String[] args){
Range range1 = new Range(-3,2);
System.out.println("Test 1");
System.out.println(range1.contains(-1));
System.out.println("\nTest 2");
System.out.println(range1.contains(3));
}
}
Range.main(null);
Test 1
true
Test 2
false
Reflection
This question was more so just our ability to implement the interface that we created in part A this is very similar to what we have had prior experience with such implementing the collection interface for our prior projects. So this portion was relatively familiar to what we had seen before and the second portion of the problem which was the implementing the contains method for the Range class was also simple with us just needing to check if the values were we greater than the smallest value in the range and less than the largest than returning true if it was than false otherwise.
Part 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{
private List<NumberGroup> groupList;
private MultipleGroups(List<NumberGroup> groupList){
this.groupList = groupList;
}
public boolean contains(int value){
for(NumberGroup group : groupList){
if(group.contains(value)){
return true;
}
}
return false;
}
public static void main(String[] args){
Range range1 = new Range(-3, 2);
Range range2 = new Range(4, 9);
List<NumberGroup> groupList = new ArrayList<NumberGroup>();
groupList.add(range1);
groupList.add(range2);
MultipleGroups multiGroup = new MultipleGroups(groupList);
System.out.println("Test 1");
System.out.println(multiGroup.contains(3));
System.out.println("\nTest 2");
System.out.println(multiGroup.contains(10));
}
}
MultipleGroups.main(null);
Test 1
false
Test 2
false
Reflection
This was again a relatively simple problem with now there being an arrayList of all the number group objects with needing to write a contains method that would be able to determine whether or not an integer value was a part of a number group in the list. Which this problem was simply solved with a for-each loop that was used to iterate over each object in the arrayList
and then running its contains()
method with value passed into the contains()
method for the MultipleGroups
class then if any one of them returned true then the method would return true otherwise it would return false.