Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Java Lab Chapter 5 Loops and Files Please help answer Tasks 2-5 You do not need to paste entire code in answer!! Just answer with

Java Lab Chapter 5 Loops and Files

Please help answer Tasks 2-5

You do not need to paste entire code in answer!! Just answer with the code for each task that are needed/missing from the program in the bolded all caps

image text in transcribedimage text in transcribed

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

}

}

Task #2 Using other types of loops 1. Change the while loop to a do-while loop. Compile and run. You should get the same results. 2. Change the do-while loop to a for loop. Compile and run. You should get the same results. Task #3 writing output to a File 1. Create a new project in NetBeans named StatsReme Copy files StatsDemo.java and Numbers.txt into your project. Compile and run. Program doesn't do much but should compile and run with no errors. If not, make sure to fix any errors before moving to next step. 2. First we will write output to a file: a. Create a FileWriter object passing it the filename Results.txt (Dont forget the needed import statement). See page 236 of textbook b. Create a PrintWriter object passing it the EileWriter object. C. Since you are using a FileWriter object, add a throws clause to them main method header. d. Print the mean and standard deviation to the output file using a three decimal format, labeling each e. Close the output file. 3. Compile, debug, and run. You will need to type in the filename Numbers.txtYou should get no output to the console, but running the program will create a file called Results .txt with your output. The output you should get at this point is: mean = 0.000, standard deviation = 0.000 . This is not the correct mean or standard deviation for the data, but we will fix this in the next tasks, 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. a. Create a file object passing it the filename. b. 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 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 5. When the program exits the loop close the input file. 6. Calculate and store the mean. The mean is calculated by dividing the accumulator by the counter. 7. Compile, debug, and run. You should now get a mean of 77.444, but the standard deviation will still be 0.000

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions