Question: I have started this program, and have got most of it done, but I cannot get to the point where the batters are switched sequentially.

I have started this program, and have got most of it done, but I cannot get to the point where the batters are switched sequentially. My code is below, if needed. Feel free to add or edit. Here is the assignment.

Write a Java program that simulates a baseball game. You can choose the players and the data structure that is needed. It is important, though, that with each at-bat, the baseball player's name and number changes sequentially with the roster order chosen, and not at random. The name should change when there is a hit, when a batter is walked, or if the batter strikes out, or gets out for a pop up, etc. That said, you will need to use the random method to switch up pitches and types of hits. You can choose the appropriate methods from there to make it work. Your output for each inning should look like the following (Note: extra credit points will be given if you can save the location within the batting order to pick up with the next batter where you left after switching each inning).

Top 1st

==========

Strike

Batter No.: 24, Name: Brooks Robinson

Ball count: 0

Strike count: 1

Outs: 0

Pitch Count: 1

Runner on 0

========

Strike

Batter No.: 24, Name: Brooks Robinson

Ball count: 0

Strike count: 2

Outs: 0

Pitch Count: 2

Runner on 0

=====

Ball

Batter No.: 24, Name: Brooks Robinson

Ball count: 1

Strike count: 2

Outs: 0

Pitch Count: 3

Runner on 0

=====

Ball

Batter No.: 24, Name: Brooks Robinson

Ball count: 2

Strike count: 2

Outs: 0

Pitch Count: 4

Runner on 0

=====

Double

Batter No.: 24, Name: Brooks Robinson

Ball count: 0

Strike count: 0

Outs: 0

Pitch Count: 5

Runner on 2

=====

pop-up out

Batter No.: 34, Name: Gary Sheffield

Ball count: 0

Strike count: 0

Outs: 1

Pitch Count: 6

Runner on 2

=====

ground out

Batter No.: 54, Name: Jackie Robinson

Ball count: 0

Strike count: 0

Outs: 2

Pitch Count: 7

Runner on 2

=====

Strike

Batter No.: 27, Name: Ted Williams

Ball count: 0

Strike count: 1

Outs: 2

Pitch Count: 8

Runner on 2

=====

Strike

Batter No.: 27, Name: Ted Williams

Ball count: 0

Strike count: 2

Outs: 2

Pitch Count: 9

Runner on 2

=====

Strike

Strikeout!

3 outs [Switch sides]

I've been working on this. This is the code that I have so far for this:

///////PtichCount class//////////////

import java.util.Map;

import java.util.Map.Entry;

import java.util.Random;

import java.util.TreeMap;

public class PitchCount

{

private int pstrike;

private int pball;

private int count = 0;

private int runnerAdvance = 0;

private int outs = 0;

private int runsScored = 0;

private int runner = 0;

private int score = 0;

private int single = 0;

private int doubleD = 0;

private int triple = 0;

private int newBatter = 0;

public PitchCount()

{

}

private static final TreeMap myMap = new TreeMap<>();

static

{

final Map m = myMap;

m.put(24, "Brooks Robinson");

m.put(45, "Jackie Robinson");

m.put(34, "Gary Sheffield");

}

public void loopUntilThree()

{

while (outs < 3)

{

Random randThrows = new Random();

int mix;

mix = randThrows.nextInt(3);

switch (mix) {

case 0:

strike();

break;

case 1:

ball();

break;

case 2:

shuffleHits();

break;

}

for (Map.Entry entry : myMap.entrySet())

{

if (outs < 1)

{

System.out.println("Batter No.: " + entry.getKey() + ", Name: " + entry.getValue());

System.out.println(displayPitches());

myMap.remove(1);

break;

}

if (outs == 1)

{

// Map.Entry next =

// myMap.higherEntry(entry.getKey());

System.out.println("Batter No.: " + entry.getKey() + ", Name: " + entry.getValue());

System.out.println(displayPitches());

myMap.remove(1);

break;

}

if (outs == 2)

{

// Map.Entry next2 =

// myMap.higherEntry(entry.getKey());

System.out.println("Batter No.: " + entry.getKey() + ", Name: " + entry.getValue());

System.out.println(displayPitches());

myMap.remove(1);

break;

}

}

}

}

public void shuffleHits()

{

Random randHits = new Random();

int hits;

hits = randHits.nextInt(7);

switch (hits) {

case 0:

System.out.println("Single");

count++;

countReset();

single = runner++;

myMap.remove(1);

break;

case 1:

System.out.println("Double");

countReset();

count++;

runner = runner + 2;

myMap.remove(1);

break;

case 2:

System.out.println("Triple");

countReset();

count++;

runner = runner + 3;

myMap.remove(1);

break;

case 3:

System.out.println("Home Run!");

countReset();

count++;

runner = 0;

runsScored++;

myMap.remove(1);

break;

case 4:

System.out.println("pop-up out");

outs++;

count++;

myMap.remove(1);

countReset();

break;

case 5:

System.out.println("ground out");

outs++;

count++;

myMap.remove(1);

countReset();

break;

case 6:

System.out.println("double play");

if (outs == 2)

{

outs++;

}

else

{

outs++;

outs++;

}

count++;

myMap.remove(1);

countReset();

break;

}

if (outs == 3)

{

System.out.println("3 outs [Switch sides]" + " ");

System.exit(0);

count = 0;

}

}

public void countReset()

{

pstrike = 0;

pball = 0;

}

public void strike()

{

if (pstrike < 3)

{

System.out.println("Strike");

pstrike++;

count++;

}

if (pstrike == 3)

{

System.out.println("Strikeout!");

countReset();

outs++;

if (outs == 3)

{

System.out.println("3 outs [Switch sides]" + " ");

System.exit(0);

count = 0;

}

}

}

public void ball()

{

if (pball < 4)

{

System.out.println("Ball");

pball++;

count++;

}

if (pball == 4)

{

System.out.println("Walk!");

runnerAdvance++;

countReset();

}

else if (outs == 3)

{

System.out.println("3 outs [Switch sides]" + " ");

System.exit(0);

count = 0;

}

}

public String displayPitches()

{

return "Ball count: " + pball + " " + "Strike count: " + pstrike + " " + "Outs: " + outs + " "

+ "Pitch Count: " + count + " " + "Runner on " + runner + " ";

}

}

///////////////Tester//////////////

public class BaseballTester

{

public static void main(String[] args)

{

PitchCount pc = new PitchCount();

pc.loopUntilThree();

}

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!