Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I have up until Task #3 figured out. Please help! Chapter 4 Lab Loops and Files Lab Objectives Be able to convert an algorithm using

I have up until Task #3 figured out. Please help!

Chapter 4 Lab Loops and Files

Lab Objectives

Be able to convert an algorithm using control structures into Java

Be able to write a while loop

Be able to write a do-while 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 end of 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, translating a given algorithm to java im 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.

Task #1 While loop

1. Copy the file DiceSimulation.java (see code listing 4.1) from the Student CD 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 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 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.

3 Compile and run the program.

Task #3 Creating a file

Add the following lines of code as needed (refer to exercise 233 in text) to print the output to a file.

import java.util.io.*;

throws IOException

Scanner keyboard = new Scanner (System.in);

Prompt the user for the file name.

Give the user the prompt to enter it in the format correct format; i.e a:\\documents\\dicesimulation

Read the filename entered by the user and create the PrintWriter object.

filename = keyboard.nextLine;

PrintWriter outputfile = new PrintWriter(filename);

Modify the final print statements that show the output to the user to also print to the file.

Use: outputFile.printline (snakeEyes), etc;

NOTE: only do this for the while loop.

Compile and run the program.

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.

Create a FileReader object passing it the filename.

File file = new File (filename);

Create a Scanner object passing it the FileReader object.

Scanner inputFile = new Scanner(file);

2 A priming read to read the first line of the file would be:

int number = inputFile.nextInt();

3 Write a loop that continues reading numbers until you are at the end of the file; totals the numbers; and keeps a count of how many numbers were read.

Use while (inputFile.hasNext() ) for the test

4 The body of the loop will

a) convert the line into a double value - double number = inputFile.next (Double);

b) add the number read from the file to the accumulator

b) increment the counter

c) read a new line from the file

while (inputFile.hasNext())

{

double number = inputFile.nextInt();

accumulator += number;

}

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 number of items.

7 Compile, debug, and run. Print out the mean.

(DiceSimulation.java:

/** This class simulates rolling a pair of dice 10,000 times and counts the number

of times doubles are rolled for each different pair of doubles.

*/

import

java.util.Random;

//to

use the

random

number generator

import

java.util.Scanner;

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

String

filename;

//name

of file

to write/read

int

accumulator

= 0;

double

mean

= 0;

int

count2

= 0;

// add

code

for

TASK

#3

//loop

while

count

is less

than

NUMBER

of times

//adjust

code

in while

loop

as instructed

in TASK

#3

while(count

< NUMBER)

{

//roll

the

dice

die1Value

= generator.nextInt(6)+1;

die2Value

= generator.nextInt(6)+1;

//Check

to see

if you

have

doubles

//Enter

code

for

Task

1 if statements

here

}

//while(count

< NUMBER)

ending

braces;

//add

code

that

closes

the

output

file

for TASK

#3 here

System.out.println

("Output

for

while

loop");

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

//Enter

code

for

TASK

#2 here

for

do while loop

System.out.println

("Output

for

do while

loop ");

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

// Enter

code

for

TASK

#2 here

for

for

loop

System.out.println

("Output

for

do while

loop ");

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

// add

code

for

TASK

#4 here

}

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

It seems like the question is incomplete While youve successfully completed up to Task 3 the given content doesnt clarify what exactly the issue is wi... 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

Recommended Textbook for

Beginning ASP.NET 2.0 And Databases

Authors: John Kauffman, Bradley Millington

1st Edition

0471781347, 978-0471781349

Students also viewed these Databases questions