Home 2015 CSA FRQ Q4
Post
Cancel

2015 CSA FRQ Q4

Question 4

Directions: SHOW ALL YOUR WORK. REMEMBER THAT PROGRAM SEGMENTS ARE TO BE WRITTEN IN JAVA.

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.

public interface NumberGroup {
    boolean contains(int value);
}

/* explanation:
 * 
 * Pretty simple, just a single method interface that returns a boolean.
 * 
 */

(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;
    }

    public boolean contains(int value) {
        return value >= min && value <= max;
    }
}

/* explanation:
 * 
 * Lets take in the min and max values in a range, then check to see if a given value is within that range. If it is, we return true
 * else we return false.
 * 
 */

(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.

public class MultipleGroups implements NumberGroup {
    private NumberGroup[] groupList;

    public MultipleGroups(NumberGroup[] groupList) {
        this.groupList = groupList;
    }

    public boolean contains(int value) {
        for (NumberGroup group : groupList) {
            if (group.contains(value)) {
                return true;
            }
        }
        return false;
    }
}

/* explanation:
 * 
 * Now we take multiple ranges and check to see if a given value is within any of them. If so, we return true, else false.
 * 
 */

NumberGroup test1 = new Range(-3, 2);
System.out.println(test1.contains(-2)); // should return true
System.out.println(test1.contains(3)); // should return false

NumberGroup test2 = new Range(4, 7);
NumberGroup test3 = new Range(10, 15);
NumberGroup multiplegroups = new MultipleGroups(new NumberGroup[] {test2, test3});
System.out.println(multiplegroups.contains(4)); // should return true
System.out.println(multiplegroups.contains(100)); // should return false
System.out.println(multiplegroups.contains(11)); // should return true


1
2
3
4
5
6
true
false

true
false
true
This post is licensed under CC BY 4.0 by the author.