Task #5 Calculating the Standard Deviation 1. We need to reconnect to the file s
ID: 3649113 • Letter: T
Question
Task #5 Calculating the Standard Deviation
1. We need to reconnect to the file so that we can start reading from the top again.
a) Create a File object passing it the filename.
b) Create a Scanner object passing it the File object.
2. Reinitialize sum and count to 0.
3. Write a loop that reads items from the file until you are at the end of the file.
The body of the loop will
a) read a double file from the file
b) subtract the mean from the value that was read from the file and store the
result in difference
c) add the square of the difference to the accumulator
d) increment the counter
4. When the loop terminates, close the input file.
5. 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 Math.sqrt ( ) to take the square root).
6. Compile, debug, and run the program. you should get a mean of 77.444 and
standard deviation of 10.021.
------------------------------------------------------------------------------------------------------------
-----------------------------------------------------------------------------------------------------------------
It is a given code for these tasks;
Please help me for part where written as "add lines for part 5."
import java.text.DecimalFormat; //for number formatting
import java.util.Scanner; //for keyboard input
//ADD AN IMPORT STATEMENT HERE //for using files
import java.io.*;
public class StatsDemo
{
public static void main(String [] args) throws FileNotFoundException
//ADD A THROWS
//CLAUSE HERE
{
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 of the
//numbers
double difference; //difference between the value
//and the mean
//create an object of type Decimal Format
DecimalFormat threeDecimals =
new DecimalFormat("0.000");
//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
PrintStream output=new PrintStream(new File("Results.txt"));
//Create a Scanner object passing it the
//File object.
Scanner input=new Scanner(new File(filename));
//write a loop that reads from the file until you
//are at the end of the file
while(input.hasNextDouble())
{
//read a double from the file and add it to sum
sum+=input.nextDouble();
//increment the counter
count++;
}
//close the input file
input.close();
mean=sum/count;
//store the calculated mean
output.println("mean= "+threeDecimals.format(mean)+", standard deviation= "+threeDecimals.format(stdDev));
//ADD LINES FOR TASK #5 HERE
//Create a File object passing it the filename
Scanner in=new Scanner(new File(filename));
//Create a Scanner object passing it the
//File object.
//reinitialize the sum of the numbers
sum=0;
//reinitialize the number of numbers added
count=0;
//write a loop that reads until you are at
//the end of the file
while(in.hasNextDouble())
{//read a double value and subtract the mean
//add the square of the difference to the sum
sum+=Math.pow(mean-in.nextDouble(),2);
//increment the counter
count++;
}
//close the input file
input.close();
//store the calculated standard deviation
stdDev=Math.sqrt(sum/count);
//ADD LINES FOR TASK #3 HERE
//create an object of type PrintWriter
//using "Results.txt" as the filename
//print the results to the output file
//close the output file
PrintStream out=new PrintStream(new File("Results.txt"));
out.println("mean= "+threeDecimals.format(mean)+", standard deviation= "+threeDecimals.format(stdDev));
out.close();
}
}
--------------------------------------------------------------------------------------------------------------------
Explanation / Answer
public class Numbers { public static void main (String[]args){ java.util.Scanner input = new java.util.Scanner(System.in); final int number_of_elements=10; double [] numbers = new double [number_of_elements]; double sum=0; for (int i=0; iRelated Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.