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
  • C++ Program 1. Create a class named BaseballGame that has fields for two team names and...

    C++ Program 1. Create a class named BaseballGame that has fields for two team names and a final score for each team. Include methods to set and get the values for each data field. Your application declares an array of 12 BaseballGame objects. Prompt the user for data for each object, and display all the values. Then pass each object to a method that displays the name of the winning team or “Tie” if the score is a tie. (main.cpp,...

  • 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.

  • Create a class to hold data about a high school sports team. The Team class holds...

    Create a class to hold data about a high school sports team. The Team class holds data fields for high school name (such as Roosevelt High), sport (such as Girls’ Basketball), and team name (such as Dolphins). Include a constructor that takes parameters for each field, and include get methods that return the values of the fields. Also include a public final static String named MOTTO and initialize it to Sportsmanship!. Create a class named Game. Include two Team fields...

  • 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:...

  • Write a class named TestScores. The class constructor should accept an array of test scores as...

    Write a class named TestScores. The class constructor should accept an array of test scores as its argument. The class should have a method that returns the average of the test scores. If any test score in the array is negative or greater than 100, the class should throw an IllegalArgumentException. Demonstrate the class in a program (create a Driver class in the same file). The program should ask the user to input the number of test scores to be...

  • 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...

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