Notes:
Assume that the classes listed in the Java Quick Reference have been imported where appropriate.
Unless otherwise noted in the question, assume that parameters in method calls are not null and that methods are called only when their preconditions are satisfied.
In writing solutions for each question, you may use any of the accessible methods that are listed in classes defined in that question. Writing significant amounts of code that can be replaced by a call to one of these methods will not receive full credit.
This question involves the design of an interface, writing a class that implements the interface, and writing a method that uses the interface.
(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.
(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.
(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
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.
A table is shown with two columns titled Call on the left and Result on the right. The first row reads multiple1 dot contains open parentheses 2 close parentheses on the left and true on the right. The second row reads multiple1 dot contains open parentheses 9 close parentheses on the left and false on the right. The third row reads multiple1 dot contains open parentheses 6 close parentheses on the left and true on the right.
Text at the top reads, Complete method contains below. A 4-line code segment reads as follows. Line 1: forward slash, asterisk, asterisk, Returns true if at least one of the number groups in this multiple group contains num, semicolon. Line 2: asterisk, false otherwise. Line 3: asterisk, forward slash. Line 4: public boolean contains, open parenthesis, int num, close parenthesis. BoldItalicUnderlineBullet listNumbered listImage (12 image limit)
FRQ Type: Methods and Control Structures/Classes
Key Algorithm:
In this FRQ, the emphasis is on methods and control structures, particularly in the context of class implementation. Let’s analyze how the provided solution aligns with this emphasis:
- Method Design and Implementation:
- The
contains(int num)
method in both theRange
andMultipleGroups
classes showcases method design and implementation. These methods utilize control structures such as conditional statements (if-else
) to determine if a given number is contained within a range or within any of the number groups, respectively. - The implementation of these methods demonstrates proper use of control structures to achieve the desired behavior efficiently. For example, in the
Range
class, thecontains(int num)
method checks if the given number falls within the specified range using conditional statements.
- The
- Class Implementation:
- The
Range
andMultipleGroups
classes represent different types of number groups and demonstrate class implementation in the context of the problem domain. - These classes encapsulate related functionality and data, promoting code organization and modularity. Each class defines its behavior through methods and controls access to its data through encapsulation.
- The
- Constructor Initialization:
- Both the
Range
andMultipleGroups
classes have constructors that initialize their instance variables (min
,max
,groupList
). - Constructor initialization ensures that the objects of these classes are properly initialized with valid data, setting the stage for correct behavior when methods are called.
- Both the
- Iterative Control Structures:
- The
MultipleGroups
class utilizes an iterative control structure (for-each
loop) to iterate over the list ofNumberGroup
objects and check if the given number is contained within any of them. - Iterative control structures are essential for traversing collections and performing operations on their elements, as demonstrated in the
MultipleGroups
class.
- The
import java.util.ArrayList;
import java.util.List;
interface NumberGroup {
boolean contains(int num);
}
class Range implements NumberGroup {
private int min;
private int max;
public Range(int min, int max) {
this.min = min;
this.max = max;
}
// check if a number is within the range
@Override
public boolean contains(int num) {
return num >= min && num <= max;
}
}
class MultipleGroups implements NumberGroup {
private List<NumberGroup> groupList;
public MultipleGroups(List<NumberGroup> groupList) {
this.groupList = groupList;
}
// Method to check if any of the number groups contains a given number
@Override
public boolean contains(int num) {
for (NumberGroup group : groupList) {
if (group.contains(num)) {
return true;
}
}
return false;
}
public static void main(String[] args) {
Range range1 = new Range(-3, 2);
Range range2 = new Range(5, 8);
Range range3 = new Range(10, 12);
Range range4 = new Range(1, 6);
System.out.println("Range 1 contains -2: " + range1.contains(-2));
System.out.println("Range 2 contains 6: " + range2.contains(6));
System.out.println();
List<NumberGroup> groupList = new ArrayList<>();
groupList.add(range2);
groupList.add(range3);
groupList.add(range4);
MultipleGroups multiple1 = new MultipleGroups(groupList);
System.out.println("multiple1.contains(2): " + multiple1.contains(2));
System.out.println("multiple1.contains(9): " + multiple1.contains(9));
System.out.println("multiple1.contains(6): " + multiple1.contains(6));
}
}