Question

In: Computer Science

Problem Description: Game: Bean Machine or Galton Box To figure out if the ball falls to...

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:

  1. A PDF with descriptions of Analysis, Design, and Testing. Must have a title with your name and assignment number. (Do not submit a Word document!!!!)
  2. Error free Java source program (Programs with syntax errors receive 0 credit) with methods, proper comments and indentation. Java source program is the program you wrote with .java file extension. (Do not submit a Word or any other document of your code!!!!)
  3. The Program outputs for the 4 different test cases in a PDF

Solutions

Expert Solution

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:


Related Solutions

ROULETTE is a casino game where a numbered wheel spins and a steel ball falls into...
ROULETTE is a casino game where a numbered wheel spins and a steel ball falls into a location marked by one particular colored number. In the United States there are 18 locations colored red, 18 locations colored black and 2 locations colored green. The red and black locations are numbered 1-36 and the green locations are labeled "0" and "00" as shown in the picture to the right. The wheel therefore has 38 locations in total. Note that the odd...
ROULETTE is a casino game where a numbered wheel spins and a steel ball falls into...
ROULETTE is a casino game where a numbered wheel spins and a steel ball falls into a location marked by one particular colored number. In the United States there are 18 locations colored red, 18 locations colored black and 2 locations colored green. The red and black locations are numbered 1-36 and the green locations are labeled "0" and "00" as shown in the picture to the right. The wheel therefore has 38 locations in total. Note that the odd...
ROULETTE is a casino game where a numbered wheel spins and a steel ball falls into...
ROULETTE is a casino game where a numbered wheel spins and a steel ball falls into a location marked by one particular colored number. In the United States there are 18 locations colored red, 18 locations colored black and 2 locations colored green. The red and black locations are numbered 1-36 and the green locations are labeled "0" and "00" as shown in the picture to the right. The wheel therefore has 38 locations in total. Note that the odd...
ROULETTE is a casino game where a numbered wheel spins and a steel ball falls into...
ROULETTE is a casino game where a numbered wheel spins and a steel ball falls into a location marked by one particular colored number. In the United States there are 18 locations colored red, 18 locations colored black and 2 locations colored green. The red and black locations are numbered 1-36 and the green locations are labeled "0" and "00" as shown in the picture to the right. The wheel therefore has 38 locations in total. Note that the odd...
You are covering a soccer game when while heading a ball an athlete falls grabbing the...
You are covering a soccer game when while heading a ball an athlete falls grabbing the back of their lower leg. You are concerned they may have strained their calf. What would the athlete complain of (symptoms) and what would you observe (signs) if they had suffered a 2nd degree strain? What are some predisposing factors that may have put the athlete at risk for this injury? For a 2nd degree strain, what positive tests would you expect to find...
Suppose that a person plays a game in which he draws a ball from a box...
Suppose that a person plays a game in which he draws a ball from a box of 10 balls numbered 0 through 9. He then puts the ball back and continue to draw a ball (with replacement) until he draws another number which is equal or higher than the first draw. Let X denote the number drawn in the first draw and Y denote the number of subsequent draws needed. (a) Find the conditional probability distribution of Y given X...
A machine is set to turn out ball bearings having a radius of 1 centimeter. A...
A machine is set to turn out ball bearings having a radius of 1 centimeter. A sample of 10 ball bearings produced by this machine has a mean radius of 1.004 centimeters with a standard deviation of 0.003 centimeters. Is there any reason to suspect the machine is turning out ball bearings having a mean radius different than 1 centimeter?
I can't figure this problem out! The problem is: Show the relationship between income and crime...
I can't figure this problem out! The problem is: Show the relationship between income and crime using indifference curves.
Part 3: Ball rolling down ramp: Loss of energy due to (you figure out what)   In...
Part 3: Ball rolling down ramp: Loss of energy due to (you figure out what)   In the projectile motion lab, a ball was placed at the top of a ramp. In one case, its initial height was 12.0 cm above the table top, and it rolled off the table with velocity v =  1.12 m/s.  Compute the initial potential energy relative to the height of the table top, final kinetic energy as it leaves the table top, and % energy lost.   Mass...
PYTHON CODING: In this problem, we will figure out if two balls are colliding. We will...
PYTHON CODING: In this problem, we will figure out if two balls are colliding. We will think in 2D to simplify things, though 3D isn’t different conceptually. For calculating collision, we only care about a ball’s position in space, as well as its size. We can store a ball’s position with the (x, y) coordinates of its center point, and we can calculate its size if we know its radius. Thus, we represent a ball in 2D space as a...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT