Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Code in Java. The bean machine is a device for statistical experiments. It consists of an upright board with evenly spaced nails (or pegs) in

Code in Java.

The bean machine is a device for statistical experiments. It consists of an upright board with evenly spaced nails (or pegs) in a triangular form, as shown in Figure 7.13 from our assigned textbook.

image text in transcribed

Balls are dropped from the opening at the top of the board. Every time a ball hits a nail, it has a 50% chance of falling to the left or to the right. The piles of balls are accumulated in the slots at the bottom of the board.

Write a program to simulate the bean machine that has 8 slots as shown in the figure. Your program should prompt the user to enter the number of balls to drop. Simulate the falling of each ball by printing its path. For example, the path for the ball in Figure 7.13(b) is LLRRLLR and the path for the ball in Figure 7.13(c) is RLRRLRR. Note that there are 7 levels of nails, so your path should be 7 letters (not 8).

Create an array called slots. Each element in slots store the number of balls in a slot. Each ball falls into a slot via a path. The number of Rs in a path is the position of the slot where the ball falls. For example, for the path LRLRLRR, the ball falls into slots[4] and for the path RRLLLLL, the ball falls into slots[2].

You should create a method to randomly drop a ball and return the position in the array that the ball dropped into. You will pass in the number of slots as that will determine how many levels of nails the ball must pass through (numberOfSlots-1). This method should also print out the path that the ball took (e.g. LRLLRLL).

Here is the suggested method header:

public static int dropBall(int numberOfSlots)

- Generate the path the ball took using the random method from the Math class

- Print the path on the screen

- Return the slot number the ball fell into

Display the final buildup of the balls in the slots using a histogram. Create a method with the following header:

public static void printGameResults(int[] slots)

Below is a sample run of my program using 10 balls and the desired output. Note that the slot number displayed is +1 more than the array index which is zero-based. In this example, 6 balls fell into slot 3 (index 2 in the array, the path would have two Rs and five Ls).

image text in transcribed

Finally, utilize a do-while loop to allow the user to try the program again (without having to re-run the program from your IDE), displaying the following message (exactly as shown) after displaying the results of the game:

// Ask user if they want to play again

System.out.print(" Do you want to play again (y)? ");

A1.2 Bean Machine The bean machine is a device for statistical experiments. It consists of an upright board with evenly spaced nails (or pegs) in a triangular form, as shown in Figure 7.13 from our assigned textbook. Figure 7.I3 Each ball takes a random path and falls into a slot. Balls are dropped from the opening at the top of the board. Every time a ball hits a nail, it has a 50% chance of falling to the left or to the right. The piles of balls are accumulated in the slots at the bottom of the board. Write a program to simulate the bean machine that has 8 slots as shown in the figure. Your program should prompt the user to enter the number of balls to drop. Simulate the falling of each ball by printing its path. For example, the path for the ball in Figure 7.13(b) is LLRRLLR and the path for the ball in Figure 7.13(c) is RLRRLRR. Note that there are 7 levels of nails, so your path should be 7 letters (not 8 ). Welcome to the Bean Game! You will drop balls in the Beam Game and they will hit a series of nails and fall into one of 8 slots. After all the balls are dropped, the resulting accumulation of balls in each slot will be displayed for you to see! Enter the number of balls to drop: 10 Ball 1: R L L R R R R Ball 2: L L L L L R R Ball 3: L L L L L R R Ball 4: L L L R L R L Ball 5: L L R L R R L Ball 6: L L L R L R L Ball 7: L R L L L R L Ball 8: R L L L L L L Ball9:Ball10:LLRLLRLRRRRLLR Ball 10: R R R R L L R Game results

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Spomenik Monument Database

Authors: Donald Niebyl, FUEL, Damon Murray, Stephen Sorrell

1st Edition

0995745536, 978-0995745537

More Books

Students also viewed these Databases questions