/* An instance of this class (UmpireClicker1) represents a hand-held ** mechanical device commonly used by baseball/softball umpires to keep ** track of the inning, outs, balls, and strikes during the course of a ** game. ** ** Author: Robert McCloskey ** Date: October 2020 */ public class UmpireClicker1 { // symbolic constants // ------------------ private static final int BALL_MAX = 4; private static final int STRIKE_MAX = 3; private static final int OUT_MAX = 3; private static final int INNING_MAX = 9; // instance variables (fields) // --------------------------- private int ballCntr; // for keeping track of the counts of private int strikeCntr; // balls, strikes, outs, and the inning private int outCntr; private int inningCntr; // constructor // ----------- /* Initializes the instance variables of a newly-created UmpireClicker1 ** object. (Because all of them are initialized to zero, which Java does ** automatically, this constructor can be omitted entirely, or, ** alternatively, its body can be made to be empty.) */ public UmpireClicker1() { ballCntr = 0; strikeCntr = 0; outCntr = 0; inningCntr = 0; } // observers // --------- // Because each method's name makes its purpose obvious, we omit comments. public int ballCount() { return ballCntr; } public int strikeCount() { return strikeCntr; } public int outCount() { return outCntr; } public int inningCount() { return inningCntr; } public String toString() { return "Inning " + inningCount() + ", Out " + outCount() + ", Ball " + ballCount() + ", Strike " + strikeCount(); } // mutators // -------- // Because each method's name makes its purpose obvious, we omit comments. public void advanceBallCount() { if (ballCntr < BALL_MAX) { ballCntr = ballCntr + 1; } else { ballCntr = 0; } // alternative method body using the % (remainder) operator: // ballCntr = (ballCntr + 1) % (BALL_MAX + 1); } public void advanceStrikeCount() { if (strikeCntr < STRIKE_MAX) { strikeCntr = strikeCntr + 1; } else { strikeCntr = 0; } } public void advanceOutCount() { if (outCntr < OUT_MAX) { outCntr = outCntr + 1; } else { outCntr = 0; } } public void advanceInningCount() { if (inningCntr < INNING_MAX) { inningCntr++; } else { inningCntr = 0; } } }