SHARE
SPREAD
HELP

The Tradition of Sharing

Help your friends and juniors by posting answers to the questions that you know. Also post questions that are not available.


To start with, Sr2Jr’s first step is to reduce the expenses related to education. To achieve this goal Sr2Jr organized the textbook’s question and answers. Sr2Jr is community based and need your support to fill the question and answers. The question and answers posted will be available free of cost to all.

 

#
Authors:
Walter Savitch ,kenrick Mock
Chapter:
File Io
Exercise:
Programming Projects
Question:10 | ISBN:9780132830317 | Edition: 5

Question

Write a program that takes its input from a binary file of numbers of type double. The file contains nothing but numbers of type double written to the file with writeDouble. The program outputs to the screen the average and standard deviation of the numbers in the file. The standard deviation of a list of numbers , , , and so forth is defined as the square root of the average of the following numbers:

, , , and so forth.

The number a is the average of the numbers , , , and so forth. Hint: Write your program so that it first reads the entire file and computes the average of all the numbers, then closes the file, and then reopens the file and computes the standard deviation. You will find it helpful to first do Programming Project 10.8 and then modify that program in order to obtain the program for this project.


TextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbook

Answer

import java.io.*;

public class FileDemo
{
	public static void main(String args[])
	{
		double avg = 0;
		int number = 10;
		double value;
		double dev = 0;
		double nextNum;
		try 
		{
	ObjectOutputStream os = new ObjectOutputStream(new FileOutputStream("numbersFile.dat"));
			for (int i = 0; i < 10; i++) {
				value = Math.random() * 20 + 1;
				System.out.println("Number contains: " + value);
				os.writeDouble(value);
			}
			os.close();
	ObjectInputStream is = new ObjectInputStream(new FileInputStream("numbersFile.dat"));

			try {
				while (true) {
					avg += is.readDouble();
				}
			} catch (IOException e) {
				avg /= number;
				System.out.println("Average: 		" + avg);
			}

			is.close();

			is = new ObjectInputStream(new FileInputStream("numbersFile.dat"));
			try 
			{
				while (true) 
				{
					nextNum = is.readDouble();
					dev += (nextNum - avg) * (nextNum - avg);
				}
			} catch (IOException e)
			{
			System.out.println("Standard deviation:" + Math.sqrt(dev / number));
			}
		} catch (IOException e) 
		{
			System.out.println("Error....");
		}
	}
}

Output:

 

0 0

Discussions

Post the discussion to improve the above solution.