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

Task #1 While loop 1. Copy the file DiceSimulation.java (see code listing 4.1) f

ID: 3628076 • Letter: T

Question

Task #1 While loop
1. Copy the file DiceSimulation.java (see code listing 4.1) from
www.aw.com/cssupport or as directed by your instructor. DiceSimulation.java
is incomplete. Since there is a large part of the program missing, the output will
be incorrect if you run DiceSimulation.java.
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 to Java 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 ifelse-
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.
3. To “roll” the dice, use the nextInt method of the random number generator to
generate an integer from 1 to 6.
Repeat while the number of dice rolls are less than the number of times the dice should
be rolled.
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
Increment the number of times snake eyes were rolled
Else if value of the first die is 2
Increment the number of times twos were rolled
Else if value of the first die is 3
Increment the number of times threes were rolled
Else if value of the first die is 4
Increment the number of times fours were rolled
Else if value of the first die is 5
Increment the number of times fives were rolled
Else if value of the first die is 6
Increment the number of times sixes were rolled
Increment the number of times the dice were rolled
4. Compile and run. 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.

Task #2 Using Other Types of Loops

1. Change the while loop to a do-while loop. Compile and run. You should get the
same results.
2. Change the do loop to a for loop. Compile and run. You should get the same
results.

Task #3 Writing Output to a File
1. Copy the files StatsDemo.java (see code listing 4.2) and Numbers.txt from
www.aw.com/cssupport or as directed by your instructor.
2. First we will write output to a file:
a) Create a PrintWriter object passing it the filename “Results.txt” (Don’t forget
the needed import statement).
b) Since you are using a PrintWriter object, add a throws clause to the main
method header.
c) Print the mean and standard deviation to the output file using a three decimal
format, labeling each.
d) Close the output file.
3. Compile, debug, and run. You will need to type in the filename Numbers.txt.
You should get no output to the console, but running the program will create a
file called Results.txt with your output. The output you should get at this point
is: mean = 0.000, standard deviation = 0.000. This is not the correct mean or
standard deviation for the data, but we will fix this in the next tasks.

Task #4 Calculating the Mean
1. Now we need to add lines to allow us to read from the input file and calculate
the mean.
a) Create a File object passing it the filename.
b) Create a Scanner object passing it the File object.
2. Write a loop that reads items from the filr until you are at the end of the file.
The body of the loop will
a) read a double from the file
b) add the value that was read from the file to the accumulator
c) increment the counter
3. When the loop terminates, close the input file.
4. Calculate and store the mean. The mean is calculated by dividing the accumulator
by the counter.
5. Compile, debug, and run. You should now get a mean of 77.444, but the standard
deviation will still be 0.000.

Task #5 Calculating the Standard Deviation
1. We need to reconnect to the file so that we can start reading from the top again.
a) Create a File object passing it the filename.
b) Create a Scanner object passing it the File object.
2. Reinitialize sum and count to 0.
3. Write a loop that reads items from the file until you are at the end of the file.
The body of the loop will
a) read a double file from the file
b) subtract the mean from the value that was read from the file and store the
result in difference
c) add the square of the difference to the accumulator
d) increment the counter
4. When the loop terminates, close the input file.
5. The variance is calculated by dividing the accumulator (sum of the squares of
the difference) by the counter. Calculate the standard deviation by taking the
square root of the variance (Use Math.sqrt ( ) to take the square root).
6. Compile, debug, and run the program. you should get a mean of 77.444 and
standard deviation of 10.021.

Code Listing 4.1 (DiceSimulation.java)
import java.util.Random;
//to use the random number
//generator

