FRQ 2024 Number 2 Homework 1
Homework
2024 FRQ 2 Solution + Explanation
This took me a bit yayayayayayay
/**
* Scoreboard Class - 2024 FRQ #2
*
* This class manages a game scoreboard for two competing teams.
* Key Features:
* - Tracks scores for both teams
* - Manages which team is currently active (taking turns)
* - Records plays that either score points or fail (end the turn)
* - Provides formatted score information
*
* Game Rules:
* - Team 1 starts as the active team
* - A play with points > 0: adds points to active team, team stays active
* - A play with points == 0: no points scored, active team switches
*/
class Scoreboard {
// PROPERTIES (Instance Variables)
// These store the state of the game
private String team1Name; // Name of team 1
private String team2Name; // Name of team 2
private int team1Score; // Current score of team 1
private int team2Score; // Current score of team 2
private int activeTeam; // Which team is currently active (1 or 2)
/**
* Constructor - Initializes a new game
*
* @param team1Name - The name of the first team
* @param team2Name - The name of the second team
*
* Preconditions: team1Name and team2Name are non-null strings
* Postconditions:
* - Both teams start with a score of 0
* - Team 1 is set as the active team
*/
public Scoreboard(String team1Name, String team2Name) {
this.team1Name = team1Name;
this.team2Name = team2Name;
this.team1Score = 0;
this.team2Score = 0;
this.activeTeam = 1; // Team 1 always starts as active
}
/**
* recordPlay - Records a single play in the game
*
* @param points - Points scored in this play (0 if play failed)
*
* Logic:
* - If points > 0: Add points to the active team's score
* The active team remains active for the next play
* - If points == 0: No points added to any team
* The turn ends and the OTHER team becomes active
*
* Example sequence:
* - recordPlay(3) when team 1 is active -> Team 1 score +3, stays active
* - recordPlay(0) when team 1 is active -> Team 1 score unchanged, team 2 now active
*/
public void recordPlay(int points) {
if (points > 0) {
// Points were scored - update the active team's score
if (activeTeam == 1) {
team1Score += points;
} else {
team2Score += points;
}
// Active team REMAINS active (continues their turn)
} else {
// points == 0 (play failed)
// Switch to the other team (end current team's turn)
activeTeam = (activeTeam == 1) ? 2 : 1;
}
}
/**
* getScore - Returns the current game state as a formatted string
*
* @return String in format: "team1Score-team2Score-activeTeamName"
*
* Examples:
* - "0-0-Red" (start of game between Red and Blue teams)
* - "5-3-Blue" (Red has 5 points, Blue has 3 points and is currently active)
* - "7-2-Lions" (Lions currently have the turn)
*
* This method does not change any game state - it only reads current values
*/
public String getScore() {
// Determine which team name to display based on activeTeam
String activeTeamName = (activeTeam == 1) ? team1Name : team2Name;
// Return formatted score string
return team1Score + "-" + team2Score + "-" + activeTeamName;
}
}
// Testing the Scoreboard class (DO NOT MODIFY this part unless you change the class, method, or constructer names)
// DO NOT MODIFY BELOW THIS LINE
class Main {
public static void main(String[] args) {
String info;
// Step 1: Create a new Scoreboard for "Red" vs "Blue"
Scoreboard game = new Scoreboard("Red", "Blue");
// Step 2
info = game.getScore(); // "0-0-Red"
System.out.println("(Step 2) info = " + info);
// Step 3
game.recordPlay(1);
// Step 4
info = game.getScore(); // "1-0-Red"
System.out.println("(Step 4) info = " + info);
// Step 5
game.recordPlay(0);
// Step 6
info = game.getScore(); // "1-0-Blue"
System.out.println("(Step 6) info = " + info);
// Step 7 (repeated call to show no change)
info = game.getScore(); // still "1-0-Blue"
System.out.println("(Step 7) info = " + info);
// Step 8
game.recordPlay(3);
// Step 9
info = game.getScore(); // "1-3-Blue"
System.out.println("(Step 9) info = " + info);
// Step 10
game.recordPlay(1);
// Step 11
game.recordPlay(0);
// Step 12
info = game.getScore(); // "1-4-Red"
System.out.println("(Step 12) info = " + info);
// Step 13
game.recordPlay(0);
// Step 14
game.recordPlay(4);
// Step 15
game.recordPlay(0);
// Step 16
info = game.getScore(); // "1-8-Red"
System.out.println("(Step 16) info = " + info);
// Step 17: Create an independent Scoreboard
Scoreboard match = new Scoreboard("Lions", "Tigers");
// Step 18
info = match.getScore(); // "0-0-Lions"
System.out.println("(Step 18) match info = " + info);
// Step 19: Verify the original game is unchanged
info = game.getScore(); // "1-8-Red"
System.out.println("(Step 19) game info = " + info);
}
}
Main.main(null);
(Step 2) info = 0-0-Red
(Step 4) info = 1-0-Red
(Step 6) info = 1-0-Blue
(Step 7) info = 1-0-Blue
(Step 9) info = 1-3-Blue
(Step 12) info = 1-4-Red
(Step 16) info = 1-8-Red
(Step 18) match info = 0-0-Lions
(Step 19) game info = 1-8-Red
How I Solved This Problem
Whats actually happening
So basically, this is a scoreboard for a two-team game. The twist is that the teams take turns, and when one team fumbles (scores 0 points), the other team gets their chance. If they score, they keep going. It’s like basketball—you keep your ball if you score, but you lose it if you miss.
My Solution
The Data I’m Tracking (Instance Variables)
- Two team names (like “Red” and “Blue”)
- A score counter for each team
- Which team gets to play right now (1 or 2)
Setting Up the Game (Constructor) When I create a new game, I:
- Store the team names
- Set both scores to 0 (nobody’s won yet)
- Make team 1 the “active” team (they go first, always)
Recording a Play (The Core Logic)
- If someone scores points (not 0): Add those points to whoever’s turn it is, and they keep their turn
- If the play fails (0 points): Don’t add anything, and switch to the other team
I use this little trick: activeTeam = (activeTeam == 1) ? 2 : 1; which just flips between 1 and 2.
Getting the Score (What the User Sees) I just return a formatted string that shows: “Team1Score-Team2Score-WhosTurnNow”
Example Runthrough w my algorithim
Let me trace through what actually happens:
- New game starts: “0-0-Red” (Red has the ball)
- Red scores 1 point → “1-0-Red” (Red keeps the ball)
- Red attempts a play but fails (0 points) → “1-0-Blue” (Blue gets the ball)
- Blue scores 3 points → “1-3-Blue” (Blue keeps the ball)
- Blue scores 1 more → “1-4-Blue” (still Blue’s turn)
- Blue fails → “1-4-Red” (Red gets it back)
You can see the pattern now—it’s all about momentum and mistakes.
What Actually Tripped Me Up
The Confusing Part: That 0-points parameter
At first, I was thinking about it wrong. It’s not just “no points were scored.” It’s more like “the play failed, so the ball is turned over.” The number 0 has this hidden meaning—it’s not just data, it’s a signal.
Why was this weird? Because:
- Every play needs data (how many points), but 0 is doing double duty (it means both “no points” AND “hand off the ball”)
- My brain wanted to switch teams every time, but actually teams only switch when points = 0
- Using 1 and 2 for the active teams works fine, but it requires you to remember the mapping in your head
How I Got It Right:
- I traced through every single test case manually
- I realized that consecutive point-scoring plays DON’T change who’s active
- The ternary operator felt like the cleanest way to flip between 1 and 2
Did It Work?
Yyayyyyyyyyyyyyyyyyyy! All the expected outputs match:
0-0-Red ← Starting position
1-0-Red ← Red scores, keeps it
1-0-Blue ← Red fails, Blue takes over
1-3-Blue ← Blue scores twice in a row
1-4-Red ← Blue fails, Red gets it
1-8-Red ← More scoring and switching
0-0-Lions ← New game, completely separate
1-8-Red ← Original game is untouched