NumberGroup free response answer 20

NumberGroup free response problem from the 2015 AP Computer Science A Exam.

NumberGroup is #4 from the from the 2015 AP Computer Science A Free Response problems.

Part (a) – NumberGroup interface

public interface NumberGroup
  boolean contains(int value);

contains could also be specified as public.

Part (b) – Range class

public class Range implements NumberGroup
  private int minValue, maxValue;

  public Range(int minValue, int maxValue)
    this.minValue = minValue;
    this.maxValue = maxValue;
  public boolean contains(int value)
    return minValue <= value && value <= maxValue;

Part (c) – MultipleGroups contains method

public boolean contains(int num)
  for(NumberGroup group : groupList)
      return true;

  return false;

20 thoughts on “NumberGroup free response answer

  1. anon May 9,2015 5:32 pm

    If in part a you said “public boolean” instead of just “boolean,” would that be acceptable?
    Thank you

  2. John May 10,2015 6:16 am

    Hey Brandon,

    For part (c), what if there was a NumberGroup object in the list that was a MultipleGroups? Wouldn’t that cause an infinite recursion?

    Maybe I’m still unsure how Interfaces work, or maybe I misunderstood the question, but I just want to clear up this question that’s bugging me.



    • Brandon Horn May 10,2015 8:28 am

      Just having a MultipleGroups object within another MultipleGroups object’s groupList wouldn’t be sufficient to cause infinite recursion. This would only occur if each object had the other in its groupList.

      Having one MultipleGroups object within another’s groupList would actually be expected behavior.

  3. Lebron May 10,2015 9:08 am

    If I did In part c , Range t = groupList.get(index);
    Would that be wrong and if so how many points would be deducted.

    • Brandon Horn May 10,2015 9:48 am

      A regular for loop would have worked fine here.

      Edited 5/12:

      As discussed below Range t should be NumberGroup t. The list access is correct.

  4. Anon1 May 10,2015 11:47 am

    For part b, instead of altering min and max value of integer. I created a new ArrayList with values from max to min, then my contains method would just scan the list. Would this still work?

    • Brandon Horn May 10,2015 10:24 pm

      This is quite silly but my best guess is that it wouldn’t be penalized. I don’t have the scoring guidelines so I can’t confirm that. Making such a list could be problematic if the range was very large.

  5. Emerald Brown May 10,2015 6:12 pm

    For the Range class, instead of having instance variables of min and max I had a List of numbers. Would this be acceptable?

    If not, how many points would the deduction be? Thanks in advance!

    • Brandon Horn May 10,2015 10:27 pm

      This is quite silly but my best guess is that it wouldn’t be penalized. I don’t have the scoring guidelines so I can’t confirm that. Making such a list could be problematic if the range was very large.

  6. Lebron May 10,2015 7:14 pm

    So , Range t = groupList.get(index), in a regular for loop and then testing whether or not the contains(int num) method returns true from the object “t” is correct?

    • Brandon Horn May 10,2015 10:27 pm

      Yes. As long as you obtain each Range object from the list, run contains properly and handle the return value properly it would work the same.

      • Lebron May 11,2015 2:56 pm

        Follow up from the last question I had,

        I have a question, sine the list comprises of NumberGroup objects and not Range objects, making a new object of type NumberGroup would be incorrect. Wouldn’t this be assuming everyone in the list is a Range when in fact it is a NumberGroup object instead, so the code, Range t = groupList.get(index) in a regular for loop, would in fact be incorrect, right?

  7. Danny May 11,2015 1:04 am

    for part a if I said
    abstract boolean contains(int value);
    would that be ok?

    • Brandon Horn May 11,2015 7:58 am

      Yes. All methods of an interface are abstract so the abstract modifier is typically omitted; however, it is permissible to include the modifier. Including the abstract modifier does not change the behavior.

  8. Jenny May 11,2015 6:46 am

    Thank you so much for the solutions!! In part c were we to assume groupList held Range objects? The question says it holds NumberGroup objects (which may or may not be Range objects), so what contains method will the if statement in part c call if they are NOT Range objects? The one in part a (the interface) doesn’t have a body so how would this work?

    • Brandon Horn May 11,2015 8:10 am

      Your question is at the heart of interfaces and polymorphism. The reference type of each item in groupList is NumberGroup. The object type of each items in groupList varies. The object type could be Range, MultipleGroups or any other class that implements NumberGroup.

      The reference type determines what methods can be run. The object type determines what method is actually run. An interface, such as NumberGroup can never be an object type. An interface can be, and often is, a reference type.

  9. David Apr 20,2016 10:03 am

    For part (b), are any points deducted for using an ArrayList or array as opposed to 2 int variables?

Comments are closed.