write a program that simulates the bean machine. your program will prompt the us
ID: 649675 • Letter: W
Question
write a program that simulates the bean machine. your program will prompt the user to enter the number of balls and the number of slots in the machine. Simulate the fall of each ball by printing its path. For example the path for the ball in the figure above is LLRRRLR. Display the final buildup of the balls in the slots in a histogram.
EXERCISE HAS TO LOOK AS THE EXAMPLE. BELLOW
HAS TO RUN WITH 2 DIMENSIONAL ARRAY
Enter the number of balls to drop: 5
Enter the number of slots in the bean machine: 8
LRLRLRR
RRLLLRR
LLRLLRR
RRLLLLL
LRLRRLR
| | | | | | | | |
| | | | | | | | |
| | | | |0 | | | |
| | | | |0 | | | |
| | |0|0|0| | | |
Explanation / Answer
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
public class BeanMachine {
public static void main(String[] args) throws Exception {
System.out.println(" Enter the number of balls ");
String noOfBalls = args[0];
System.out.println("Enter the number of slots");
String noOfSlots = args[1];
Random r = new Random();
String alphabet = "LR";
// Prints 50 random characters from alphabet
for (int i = 0; i < noOfBalls; i++)
{
System.out.println(alphabet.charAt(r.nextInt(noOfSlots)));
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.