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: 3673363 • 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. Save the file as BaseballGame.java

Explanation / Answer

DemoBaseballGame.java

import java.util.*;
public class DemoBaseballGame
{
   public static void main(String[] args)
   {
      BaseballGame bbGame = new BaseballGame();
      bbGame.setNames("Red Sox", "Yankees");
      System.out.println(" At start");
      display(bbGame);     
      bbGame.setScore(0, 0, 2);
      bbGame.setScore(1, 0, 0);
      System.out.println(" After setting first inning");
      display(bbGame);
      System.out.println(" Trying to set fifth inning (inning 4) too soon");
      bbGame.setScore(0, 4, 1);
      System.out.println(" After setting second inning scores");
      bbGame.setScore(0, 1, 1);
      bbGame.setScore(1, 1, 1);
      display(bbGame);
      System.out.println(" Tryng to use an invalid inning");
      bbGame.setScore(0, 9, 2);
      System.out.println(" Trying to use an invalid team");
      bbGame.setScore(2, 2, 2);
      System.out.println(" After setting the rest of the innings");
      for(int t = 0; t < 2; ++t)
         for(int i = 2; i < bbGame.getInnings(); ++i)
            bbGame.setScore(t, i, 0);
      display(bbGame);
   }
   public static void display(BaseballGame b)
   {
      int[] total = {0, 0};
      boolean areInningsLeft = false;
      int score;
      System.out.println("Game between " + b.getNames());
      for(int t = 0; t < 2; ++t)
      {
         for(int i = 0; i < b.getInnings(); ++i)
         {
             score = b.getScore(t, i);
             if(score != BaseballGame.UNPLAYED)
             {
               if(i == 0)
                  System.out.print("Team " + t + ": ");
               System.out.print(score + " ");
               total[t] += score;
             }
             else
               areInningsLeft = true;
         }
         System.out.println();
      }
      if(b.getScore(0, 0) == BaseballGame.UNPLAYED && b.getScore(1, 0) == BaseballGame.UNPLAYED)
         System.out.println("No innings played yet.");
      if(!areInningsLeft)
      {
         if(total[0] > total[1])
            System.out.println(b.getName(0) + " win!");
         else
            if(total[0] < total[1])
               System.out.println(b.getName(1) + " win!");
            else
               System.out.println("Tie Game!");
      }
   }
}


BaseballGame.java

public class BaseballGame
{
   private String[] names = new String[2];
   protected int[][] scores;
   public static final int UNPLAYED = -99;
   private final int INNINGS = 9;
   public BaseballGame()
   {
      scores = new int[2][INNINGS];
      for(int t = 0; t < scores.length; ++t)
         for(int i = 0; i < scores[t].length; ++i)
            scores[t][i] = UNPLAYED;
   }
   public void setNames(String n1, String n2)
   {
      names[0] = n1;
      names[1] = n2;
   }
   public void setScore(int team, int inning, int score)
   {
      int x;
      boolean isBadTeam = false;
      boolean isBadInning = false;
      if(team < 0 || team > scores.length - 1)
          System.out.println("****** " + team + " is not a valid team value");
      else
         if(inning < 0 || inning > scores[0].length - 1)
            System.out.println("****** " + inning + " is not a valid inning value");
         else
         {
            for(x = 0; x < inning; ++x)
               if(scores[team][x] == UNPLAYED)
                  isBadInning = true;
            if(isBadInning)
               System.out.println("****** A score cannot yet be set for inning " + inning);
            else
               scores[team][inning] = score;
         }
   }
   public String getNames()
   {
      return names[0] + " vs. " + names[1];
   }
   public String getName(int pos)
   {
      return names[pos];
   }
   public int getScore(int team, int inning)
   {
      int score = -99;
      if(team < 0 || team > scores.length - 1)
         System.out.println(team + " is an invalid team number");
      else
         if(inning < 0 || inning > scores[0].length - 1)
            System.out.println(inning + " is an invalid inning");
         else
            score = scores[team][inning];
      return score;
   }
   public int getInnings()
   {
      return INNINGS;
   }
}


output

At start                                                                                                                                                    
Game between Red Sox vs. Yankees                                                                                                                            
                                                                                                                                                            
                                                                                                                                                            
No innings played yet.                                                                                                                                      
                                                                                                                                                            
After setting first inning                                                                                                                                  
Game between Red Sox vs. Yankees                                                                                                                            
Team 0: 2                                                                                                                                                   
Team 1: 0                                                                                                                                                   
                                                                                                                                                            
Trying to set fifth inning (inning 4) too soon                                                                                                              
****** A score cannot yet be set for inning 4                                                                                                               
                                                                                                                                                            
After setting second inning scores                                                                                                                          
Game between Red Sox vs. Yankees                                                                                                                            
Team 0: 2 1                                                                                                                                                 
Team 1: 0 1                                                                                                                                                 
                                                                                                                                                            
Tryng to use an invalid inning                                                                                                                              
****** 9 is not a valid inning value                                                                                                                        
                                                                                                                                                            
Trying to use an invalid team                                                                                                                               
****** 2 is not a valid team value                                                                                                                          
                                                                                                                                                            
After setting the rest of the innings                                                                                                                       
Game between Red Sox vs. Yankees                                                                                                                            
Team 0: 2 1 0 0 0 0 0 0 0                                                                                                                                   
Team 1: 0 1 0 0 0 0 0 0 0                                                                                                                                   
Red Sox win!                                                                                                                                                

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