Question: You will find Java source code for two classes (Write_ID_GPA and Student) with this assignment. Do the followings. 1. Run the Write_ID_GPA class. It will
You will find Java source code for two classes (Write_ID_GPA and Student) with this assignment. Do the followings.
1. Run the Write_ID_GPA class. It will save the ID and GPA (randomly generated values) of 10 students to a data file.
2. Write a java class to read all the ID and GPA values from the data file (created in Step 1) and create Student objects (one for each ID & GPA) using a loop. The program should print each student object on the screen (using the toString method). The program should also write each student object to another data file (using the writeObject method of ObjectOutputStream). All this can be done using a single loop. Run the class so that another data file having student data is created. Note that the Student class is provided.
3. Write another java class to read all student objects from the data file (created by the class in Step 2) and print each student on the screen, using a single loop. The program should also calculate & print the average gpa. The getGPA method can be used to get the gpa of a student object.
import java.io.*; import java.util.*; public class Write_ID_GPA { public static void main(String[] args) throws IOException { try ( FileOutputStream fos = new FileOutputStream("idgpa.dat"); DataOutputStream dos = new DataOutputStream(fos); ){ int[] ids = {101, 102, 105, 106, 107, 109, 111, 113, 115, 120}; double gpa; Random random = new Random(new Date().getTime()); for (int i=0;i
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
import java.io.Serializable; public class Student implements Serializable { int id; double gpa; public Student(int i, double g){ id = i; gpa = g; } public String toString(){ return id + ", " + gpa; } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
