Question: In this assignment, write a Java class to simulate part of a game of baseball, using simplified rules. There should be a function that chooses

In this assignment, write a Java class to simulate part of a game of baseball, using simplified rules. There should be a function that chooses at random whether a hit is a single, double, triple, home-run or bunt. When a runner reaches home base, the score should go up.

The logic for the baserunners is as follows: A base hit is worth N bases where 1 N 4. The batter becomes a runner and advances N bases in the base sequence (example: in a 3-base hit, the batter becomes a runner and advances to Third Base.) In addition, all runners who currently occupy bases also advance N bases in the base sequence (example: in a 2-base hit, a runner on First Base advances to Third Base.) Any runner (including the batter) who advances to Home Base scores a run.

There should be a method called hit(), which randomizes the hits, and has a series of if statements. You may use a boolean array or another data structure to keep track of bases as occupied or not.

The output should look like the following:

Double

runs = 0, occupied bases: 2 (2nd base)

==================

Single

runs = 0, occupied bases: 1 3 (1st and 3rd)

==================

Single

runs = 1, occupied bases: 1 2 (1st and 2nd)

==================

Single

runs = 1, occupied bases: 1 2 3 (1st, 2nd, and 3rd)

==================

Triple

runs = 4, occupied bases: 3 (3rd)

==================

Home run

runs = 5, occupied bases: none

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!