public class Scorekeeper { String homeTeam, visitingTeam; int inning; boolean topOfInning; int home, visitors; // score int balls, strikes, outs; // count boolean gameOver; Scorekeeper(String homeTeam, String visitingTeam) { this.homeTeam = homeTeam; this.visitingTeam = visitingTeam; inning = 1; topOfInning = true; home = visitors = balls = strikes = outs = 0; gameOver = false; } void resetBallsAndStrikes() { balls = strikes = 0; } public void hit() { resetBallsAndStrikes(); } public void ball() { balls++; if (balls == 4) walk(); } public void strike() { strikes++; if (strikes == 3) batterOut(); } public void foul() { if (strikes < 2) strike(); } public void walk() { resetBallsAndStrikes(); } public void batterOut() { resetBallsAndStrikes(); out(); } public void out() { outs++; if (outs == 3) newHalf(); } public void run() { if (topOfInning) visitors++; else { home++; gameOver = ((inning >= 9) && (home > visitors)); } } void newHalf() { resetBallsAndStrikes(); outs = 0; if ((inning > 8) && (topOfInning == false) && (home != visitors)) gameOver = true; else { if (!topOfInning) inning++; topOfInning == !topOfInning; } } public String toString() { if gameOver return ("Final Score after " + inning + " innings: " + score()); else { String topBot; if topOfInning topBot = "top" else topBot = "bottom" return ("In the " + topBot + " of inning " + inning + "with " + balls + " balls, " + strikes + " strikes, and " + outs + " outs, it's " + score()); } } String score() { if (home > visitors) return (homeTeam + " over " + visitingTeam + ", " + home + " to " + visitors + "."); else if (visitors > home) return (visitingTeam + " over " + homeTeam + ", " + visitors + " to " + home + "."); else return (visitingTeam + " and " + homeTeam + " tied " + home + " to " + visitors + "."); } }