/**
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; //the number of times to
//roll the dice
//a random number generator used in simulating
//rolling a dice

Random generator = new Random();

int die1Value; // number of spots on the first
// die
int die2Value; // number of spots on the second
// die
int count = 0; // number of times the dice were
// rolled
int snakeEyes = 0; // number of times snake eyes is
// rolled
int twos = 0; // number of times double
// two is rolled
int threes = 0; // number of times double three
// is rolled
int fours = 0; // number of times double four
// is rolled
int fives = 0; // number of times double five
// is rolled
int sixes = 0; // number of times double six is
// rolled


//ENTER YOUR CODE FOR THE ALGORITHM HERE

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.text.DecimalFormat; //for number formatting
import java.util.Scanner; //for keyboard input

//ADD AN IMPORT STATEMENT HERE //for using files

public class StatsDemo
{
public static void main(String [] args) //ADD A THROWS
//CLAUSE HERE
{
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 of the
//numbers
double difference; //difference between the value
//and the mean

//create an object of type Decimal Format

DecimalFormat threeDecimals =
new DecimalFormat("0.000");

//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 it the
//File object.

//write a loop that reads from the file until you
//are at the end of the file
//read a double from the file and add it to sum
//increment the counter
//close the input file
//store the calculated mean

//ADD LINES FOR TASK #5 HERE
//Create a File object passing it the filename
//Create a Scanner object passing it the
//File object.
//reinitialize the sum of the numbers
//reinitialize the number of numbers added
//write a loop that reads until you are at
//the end of the file
//read a double value and subtract the mean
//add the square of the difference to the sum
//increment the counter
//close the input file
//store the calculated standard deviation

//ADD LINES FOR TASK #3 HERE
//create an object of type PrintWriter
//using "Results.txt" as the filename
//print the results to the output file
//close the output file
}
}

Explanation / Answer

please rate - thanks

import java.util.Random;
//to use the random number
//generator

/**
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; //the number of times to
//roll the dice
//a random number generator used in simulating
//rolling a dice

Random generator = new Random();

int die1Value; // number of spots on the first
// die
int die2Value; // number of spots on the second
// die
int count = 0; // number of times the dice were
// rolled
int snakeEyes = 0; // number of times snake eyes is
// rolled
int twos = 0; // number of times double
// two is rolled
int threes = 0; // number of times double three
// is rolled
int fours = 0; // number of times double four
// is rolled
int fives = 0; // number of times double five
// is rolled
int sixes = 0; // number of times double six is
// rolled
//ENTER YOUR CODE FOR THE ALGORITHM HERE
while (count<NUMBER)
    {die1Value=generator.nextInt(6)+1;
    die2Value=generator.nextInt(6)+1;
    if(die1Value==die2Value)
        {if(die1Value==1)
             snakeEyes++;
        else if(die1Value==2)
             twos++;
        else if(die1Value==3)
             threes++;
        else if(die1Value==4)
            fours++;
        else if(die1Value==5)
            fives++;
       else if(die1Value==6)
            sixes++;
       
        }
        count++;
    }



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.");
}
}

-------------------------------------------------------------------------------

import java.util.Random;
//to use the random number
//generator

/**
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; //the number of times to
//roll the dice
//a random number generator used in simulating
//rolling a dice

Random generator = new Random();

int die1Value; // number of spots on the first
// die
int die2Value; // number of spots on the second
// die
int count = 0; // number of times the dice were
// rolled
int snakeEyes = 0; // number of times snake eyes is
// rolled
int twos = 0; // number of times double
// two is rolled
int threes = 0; // number of times double three
// is rolled
int fours = 0; // number of times double four
// is rolled
int fives = 0; // number of times double five
// is rolled
int sixes = 0; // number of times double six is
// rolled
//ENTER YOUR CODE FOR THE ALGORITHM HERE
do
    {die1Value=generator.nextInt(6)+1;
    die2Value=generator.nextInt(6)+1;
    if(die1Value==die2Value)
        {if(die1Value==1)
             snakeEyes++;
        else if(die1Value==2)
             twos++;
        else if(die1Value==3)
             threes++;
        else if(die1Value==4)
            fours++;
        else if(die1Value==5)
            fives++;
       else if(die1Value==6)
            sixes++;
       
        }
        count++;
    }while (count<NUMBER);




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.");
}
}

---------------------------------------------------------------------------

import java.util.Random;
//to use the random number
//generator

/**
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; //the number of times to
//roll the dice
//a random number generator used in simulating
//rolling a dice

Random generator = new Random();

int die1Value; // number of spots on the first
// die
int die2Value; // number of spots on the second
// die
int count = 0; // number of times the dice were
// rolled
int snakeEyes = 0; // number of times snake eyes is
// rolled
int twos = 0; // number of times double
// two is rolled
int threes = 0; // number of times double three
// is rolled
int fours = 0; // number of times double four
// is rolled
int fives = 0; // number of times double five
// is rolled
int sixes = 0; // number of times double six is
// rolled
//ENTER YOUR CODE FOR THE ALGORITHM HERE
for (count=0;count<NUMBER;count++)
    {die1Value=generator.nextInt(6)+1;
    die2Value=generator.nextInt(6)+1;
    if(die1Value==die2Value)
        {if(die1Value==1)
             snakeEyes++;
        else if(die1Value==2)
             twos++;
        else if(die1Value==3)
             threes++;
        else if(die1Value==4)
            fours++;
        else if(die1Value==5)
            fives++;
       else if(die1Value==6)
            sixes++;
       
        }

    }




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.");
}
}

-------------------------------------------------------------------------------

I don't understand what's supposed to be opened when, and you didn't provided the data file, and this was 2 questions, which violated the CRAMSTER rule of 1 question per post

import java.text.DecimalFormat; //for number formatting
import java.util.Scanner; //for keyboard input
//ADD AN IMPORT STATEMENT HERE //for using files
import java.io.*;

public class StatsDemo
{
public static void main(String [] args) throws FileNotFoundException
//ADD A THROWS
//CLAUSE HERE
{
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 of the
//numbers
double difference; //difference between the value
//and the mean

//create an object of type Decimal Format

DecimalFormat threeDecimals =
new DecimalFormat("0.000");

//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
PrintStream output=new PrintStream(new File("Results.txt"));

//Create a Scanner object passing it the
//File object.
Scanner input=new Scanner(new File(filename));
//write a loop that reads from the file until you
//are at the end of the file
while(input.hasNextDouble())
    {
//read a double from the file and add it to sum
     sum+=input.nextDouble();
//increment the counter
       count++;
        }   
//close the input file
input.close();
mean=sum/count;
//store the calculated mean
output.println("mean= "+threeDecimals.format(mean)+", standard deviation= "+threeDecimals.format(stdDev));
//ADD LINES FOR TASK #5 HERE
//Create a File object passing it the filename
Scanner in=new Scanner(new File(filename));
//Create a Scanner object passing it the
//File object.
//reinitialize the sum of the numbers
sum=0;
//reinitialize the number of numbers added
count=0;
//write a loop that reads until you are at
//the end of the file
while(in.hasNextDouble())
    {//read a double value and subtract the mean
//add the square of the difference to the sum
     sum+=Math.pow(mean-in.nextDouble(),2);
//increment the counter
       count++;
        }   

//close the input file
input.close();
//store the calculated standard deviation
stdDev=Math.sqrt(sum/count);
//ADD LINES FOR TASK #3 HERE
//create an object of type PrintWriter
//using "Results.txt" as the filename
//print the results to the output file
//close the output file
PrintStream out=new PrintStream(new File("Results.txt"));
out.println("mean= "+threeDecimals.format(mean)+", standard deviation= "+threeDecimals.format(stdDev));
out.close();
}
}

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