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

Java Lab Task #5 Calculating the Standard Deviation 1) We need to reconnect to t

ID: 3601682 • Letter: J

Question

Java Lab

Task #5 Calculating the Standard Deviation

1) We need to reconnect to the file so we can start reading from the top again.

-Create a File object passing it the filename.

-Create a Scanner object passing it the File object.

2) Reinitialize the sum and count to 0.

3) Write a priming read to read the first line of the file.

4) Write a loop that continues until you are at the end of the file.

5) The body of the loop will:

-convert the line into a double value and subtract the mean, store the result in difference

-add the square of the difference to the accumulator

-increment the counter

-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 standarddeviation of 10.021.

///

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

}

}

Explanation / Answer

inputNosFile.txt

11
22
33
44
55
66
77
88
12
13
14
15
16
17
18
19
23
24
25
26
27

_______________

StatsDemo.java

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.text.DecimalFormat;
import java.util.Scanner;

public class StatsDemo {

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

Scanner sc = null;
File f = new File(filename);
try {
sc = new Scanner(f);
while (sc.hasNext()) {
sum += Double.parseDouble(sc.next());
count++;
}
sc.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
}

mean = sum / count;

sum = 0;
count = 0;
try {
sc = new Scanner(f);
/* This loop Calculating the sum of square of
* each element in the deviation[] array
*/
while (sc.hasNext()) {
sum += Math.pow(Double.parseDouble(sc.next()) - mean, 2);
count++;
}
sc.close();
stdDev = Math.sqrt(sum / (count - 1));


} catch (FileNotFoundException e) {
e.printStackTrace();
}

FileWriter fw = null;
// DecimalFormat class is used to format the output
DecimalFormat df = new DecimalFormat(".000");
try {
fw = new FileWriter("Results.txt");
fw.write("Mean :" + df.format(mean) + " ");
fw.write("Standard deviation :" + df.format(stdDev));
fw.close();
} catch (IOException e) {
e.printStackTrace();
}


}

}

__________________

Output:

This program calculates statistics on a file containing a series of numbers
Enter the file name: inputNosFile.txt

___________________

Results.txt

Mean :30.714
Standard deviation :22.330

_____________Could you rate me well.Plz .Thank You

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote