LAB 3
unknown
java
3 years ago
4.1 kB
8
Indexable
package statsdemo;
import java.util.Scanner;
import java.io.*;
public class StatsDemo {
// TASK #1 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();
//------------------------------------------------------------ TASK 2
// ADD LINES FOR TASK #2 HERE
// Create a File object passing it the filename
File file = new File(filename);
// Create a Scanner object passing File object
Scanner inputFile = new Scanner(file);
// Perform a priming read to read the first line of the file
line = inputFile.nextLine();
// Loop until you are at the end of the file
while(inputFile.hasNext()){
if(line == null)
break;
else{
// Add value to sum
sum += Double.parseDouble(line);
// Increment the counter
count += 1;
// Read a new line from the file
line = inputFile.nextLine();
}
}
// Close the input file
inputFile.close();
// Store the calculated mean
mean = (sum)/(count);
//------------------------------------------------------------ TASK 3
// ADD LINES FOR TASK #3 HERE
// Reconnect File object passing it the filename
file = new File(filename);
// Reconnect Scanner object passing File object
inputFile = new Scanner(file);
// 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 = inputFile.nextLine();
// Loop until you are at the end of the file
while(inputFile.hasNext()){
if(line == null)
break;
else{
// subtract the mean
difference = (Double.parseDouble(line) - mean);
// Add the square of the difference to the sum
sum += Math.pow(difference, 2);
// Increment the counter
count += 1;
// Read a new line from the file
line = inputFile.nextLine();
}
}
// Close the input file
inputFile.close();
// Store the calculated standard deviation
stdDev = Math.sqrt(sum / count);
//------------------------------------------------------------ TASK 1
// ADD LINES FOR TASK #1 HERE
// Create a FileWriter object using "Results.txt"
FileWriter fw = new FileWriter("Results.txt", true);
// Create a PrintWriter object passing the FileWriter object
PrintWriter pw = new PrintWriter(fw);
// Print the results to the output file
pw.printf("Mean = %.3f.\n", mean);
pw.printf("Standard Deviation = %.3f.\n", stdDev);
pw.close();
// Close the output file
}
}Editor is loading...