Exercise Write a program that takes its input from a binary file of numbers of t
ID: 3728076 • Letter: E
Question
Exercise 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 n1, n2, n3, and so forth is defined as the square root of the average of the following numbers (n1 - a)2, (n2- a)2, (n3 - a)2, and so forth. The number a is the average of the numbers n1, n2, n3 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 and then modify that program in order to obtain the program for this project. Project visit www.myprogramminglab.com to complete select exercises online and get instant feedback Exercise Write a program that will search a binary file of numbers of type int and wrte the largest and the smallest numbers to the screen. The file contains nothing but numbers of type int wrtten to the file with writeInt.Explanation / Answer
As per Chegg policy we are supposed to answer the first complete question. So answering the first one. Request you to post the other question separately.
The class StandardDeviation contains the code to calculate the standard deviation for numbers in a binary file. In order to test the program, I had to create a binary file and therefore wrote a program that will create a binary file. This class is called CreateBinaryFile. It is not asked as part of the question. But will help you create a binary file. You only need to submit class StandardDeviation file.
First run the CreateBinaryFile and then run StandardDeviation.
StandardDeviation.java
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Scanner;
public class StandardDeviation {
public static void main(String[] args) {
double sum = 0, avg = 0, stdDeviation = 0, n, diff;
int count = 0;
String filename;
Scanner keyboard = new Scanner(System.in);
System.out.println("Enter the binary input filename containing double values: ");
filename = keyboard.nextLine();
try {
DataInputStream dis = new DataInputStream(new FileInputStream(filename)) ; //open the binary file
//read all data to compute avg
while(dis.available() > 0)
{
sum += dis.readDouble();
count++;
}
dis.close();
if(count > 0)
avg = sum / count;
//now reopen file and read each number and calculate the difference and then finally stadarad deviation
dis = new DataInputStream(new FileInputStream(filename)) ; //open the binary file again
sum = 0;
for(int i = 1; i <= count; i++)
{
n = dis.readDouble();
diff = n - avg;
sum = diff * diff;
}
dis.close();
stdDeviation = Math.sqrt(sum / count);
System.out.printf("There were %d numbers in the file ", count);
System.out.printf("The average of the numbers is %f ", avg);
System.out.printf("The standard deviation of the numbers is %f ", stdDeviation);
} catch (FileNotFoundException e) {
System.out.println(e.getMessage());
} catch (IOException e) {
System.out.println(e.getMessage());
}
}
}
-----output----
Enter the binary input filename containing double values:
sample.bin
There were 5 numbers in the file
The average of the numbers is 5.600000
The standard deviation of the numbers is 0.268328
-------------------
CreateBinaryFile.java
import java.io.DataOutputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Scanner;
//program to create a binary file containing double values
public class CreateBinaryFile {
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
String filename;
int count;
double n;
System.out.println("Enter the binary output filename: ");
filename = keyboard.nextLine();
System.out.println("How many double values do you want to write to file? ");
count = keyboard.nextInt();
System.out.println("Enter the double values: ");
try {
DataOutputStream dos = new DataOutputStream(new FileOutputStream(filename));
for(int i = 1; i <= count; i++)
{
n = keyboard.nextDouble();
dos.writeDouble(n);
}
dos.close();
System.out.println("Binary File " + filename + " created" );
} catch (FileNotFoundException e) {
System.out.println(e.getMessage());
} catch (IOException e) {
System.out.println(e.getMessage());
}
}
}
output
=====
Enter the binary output filename:
sample.bin
How many double values do you want to write to file?
5
Enter the double values:
4 5 6 8 5
Binary File sample.bin created
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.