Program in Java! Student need to create a program takes its input from a binary
ID: 3803097 • Letter: P
Question
Program in Java!
Student need to create a program takes its input from a binary file of numbers of type double. Also, the program will output all the numbers and the average of the numbers in the file, and then print the results to the screen. The binary file only contains numbers of type double written to the file using writeDouble. One scenarios of this situation will be the data file stored your quiz grades, and you would like to find out yum average grade for the quizzes. An example test file testData.bin has been uploaded to mycourses. Use this file to test your program. For advanced student: The format of file is slightly different, Instead of containing only binary data, the file contain first the "firstname" and then "lastname" of the person, then a list of number in binary. You can use another test data file advTestData.bin to test your program. General comment on submission For each question, please submit a separate Java programs source code. The source code should well documented and using the good practices that were described in the TWU 140/166 Coding Standard. Submit one file: lab4. java. The file(s) should contain all functions necessary to run and show the operation in the question.Explanation / Answer
Here is the code for you:
import java.io.*;
import java.util.*;
class ReadAndWriteBinaryFile
{
public static void main(String[] args) throws FileNotFoundException
{
Scanner sc = new Scanner(new File("testData.bin")); //Opens the binary file for input stream.
double sum = 0;
int count = 0;
while(sc.hasNext())
{
double number = sc.nextDouble();
System.out.print(number + " ");
sum += number;
count++;
}
double average = sum / count;
System.out.println("The average of the numbers is: " + average);
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.