6
\$\begingroup\$

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 < numberOfBalls; i++) {
            simulateDrop(slots);
        }

        //Display histogram
        for (int i = numberOfBalls; i > 0; i--) {
            for (int j = 0; j < slots.length; j++) {
                if (slots[j] >= 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 < 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]++;
    }
}
\$\endgroup\$
  • 1
    \$\begingroup\$ What is the goal here? How do you know if your program works correctly? (Does it?) \$\endgroup\$ – janos Aug 23 '15 at 6:04
  • \$\begingroup\$ @janos The question seems clear enough. The code appears to work as advertised. \$\endgroup\$ – 200_success Aug 23 '15 at 8:08
  • \$\begingroup\$ @Legate have you read the Wikipedia article? I'm not sure if your program simulates one of those (it may, but I'm not convinced). \$\endgroup\$ – mkrieger1 Aug 23 '15 at 8:24
  • 1
    \$\begingroup\$ I thing you shouldn't try to find an easy solution by writing two methods but a clean solution with OOP. Separate input, presentation and business logic. \$\endgroup\$ – Obenland Aug 23 '15 at 22:23
  • \$\begingroup\$ There are two things that seem to be off here: 1. you never touch the slotIndex in your simulateDrop method 2. you initialize your slotIndex with 0 does a ball really drop into the 0th slot if it would drop straight down passing through each nail miracoulusly? \$\endgroup\$ – Aron_dc Aug 28 '15 at 7:45
3
\$\begingroup\$

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.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.