Question: Create a FileWriter object passing it the filename Results.txt (Dont forget the needed import statement). Create a PrintWriter object passing it the FileWriter object. Since

Create a FileWriter object passing it the filename Results.txt (Dont forget the needed import statement).

Create a PrintWriter object passing it the FileWriter object.

Since you are using a FileWriter object, add a throws clause to the

main method header.

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

Close the output file.

3.Compile,run.YouwilltointheNumbers.txt.YoushouldnooutputtothebutrunningthewillafileResults.txt withoutput.Theoutputshouldthispoint=0.000,standard=0.000.Thisisnotthe or for the but we will fix in thenext

Task #4 Calculating the Mean

1.Nowweneedtoaddlinestoallowustoreadfromtheinputfileandcalculatethe mean.

Create a File object passing it the filename.

Create a Scanner object passing it the File object.

2.Writeaprimingread to read the first line ofthefile.

3.Writealoop that continues untilyou areattheend of thefile

4.Thebodyof theloop will:

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

increment the counter

read a new line from the file

5.When the program exits theloop closethe input file.

6.Calculateandstorethemean.Themeaniscalculatedbydividingtheaccumulator bythecounter.

7.Compile,debug,andrun.Youshouldnowgetameanof77.444,butthestandard deviation will still be 0.000.

Code Listing 4.2 (StatsDemo.java)

import java.util.Scanner;

// TASK #3 Add the file I/O import statement here

/**

This class reads numbers from a file, calculates the mean and standard deviation, and writes the results to a file.

*/

public class StatsDemo

{

// TASK #3 Add the throws clause public static void main(String[] args)

{

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 String line; // To hold a line from the file

double difference; // The value and mean difference

// 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 File object

// Perform a priming read to read the first line

//of the file

// Loop until you are at the end of the file

// Convert the line to a double value and add the

// value to sum

// Increment the counter

// Read a new line from the file

// Close the input file

// Store the calculated mean

// ADD LINES FOR TASK #5 HERE

// Reconnect File object passing it the

// filename

// Reconnect Scanner object passing

// File object

// Reinitialize the sum of the numbers

// Reinitialize the number of numbers added

// Perform a priming read to read the first line of

// the file

// Loop until you are at the end of the file

// Convert the line into a double value and

// subtract the mean

// Add the square of the difference to the sum

// Increment the counter

// Read a new line from the file

// Close the input file

// Store the calculated standard deviation

// ADD LINES FOR TASK #3 HERE

// Create a FileWriter object using "Results.txt"

// Create a PrintWriter object passing the// FileWriter object

// Print the results to the output file

// Close the output file

}

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!