Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Java program to simulate a bean machine. Path of the ball for example would be :

ID: 3631353 • Letter: J

Question

Java program to simulate a bean machine. Path of the ball for example would be :

LRLLRRL

Main method given, please complete methods call.

Here is the program: the .... spaces are for the code

import java.util.Scanner;

public class Exercise6_21 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter the number of balls to drop: ");
int numberOfBalls = input.nextInt();

System.out.print("Enter the number of slots: ");
int numberOfSlots = input.nextInt();

int[] slots = new int[numberOfSlots];

for (int i = 0; i < numberOfBalls; i++) {
slots[onePath(numberOfSlots)]++;
}

printHistogram(slots);
}

/** Return the slot position of the ball for a path and also print
* the path */
public static int onePath(int numberOfSlots) {
int position = 0;

.
.
.
.
.
return position;
}

/** Print the histogram for the slots */
public static void printHistogram(int[] slots) {
int maxSlotHeight = max(slots);

.
.
.
.

/** Get the max element in slots */
public static int max(int[] slots) {
int result = slots[0];

.
.
.


return result;
}
}

Explanation / Answer

please rate - thanks

you didn't say where the bean starts, I started it in the middle

you didn't say what to do when it hit an edge, so I printed L or R and kept it where it was

hope this is good, message me if any problem

import java.util.Scanner;

public class Exercise6_21 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter the number of balls to drop: ");
int numberOfBalls = input.nextInt();

System.out.print("Enter the number of slots: ");
int numberOfSlots = input.nextInt();

int[] slots = new int[numberOfSlots];

for (int i = 0; i < numberOfBalls; i++) {
slots[onePath(numberOfSlots)]++;
}

printHistogram(slots);
}

/** Return the slot position of the ball for a path and also print
* the path */
public static int onePath(int numberOfSlots) {
int position = 0,j;
position=numberOfSlots/2;
for(j=0;j<numberOfSlots;j++)
if(Math.random()<=.5)
         { position--;
            if(position<0)
                 position++;
            System.out.print("L");
            }
       else
          {position++;
            if(position>=numberOfSlots)
                   position--;
            System.out.print("R");
          }
System.out.println();
return position;
}

/** Print the histogram for the slots */
public static void printHistogram(int[] slots) {
int maxSlotHeight = max(slots);
int i,j;
System.out.println(maxSlotHeight);   
for(i=maxSlotHeight;i>0;i--)
{for(j=0;j<slots.length;j++)
     if(slots[j]>=i)
         System.out.print(" o ");
    else
         System.out.print("   ");
    System.out.println();
    }
for(i=1;i<=slots.length*3;i++)
    System.out.print("-");
System.out.println();        
for(i=1;i<=slots.length;i++)
    System.out.printf("%2d ",i);
System.out.println();
}
/** Get the max element in slots */
public static int max(int[] slots) {
int result = slots[0];
int i;
for(i=1;i<slots.length;i++)
     if(slots[i]>result)
            result=slots[i];
return result;
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote