Bean Machine Simulation I could not find a solution from my book's website (student-level access) but I feel that I am missing something that I'm supposed to learn from this exercise.

Instructions: 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. 

Book's example:




Enter the number of balls to drop: 5
Enter the number of slots in the bean machine: 7

LRLRLRR 

RRLLLRR 

LLRLLRR 

RRLLLLL 

LRLRRLR

        0
        0
      000



My attempt:



import java.util.*;

public class BeanMachine {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);

        System.out.print("Enter number of balls to drop: ");
        int numberOfBalls = input.nextInt();
        System.out.print("Enter the number of slots in the bean machine: ");
        int numberOfSlots = input.nextInt();

        //Simulate drops
        int[] slots = new int[numberOfSlots];
        for (int i = 0; i &lt; numberOfBalls; i++) {
            simulateDrop(slots);
        }

        //Display histogram
        for (int i = numberOfBalls; i &gt; 0; i--) {
            for (int j = 0; j &lt; slots.length; j++) {
                if (slots[j] &gt;= i) {
                    System.out.print("0");
                }
                else {
                    System.out.print(" ");
                }
            }
            System.out.println();
        }
    }

    public static void simulateDrop(int[] slots) {
        StringBuilder path = new StringBuilder(slots.length);
        int slotIndex = 0;
        int numberOfNails = slots.length - 1;
        for (int i = 0; i &lt; numberOfNails; i++) {
            int direction = (int)(Math.random() * 2);

            if (direction == 0) {
                path.append("L");
            }
            else {
                path.append("R");
                slotIndex++;
            }
        }

        System.out.println(path.toString());
        slots[slotIndex]++;
    }
}
Java has good support for OOP techniques - Using these will allow you to create a more elegant solution. Your code could be refactored with the correct use of these techniques to produce something of the form:

PegMachine sampleTest = PegMachine(500,100);
sampleTest.histogram;


This approach hides the complexities of the program and allows you to conduct tests by simply creating a PegMachine specifying the number of pegs and balls for each sample.

I noticed Aron_dc pointed out the slotIndex variable in your simulateDrop method is never used - This can be removed as it does not contribute anything to your program.

Out of curiosity I conducted a larger sample using your code to see if it produced the correct normal-distribution curve. The results were as expected; confirming your codes correctness.

Besides suggesting the use of object orientation your code seems well written and correct.