This was the JAVA assignment. I have a functioning code, but not sure how to add
ID: 3837237 • Letter: T
Question
This was the JAVA assignment. I have a functioning code, but not sure how to add in code to find the MAX random number of dice rolled. I will also attach my code so you can determine what specifically i need to add.
EGR 140: Computer Programming
Assignment # 10:
Assigned: Monday, 6 March 2017
Due: Friday, 10 March 2017
Points: 20
We want a program that generates a simple histogram based on the roll of a single die. Write an error-free Java program to do the following things.
Generate 18 random numbers that are integers from 1 to 6. (Use a loop to generate the numbers.) The program only needs to generate one random number at a time. The number represents a simulated die roll.
Use an array to count how many times each die roll occurs.
Display how many times each die roll occurred. That is, display how many times a 1 happened, a 2 happened, etc.
Determine which die roll occurred most often. One can ignore ties; that is, if two or more numbers occur the same amount, just specify one of the numbers.
Sample output:
MM§MNumber of 1 die rolls = 3
MM§MNumber of 2 die rolls = 3
MM§MNumber of 3 die rolls = 4
MM§MNumber of 4 die rolls = 3
MM§MNumber of 5 die rolls = 2
MM§MNumber of 6 die rolls = 3
MM§M3 had the most simulated tosses
MM§M
Remember to put the usual header at the top of the program and to submit via Canvas.
public class DiceRolling
{
public static void main( String[] args)
{
Random randomNumbers = new Random(); // Generates random numbers
int[] array = new int[ 7 ]; // Declares the array
//Roll the die 18 times
for ( int roll = 1; roll <=18; roll++ )
++array[ 1 + randomNumbers.nextInt ( 6 ) ];
System.out.printf( "%s%10s ", "Face"," Number of Rolls" );
// outputs array values
for ( int face = 1; face < array.length; face++ )
System.out.printf( "%4d%10d ", face, array[ face ] );
} // end main
} // end class DiceRolling
Explanation / Answer
HI, Please find my implemntation.
import java.util.Random;
public class DiceRolling
{
public static void main( String[] args)
{
Random randomNumbers = new Random(); // Generates random numbers
int[] array = new int[ 7 ]; // Declares the array
//Roll the die 18 times
for ( int roll = 1; roll <=18; roll++ )
++array[ 1 + randomNumbers.nextInt ( 6 ) ];
System.out.printf( "%s%10s ", "Face"," Number of Rolls" );
// outputs array values
for ( int face = 1; face < array.length; face++ )
System.out.printf( "%4d%10d ", face, array[ face ] );
int maxRoll = 1;
for ( int face = 1; face < array.length; face++ )
if(array[face] > array[maxRoll])
maxRoll = face;
System.out.println(maxRoll+" has rolled maximum "+array[maxRoll]+" times");
} // end main
} // end class DiceRolling
/*
Sample run:
Face Number of Rolls
1 3
2 2
3 3
4 2
5 3
6 5
6 has rolled maximum 5 times
*/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.