Classes should be written in the order below.
- Class header & all public method headers
- Instance variables
- Code inside methods
This is true for code in actual projects and for AP CS A Free Response that ask students to write an entire class. To follow the order above when responding on paper, see the section below titled “When responding on paper”.
The examples below are from the Coordinate2D
class.
Step 1: Class header & all public method headers
The class header and public method headers define how the class can be used by other classes. Most AP CS A Free Response that ask for an entire class provide examples of that class being used by a client class. In recent free response, these examples have been provided in the form of a table. Past free response that request an entire class are linked near the bottom of this page.
If the class extends another class, include this in the class header.
When writing a method header, include the access specifier, return type (for methods other than constructors), method name, and complete parameter list. Don’t write half of a method header then return to it later.
If you aren’t sure about a method header, start with the access specifier and the method name, then add the parameters, then add the return type.
Example 1
public void setX(int newX)
Access specifier: public
Method name: setX
Parameter: int newX
Return type: void
Example 2
public int getY()
Access specifier: public
Method name: getY
Parameters: None
Return type: int
Step 2: Instance variables
Your instance variables exist to support your methods. They serve no other purpose. Make a deliberate decision about what you need to store to be able to write your methods. It is common for students to choose inappropriate instance variables. Inappropriate instance variables can make the implementation more challenging or impossible.
Although it is possible for the instance variables to match the constructor parameters, this is not always the case. Do not just copy the constructor parameters as your instance variables unless that is actually what you want to store.
If you decide to store a list as an instance variable, take a moment to ensure that you actually need the list. It is common for students to unnecessarily store a list of values when storing information about those values would have made the problem easier.
Example
private int x, y;
Step 3: Code inside methods
For many AP CS A Free Response that request an entire class, the implementation code for the methods themselves is simple. As with any other method implementation, make sure you understand what the method is doing, determine your algorithm, then implement the algorithm in code.
AP CS A Free Response that request an entire class often include something related to method implementations that is easily missed. Maybe there can be 0 inputs. Maybe something needs to be computed as a double
. Pay attention to all of the examples, just as you would for any free response.
Example
public void setX(int newX) // method header (part of Step 1)
{
x = newX; // code inside method
}
When responding on paper
AP CS A students taking the Exam on paper should still follow the order above. Since it is impossible to insert line breaks on paper, use the simple trick below.
- Write the class header on your real answer page.
- Write the public method headers on a piece of scratch paper.
- Write the instance variables on your real answer page, under the class header as usual.
- Copy the first method header to your real answer page. Write the implementation for that method under the method as usual. Repeat this for each method.
Practice writing classes with AP CS Tutor Brandon Horn.
Selected AP CS A FR that request an entire class
- 2022 A FR #2 Textbook: 2022 FR PDF / Textbook solution
- 2021 A FR #2 CombinedTable: 2021 FR PDF / CombinedTable solution
- 2019 A FR #2 StepTracker: 2019 FR PDF / StepTracker solution
- 2018 A FR #3 CodeWordChecker: 2018 FR PDF / CodeWordChecker solution
- 2017 A FR #2 MultPractice: 2017 FR PDF / MultPractice solution
- 2016 A FR #1 RandomStringChooser: 2016 FR PDF / RandomStringChooser solution
- 2015 A FR #2 HiddenWord: 2015 FR PDF / HiddenWord solution
- 2015 A FR #4 NumberGroup: 2015 FR PDF / NumberGroup solution
- 2010 A FR #2 APLine: 2010 FR PDF / APLine solution
Additional resources
- Class writing exercise
- Constructing objects
- Primitive types vs references exercises
- Primitive types vs references exercises with calls
- ZombiePlant FR
- Inheritance and polymorphism
- PirateParrot inheritance FR
Help & comments
Get help from AP CS Tutor Brandon Horn
Comment on Class writing order