/* BaseballGame.java ** An instance of this class represents a baseball game, viewed as being ** a sequence of events of two kinds: run scored, out made. An occurrence ** of such an event is recorded by invoking a mutator method. Observer ** methods allow a client to ascertain, among other things, the current ** score, the current half-inning, and whether or not the game is over). ** ** Authors: P.M. Jackowitz & R. McCloskey and < YOUR NAME > ** CMPS 134 Prog. Assg. #6 ** Date: November 2019 ** Collaborated with: ... ** Known Defects: ... */ public class BaseballGame { // public class constants // ---------------------- public static final char HOME = 'H'; // used for identifying the teams public static final char VISITOR = 'V'; // and the halves of innings // private constants // ----------------- private static final int OUTS_PER_HALF_INNING = 3; private static final int OUTS_PER_INNING = 2 * OUTS_PER_HALF_INNING; private static final int INNINGS_REQUIRED_DEFAULT = 9; // instance variables // ------------------ private final int INNINGS_REQUIRED; // intended duration of the game // **** other instance variables needed **** // constructors // ------------ /* Initializes this baseball game to be in the visitor's half of the ** first inning, with no outs or runs having been recorded, and having ** the given number as the "required" number of innings. ** (Specifically, the game cannot end before the visitor's half of ** the specified inning is completed.) */ public BaseballGame(int numRequiredInnings) { INNINGS_REQUIRED = numRequiredInnings; } /* Initializes this to be a baseball game having the default number of ** required innings and otherwise as described in the constructor above. */ public BaseballGame() { this(INNINGS_REQUIRED_DEFAULT); } // observers // --------- /* Returns the minimum duration of this game, measured in innings. */ public int inningsRequired() { return INNINGS_REQUIRED; } /* Returns the number of completed half-innings in which the specified ** team has batted. ** Note: A half-inning during which the home team wins via "walkoff" ** is not considered to have been completed. ** pre: team == HOME || team == VISITOR */ public int inningsCompleted(char team) { return 0; // STUB!! (First Increment) } /* Returns the number identifying the current inning; ** innings are numbered starting at one. */ public int currentInning() { return 0; } // First Increment /* Returns either VISITOR or HOME, according to which team is batting. ** (The visiting team bats in the visitor/top/first half of each ** inning and the home team bats in the home/bottom/last half.) */ public char currentHalfInning() { return HOME; // STUB!! (First Increment) } /* Returns the number of outs that have been recorded during the ** current half-inning. */ public int currentHalfInningOuts() { return 0; // STUB!! (First Increment) } /* Reports the number of runs scored by the specified team. ** pre: team == HOME || team == VISITOR */ public int runsScored(char team) { return 0; // STUB!! } /* Reports whether or not the game is over. */ public boolean isOver() { return false; // STUB!! } /* Reports which team (HOME or VISITOR) won the game. ** pre: isOver() */ public char winningTeam() { return VISITOR; // STUB!! } /* Returns a String showing the current state of the game, including the ** score, current half-inning, and required number of innings. ** Example 1: "[5-3][H4,1:9]" indicates a 9-inning game that is in the ** home half of the 4th inning, with one out, and the home team having ** scored five runs and the visitors three runs. ** Example 2: A game that is over includes that information, as in ** "[2-4][V10,0:9] Game Over!", which describes a 9-inning game won by ** the visitors, four runs to two. (The game ended at the completion ** of the home half of the 9th inning, which explains why the string ** indicates there being zero outs in the visitor half of the 10th inning.) */ public String toString() { String scoreStr = bracketed(runsScored(HOME) + "-" + runsScored(VISITOR)); String inningStr = bracketed(currentHalfInning() + "" + currentInning() + ',' + currentHalfInningOuts() + ":" + inningsRequired()); String over = ""; if (isOver()) { over = " Game Over!"; } return scoreStr + inningStr + over; } // mutators // -------- /* Records that a run has been scored by the team batting during the ** current half inning (possibly resulting in a "walkoff" win by ** the home team). */ public void recordRun() { // STUB!! } /* Records that an out has occurred during the current half-inning ** (possibly resulting in that half-inning, and perhaps even the game, ** coming to an end). */ public void recordOut() { // STUB!! (First increment) } // private utility methods // ----------------------- /* Returns the String formed by placing the given string between a ** mated pair of square brackets. */ private String bracketed(String s) { return '[' + s + ']'; } }