Question: Code Listing 4.1 (DiceSimulation.java) import java.util.Random; // Needed for the Random class /** This class simulates rolling a pair of dice 10,000 times and counts

 Code Listing 4.1 (DiceSimulation.java) import java.util.Random; // Needed for the Randomclass /** This class simulates rolling a pair of dice 10,000 timesand counts the number of times doubles of are rolled for each

Code Listing 4.1 (DiceSimulation.java)

import java.util.Random; // Needed for the Random class

/**

This class simulates rolling a pair of dice 10,000 times and counts the number of times doubles of are rolled for each different pair of doubles.

*/

public class DiceSimulation

{

public static void main(String[] args)

{

final int NUMBER = 10000; // Number of dice rolls

// A random number generator used in

// simulating the rolling of dice Random generator = new Random();

int die1Value; // Value of the first die

int die2Value; // Value of the second die

int count = 0; // Total number of dice rolls

int snakeEyes = 0; // Number of snake eyes rolls

int twos = 0; // Number of double two rolls

int threes = 0; // Number of double three rolls

int fours = 0; // Number of double four rolls

int fives = 0; // Number of double five rolls

int sixes = 0; // Number of double six rolls

// TASK #1 Enter your code for the algorithm here

// Display the results

System.out.println ("You rolled snake eyes " + snakeEyes + " out of " + count + " rolls.");

System.out.println ("You rolled double twos " + twos + " out of " + count + " rolls.");

System.out.println ("You rolled double threes " + threes + " out of " + count + " rolls.");

System.out.println ("You rolled double fours " + fours + " out of " + count + " rolls.");

System.out.println ("You rolled double fives " + fives + " out of " + count + " rolls.");

System.out.println ("You rolled double sixes " + sixes + " out of " + count + " rolls.");

}

}

Code Listing 4.2 (StatsDemo.java)

import java.util.Scanner;

// TASK #3 Add the file I/O import statement here

/**

This class reads numbers from a file, calculates the mean and standard deviation, and writes the results to a file.

*/

public class StatsDemo

{

// TASK #3 Add the throws clause public static void main(String[] args)

{

double sum = 0; // The sum of the numbers

int count = 0; // The number of numbers added

double mean = 0; // The average of the numbers

double stdDev = 0; // The standard deviation

String line; // To hold a line from the file

double difference; // The value and mean difference

// Create an object of type Scanner

Scanner keyboard = new Scanner (System.in);

String filename; // The user input file name

// Prompt the user and read in the file name

System.out.println("This program calculates " +

"statistics on a file " + "containing a series of numbers"); System.out.print("Enter the file name: ");

filename =

keyboard.nextLine();

// ADD LINES FOR TASK #4 HERE

// Create a File object passing it the filename

// Create a Scanner object passing File object

// Perform a priming read to read the first line

//of the file

// Loop until you are at the end of the file

// Convert the line to a double value and add the

// value to sum

// Increment the counter

// Read a new line from the file

// Close the input file

// Store the calculated mean

// ADD LINES FOR TASK #5 HERE

// Reconnect File object passing it the

// filename

// Reconnect Scanner object passing

// File object

// Reinitialize the sum of the numbers

// Reinitialize the number of numbers added

// Perform a priming read to read the first line of

// the file

// Loop until you are at the end of the file

// Convert the line into a double value and

// subtract the mean

// Add the square of the difference to the sum

// Increment the counter

// Read a new line from the file

// Close the input file

// Store the calculated standard deviation

// ADD LINES FOR TASK #3 HERE

// Create a FileWriter object using "Results.txt"

// Create a PrintWriter object passing the// FileWriter object

// Print the results to the output file

// Close the output file

}

}

Page |1 Chapter 5 Lab Loops and Files PLEASE ANSWER EACH TASK SEPARATELY AND INDIVIDUALLY Task #1 The while Loop Open the file DiceSimulation.java attached below. Create a new project on NetBeans called RiceSimulatien, Copy the code from the file into it. Note that DiceSimulation.java is incomplete. Since there is a large part of the program missing, the output will be incorrect if you run DiceSimulation.java 1. 2. I have declared all the variables. You need to add code to simulate rolling the dice and keeping track of the doubles. Convert the algorithm below into Java code and place it in the main method after the variable declarations, but before the output statements. You will be using several control structures: a while loop and an if-else-if statement nested inside another if statement. Use the indenting of the algorithm to help you decide what is included in the loop what is included in the if statement, and what is included in the nested if-else-if statement. Repeat while the number of dice rolls are less than the number of times the dice should be olled a. b. c. Get the value of the first die by "rolling" the first die Get the value of the second die by "rolling" the second die If the value of the first die is the same as the value of the second die If value of first die is 1 Else if value of the first die is 2 Else if value of the first die is 3 Else if value of the first die is 4 Else if value of the first die is 5 Else if value of the first die is 6 Increment the number of times snake eyes were rolled Increment the number of times twos were rolled Increment the number of times threes were rolled Increment the number of times fours were rolled Increment the number of times fives were rolled Increment the number of times sixes were rolled d. Increment the number of times the dice were rolled Note: To "roll" the dice, use the nextint method of the random number generator to generate an integer between 1 and 6 3. Compile and run you program. You should get numbers that are somewhat close to 278 for each of the different pairs of doubles. Run it several times. You should get different results than the first time, but again it should be somewhat close to 278

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!