Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

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

ID: 3675633 • Letter: C

Question

Create a class named BaseballGame that contains data fields for two team names and scores for each team in each of nine innings. 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 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 baseball games have six innings. Ensure that scores for later innings cannot be accessed for objects of these subtypes. Write three applications that each instantiate one of the object types and demonstrate their methods. Save the files as BaseballGame.java, HighSchoolBaseballGame.java, LittleLeagueBaseballGame.java, DemoBaseballGame.java, DemoHSBaseballGame.java, DemoLLBaseballGame.java.

Explanation / Answer

Before writing the program code in java,following are the imporant points to be noted:

package description

Package javax.swing Description:

-->Provides a set of "lightweight" (all-Java language) components that, to the maximum degree possible, work the same on all platforms.

Swing's Threading Policy :

--->In general Swing is not thread safe.

All Swing components and related classes, unless otherwise documented, must be accessed on the event dispatching thread.

-->Typical Swing applications do processing in response to an event generated from a user gesture.

For example, clicking on a JButton notifies all ActionListeners added to the JButton. As all events generated from a user gesture are dispatched on the event dispatching thread, most developers are not impacted by the restriction.

Where the impact lies, however, is in constructing and showing a Swing application.

Calls to an application's main method, or methods in Applet, are not invoked on the event dispatching thread. As such, care must be taken to transfer control to the event dispatching thread when constructing and showing an application or applet. The preferred way to transfer control and begin working with Swing is to use invokeLater. The invokeLater method schedules a Runnable to be processed on the event dispatching thread. The following two examples work equally well for transferring control and starting up a Swing application:

the below mentioned is Abstract Data Type(ADT) (Here,the term " Abstract" means,for which we cannot create an object)

And in the given question,they asked for creation of a base class with two subclasses(that is, parent class and child class.)

In order to inherit the properties of base class by the sub class we should use the concept of "INHERITANCE", and for that the following keyword should be used and is "implements"

   this is how the ADT follows :

Or:

This restriction also applies to models attached to Swing components. For example, if a TableModel is attached to a JTable, the TableModel should only be modified on the event dispatching thread. If you modify the model on a separate thread you run the risk of exceptions and possible display corruption.

As all events are delivered on the event dispatching thread, care must be taken in event processing. In particular, a long running task, such as network io or computational intensive processing, executed on the event dispatching thread blocks the event dispatching thread from dispatching any other events. While the event dispatching thread is blocked the application is completely unresponsive to user input. Refer to SwingWorker for the preferred way to do such processing when working with Swing.

import javax.swing.*;

public class Baseball {
     
    public static void main (String args[]) {
        int inning = 1;
        int outs = 0;
        int runs = 0;
        int randomValue;
        int team_at_bat = 1;
        int team1_score = 0;
        int team2_score = 0;
        boolean firstBase = false;
        boolean secondBase = false;
        boolean thirdBase = false;
         
        while (inning <= 9 ) {
             
            JOptionPane.showMessageDialog(null, "Batter Up!");
             
            while (outs < 3) {
                 
                randomValue = (int) (Math.random()*20) + 1;
             
                switch (randomValue) {
                     
                    case 1:
                    case 11:
                        JOptionPane.showMessageDialog(null, "Strikeout!");
                        outs++;
                        break;
                         
                    case 2:
                    case 12:
                        JOptionPane.showMessageDialog(null, "Walk!");
                        if (firstBase)
                            if (secondBase)
                                if (thirdBase)
                                    runs++;
                                else thirdBase = true;
                            else secondBase = true;
                        else firstBase = true;
                        break;
                         
                    case 3:
                        JOptionPane.showMessageDialog(null, "Hit By Pitch!");
                        if (firstBase)
                            if (secondBase)
                                if (thirdBase)
                                    runs++;
                                else thirdBase = true;
                            else secondBase = true;
                        else firstBase = true;
                        break;
                     
                    case 4:  
                    case 13:
                        JOptionPane.showMessageDialog(null, "Single!");
                        if (firstBase)
                            if (secondBase)
                                if (thirdBase)
                                    runs++;
                                else thirdBase = true;
                            else secondBase = true;
                        else firstBase = true;
                        break;
                         
                    case 5:
                    case 14:
                        JOptionPane.showMessageDialog(null, "Double!");
                        if (thirdBase) {
                            runs++;
                            thirdBase = false;
                        }         
                        if (secondBase) {
                            runs++;
                            secondBase = false;
                        }
                        if (firstBase) {
                            thirdBase = true;
                            firstBase = false;
                        }
                        secondBase = true;
                        break;
                         
                    case 6:
                        JOptionPane.showMessageDialog(null, "Triple!");
                        if (thirdBase) {
                            runs++;
                            thirdBase = false;
                        }
                        if (secondBase) {
                            runs++;
                            secondBase = false;
                        }
                        if (firstBase) {
                            runs++;
                            firstBase = false;
                        }
                        thirdBase = true;
                        break;
                    case 7:
                        JOptionPane.showMessageDialog(null, "Home Run!");
                        if (thirdBase) {
                            runs++;
                            thirdBase = false;
                        }
                        if (secondBase) {
                            runs++;
                            secondBase = false;
                        }
                        if (firstBase) {
                            runs++;
                            firstBase = false;
                        }
                        runs++;
                        break;
                    case 8:
                    case 16:
                    case 17:
                        JOptionPane.showMessageDialog(null, "Ground Out!");
                        outs++;
                        if (thirdBase) {
                            runs++;
                            thirdBase = false;
                        }
                        if (secondBase) {
                            thirdBase = true;
                            secondBase = false;
                        }
                        if (firstBase) {
                            secondBase = true;
                            firstBase = false;
                        }
                        break;
                    case 9:
                    case 18:
                    case 19:
                        JOptionPane.showMessageDialog(null, "Fly Out!");
                        outs++;
                        if (thirdBase) {
                            runs++;
                            thirdBase = false;
                        }
                        break;
                    case 10:
                    case 20:
                    case 15:
                        JOptionPane.showMessageDialog(null, "Line Out!");
                        outs++;
                        break;
                    default:
                     
                } // end of switch statement
                 
            } // ends while loop
             
            if (team_at_bat == 1) {
                team1_score += runs;
                team_at_bat = 2;
            }
            else {
                team2_score += runs;
                team_at_bat = 1;
            }
            runs = 0;
            outs = 0;
            firstBase = false;
            secondBase = false;
            thirdBase = false;
             
            JOptionPane.showMessageDialog(null, "Inning" + inning + " Team 1: " +
                team1_score + " Team 2: " + team2_score, "Score Board",  
                JOptionPane.INFORMATION_MESSAGE);
                 
            if (team1_score < team2_score && inning == 9)  
                break;
            if (team_at_bat == 1)
                inning++;
                 
             
        } // ends outer while loop
         
        System.exit(0);
         
    } // ends main method

} // ends Baseball class  

javax.swing Provides a set of "lightweight" (all-Java language) components that, to the maximum degree possible, work the same on all platforms.
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote