|
| 1 | +/** |
| 2 | + * (Game: bean machine) The bean machine, also known as a quincunx or the Galton |
| 3 | + * box, is a device for statistics experiments named after English scientist Sir |
| 4 | + * Francis Galton. It consists of an upright board with evenly spaced nails (or pegs) |
| 5 | + * in a triangular form, as shown in Figure 7.13. |
| 6 | + * |
| 7 | + * Balls are dropped from the opening of the board. Every time a ball hits a nail, it |
| 8 | + * has a 50% chance of falling to the left or to the right. The piles of balls are accumulated |
| 9 | + * in the slots at the bottom of the board. |
| 10 | + * Write a program that simulates the bean machine. Your program should prompt the |
| 11 | + * user to enter the number of the balls and the number of the slots in the machine. |
| 12 | + * Simulate the falling of each ball by printing its path. For example, the path for |
| 13 | + * the ball in Figure 7.13b is LLRRLLR and the path for the ball in Figure 7.13c is |
| 14 | + * RLRRLRR. Display the final buildup of the balls in the slots in a histogram. Here |
| 15 | + * is a sample run of the program: |
| 16 | + * (Hint: Create an array named slots. Each element in slots stores the number |
| 17 | + * of balls in a slot. Each ball falls into a slot via a path. The number of Rs in a path |
| 18 | + * is the position of the slot where the ball falls. For example, for the path LRLRLRR, |
| 19 | + * the ball falls into slots[4], and for the path RRLLLLL, the ball falls into |
| 20 | + * slots[2].) |
| 21 | + * |
| 22 | + * Enter the number of balls to drop: 5 |
| 23 | + * Enter the number of slots in the bean machine: 8 |
| 24 | + * LRLRLRR |
| 25 | + * RRLLLRR |
| 26 | + * LLRLLRR |
| 27 | + * RRLLLLL |
| 28 | + * LRLRRLR |
| 29 | + * O |
| 30 | + * O |
| 31 | + * OOO |
| 32 | + * Created by Sven on 08/29/19. |
| 33 | + */ |
| 34 | +package Chapter07; |
| 35 | + |
| 36 | +import com.sun.scenario.effect.impl.sw.sse.SSEBlend_SRC_OUTPeer; |
| 37 | + |
| 38 | +import java.util.Scanner; |
| 39 | + |
| 40 | +public class Exercise0737_Game_bean_machine { |
| 41 | + public static void main(String[] args) { |
| 42 | + Scanner input = new Scanner(System.in); |
| 43 | + System.out.print("Enter the number of balls to drop: "); |
| 44 | + int numOfBalls = input.nextInt(); |
| 45 | + System.out.print("Enter the number of slots in the bean machine: "); |
| 46 | + int numOfSlots = input.nextInt(); |
| 47 | + |
| 48 | + int[] slots = new int[numOfSlots]; |
| 49 | + for (int i = 0; i < numOfBalls; i++) { |
| 50 | + int rightCount = 0; |
| 51 | + for (int j = 0; j < numOfSlots - 1; j++) { |
| 52 | + boolean isRight = Math.random() > 0.5; |
| 53 | + System.out.print(isRight ? "R" : "L"); |
| 54 | + if (isRight) { |
| 55 | + rightCount++; |
| 56 | + } |
| 57 | + } |
| 58 | + System.out.println(); |
| 59 | + slots[rightCount]++; |
| 60 | + } |
| 61 | + System.out.println(); |
| 62 | + |
| 63 | + display(slots); |
| 64 | + } |
| 65 | + |
| 66 | + public static void display(int[] slots) { |
| 67 | + int maxHeight = slots[0]; |
| 68 | + for (int i = 0; i < slots.length; i++) { |
| 69 | + if (slots[i] > maxHeight) { |
| 70 | + maxHeight = slots[i]; |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + for (int i = maxHeight; i > 0; i--) { |
| 75 | + for (int j = 0; j < slots.length; j++) { |
| 76 | + if (slots[j] == i) { |
| 77 | + System.out.print("0"); |
| 78 | + slots[j]--; |
| 79 | + } else { |
| 80 | + System.out.print(" "); |
| 81 | + } |
| 82 | + } |
| 83 | + System.out.println(); |
| 84 | + } |
| 85 | + } |
| 86 | +} |
0 commit comments