
Code:
// Create the Scoreboard Class HERE
class Scoreboard {
// Define your properties HERE
private String firstTeam, secondTeam;
private int currentTeam;
private int firstTeamScore, secondTeamScore;
public Scoreboard(String teamA, String teamB) {
// This is your CONSTRUCTOR
// Initialize your properties HERE (team names and active team)
firstTeam = teamA;
secondTeam = teamB;
currentTeam = 1;
firstTeamScore = 0;
secondTeamScore = 0;
}
public void recordPlay(int points) {
// Create the recordPlay Method HERE
if (points == 0){
currentTeam = (currentTeam == 1) ? 2 : 1;
} else {
if (currentTeam == 1) {
firstTeamScore += points;
} else {
secondTeamScore += points;
}
}
}
public String getScore() {
// Create the getScore Method HERE
String scoreStr = firstTeamScore + "-" + secondTeamScore + "-";
scoreStr += (currentTeam == 1) ? firstTeam : secondTeam;
return scoreStr; // Modify this return statement to return the actual score with the given format
}
}
// 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);
}
}
Approach
In this problem, I created a Scoreboard class to track points for two teams and manage whose turn it is. Here’s how I approached it:
- Properties
firstTeamandsecondTeamstore the names of the two teams.firstTeamScoreandsecondTeamScorestore the points for each team.currentTeamkeeps track of which team’s turn it is (1 = first team, 2 = second team).
- Constructor
- Initializes the team names, scores to 0, and sets the starting turn to the first team.
- recordPlay Method
- If the
pointsinput is 0, it switches the active team. - If the
pointsinput is greater than 0, it adds the points to the active team’s score.
- If the
- getScore Method
- Returns the current score as a string in the format:
firstTeamScore-secondTeamScore-currentTeamName - Uses the
currentTeamproperty to determine whose turn it is.
- Returns the current score as a string in the format: