Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

home / study / engineering / computer science / questions and answers / task #3

ID: 3676498 • Letter: H

Question

home / study / engineering / computer science / questions and answers /

task #3 writing output to a file 1. copy the files ... Question Task #3 Writing Output to a File 1. Copy the files StatsDemo.java (see Code Listing 4.2) 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.

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

Task #5 Calculating the Standard Deviation 1. 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. 2. Reinitialize the sum and count to 0. 3. Write a priming read to read the first line of the file. Copyright © 2016 Pearson Education, Inc., Hoboken NJ 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 newline 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 the Math.sqrt method to take the square root). 8. Compile, debug, and run. You should get a mean of 77.444 and standard deviation of 10.021.

HERES MY CODE

import java.io.BufferedReader;

import java.io.FileReader;

import java.io.FileWriter;

import java.io.IOException;

import java.io.PrintWriter;

import java.text.DecimalFormat;

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) throws IOException

{

   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();

   keyboard.close();

   // ADD LINES FOR TASK #4 HERE

   // Create a FileReader object passing it the filename

   FileReader reader = new FileReader(filename);

// Create a BufferedReader object passing FileReader

   // object

   BufferedReader bufferReader = new BufferedReader(reader);

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

   // the file

   line = bufferReader.readLine();

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

   while(line != null){

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

   // value to sum

   sum = sum + Double.parseDouble(line);

   count++; // Increment the counter

   // Read a new line from the file

   line = bufferReader.readLine();

   }

// Close the input file

   bufferReader.close();

   reader.close();

// Store the calculated mean

   mean = sum/count;

   // ADD LINES FOR TASK #5 HERE

   // Reconnect FileReader object passing it the

   // filename

   reader = new FileReader(filename);

   // Reconnect BufferedReader object passing

   // FileReader object

   bufferReader = new BufferedReader(reader);

   // Reinitialize the sum of the numbers

   sum = 0;

   // Reinitialize the number of numbers added

   count = 0;

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

   // the file

   line = bufferReader.readLine();

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

   while(line != null){

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

   // value to sum

   double num = Double.parseDouble(line);

   // subtract the mean

   difference = num - mean;

   // Add the square of the difference to the sum

   sum = sum + difference*difference;

   count++; // Increment the counter

   // Read a new line from the file

   line = bufferReader.readLine();

   }

// Close the input file

   bufferReader.close();

   reader.close();

   stdDev = Math.sqrt(sum/count);

   // Store the calculated standard deviation

   // ADD LINES FOR TASK #3 HERE

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

   FileWriter writer = new FileWriter("Results.txt");

   // Create a PrintWriter object passing the FileWriter object

   PrintWriter print = new PrintWriter(writer);

   // this is to format double number upto 3 decimal point

   DecimalFormat _numberFormat= new DecimalFormat("#0.000");

   // Print the results to the output file

   print.print("Mean = "+_numberFormat.format(mean));

   print.println();

   print.print("standard deviation = "+_numberFormat.format(stdDev));

   // Close the output file

   print.close();

   writer.close();

}

}

/*

Output:

Mean = 77.444

standard deviation = 10.021

*/

how do import the numbers.txt file into the project?

Explanation / Answer

The code here is perfectly fine .
When you compile and run the code , you will be asked to enter the file name which contains the numbers.
enter it as numbers.txt.
In this way , the file is imported into the project and the code takes the numbers from the files and calculate the mean and standard deviation of the numbers, outputting the result fiule in the same directory as the source code.