Question
package mru.lab2.application; import java.util.Scanner; public class Task5andTask6StatsDemo { // TASK #5 Add the throws clause public static void main(String[] args) { double sum = 0;
package mru.lab2.application;
import java.util.Scanner;
public class Task5andTask6StatsDemo {
// TASK #5 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 #5 HERE
// Create a FileReader object passing it the filename
// Create a BufferedReader object passing FileReader
// 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
// Print the calculated mean on the console
// ADD LINES FOR TASK #6 HERE
// Create a FileWriter object using "Results.txt" (the file MUST be created under the res folder)
// Create a PrintWriter object passing the
// FileWriter object
// Print the result to the output file
// Close the output file
}
}
Task \#5 Reading inputs from a file, Numbers. txt 1. Open the Task5andTask6StatsDemo class. This class is incomplete. 2. There is also a txt file, Numbers . txt, under the res folder that will be used for Task 5 and 6 . 3. 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. 4. Write a priming read to read the first line of the file. 5. Write a loop that continues until you are at the end of the file. 6. 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 7. When the program exits the loop close the input file. 8. Calculate and store the mean. The mean is calculated by dividing the accumulator by the counter. 9. Print the mean on the console 10. Compile, debug, and run. You will need to type in the filename Numbers.txt. You should now see a mean of 77.444 on the console. Task \#6 Writing output to a file 1. 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). The Results . txt file should be created in the res folder. 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 to the output file using a three decimal format, labeling each. e. Close the output file. 2. Compile, debug, and run. You should now get a mean of 77.444 on the console and also in
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started