(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.
### Solution
The NumberGroup interface sets the rules for groups of numbers, the Test class runs tests on these groups, and the NormalNumberGroup class is one type of group that follows the rules and can check if certain numbers are in its list.
public interface NumberGroup {
boolean contains(int number);
}
class Object {
public static void main(String[] args) {
testNumberGroup();
}
// tests the contains method of the NumberGroup implementation
// for various numbers and prints the results.
public static void testNumberGroup() {
NumberGroup group = createNumberGroup();
System.out.println("Testing number group:");
System.out.println("Group contains 5: " + group.contains(5)); // contains 5, true
System.out.println("Group contains -3: " + group.contains(-3)); // contains -3, true
System.out.println("Group contains 100: " + group.contains(100)); // does not contain 100, false
System.out.println("Group contains 23: " + group.contains(23)); // does not contain 23, false
}
// method is responsible for creating an instance
// of a class that implements the NumberGroup interface.
public static NumberGroup createNumberGroup() {
return new NormalNumberGroup();
// returns an instance of the NormalNumberGroup class.
}
}
// implements the NumberGroup interface, which means
// it must provide an implementation for the contains method.
class NormalNumberGroup implements NumberGroup {
private int[] numbers = {5, -3}; // array to hold the numbers in the group
@Override
public boolean contains(int number) {
for (int n : numbers) { // iterate through the numbers in the group
if (n == number) { // check if the current number matches the given number
return true; // return true if the number is found in the group
}
}
return false; // return false if the number is not found in the group
}
}
Object.main(null);
Testing number group:
Group contains 5: true
Group contains -3: true
Group contains 100: false
Group contains 23: false
(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.
Solution
the code creates a class to represent a range of numbers and checks if certain numbers are within that range. It then tests this functionality using the main method and prints the results. The NumberGroup interface sets rules for classes that represent groups of numbers.
public class Range implements NumberGroup {
private int min; // minimum value of the range
private int max; // maximum value of the range
public Range(int min, int max) {
this.min = min;
this.max = max;
}
@Override
public boolean contains(int number) {
return number >= min && number <= max; // check if the number is within the range
}
public static void main(String[] args) {
Range range = new Range(10, 30); // create a range from 10 to 30
testNumberGroup(range); // call the test method with the created range
}
public static void testNumberGroup(NumberGroup range) {
System.out.println("Range contains 15: " + range.contains(15)); // true
System.out.println("Range contains 25: " + range.contains(25)); // true
System.out.println("Range contains 5: " + range.contains(5)); // false
}
}
interface NumberGroup {
boolean contains(int number);
}
| public class Range implements NumberGroup {
cannot find symbol
symbol: class NumberGroup
| public static void testNumberGroup(NumberGroup range) {
cannot find symbol
symbol: class NumberGroup
| @Override
method does not override or implement a method from a supertype
(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.
Solution
the code creates a class to represent a range of numbers and checks if certain numbers are within that range. It then tests this functionality using the main method and prints the results. The NumberGroup interface sets rules for classes that represent groups of numbers.
public class Range implements NumberGroup {
private int start; // starting value of the range
private int end; // ending value of the range
public Range(int start, int end) {
this.start = start;
this.end = end;
}
@Override
public boolean contains(int number) {
return number >= start && number <= end; // check if the number is within the range
}
public static void main(String[] args) {
Range range = new Range(5, 15); // create a range from 5 to 15
testNumberGroup(range); // call the test method with the created range
}
public static void testNumberGroup(NumberGroup range) {
System.out.println("Range contains 10: " + range.contains(10)); // true
System.out.println("Range contains 20: " + range.contains(20)); // false
System.out.println("Range contains -5: " + range.contains(-5)); // false
}
}
interface NumberGroup {
boolean contains(int number);
}
| public class Range implements NumberGroup {
cannot find symbol
symbol: class NumberGroup
| public static void testNumberGroup(NumberGroup range) {
cannot find symbol
symbol: class NumberGroup
| @Override
method does not override or implement a method from a supertype
Key Learnings and Reflection
This FRQ enhanced my understanding of interfaces in Java and their role in defining a contract for classes to implement. The NumberGroup interface serves as a contract defining the behavior that classes like Range must implement. I also learned more about encapsulation as I encapusulated the start and end fields within the Range class which promoted better code organization and reduces the risk of unintended modifications by external classes. Overall, this FRQ deepened my understanding of Java programming concepts such as classes, interfaces, encapsulation, and testing methodologies, which builds up my Java skills.