Home | Review | Approach and Practice | Homework |
FRQ Teaching | Classes | Approach
Lesson for class FRQs
The Problem - 2024 FRQ #2
0 - Penalties
These are ways to lose points. They apply to all kinds of FRQs, not just classes-related ones. Do not do these things!!
1. Array/Collection Access Confusion ([]
vs get
)
Explanation
This penalty is applied when there is confusion between accessing elements in arrays and collections:
int[] arr = {1, 2, 3};
System.out.println(arr[1]); // Correct for arrays
ArrayList<Integer> list = new ArrayList<>();
list.add(10);
System.out.println(list[0]); // Incorrect: should use list.get(0)
2. Extraneous Code That Causes Side Effects
Explanation
This penalty is given for including unnecessary code that impacts the program’s behavior, often unintentionally. For example:
public int addNumbers(int a, int b) {
System.out.println("Adding numbers..."); // Side-effect: unnecessary printing
return a + b;
}
3. Local Variables Used but None Declared
Explanation
This penalty is for using local variables in a method or block without declaring them:
public int calculateSum() {
total += 5; // Incorrect: 'total' is used but not declared
int total = 0; // This declaration should come first
total += 5; // Correct
return total;
}
4. Destruction of Persistent Data
Explanation
This deduction is for modifying data in a way that unintentionally alters persistent values (like those referenced by parameters):
public void changeName(StringBuilder name) {
name.append(" Smith"); // Modifies the original object unintentionally
}
StringBuilder studentName = new StringBuilder("John");
changeName(studentName);
System.out.println(studentName); // Output: "John Smith" (persistent data altered)
5. Void Method or Constructor That Returns a Value
Explanation
This deduction is for attempting to return a value from a method or constructor declared void:
public void printMessage() {
return "Hello"; // Incorrect: void methods cannot return a value
}
public class MyClass {
public MyClass() {
return; // Correct: constructors can return nothing implicitly
}
}
I - The Free Points
4 points that you should get, even if you have no idea how to code.
Point 1
Declares class header: class Scoreboard
- Do NOT forget public
- Do NOT write the name wrong
public class Scoreboard {
}
Point 2
Declares at least one private String instance variable and one private int instance variable
Figuring out variables
- Identify what is fed into construction. Create variables for those.
- Identify what is being returned by the methods
- If it can be constructed from already existing variables, no need to create mote
- In this case, it would be easier to store integer variables rather than using casting and substrings to modify an output string
- Identify any other variable you might need. This can be done later. In this case, we need a variable to identify whose turn it is
public class Scoreboard {
private String Team1;
private String Team2;
private int turn;
private int score1;
private int score2;
}
Point 3
Declares constructor header: Scoreboard(String __, String __) and constructor initializes both team name instance variables using parameters
- Identify what the constructor takes in
- Assign variables appropriately
- Make sure your constructure is PUBLIC
public class Scoreboard {
private String Team1;
private String Team2;
private int turn;
private int score1;
private int score2;
public Scoreboard(String team1, String team2) {
this.Team1 = team1;
this.Team2 = team2;
this.whoseTurn = 1;
this.score1 = 0;
this.score2 = 0;
}
}
Point 4
Declares method headers: public void recordPlay(int ___)
and public String getScore()
- Do not mess up the names
- Do not mess up the return types or argument types. These are given in the specifications
public class Scoreboard {
private String Team1;
private String Team2;
private int turn;
private int score1;
private int score2;
public Scoreboard(String team1, String team2) {
this.Team1 = team1;
this.Team2 = team2;
this.whoseTurn = 1;
this.score1 = 0;
this.score2 = 0;
}
public void recordPlay(int points) {
}
public String getScore() {
}
}
II - Rest of the Problem
Two methods to create, getScore is just returning a String, while recordPlay has the algorithm. Hence, recordPlay is worth more points, 4, whereas getScore is only worth 1.
Point 5
Accessor method builds and returns specified string (algorithm)
- Build string to return from current variables
- Always score 1 + “-“ + score 2 + “-“
- Team’s name depends on whose turn –> if statement
public class Scoreboard {
private String team1;
private String team2;
private int turn;
private int score1;
private int score2;
public Scoreboard(String team1, String team2) {
this.team1 = team1;
this.team2 = team2;
this.turn = 1;
this.score1 = 0;
this.score2 = 0;
}
public void recordPlay(int points) {
}
public String getScore() {
String result = this.score1 + "-" + this.score2 + "-";
if (this.turn == 1) {
result += this.team1;
} else {
result += this.team2;
}
return result;
}
}
The rest - recordPlay
- Consider Outcomes:
- turn changes: occurs if points is 0
- score is incremented: occurs if points is not 0
Issue with the above:
- depending on the turn, a different variable is incremented –> if statement
- turn will change to different person depending on existing turn –> if statement
public class Scoreboard {
private String team1;
private String team2;
private int turn;
private int score1;
private int score2;
public Scoreboard(String team1, String team2) {
this.team1 = team1;
this.team2 = team2;
this.turn = 1;
this.score1 = 0;
this.score2 = 0;
}
public void recordPlay(int points) {
if (points == 0) {
if (this.turn == 1) {
this.turn = 2;
} else {
this.turn = 1;
}
} else {
if (this.turn == 1) {
this.score1 += points;
} else {
this.score2 += points;
}
}
}
public String getScore() {
String result = this.score1 + "-" + this.score2 + "-";
if (this.turn == 1) {
result += this.team1;
} else {
result += this.team2;
}
return result;
}
}
Key Takeaways/Tips & Tricks
- Always start off with getting your easy points. You can get up to 4 points without understanding what the question is asking you at all. These points include declaring your class header, declaring private instance variables,
- Read the requirements carefully. For example, when the question stated that the
recordPlay
method does not return a value, this means that its method signature needs to bepublic void
. - Use the table. In classes related FRQs, there is no code given. You need to use what is expected based on the outputs of the table and explanations to write all the code yourself. I would highly recommend reading through everything before starting.
- Follow all instructions. Luckily the classes FRQ is generally the most straightforward out of all FRQs given during the test. From what I’ve seen so far, there are no real trick questions like the MCQ. You are graded on how well you followed the instructions given to you.