Question

Create a class named BaseballGame that contains data fields for two team names and scores for...

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.

0 0
Add a comment Improve this question Transcribed image text
Answer #1
Thanks for the question.

Here is the completed code for this problem.

 Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. 

If you are satisfied with the solution, please rate the answer.

 Thanks

===========================================================================

public class BaseballGame {

    //names should be an array of two strings
    protected String[] teamNames;
    //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.
    protected int[][] innings;

    public BaseballGame() {
        //names should be an array of two strings
        teamNames = new String[2];
        //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.
        innings = new int[2][9];
        // initialize the values in the array to -1 as default value
        for (int i = 0; i < 9; i++) {
            // set default value as -1
            innings[0][i] = innings[1][i] = -1;
        }
    }

    public int getInningsScore(int teamIndex, int inning) {
        if (teamIndex == 0 || teamIndex == 1) {
            if (0 <= inning && inning < innings[0].length) {
                return innings[teamIndex][inning];
            }
        }
        return -1;
    }

    public void setScore(int teamIndex, int inning, int score) {
        if (teamIndex == 0 || teamIndex == 1) {

            if (0 <= inning && inning < innings[0].length) {
                //Do not allow an inning score to be set if all the
                // previous innings have not already been set.
                for (int index = 0; index < inning; index++) {
                    if (innings[teamIndex][index] == -1) {
                        //If a user attempts to set an inning
                        // that is not yet available, issue an error message.
                        System.out.println("Previous innings values are not set.");
                        return;
                    }
                }
                innings[teamIndex][inning] = score;
            }
        }

    }
    //returns the array of names
    public String[] getTeamNames() {
        return teamNames;
    }

    //setNames
    public void setTeamNames(String teamOne, String teamTwo) {
        teamNames[0] = teamOne;
        teamNames[1] = teamTwo;
    }

    public void setTeamNames(String[] teamNames) {
        this.teamNames = teamNames;
    }
}

============================================================

public class HighSchoolBaseballGame extends BaseballGame {


    public HighSchoolBaseballGame() {
        //names should be an array of two strings
        teamNames = new String[2];
        //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.
        innings = new int[2][7];
        // initialize the values in the array to -1 as default value
        for (int i = 0; i < 7; i++) {
            // set default value as -1
            innings[0][i] = innings[1][i] = -1;
        }
    }

    public void setScore(int teamIndex, int inning, int score) {
        if (teamIndex == 0 || teamIndex == 1) {

            if (0 <= inning && inning < 7) {
                super.setScore(teamIndex, inning, score);
            }
        }

    }

    public int getInningsScore(int teamIndex, int inning) {
        if (teamIndex == 0 || teamIndex == 1) {
            if (0 <= inning && inning < 7) {
                return super.getInningsScore(teamIndex, inning);
            }
        }
        return 0;
    }
}

============================================================

public class LittleLeagueBaseballGame extends BaseballGame {

    //names should be an array of two strings
    private String[] teamNames;
    //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.
    private int[][] innings;

    public LittleLeagueBaseballGame() {
        //names should be an array of two strings
        teamNames = new String[2];
        //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.
        innings = new int[2][6];
        // initialize the values in the array to -1 as default value
        for (int i = 0; i < 6; i++) {
            // set default value as -1
            innings[0][i] = innings[1][i] = -1;
        }
    }
    public void setScore(int teamIndex, int inning, int score) {
        if (teamIndex == 0 || teamIndex == 1) {

            if (0 <= inning && inning < 6) {
                super.setScore(teamIndex, inning, score);
            }
        }

    }

    public int getInningsScore(int teamIndex, int inning) {
        if (teamIndex == 0 || teamIndex == 1) {
            if (0 <= inning && inning < 6) {
                return super.getInningsScore(teamIndex, inning);
            }
        }
        return 0;
    }
}

============================================================

import java.util.Random;

public class DemoBaseballGame {

    public static void main(String[] args) {


        Random random = new Random();
        BaseballGame game = new BaseballGame();
        String[] teams = {"Butterfly", "Dragonfly"};
        game.setTeamNames(teams);
        for (int i = 0; i < 9; i++) {

            game.setScore(0, i, random.nextInt(20));
            game.setScore(1, i, random.nextInt(20));
        }

        int teamOneTotal = 0, teamTwoTotal = 0;
        for (int i = 0; i < 9; i++) {

            teamOneTotal += game.getInningsScore(0, i);
            teamTwoTotal += game.getInningsScore(1, i);

        }
        System.out.println(game.getTeamNames()[0]+" Total Score: "+teamOneTotal);
        System.out.println(game.getTeamNames()[1]+" Total Score: "+teamTwoTotal);

        if (teamOneTotal > teamTwoTotal) {
            System.out.println(game.getTeamNames()[0] + " won the game.");
        } else if (teamTwoTotal > teamOneTotal) {
            System.out.println(game.getTeamNames()[0] + " won the game.");
        } else {
            System.out.println("Its a tie.");
        }
    }
}

