By LearnAllStudents Editorial Team. Source check dates are listed below.
Follow the worked example
A PassCounter object starts with an integer number of unused passes. Store that number in a private remaining field.
The constructor PassCounter(int remaining) assigns this.remaining = remaining. A spend() call decrements the field only if it is above zero and reports whether it changed.
With a count of one, the first call returns true and leaves zero. The next returns false and leaves zero. Construct two objects to check their counts are independent.
Place the listing inside a Java class. Try the worked example’s inputs from its main method.
static final class PassCounter {
private int remaining;
PassCounter(int remaining) {
if (remaining < 0) {
throw new IllegalArgumentException("Use a nonnegative count.");
}
this.remaining = remaining;
}
boolean spend() {
if (remaining == 0) {
return false;
}
remaining--;
return true;
}
int remaining() {
return remaining;
}
}Define what must still be true after each call
If an object stores unused passes, the count should not go below zero for valid starting values. Test the state before and after a successful spend and an attempted spend at zero. These are different cases even when both call the same method.
Try it yourself
PassCounter a starts at 2 and b starts at 1. Call a.spend() twice and b.spend() once. Give each return value and both final counts.
Check your reasoning
All three calls return true. The final counts for a and b are both zero. The order does not merge the two objects’ state.
Use the idea in a different situation
What bug would a static remaining field introduce?
Check the second task
Both objects would share the same field, so constructing or spending on one could affect the other.
For parents
Ask, “If two counters start with different amounts, which one changes when you spend from the first?”
They should explain that each object owns its own count and show both states after one call.
Use two cards labelled A and B. Put a number on each card and update only the card named by a method call.
How a tutor would approach this
Write down the expected output or state before running the Java code. Compare it with the actual output, trace where they first differ and test the fix against another input that arrives at the same kind of boundary.
Frequently asked questions
What mistake should I look for first?
Writing remaining = remaining in the constructor assigns the parameter to itself. Qualify the field with this when names match.
How can I use this with a longer practice set?
Read the current course framework and official sample questions before deciding how to practise this technique in an exam-length task.
Are these official past-paper questions?
The worked example and practice tasks here are original. The sources section links to the official material for your course.
How can I tell if I can use the method on a new question?
Try the second task with the worked example covered. Explain which step needs to change and why before checking the answer.
Official sources
- College Board AP Computer Science A course and exam description (checked 2026-11-26)
Related pages
Try a free 30-minute demo
Work through a question with a tutor. No payment needed.
Request a free 30-minute demo