Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

The bean machine, also known as a quincunx or the Galton box, is a device for st

ID: 645061 • Letter: T

Question

The bean machine, also known as a quincunx or the Galton box, is a device for statistics experiments named after Francis Galton. It consists of an upright board with evenly spaced nails (or pegs) in a triangular form.

Balls are dropped from the opening 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 Java program named GaltonBox.java that simulates the bean machine. Your program should prompt the user to enter the number of the balls and the number of the slots in the machine. Simulate the falling of each ball by printing its path. Display the final buildup of the balls in the slots in a histogram. Hint: Create an array named slots. Each element in slots stores 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 RRLLLL, the ball falls into slot[2].

Sample run of the program (Note: since we are using Random to simulate the paths, your paths will be different.):

Enter the number of balls to drop: 20

Enter the number of slots in the machine: 8

LLLLRRR slot is 3

RLLLLLL slot is 1

LRRRRRR slot is 6

LRRRLRR slot is 5

RRLRLLL slot is 3

RRRRRRR slot is 7

LRRLLRR slot is 4

LLLLLRR slot is 2

RRLLRLL slot is 3

RLRLRLL slot is 3

RRLLLRR slot is 4

LLRRLRR slot is 4

RLLLRRR slot is 4

LRLRRLR slot is 4

LLLLRLL slot is 1

RRRLRRR slot is 6

LRRRLRL slot is 4

LLRRRLL slot is 3

LLLRLLL slot is 1

LLRRLRL slot is 3

0:

1: OOO

2: O

3: OOOOOO

4: OOOOOO

5: O

6: OO

7: O

Explanation / Answer

import java.util.Scanner; public class KedgeBean{ public static void main(String[] args){ Scanner input = new Scanner(System.in); System.out.print("Enter the number of balls you want drop: "); int ball_number = input.nextInt(); while(ball_number < 0) { System.out.print("Please enter a positive number: "); ball_number = input.nextInt(); } System.out.print("Enter the number of slots in the machine: "); int slot_number = input.nextInt(); while(slot_number < 0) { System.out.print("Please enter a positive number: "); slot_number = input.nextInt(); } int[] slots_array = new int[slot_number]; for(int i = 0; i
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