Question: Students will be introduced to file input and output. We will read a file, line by line, converting each line into a number. We will
Students 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.
Problem:
Task #Writing Output to a File
Copy the file Numbers.txt from the Student Files or as directed by your instructor.
First we will write output to a file: a Create a FileWriter object passing it the filename Results.txt
Dont 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.
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 standard deviation This is not the correct
mean or standard deviation for the data, but we will fix this in the next tasks.
Task # Calculating the Mean
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.
Write a priming read to read the first line of the file.
Write a loop that continues until you are at the end of the file.
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
When the program exits the loop close the input file.
Calculate and store the mean. The mean is calculated by dividing the accumulator by the counter.
Compile, debug, and run. You should now get a mean of but the standard deviation will still
be
Task # Calculating the Standard Deviation
We need to reconnect to the file so 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.
Reinitialize the sum and count to
Write a priming read to read the first line of the file.
Write a loop that continues until you are at the end of the file.
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 newline from the file.
When the program exits the loop close the input file.
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 the Math.sqrt
method to take the square root
Compile, debug, and run. You should get a mean of and standard deviation of
CAN YOU PLEASE SHOW HOW TO MAKE THE NUMBERS.TXT DOCUMENT WITH THE FOLLOWING NUMBERS
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
