Question

In: Computer Science

In java Q2. Write a program that reads grades of type double of eight students that...

In java
Q2. Write a 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.


Solutions

Expert Solution

JAVA Program:

import java.io.DataOutputStream;
import java.io.FileOutputStream;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Scanner;

public class WriteDoubleToFile {

public static void main(String[] args) {

String strFilePath = "E://WriteDouble.txt";
  
   double []grades = new double[8];
   double []gradesReadFromFile = new double[8];
  
   int i;
  
   Scanner sc = new Scanner(System.in);
  
   for(i=0;i<8;i++){
       System.out.print("Enter the grade between 0 and 10 for student "+(i+1)+": ");
       grades[i] = sc.nextDouble();
   }
  
   try
{
       FileOutputStream fos = new FileOutputStream(strFilePath);

       DataOutputStream dos = new DataOutputStream(fos);
  
       for(i=0;i<8;i++)
dos.writeDouble(grades[i]);
       dos.close();
      
       FileInputStream fin = new FileInputStream(strFilePath);
      
       DataInputStream din = new DataInputStream(fin);
      
       for(i=0;i<8;i++)
       gradesReadFromFile[i]=din.readDouble();
       din.close();
  
}
catch (IOException e)
{
System.out.println("IOException : " + e);
}
  
   double highest = gradesReadFromFile[0];
   double lowest = gradesReadFromFile[0];
  
   for(i=1;i<8;i++){
       if(gradesReadFromFile[i]>highest)
       highest=gradesReadFromFile[i];
       if(gradesReadFromFile[i]<lowest)
       lowest=gradesReadFromFile[i];
   }
   System.out.println("Highest grade: "+highest);
   System.out.println("Lowest grade: "+lowest);
}
}

Output:


Related Solutions

Write a C++ program that reads a students name followed by 7 numeric grades from a...
Write a C++ program that reads a students name followed by 7 numeric grades from a file.  Compute the average grade after dropping the  lowest grade.   Assign letter grades via this scale .Please show steps in the comments . A 90 – 100 B 80  --89 C 70 –79 D 60 -69 F 0  - 59 Input format: Sam 100 90 87 23 12 67 95 Mary 30 20 90 90 90 90 88 Mark 80 90 80 80 90 87 100 End of file...
Using Java Write a program that reads a file of numbers of type int and outputs...
Using Java Write a program that reads a file of numbers of type int and outputs all of those numbers to another file, but without any duplicate numbers. You should assume that the input file is sorted from smallest to largest with one number on each line. After the program is run, the output file should contain all numbers that are in the original file, but no number should appear more than once. The numbers in the output file should...
Using Java Write a program that reads a file of numbers of type int and outputs...
Using Java Write a program that reads a file of numbers of type int and outputs all of those numbers to another file, but without any duplicate numbers. You should assume that the input file is sorted from smallest to largest with one number on each line. After the program is run, the output file should contain all numbers that are in the original file, but no number should appear more than once. The numbers in the output file should...
Problem 1 Write a Java program that implements a two-dimensional array of grades for students in...
Problem 1 Write a Java program that implements a two-dimensional array of grades for students in a class. The grades should be as follows: Row 1: 87, 45 Row 2: 69, 88, 90, 94 Row 3: 79, 87, 94 Compute and print out the maximum number of columns in the array. Hint: use a for loop. (5 Points.) */ /* Problem 2 * Suppose there are 4 candidates in 6 districts running for an election. Let's assume the candidates' names...
Using Java Project 2: Deduplication Write a program that reads a file of numbers of type...
Using Java Project 2: Deduplication Write a program that reads a file of numbers of type int and outputs all of those numbers to another file, but without any duplicate numbers. You should assume that the input file is sorted from smallest to largest with one number on each line. After the program is run, the output file should contain all numbers that are in the original file, but no number should appear more than once. The numbers in the...
you are to write a program in Java, that reads in a set of descriptions of...
you are to write a program in Java, that reads in a set of descriptions of various geometric shapes, calculates the areas and circumferences of the shapes, and then prints out the list of shapes and their areas in sorted order from smallest to largest area. There are four possible shapes: Circle, Square, Rectangle, and Triangle. The last is always an equilateral triangle. The program should read from standard input and write to standard output. The program should read until...
Write a Java program that reads a name and displays on the screen.
Write a Java program that reads a name and displays on the screen.
Write a Java program that reads a list of integers into an array. The program should...
Write a Java program that reads a list of integers into an array. The program should read this array from the file “input.txt”. You may assume that there are fewer than 50 entries in the array. Your program determines how many entries there are. The output is a two-column list. The first column is the list of the distinct array elements; the second column is the number of occurrences of each element. The list should be sorted on entries in...
JAVA Program The program below the text reads in the pet type …dog, cat or other...
JAVA Program The program below the text reads in the pet type …dog, cat or other (only the words dog, cat, other) and the appointment amount. It is supplied as a beginning code to the assignment: _______________________________________________________________ pet_lab package; import javax.swing.JOptionPane; public class pet_lab { public static void main (String [] args) { String pet, temp; double payment; pet = JOptionPane.showInputDialog (null, "Enter the pet type", "", JOptionPane.QUESTION_MESSAGE); temp = JOptionPane.showInputDialog (null, "Enter the payment for the appointment", "", JOptionPane.QUESTION_MESSAGE);...
Write a program that reads a student's code and grades from all four tests of any...
Write a program that reads a student's code and grades from all four tests of any matter; and calculate and print the final grade for the subject, along with the student code. Taking into account that: The value of the first is 15% The value of the second is 25% The value of the third is 30% The value of the fourth is 40% Programming language is VB8
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT