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

Java Lab Task #4 Calculating the Mean 1) Now we need to add lines to allow us to

ID: 3601676 • Letter: J

Question

Java Lab

Task #4 Calculating the Mean

1) Now we need to add lines to allow us to read from the input file and calculate the mean.

-Create a File object passing it the filename.

-Create a Scanner object passing it the File object.

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

3) Write a loop that continues until you are at the end of the file (see Example 4-19 on page 241-242 of textbook)

4) The body of the loop will:

-convert the line into a double value and add the value to the accumulator

-increment the counter

-read a new line from the file

4) When the program exits the loop close the input file.

5) Calculate and store the mean. The mean is calculated by dividing the accumulator by the counter.

6) Compile, debug, and run. You should now get a mean of 77.444, but the standard deviation will still be 0.000.

///

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

Hi,
here is the complete code for all the tasks with comments to help you understand.

import java.io.File;

import java.io.FileWriter;

import java.io.IOException;

import java.io.PrintWriter;

import java.util.Scanner;

public class StatsDemo {

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

File file = new File(filename);//creating a file object

Scanner sc = new Scanner(file);//passing it to scanner to read file

while (sc.hasNextLine()) {//loop till EOF reached

double i = sc.nextDouble();//getting double value

sum+=i;//adding to sum

count++;//incrementing counter

}

sc.close();//closing file

mean=sum/count;//calculating mean

sum=0;//re initializing sum and count

count=0;

sc = new Scanner(file);//passing file again

while (sc.hasNextLine()) {//loop till EOF reached

double i = sc.nextDouble();//getting double value

sum+=(i-mean)*(i-mean);//squaring difference by mean

count++;

}

sc.close();

stdDev=Math.sqrt(sum/count-1);//calculating std deviation

File file1 = new File("Result.txt");// new o/p file object

// creates the file

file1.createNewFile();

// creates a FileWriter Object

FileWriter writer = new FileWriter(file1);

PrintWriter out = new PrintWriter(writer); //print write object

// Write to the file

out.println(mean);   

out.println(stdDev);

// Close the file.

out.close();

keyboard.close();

  

}

}

Thumbs up if this was helpful, otherwise let me know in comments

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