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: 651665 • 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 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].

Has to use two dimensional array

****Code has to use Math.random() method*****

Output has to look like the picture: (input is only 5 and 8... output is all the LRLRLRLRR stuff, plus the slots)

Enter the number of balls to drop: 5 Ente Enter the number of slots in the bean machine: 8 LRLRLRR RRLLLRR LLRLLRR number of rs slots in the bear Enter LRLRRLR IIIIoII II101o1oL 1I1 I I 101010I I I

Explanation / Answer

Program:

import java.util.Scanner;

public class BeanMachine {

public static void main(String[] args) {

Scanner input = new Scanner(System.in);

System.out.print("Enter the number of balls to drop: ");

int numberOfBalls = input.nextInt();

System.out.print("Enter the number of slots: ");

int numberOfSlots = input.nextInt();

int[] slots = new int[numberOfSlots+1];

for (int i = 0; i < numberOfBalls; i++) {

slots[onePath(numberOfSlots)]++;

}

printHistogram(slots);

}

public static int onePath(int numberOfSlots) {

int direction;

int right = 0;

for(int i = 1; i <= numberOfSlots; i++) {

direction = (int)(Math.random() * 2);

if (direction == 0)

System.out.print("L");

else

System.out.print("R");

right++;

}

System.out.println();

return right;

}

public static void printHistogram(int[] slots) {

int maxSlotHeight = 0;

for(int m = 0; m < slots.length; m++){

maxSlotHeight = max(slots);

for(int i = 0; i <= maxSlotHeight; i++){

if(slots[i] > 1)

System.out.println("0");

else

System.out.print(" ");

}

}

}   

public static int max(int[] slots) {

int result = slots[0];

for(int i = 0; i< slots.length; i++){

if(slots[i] > result)

result = slots[i];
}

return result;

}

}

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