// 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);


Summary of Process

Things I Learned

  • I learned how to design a class using instance variables to keep track of an object’s state.
  • I learned how constructors initialize an object so it starts in a valid state.
  • I learned how to use conditional statements to control program flow based on different cases.
  • I learned how a boolean or numeric flag can be used to track which team is currently active.
  • I learned how method calls can change an object’s state over time without returning a value.
  • I learned how to format and return a string that combines multiple pieces of information.
  • I learned how to trace method calls step by step to verify that the program matches expected output.
  • I learned how multiple objects of the same class can exist independently.

Things I Struggled With

  • I struggled with correctly switching the active team only when a play failed.
  • I struggled with deciding whether to use a boolean or an integer to track the active team.
  • I struggled with remembering that scoring plays should not change which team is active.
  • I struggled with making sure the constructor fully initialized all instance variables.
  • I struggled with tracing the program when several method calls happened in a row.
  • I struggled with small logic mistakes that caused the output to be slightly off.
  • I struggled with understanding how object state persists between method calls.
  • I struggled with debugging because one incorrect condition affected later outputs.