This is a simulation of rolling dice actual results

Assignment Help JAVA Programming
Reference no: EM13347721

This is a simulation of rolling dice. Actual results approach theory only when the sample size is large. So we will need to repeat rolling the dice a large number of times (we will use 10,000). The theoretical probability of rolling doubles of a specific number is 1 out of 36 or approximately 278 out of 10,000 times that you roll the pair of dice. Since this is a simulation, the numbers will vary a little each time you run it.

Check out how to use the random number generator (introduced in section 4.11 of the text) to get a number between 1 and 6 to create the simulation.

We will continue to use control structures that we have already learned, while exploring control structures used for repetition. We shall also continue our work with algorithms, translating a given algorithm to java in order to complete our program. We will start with a while loop, then use the same program, changing the while loop to a do-while loop, and then a for loop.

We will be introduced to file input and output. We will read a file, line by line, converting each line into a number. We will then use the numbers to calculate the mean and standard deviation.

First we will learn how to use file output to get results printed to a file. Next we will use file input to read the numbers from a file and calculate the mean. Finally, we will see that when the file is closed, and then reopened, we will start reading from the top of the file again so that we can calculate the standard deviation.

Part-1

1. You need to add code to simulate rolling the dice and keeping track of the doubles. 

2. 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 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.

3.  To “roll” the dice, use the nextInt method of the random number generator to generate an integer from 1 to 6. 

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.

Part-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.

Part-3 Writing Output to a File

1. Copy the files StatsDemo.java and Numbers.txt from the Student CD or as directed by your instructor.   

2.  First we will write output to a file:

a)  Create a FileWriter object passing it the filename “Results.txt” (Don’t forget the needed import statement).

b)  Create a PrintWriter object passing it the FileWriter object.

c)  Since you are using a FileWriter object, add a throws clause to the main method header.

d)  Print the mean and standard deviation to the output file using a three decimal format, labeling each.

e)  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.

Part - 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 FileReader object passing it the filename.

b)  Create a BufferedReader object passing it the FileReader object.

2.  Write a priming read to read the first line of the file.

3.  Write a loop that continues until you are at the end of the file.

4.  The body of the loop will

a)  convert the line into a double value and add the value to the accumulator

b)  increment the counter

c)  read a new line from the file

5.  When the program exits the loop close the input file.

6. Calculate and store the mean.  The mean is calculated by dividing the accumulator by the counter.

7. Compile, debug, and run. You should now get a mean of 77.444, but the standard deviation will still be 0.000.

Part -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 FileReader object passing it the filename.

b)  Create a BufferedReader object passing it the FileReader object.

2.  Reinitialize sum and count to 0.

3.  Write a priming read to read the first line of the file.

4.  Write a loop that continues until you are at the end of the file.

5.  The body of the loop will

a)  convert the line into a double value and subtract the mean, store the result in difference

b)  add the square of the difference to the accumulator

c)  increment the counter

d)  read a new line from the file

6.  When the program exits the loop close the input file.

7.  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). 

8.  Compile, debug, and run.  You should get a mean of 77.444 and standard deviation of 10.021.

Reference no: EM13347721

Questions Cloud

Write a market research reportfinal conclusion of the : write a market research reportfinal conclusion of the launching of this mall either this shopping malls idea is
Part a write a program to evaluate the first 20 numbers of : part a write a program to evaluate the first 20 numbers of fibonacci series. use the stack memory to store the
Part-1reinforce understanding of power beta and : part-1reinforce understanding of power beta and alpha.problem a packing process is designed to fill steel drums with
Methods are commonly used to break a problem down into : methods are commonly used to break a problem down into small manageable pieces. a large task can be broken down into
This is a simulation of rolling dice actual results : this is a simulation of rolling dice. actual results approach theory only when the sample size is large. so we will
Problem 1 in a study relating college grade point average : problem 1 in a study relating college grade point average to time spent in various activities students are asked how
You will build a recursive-descent parser for the right : you will build a recursive-descent parser for the right recursive version of the classic expression grammar found in
The comma-separated values csv file format is a delimited : the comma-separated values csv file format is a delimited data format commonly used as a portable representation of
The virgin brand entered the australian aviation market in : the virgin brand entered the australian aviation market in 2000 in competition with qantas and the other major airlines

Reviews

Write a Review

JAVA Programming Questions & Answers

  Represent one book in java

Represent one book in java

  Discuss the legal ramifications of the division of condo

Discuss the legal ramifications of the division of condo, BioTech and other assets. Would it make any difference if Violet sold the property after filing for the dissolution of marriage?

  Return a reverse queue

represent my queue object as a parameter, what the program should be doing. It should return a reverse queue

  Simple java application that uses the string

Create a simple Java application that uses the String class and/or the StringBuffer class and at least 4 of the class methods. Show the code, demonstrate it works properly and describe what it is doing.

  Build a simple text-based adventure game

Build a simple text-based adventure game. You can and probably should use our Missed Connections example as the foundation for your game.

  If there are 4 command line arguments

If there are 4 command line arguments (a b c filename.dat) then the engine should take a,b,c and store the resulting x1 and x2 in filename.dat working in silent mode (no use of screen)

  Mean and standard deviation using using eclipse

Java programming to calculate Mean and standard deviation using Using Eclipse.

  Write java program to select pine for furniture company

Write down the Java program for the furniture company. Ask user to select P for Pine, O for Oak or M for Mahogany.

  Create three classes for a customer

a travel manager and have a job of buying a travel package for a customer. You must create three classes for this programming challenge: Customer, Ticket, and TravelManagerDemo.

  Write a method called rectsum

Write a method called rectSum that will get the sum of a rectangle of elements within a 2D array of ints

  Compute the fifties and the remaining amount

Prompt the user for a string input value for the amount in dollars and store it in a variable named input and convert the string to an integer and store it in the variable named amount.

  Implement the finite field gf

Write a program called "GF2.java" to implement the finite field GF(p n )where p is a prime number andn is a positive integer.You also need to write four methodsto realize.

Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd