Create a class named BaseballGame that contains data fields for two team names a
ID: 3673683 • 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
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
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.