197k views
1 vote
Create a class named BaseballGame that contains data fields for two team names and scores for each team in each of nine innings. names should be an array of two strings and scores should be a two-dimensional array of type int; the first dimension indexes the team (0 or 1) and the second dimension indexes the inning. Create get and set methods for each field. The get and set methods for the scores should require a parameter that indicates which inning’s score is being assigned or retrieved. Do not allow an inning score to be set if all the previous innings have not already been set. If a user attempts to set an inning that is not yet available, issue an error message. Also include a method named display in DemoBaseballGame.java that determines the winner of the game after scores for the last inning have been entered. (For this exercise, assume that a game might end in a tie.) Create two subclasses from BaseballGame: HighSchoolBaseballGame and LittleLeagueBaseballGame. High school baseball games have seven innings, and Little League games have six innings. Ensure that scores for later innings cannot be accessed for objects of these subtypes.

User Amiya
by
3.5k points

1 Answer

3 votes

Answer:

Check the explanation

Step-by-step explanation:

BaseballGame:

public class BaseballGame {

protected String[] names = new String[2];

protected int[][] scores;

protected int innings;

public BaseballGame() {

innings = 9;

scores = new int[2][9];

for(int i = 0; i < 9; i++)

scores[1][i] = scores[0][i] = -1;

}

public String getName(int team) {

return names[team];

}

public void setNames(int team, String name) {

names[team] = name;

}

public int getScore(int team, int inning) throws Exception

public void setScores(int team, int inning, int score) throws Exception

if(team < 0

}

HighSchoolBaseballGame:

public class HighSchoolBaseballGame extends BaseballGame {

public HighSchoolBaseballGame() {

innings = 7;

scores = new int[2][7];

for(int i = 0; i < 7; i++)

scores[1][i] = scores[0][i] = -1;

}

}

LittleLeagueBaseballGame:

public class LittleLeagueBaseballGame extends BaseballGame {

public LittleLeagueBaseballGame() {

innings = 6;

scores = new int[2][6];

for(int i = 0; i < 6; i++)

scores[1][i] = scores[0][i] = -1;

}

}

User Sebastian Nemeth
by
3.7k points