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: 652096 • 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

import java.util.Scanner;

public class Bean{
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 < ball_number; i++)
    {
        slots_array[ball_path(slot_number)]++;
    }

    display(slots_array);     
}
public static int ball_path (int slot_number)
{
    int ball_position = (slot_number / 2);

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

        if(Math.random() * 10 <= 5)
        {
            ball_position++;

            if(ball_position >= slot_number)
            {
                ball_position--;
            }

            System.out.print("R");
        }

        else
        {
                    ball_position--;

            if(ball_position < 0)
            {
                ball_position++;
            }

            System.out.print("L");
        }
    }

    System.out.println();

    return ball_position;
}

/** display */
public static void display(int[] slots_array)
{
    int slot_height = maximum(slots_array);

    for(int i = slot_height; i > 0; i--)
    {
        for(int j = 0; j < slots_array.length; j++)
        {
            if(slots_array[j] >= 1)
            {
                System.out.print("o");
            }

            else
            {
                System.out.print("-");
            }

        }
    System.out.println();
    }
}

/** Gets the maximum number in a slot */
public static int maximum(int[] slots_array)
{
    int max = slots_array[0];

    for(int i = 1; i < slots_array.length; i++)
    {
        if(slots_array[i] > max)
        {
            max = slots_array[i];
        }
    }

return max;

}
}

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