// CODE_RUNNER: Write the constructor for the SumOrSameGame class. Then, complete method clearPair
class Scoreboard {
private String name1;
private String name2;
private int pts1;
private int pts2;
private int active; // 1 = team1, 2 = team2
public Scoreboard(String team1Name, String team2Name) {
// This is your CONSTRUCTOR
// Initialize your properties HERE (team names and active team)
name1 = team1Name;
name2 = team2Name;
pts1 = 0;
pts2 = 0;
active = 1; // team 1 starts
}
private void switchTurn() {
active = (active == 1) ? 2 : 1;
}
private void addPointsToActive(int points) {
if (active == 1) {
pts1 += points;
} else {
pts2 += points;
}
}
public void recordPlay(int points) {
// Create the recordPlay Method HERE
if (points == 0) {
switchTurn();
} else {
addPointsToActive(points);
}
}
public String getScore() {
// Create the getScore Method HERE
String who = (active == 1) ? name1 : name2;
return pts1 + "-" + pts2 + "-" + who;
}
}
// 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);