AP Computer Science A · Exam guide
AP Computer Science A Question 2 Explained: Writing a Complete Class
Question 2 asks you to write an entire class from a description. It is worth 9 points, and the design point is won by keeping data private and writing a constructor that sets it. The example below was compiled and run.
A sample question
A ParkingLot has a fixed capacity and counts the cars inside. The constructor takes the capacity, and the lot starts empty. The method enter() admits a car and returns true if there is space, and returns false otherwise. The method leave() removes a car if there is one. The method spacesLeft() returns the number of open spaces. Write the complete class.
A full-credit solution
public class ParkingLot
{
private int capacity;
private int cars;
public ParkingLot(int cap)
{
capacity = cap;
cars = 0;
}
public boolean enter()
{
if (cars < capacity)
{
cars++;
return true;
}
return false;
}
public void leave()
{
if (cars > 0)
{
cars--;
}
}
public int spacesLeft()
{
return capacity - cars;
}
}
Tested: with a capacity of 2, three calls to enter() return true, true, false, and spacesLeft() is then 0. After one call to leave(), spacesLeft() is 1.
How the points are earned
| Piece | Where it appears |
|---|---|
| Class header and private instance variables | public class ParkingLot with private capacity and cars |
| Constructor with the right header | public ParkingLot(int cap) sets both variables, with no return type |
| Methods with the right headers and return types | boolean enter(), void leave(), int spacesLeft() |
| Condition and update | cars < capacity guards the increment, and cars > 0 guards the decrement |
| Returns the right value | enter() returns true after adding a car and false otherwise |
| Row | Points | What earns it |
|---|---|---|
| Correctness of the algorithm | 6 | Six one-point pieces: declares the needed variables; loops over the correct range; tests the stated condition; updates the right variable on each pass; handles edge cases; returns the right value at the right time. |
| Use of the Java API and syntax | 2 | One point for using the given methods and the ArrayList and array syntax correctly; one for Java that would compile, with matching headers and declared variables. |
| Program design and encapsulation | 1 | Uses the class's own methods and keeps data private, as the question's design requires. |
| Total | 9 |
Habits for Question 2
- Make every instance variable private, and every method that the question names public.
- Give the constructor the same name as the class and no return type.
- Think through the edge cases the description mentions, such as a full lot or an empty one.
- Use the instance variables directly inside the class. You do not need get methods for them.
Common mistakes
- Declaring instance variables public, which costs the design point.
- Giving the constructor a return type, which makes it an ordinary method.
- Declaring a new local variable with the same name as an instance variable, so the field never changes.
- Forgetting to guard against going below zero or above the capacity.
Frequently asked questions
What is Question 2 about?
Writing a complete class: instance variables, a constructor and methods, from a written description.
Should every instance variable be private?
Yes. Keeping data private is part of the design and encapsulation point.
Do I need to write comments?
No. Credit comes from the code.
Do I need to write get and set methods?
Only the methods the question asks for.
Where can I find the official scoring rules?
Exam formats change a little from year to year, so check the current course and exam description on the College Board site before test day.
Keep practicing
The AP Computer Science A course has eight free-response prompts, two of each exam type, each with a coding space, the scoring notes, and the canonical solution explained point by point. These free lessons also build the skills the free-response section tests:
See the AP Computer Science A course Free lessons and lesson list
Other free-response guides:
- AP Computer Science A FRQ Guide: The Four Coding Questions and the 9-Point Rubric
- AP Computer Science A Question 1 Explained: Methods and Control Structures
- AP Computer Science A Question 3 Explained: Data Analysis with an ArrayList
- AP Computer Science A Question 4 Explained: Working with a 2D Array
AP is a registered trademark of the College Board, which is not affiliated with and does not endorse this site. This guide is our own explanation of the question types and scoring. For the official scoring guidelines, see the College Board.