Write a Java application which tests the random number generation abilities of J
ID: 3536512 • Letter: W
Question
Write a Java application which tests the random number generation abilities of Java. Random number generation is explained below.Your application should simulate rolling a pair of dice some number of times. Use a JOptionPane to ask the user how many times the dice should be rolled. Each dice roll can be a value between 1 and 6, so rolling 2 dice will result in values between 2 and 12. You need to generate a random number between 1 and 6 to simulate rolling each dice. Keep track of the result of each roll using an array that is indexed by the sum of the roll of the two dice. Output your result in a table which shows each value (ie. 2 %u2013 12) and the number of times that value was rolled.
The program must have the following structure:
For output, you should build a formatted string which includes tabs and new lines to represent your table. JOptionPanes by themselves are not capable of properly formatting tabs. However, there is another Java class which can be used with JOptionPanes to correctly display a formatted string. The JTextArea class understands all string formatting instructions. The following illustrates how to use the JTextArea class with a JOptionPane to display a formatted string.
#import javax.swing.JOptionPane;
#import javax.swing.JTextArea;
String output = %u201CThis is a formatted string. %u201D;
// Create a new JTextArea object
JTextArea area = new JTextArea( );
// Place a formatted string into the JTextArea
area.setText( output );
// Place the text area into the JOptionPane
JOptionPane.showMessageDialog( null, area, %u201CTitle for dialog box%u201D, JOptionPane.INFORMATION_MESSAGE );
For each of the two programming problems, create an Eclipse project and develop a Java program to solve the problem. Make sure to capture a sample of your program%u2019s output. The best way to do this is to click on the console window you want to capture and then press the Alt and PrintScreen keys at the same time. Then paste your captured screen image into a Word document. For each of the two programs, put the screen capture followed by a copy of your source code into your Word document.
Random Numbers in Java
There are two different classes available to generate random numbers in Java. The static method random from the Math class can be used to generate a random floating point number from 0 up to but not including 1. This number can be scaled to whatever range of random numbers is desired.
(int)(Math.random( ) * 10); //results in an integer between 0 and 9
The Random class from the java.util package can also be used to get random numbers. The nextInt method returns an integer from 0 up to but not including the argument value provided to the nextInt method.
Random rand = new Random( );
int number = rand.nextInt(10); //results in an integer between 0 and 9
minimumValue + differenceBetweenValues * rand.nextInt( scalingFactor )
The formula shown above is used to generate a range of random numbers starting at some minimum value and generating some specified number of random values. The scalingFactor determines how many different random values will be generated by the call to nextInt, starting with the value 0. The difference between values is usually 1.
Explanation / Answer
import java.util.Random;
import javax.swing.JOptionPane;
import javax.swing.JTextArea;
public class TwoDiceRoll {
public static void main(String[] args) {
int rolls=-1;
String str;
int[] counts=new int[11];
//get number of rolls
do{
str=JOptionPane.showInputDialog(null,"Enter the number of rolls(must be positive): ","1000");
if(str!=null)
rolls=Integer.parseInt(str);
}while(rolls<0);
counts=Toss(rolls);
String output = "Sum Frequency ";
for(int i=0;i<counts.length;i++){
float n= (float) (counts[i]*100.0/(float)rolls);
output+=(i+2)+" "+ n+"% ";
}
// Create a new JTextArea object
JTextArea area = new JTextArea( );
// Place a formatted string into the JTextArea
area.setText( output );
// Place the text area into the JOptionPane
JOptionPane.showMessageDialog( null, area, ("Results of "+rolls+" tosses"), JOptionPane.PLAIN_MESSAGE);
}//end main
public static int[] Toss(int tosses){
int sum;
int dice1,dice2;
int[] counts=new int[11];
Random rand=new Random();
//roll dice tossess times
for(int i=0;i<tosses;i++){
dice1=rand.nextInt(6)+1;
dice2=rand.nextInt(6)+1;
sum=dice1+dice2;
counts[sum-2]++;
}
return counts;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.