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;
}
Hi,
If in part a you said “public boolean” instead of just “boolean,” would that be acceptable?
Thank you
Yes.
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
Just having a
MultipleGroups
object within anotherMultipleGroups
object’sgroupList
wouldn’t be sufficient to cause infinite recursion. This would only occur if each object had the other in itsgroupList
.Having one
MultipleGroups
object within another’sgroupList
would actually be expected behavior.If I did In part c , Range t = groupList.get(index);
Would that be wrong and if so how many points would be deducted.
A regular
for
loop would have worked fine here.Edited 5/12:
As discussed below
Range t
should beNumberGroup t
. The list access is correct.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?
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.
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!
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.
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?
Yes. As long as you obtain each
Range
object from the list, runcontains
properly and handle the return value properly it would work the same.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?
Yes. The code should be
NumberGroup t = groupList.get(index)
.for part a if I said
abstract boolean contains(int value);
would that be ok?
Yes. All methods of an interface are abstract so the
abstract
modifier is typically omitted; however, it is permissible to include the modifier. Including theabstract
modifier does not change the behavior.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?
Your question is at the heart of interfaces and polymorphism. The reference type of each item in
groupList
isNumberGroup
. The object type of each items ingroupList
varies. The object type could beRange
,MultipleGroups
or any other class that implementsNumberGroup
.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.For part (b), are any points deducted for using an ArrayList or array as opposed to 2 int variables?
No, as long is it is done correctly.