Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I NEED THIS FAST COMPLETE THE CODE Task #2 Import the required packages In order to read and write object into a file, you need

I NEED THIS FAST

COMPLETE THE CODE

Task #2 Import the required packages

In order to read and write object into a file, you need to import the required classes that help to perform the required tasks.

Task #3 writing objects to a file

You need to create objects from the FileOutputStream, and ObjectOutputStream in order to be able to write objects to a file.

Note: please name the file (UJStudents.dat)

Task #4 try-catch statement

The main method contains a loop that will read a student name, his major and his GPA from the user. The main method should contain a try-catch statement. This statement tries to check if the GPA is valid by using IF statement. If the GPA is valid, it creates the Student Object. Otherwise, an IllegalArgumentException is thrown, the main should catch it and print out the name and an associated error message indicating why the GPA is invalid and ask the user to re-enter the valid GPA (it should be in a loop)

Note: the entered GPA should be between 0 to 5

Task #5 Object file (Writing)

Task 5.1: Once you create the Student object successfully, and to serialize this object and write it to the file, the ObjectOutputStream classs writeObject method is used.

Note: during the writing process, The writeObject method might throw an IOException if an error occurs, so make sure to handle this kind of exception.

Task 5.2: please close the file

Task #6 Object file (reading)

Task 6.1: To deserialize an object an ObjectInputStream object is used in conjunction with a FileInputStream object.

Note: please open the file (UJStudents.dat)

Task 6.2: to read a serialized object from the file, the ObjectInputStream classs readObject method is used. The readObject method returns the deserialized object.

Notice that you must cast the return value to the desired class type.

Note: The readObject method throws a number of different exceptions such as ClassNotFoundException, and IOException if an error occurs, so make sure to handle this kind of exception.

Task 6.3: please close the file

//import statments

//add line here for Task 2:

public class UJDemo { public static void main (String [] arg) { final int StudentNu=3; //specify the student number String studentN, //store student name studentM; //store student major double studentGPA; //store student GPA FileOutputStream outStream; ObjectOutputStream objectOutputFile; Student[] ujStudents=new Student[StudentNu]; Scanner input=new Scanner(System.in); //-------------------------------- System.out.println("This program will write 3 student objects"+ "into a file"); try{ //add line here for Task 3: for (int i=0;i5); //create a student object ujStudents[i]=new Student(studentN,studentGPA,studentM); //add line here for Task 5.1: input.nextLine(); } //add line here for Task 5.2: }catch(){ } //-------------------------------------- System.out.println("Now, this program will read student object "+ "from file "); boolean endOfFile=false; //flag to test to test if the end of file reach or not Student s; FileInputStream inStream; ObjectInputStream objectInputFile; try{ //add line here for Task 6.1: while (!endOfFile){ try{ //add line here for Task 6.2: System.out.print("the student name is "+s.getStudentName()); System.out.print(", his major is "+s.getMajor()); System.out.println(", and his GPA is "+s.getGPA()); }catch(EOFException e){ endOfFile = true; } } //add line here for Task 6.3: }catch(){ } catch(){ } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Understanding Oracle APEX 5 Application Development

Authors: Edward Sciore

2nd Edition

1484209893, 9781484209899

Students also viewed these Databases questions