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.

https://secure-media.collegeboard.org/digitalServices/pdf/ap/ap15_frq_computer_science_a.pdf

## 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)
if(group.contains(num))
return true;

return false;
}
```

## 20 thoughts on “NumberGroup free response answer”

1. anon

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

• Brandon Horn

Yes.

2. John

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.

Thanks,

John

• Brandon Horn

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

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

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

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

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

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

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

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

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

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?

• Brandon Horn

Yes. The code should be `NumberGroup t = groupList.get(index)`.

7. Danny

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

• Brandon Horn

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

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

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

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

• Brandon Horn

No, as long is it is done correctly.