Register Now

Login

Lost Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Login

Register Now

Welcome to All Test Answers

Java program that reads grades and outputs the highest and lowest grades

Write a java program that reads grades of type double of eight students that the user
provides. The grades lie between 0 and 10. These grades should be written to a
binary file and read from it. The program outputs the highest and lowest grades
achieved by students on the screen. The file contains nothing but numbers of type
double written to the file with writeDouble.

 


import java.io.EOFException;
import java.io.IOException;
import java.io.FileInputStream;
import java.io.ObjectInputStream;

public class Question10 {

   public static void main(String[] args){
      int count = 0;
      double sum = 0.0;
      try {
		ObjectInputStream inputStream = new ObjectInputStream(new
	   		FileInputStream("myFile.dat"));

	 	try {
	 		while (true) {
	 			sum += inputStream.readDouble();
	 			count++;
	 		} // end of while ()
	 	}
	 	catch (EOFException e)
	 	{
	 	}

	 	inputStream.close();
	 	System.out.println("Average: " + sum/count);
     }
     catch (IOException e)
     {
	 	System.out.println("Error reading from myFile.dat");
     }
   }
}

About

Leave a reply

Captcha Click on image to update the captcha .

error: Content is protected !!