Welcome to the Architect's Studio: FRQ 2 Class Design
If AP Computer Science A was a video game, Free-Response Question (FRQ) 2 would be the "Creative Mode." While other questions ask you to fix or use existing code, FRQ 2 asks you to build a blueprint from scratch. You are given a scenario—like a scoreboard, a fitness tracker, or a digital menu—and you must design the Class that makes it work.
This question is worth 7 points. It doesn't just test if you can write code; it tests if you understand how the different parts of a class (variables, constructors, and methods) fit together to solve a problem.
The Anatomy of a Class Design Response
On the exam, you will be given a specification table. This table shows example calls to methods and what those methods should return or do. Your job is to write the entire class based on that table. Every FRQ 2 response must include:
- A Class Header: The "shell" that holds everything.
- Instance Variables: The data the class needs to remember (its "state").
- A Constructor: The instructions for "birthing" a new object.
- Methods: The actions the object can perform (its "behavior").
Note: For more on basic method logic, see "FRQ 1: Methods and Control Structures."
Step 1: The Class Header and Instance Variables
Every class starts with a header. It almost always looks like this: public class NameOfClass. Inside that class, you immediately declare your instance variables.
Important Rule: Instance variables must be marked private. This is called encapsulation. It ensures that the data inside your object is "locked" and can only be changed by the methods you write.
Example Scenario: Designing a \(StepTracker\) class.
private \(int\) \(targetSteps\);
private \(int\) \(totalSteps\);
Quick Tip: Look at the specification table. If the table shows a method returning an average, you probably need a variable to track a "total" and another to track a "count."
Step 2: The Constructor
The constructor is a special block of code that runs only once when an object is created (using the \(new\) keyword). Its job is to give the instance variables their starting values.
- It must have the exact same name as the class.
- It has no return type (not even \(void\)).
- Overloaded Constructors: You can have more than one constructor as long as they have different "parameter lists" (the stuff inside the parentheses).
Common Mistake: Do not re-declare your variables inside the constructor!
Wrong: \(int\) \(totalSteps = 0;\) (This creates a local variable that disappears when the constructor ends).
Right: \(totalSteps = 0;\) (This assigns a value to the instance variable you created at the top).
Step 3: Writing the Methods
Methods define what your object does. There are three main types you will need to implement:
- Accessor Methods (Getters): These "get" information for the user. They usually have a return type like \(int\), \(double\), or \(boolean\).
- Mutator Methods (Setters): These "change" or update the data inside the object. These are usually \(void\) because they don't return anything; they just update a variable.
- Helper Methods: These perform internal calculations.
Naming Convention: Use lowerCamelCase for methods (e.g., \(calculateAverage\)). The exam expects you to follow the names provided in the specification table exactly!
Step 4: Understanding Scope and "this"
Scope refers to where a variable "lives."
- Instance Variables: Live as long as the object exists. They can be used in any method in the class.
- Local Variables: Created inside a method or loop. They "die" as soon as the method or loop finishes.
The \(this\) Keyword: Sometimes, your constructor parameter has the exact same name as your instance variable. We use this to tell Java which one is which.
Example:
public \(StepTracker\)(\(int\) \(steps\)) {
this.\(steps\) = \(steps\); // "this.steps" is the instance variable; "steps" is the parameter.
}
Did you know? Using \(this\) is not always required, but it is a professional way to clear up confusion when names overlap!
The "FRQ 2 Checklist" for Success
Before you finish your answer, run through this mental checklist to avoid losing easy points:
- Is the class header correct? (Check the spelling against the prompt!)
- Are all instance variables private? (The AP graders are very strict about this!)
- Does the constructor have the same name as the class?
- Do the methods match the return types in the table? (If the table shows \(2.5\), your method must return a \(double\)).
- Did you avoid "Hard-Coding"? Don't just return the numbers from the example table; use your variables to calculate the answer so it works for any data.
Key Takeaways Summary
1. Structure: Class Header > Private Variables > Constructor > Methods.
2. Encapsulation: Always use the private modifier for instance variables to keep data safe.
3. The Constructor: Initializes the "state" of the object. No return type allowed!
4. Parameters vs. Variables: Use this to distinguish between instance variables and parameters if they share a name.
5. Scope: Remember that variables created inside a method cannot be accessed outside that method.
Don't worry if class design feels like a lot of moving parts at first! Once you master the structure (Variables -> Constructor -> Methods), you'll find that most FRQ 2 questions follow the exact same pattern. Happy coding!