Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Objectives - Be able to convert an algorithm using control structures into Java code - Be able to write a while loop - Be able

image text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribed

Objectives - Be able to convert an algorithm using control structures into Java code - Be able to write a while loop - Be able to write a do-whi le loop - Be able to write a for loop - Be able to use the Random class to generate random numbers - Be able to use file streams for I/O - Be able to write a loop that reads until the end of a file - Be able to implement an accumulator and a counter Introduction 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, by translating a given algorithm into java code, 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.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. Task \#1 Import the Skeleton In the supplied package for this lab you will find a skeleton for this project. Import the project into Eclipse: 1. Copy/paste the supplied project folder into your workspace or any other place you like 2. Open Eclipse File Import General Existing Project into Workspace 3. In the "Select root directory" click on the "Browse" button and navigate to the place the project is (place that you copied the file). 1. Open the Task2Di ceSimulation class. This class is incomplete. 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 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 . 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 foreach of the different pairs of doubles. Run it several times! Task \#3 The do-while loop 1. Open the Task3Dicesimulation class. This class is incomplete. 2. Implement the previous task using a do-while loop. Compile and run. You should get the same results. Task \#4 The For loop 1. Open the Task4Dicesimulation class. This class is incomplete. 2. Implement the previous task using a for loop. Compile and run. You should get the same results. Task \#5 Reading inputs from a file, Numbers. txt 1. Open the Task5andTask6StatsDemo class. This class is incomplete. 2. There is also a txt file, Numbers . txt, under the res folder that will be used for Task 5 and 6 . 3. 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. 4. Write a priming read to read the first line of the file. 5. Write a loop that continues until you are at the end of the file. 6. 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 7. When the program exits the loop close the input file. 8. Calculate and store the mean. The mean is calculated by dividing the accumulator by the counter. 9. Print the mean on the console 10. Compile, debug, and run. You will need to type in the filename Numbers.txt. You should now see a mean of 77.444 on the console. Task \#6 Writing output to a file 1. 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). The Results . txt file should be created in the res folder. 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 to the output file using a three decimal format, labeling each. e. Close the output file. 2. Compile, debug, and run. You should now get a mean of 77.444 on the console and also in

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions

Question

Explain thie following regualer rexpression : ( 1 0 ? 1 )

Answered: 1 week ago

Question

Write short notes on Interviews.

Answered: 1 week ago

Question

4. Devise an interview strategy from the interviewers point of view

Answered: 1 week ago