FRQ 2 Class Design
In FRQ 2: Class Design, you are expected to write a class that meets the given specification. The question might require you to use inheritance and extend a given class.
You will be provided with a description of the class that you will have to write as well as a table that represents the use of the class and the expected result. In some cases, you will be provided with a helper class.
As always:
- be sure that you read carefully,
- use the highlighting tools in Blue Book,
- consult the AP Java Quick Reference if these classes are involved.
Some more specific tips for approaching FRQ 2: Class Design are as follows:
Look closely as the provided table. You will gain a better understanding of constructor parameters and instance variables as well as method return types and their parameters by looking at the examples. You can use this to glean the number of constructors and methods that are required.
- For example, in the 2024 #2, you are given the call:
Scoreboard game = new Scoreboard ("Red", "Blue");
This tells us that the constructor has two String
objects as parameters. This also means that you will need to have two String
instance variables. Any other data that is being stored for the object will need to be set in the constructor, but isn't coming from the user.
- Another example from the 2024 #2 is the call
info = game.getScore();
This line tells us that we have to write a method getScore
that doesn't have any parameters and has a return type of String
, since info
is a String
variable and the Value Returned column has a String
in it.
- The next call is
game.recordPlay(1);
. This line tells us we have a void
method recordPlay
that has a single int
parameter.
You may need more instance variables than your constructor has parameters. Often, there are additional instance variable than just the constructor parameters. Let's look at 2024 #2 more closely. In reading the bulleted paragraphs, we learn that we need a way to determine which team has the current turn and we need to keep scores for both of the teams. So this class has 5 instance variables. Two String
objects for the team names, a variable to determine the team currently playing, and two int
variables for the score. These 3 additional variables will need initial values provided in the constructor.
Only write what you are required to write. You may have been trained that you need to have a multiple constructors, such as every class needs to have a default constructor. And that you should have accessor and mutator methods for all your instance variables. These are great rules for the real world, but for the AP CSA exam, please, please, please, ONLY write the constructor and methods that are required. You could lose points for doing more than you are supposed to do if it causes a unintended side effect.
Know the basic structure of a class. You will need to have a header, private instance variables, a constructor and methods. There is a basic formula that you can and should follow to maximize your number of points. If you forget, you should look at the other provided classes in the FRQ section. Chances are you can get some tips from those questions as well. Here is a basic outline of a class to follow:
public class ClassName {
//instance variables
private type name;
//constructor
public ClassName (type param1, type param2, ...) {
//body
}
public returnType methodName (type param1, type param2...) {
//body
}
}
- Just because you are given a class doesn't mean you are extending it. Sometimes you will be given a helper class. This class could be included because you need to create an instance variable of that type or it could be provided because you need to extend this class. The is-a test is a good one for inheritance. Let's look at an example or two.
- Check of 2022 FRQ #2. In this question, you are provided with a
Book
class and asked to write a Textbook
class. Does this pass the is-a question? Would you say yes or no to the following statement? A Textbook
is a Book
. You would answer yes. So most likely, you need to use inheritance. And in this question, you definitely were asked to extend Book
. Another clue, well they tell you: "You will write a Textbook
class, which is a subclass of Book
".
- Let's look at 2021 FRQ #2. In this question you are provided a class
SingleTable
and asked to write the class CombinedTable
. Does this pass the is-a question? Would you say yes or no to the following statement? A CombinedTable
is a SingleTable
. Hopefully you answered no. But this question caught many students who were advised that if you were given another class, you had inheritance. That's not true. You have to be sure that you are understanding the relationship between the classes. In this question, a CombinedTable
was comprised of two SingleTable
objects. This is a has-a relationship, which tells us that we need instance variables of type SingleTable
. We would say yes to the statement: CombinedTable
has a SingleTable
.
More Resources