============================================================

Add a comment
Know the answer?
Add Answer to:
Create a class named BaseballGame that contains data fields for two team names and scores for...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Named TennisGame that holds data about a single tennis T he class has six fields: the names of th...

    java named TennisGame that holds data about a single tennis T he class has six fields: the names of the two players, the integer final tor the players, and the String values of the final scores. Include a get or each of the six fields. Also include a set method that accepts two names, and another set method that accepts the two integer final players re values. The integer final score for a player is the number of points the...

  • In Java Problem Description/Purpose:  Write a program that will compute and display the final score...

    In Java Problem Description/Purpose:  Write a program that will compute and display the final score of two teams in a baseball game. The number of innings in a baseball game is 9.    Your program should read the name of the teams. While the user does not enter the word done for the first team name, your program should read the second team name and then read the number of runs for each Inning for each team, calculate the...

  • Write you code in a class named HighScores, in file named HighScores.java. Write a program that...

    Write you code in a class named HighScores, in file named HighScores.java. Write a program that records high-score data for a fictitious game. The program will ask the user to enter five names, and five scores. It will store the data in memory, and print it back out sorted by score. The output from your program should look approximately like this: Enter the name for score #1: Suzy Enter the score for score #1: 600 Enter the name for score...

  • Create a class named Book that contains data fields for the title and number of pages....

    Create a class named Book that contains data fields for the title and number of pages. Include get and set methods for these fields. Next, create a subclass named Textbook, which contains an additional field that holds a grade level for the Textbook and additional methods to get and set the grade level field. Write an application that demonstrates using objects of each class. Save the files as Book.java, Textbook.java, and DemoBook.java.

  • Please need help, programming in C - Part A You will need to create a struct...

    Please need help, programming in C - Part A You will need to create a struct called Team that contains a string buffer for the team name. After you've defined 8 teams, you will place pointers to all 8 into an array called leaguel], defined the following way: Team leaguel8]. This must be an aray of pointers to teams, not an array of Teams Write a function called game) that takes pointers to two teams, then randomly and numerically determines...

  • Create a class named Horse that contains data fields for the name, color, and birth year....

    Create a class named Horse that contains data fields for the name, color, and birth year. Include get and set methods for these fields. Next, create a subclass named RaceHorse, which contains an additional field that holds the number of races in which the horse has competed and additional methods to get and set the new field. Write an application that demonstrates using objects of each class. Save the files as Horse.java, RaceHorse.java, and DemoHorses.java. Program 1 Submission Template Fields:...

  • You must create a Java class named TenArrayMethods in a file named TenArrayMethods.java. This class must...

    You must create a Java class named TenArrayMethods in a file named TenArrayMethods.java. This class must include all the following described methods. Each of these methods should be public and static.Write a method named getFirst, that takes an Array of int as an argument and returns the value of the first element of the array. NO array lists. Write a method named getLast, that takes an Array of int as an argument and returns the value of the last element...

  • a) Create an abstract class Student. The class contains fields for student Id number, name, and...

    a) Create an abstract class Student. The class contains fields for student Id number, name, and tuition. Includes a constructor that requires parameters for the ID number and name. Include get and set method for each field; setTuition() method is abstract. b) The Student class should throw an exception named InvalidNameException when it receives an invalid student name (student name is invalid if it contains digits). c) Create three student subclasses named UndergradStudent, GradeStudent, and StudentAtLarge, each with a unique...

  • Create a class named Poem that contains the following fields: title - the name of the...

    Create a class named Poem that contains the following fields: title - the name of the poem (of type String) lines - the number of lines in the poem (of type int) Include a constructor that requires values for both fields. Also include get methods to retrieve field values. Create three subclasses: Couplet, Limerick, and Haiku. The constructor for each subclass requires only a title; the lines field is set using a constant value. A couplet has two lines, a...

  • Football Game Scores Write a C++ program that stores the following data about a football game...

    Football Game Scores Write a C++ program that stores the following data about a football game in a structure: Field Name Description visit_team string (name of visiting team) home_score int visit_score int The program should read the number of games from a data file named “games.txt”, dynamically allocate an array of structures for the games using one structure for each game, and then read the information for each game from the same file (visiting team, home score, visiting team’s score)....

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT