In: Computer Science
Problem Description: Game: Bean Machine or Galton Box
To figure out if the ball falls to Left or Right, you can generate a random number using Math.random(). If this random number is greater than or equal to 0.5 we assume the ball falls to the Right otherwise it falls to the Left (or vise versa).
If there are 8 slots, the ball should hit 7 nails (8 -1), thus you should run a loop 7 times to figure out the path for that ball. If you have N balls, this whole process should be repeated N times. Here is a basic algorithm.
# of balls = N
# of slots = K
array of K elements
for ( i = 0 to N) {
int R = 0;
for (j = 0 to K-1) {
if ( random number >= 0.5)
R++;
}//end loop j
array[R] ++;
}// end loop i
Output the array to show how many balls are in each slot.
Things to DO!!!!!
Analysis: (3 points)
(Describe the problem including inputs and outputs in your own words.)
Design: (3 points)
(Describe the major steps in your algorithm for solving the problem.)
Coding: Write a well documented and properly indented Java source program. Your program should have a block comment with your name and a statement of purpose at the very top. Use meaningful variable names. You must have proper labels and informative statements for all inputs and outputs. (10 points)
Testing: (Describe how you test this program, you should use your own input data to test not just the test cases given in sample runs.) (4 points)
Test your program according to following test schedule and generate output to show the number of balls in each slot.
N | K |
10 | 8 |
50 | 10 |
100 | 20 |
500 | 30 |
What to Submit:
Program:=
import java.util.*;
public class gbox
{
public static void main(String[] args)
{
Scanner input = new
Scanner(System.in);
// user to enter the number of
the balls
System.out.print("Enter
the number of balls to drop: ");
int numberOfBalls =
input.nextInt();
String[] ballPaths = new
String[numberOfBalls];
// user to enter the
number of slots
System.out.print("Enter
the number of slots in the bean machine: ");
int numberOfSlots =
input.nextInt();
int[] slots = new
int[numberOfSlots];
// display ball
paths
for (int i = 0; i <
numberOfBalls; i++) {
ballPaths[i] = dropBall(slots);
System.out.printf("%10s\n", ballPaths[i]);
}
System.out.println("");
printArray(slots,
numberOfBalls);
}
//dropBall method returns the string L or R
randomly
public static String dropBall(int[] slot)
{
StringBuilder ballPath = new StringBuilder();
for
(int i = 0; i < slot.length - 1; i++)
{
int random = (int)(Math.random() * 10) % 2;
if (random >=0.5)
ballPath.append("R");
else
ballPath.append("L");
}
int position
= getBallPosition(ballPath.toString(), 'R');
slot[position]++;
return
ballPath.toString();
}
public static int getBallPosition(String str,
char a)
{
int count = 0;
for (int i = 0; i <
str.length(); i++)
{
if (str.charAt(i) == a) count++;
}
return count;
}
public static void printArray(int[] slots, int
numberOfBalls)
{
while (!isEmpty(slots))
{
if (isRowEmpty(slots, numberOfBalls))
{
numberOfBalls--;
continue;
}
for (int i = 0; i < slots.length; i++)
{
if (slots[i] >= numberOfBalls)
{
System.out.printf("%2c", 'O');
slots[i]--;
}
else System.out.printf("%2c", ' ');
}
numberOfBalls--;
System.out.println("");
}
}
public static boolean isEmpty(int[]
slots)
{
for (int slot :
slots)
{
if (slot != 0)
{
return false;
}
}
return true;
}
public static boolean isRowEmpty(int[] slots,
int rowNum)
{
for (int slot :
slots)
{
if (slot == rowNum)
{
return false;
}
}
return true;
}
}
Output: