2015 FRQ Question 4
None
Question 4: Interfaces
Part A: 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(100, 250));
System.out.println(group1.contains(100)); // true
System.out.println(group1.contains(300)); // false
}
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: 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 interface NumberGroup {
boolean contains(int num);
}
public 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 value) {
return value >= min && value <= max;
}
// returns a string representation of all values in the range
@Override
public String toString() {
StringBuilder builder = new StringBuilder();
builder.append("Range: ");
for (int i = min; i <= max; i++) {
builder.append(i).append(" ");
}
return builder.toString().trim();
}
}
// Test cases
class RangeTest {
public static void main(String[] args) {
Range range = new Range(-3, 2);
// Printing all values in the range
System.out.println(range);
// Test cases
System.out.println("Range contains -3: " + range.contains(-3)); // should return true
System.out.println("Range contains 0: " + range.contains(0)); // should return true
System.out.println("Range contains 3: " + range.contains(3)); // should return false
}
}
RangeTest.main(null);
Range: -3 -2 -1 0 1 2
Range contains -3: true
Range contains 0: true
Range contains 3: false
Part C: 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.
import java.util.List;
import java.util.ArrayList;
public class MultipleGroups implements NumberGroup {
private List<NumberGroup> groupList;
public MultipleGroups() {
groupList = new ArrayList<>();
groupList.add(new Range(5, 8));
groupList.add(new Range(10, 12));
groupList.add(new Range(1, 6));
}
public boolean contains(int num) {
for (NumberGroup group : groupList) {
if (group.contains(num)) {
return true;
}
}
return false;
}
}
// Test cases
class MultipleGroupsTest {
public static void main(String[] args) {
MultipleGroups multiple = new MultipleGroups();
// Test cases
System.out.println("Multiple contains 6: " + multiple.contains(6)); // should return true
System.out.println("Multiple contains 9: " + multiple.contains(9)); // should return false
System.out.println("Multiple contains 11: " + multiple.contains(11)); // should return true
System.out.println("Multiple contains 11: " + multiple.contains(13)); // should return false
}
}
MultipleGroupsTest.main(null);
Multiple contains 6: true
Multiple contains 9: false
Multiple contains 11: true
Multiple contains 11: false
FRQ 4 Reflection
This FRQ was the most challenging for me out of all of them. I am unfamiliar with interfaces and it was super cool to learn a ton about them from this FRQ. I had to do some research, but my understanding is it’s similar to a class, but it can only have certain things within it such as constants, methid sugbatyresm default methodsm statuc methods, and nested types. We don’t have any interfaces within our AI code, so it would be really interesting to try and implement it into our code in a useful way. One idea would be to have an interface for predictionlogic and then extend it in predictionlogic.