Task #4 Calculating the Mean 1. Now we need to add lines to allow us to read fro
ID: 3649112 • Letter: T
Question
Task #4 Calculating the Mean1. Now we need to add lines to allow us to read from the input file and calculate
the mean.
a) Create a File object passing it the filename.
b) Create a Scanner object passing it the File object.
2. Write a loop that reads items from the filr until you are at the end of the file.
The body of the loop will
a) read a double from the file
b) add the value that was read from the file to the accumulator
c) increment the counter
3. When the loop terminates, close the input file.
4. Calculate and store the mean. The mean is calculated by dividing the accumulator
by the counter.
5. Compile, debug, and run. You should now get a mean of 77.444, but the standard
deviation will still be 0.000.
------------------------------------------------------------------------------------------------------------
-----------------------------------------------------------------------------------------------------------------
It is a given code for these tasks; Please help me for part where written as "add lines for task 4."
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
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class AvarageTest { public static void main(String[] args) throws IOException { System.out.println("Input any No."); BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String data = (String) br.readLine(); int num = Integer.parseInt(data); int sum = 0; float avg = 0.0f; for (int i = 1; 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.