Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Lab 12 Files, Streams and Object Serialization Given: 1. UML Diagram of classes InputFromFile, OutputToFile and Student. 2. Sample_output_1.jpg, Sample_output_2.jpg, input_file.txt, output_file.txt, object_input.ser, object_output.ser 3.

Lab 12 Files, Streams and Object Serialization

Given: 1. UML Diagram of classes InputFromFile, OutputToFile and Student.

2. Sample_output_1.jpg, Sample_output_2.jpg, input_file.txt, output_file.txt, object_input.ser, object_output.ser

3. These instructions

Task: Write code for classes InputFromFile, OutputToFile and Student, that meets the listed requirements and returns the expected results as shown in sample outputs.

[To demonstrate the use of Scanner to retrieve input from a file.

To demonstrate the use of Formatter to output formatted strings to a file.

Creating a sequential access file using object serialization To demonstrate writing an object to a file or reading an object from a file. i.e. Objects serialized and deserialized with ObjectOutputStreams and ObjectInputStreams, respectively]

==========================================================================

Requirements for class InputFromFile:

Declare a public class InputFromFile

-Declare the main method

-Call two methods useScanner() and useInputStreams() from main method.

-No parameters passed to both methods.

-Declare a static void method useScanner()

-No parameters received.

-Declare a Scanner object that will be used to retrieve input from the file input_file.txt.

-Scan the input data from the input_file.txt text file for all instance variables in the following order: firstName, lastName, age, gender, major and cgpa. [Hint: Read a string delimited by white space].

-Print the retrieved values of all instance variables as shown in Sample_output_1.jpg

-Close the input_file.txt file.

-Code the try catch blocks to catch FileNotFoundException.

-Declare a static void method useInputStreams()

-No parameters received.

This method will have code to read deserialized data sequentially from a file.

-Declare an ObjectInputStream instance named input, which will be used to read objects from the file. Assign input variable with ObjectInputStream wrapped around the Files static method newInputStream, which receives a Path specifying the file object_input.ser to open.

-Scan the input data from object_input.ser file for all instance variables in the following order: firstName, lastName, age, gender, major and cgpa. [Hint: loop until there is end-of-file exception].

-To retrieve the current values of all instance variables, use specific set and get methods from Student class. To do this, downcast the returned object to type Student.

-Call ObjectInputStream method readObject() to read an object from the file.

-Print the retrieved values of all instance variables as shown in Sample_output_1.jpg

-Close the object_input.ser file.

-Code the try catch blocks to catch ClassNotFoundException and IOException.

======================================================================================================

-Requirements for class OutputToFile:

-Declare a public class OutputToFile

-Declare the main method

-Call two methods useFormatter () and useOutputStreams() from main method.

-No parameters passed to both methods.

-Declare a static void method useFormatter ()

-No parameters received.

-Declare a Scanner object input that will be used to get user input.

-Scan the user input data for all instance variables in the following order: firstName, lastName, age, gender, major and cgpa. [Hint: Read a string delimited by white space]. Refer to Sample_output_2.jpg

-Instantiate a Formatter object output to create a new file named output_file.txt.

-Print values of all instance variables to output_file.txt using the Formatter method format(). Refer to attached output_file.txt file.

-Close the output_file.txt file.

-Code the try catch blocks to catch FileNotFoundException.

-Declare a static void method useOutputStreams()

-No parameters received.

This method will have code to write serialized objects sequentially to a file.

-Declare a Scanner object that will be used to get user input.

-Declare an ObjectOutputStream instance named output, which will be used to write serialized objects to a file. Assign output variable with ObjectOutputStream wrapped around the Files static method newOutputStream, which receives a Path specifying the file object_output.ser to open.

-Scan the user input data for all instance variables in the following order: firstName, lastName, age, gender, major and cgpa. [Hint: loop until user enters end-of-file indicator (ctrl + d) to end input]. Refer to Sample_output_2.jpg

For all instance variables, use Student specific set and get methods to retrieve the current values. To do this, create an object of Student class and access the Student constructor.

-Call ObjectOutputStream method writeObject() to write the serialized objects to the file object_output.ser.

-Close the object_output.ser file.

-Code the try catch blocks to catch FileNotFoundException and IOException.

====================================================================================================

-Requirements for class Student:

-The class Student implements interface Serializable. [This allows objects of this class to be serialized and deserialized with ObjectOutputStreams and ObjectInputStreams, respectively.]

-Declare the following instance variables:

-String firstName to store first name of the student.

-String lastName to store last name of the student.

-Integer age to store age of the student.

-String gender to store gender of the student.

-String major to store undergraduate major of the student.

-Double cgpa to store the students undergraduate cgpa.

The class Student will require one constructor:

-Constructor will call the appropriate set methods to assign a value to the instance variables in question.

-The constructor will accept six parameters and instantiate a Student object based on those parameters. The parameter values received by the constructor will be assigned to instance variables firstName, lastName, age, gender, major and cgpa in that order.

Set methods will be written all instance variables namely firstName, lastName, age, gender, major and cgpa. Each set method will accept a value of suitable type and assign it to the specific instance variable. Set methods will not return any value.

Get methods will be written for all instance variables namely firstName, lastName, age, gender, major and cgpa. Each get method will have a suitable return type; the returned value will be the current value of the given instance variable. Get methods do not accept any input parameters or values.

There must be no unneeded (not related to one of the requirements) code in your Java class.

==============================================================

==============================================================

input_file.txt is

Mark Gates 29 Male Information systems and cybersecurity 3.75

=============================================================

=============================================================

output_file.txt is

Name: Mark Gates Age: 32 Gender: male Major: cybersecurity CGPA: 3.20

below are how the sample outputs are suppose to look.

i have the .ser files for this just need the code

image text in transcribed

image text in transcribed

UML diagram

InputFromFile

Instance Variables

Description

-firstName : private String

String to hold the students first name

-lastName : private String

String to hold the student's last name

-age : private integer

Integer to hold the student's age

-gender : private String

String to hold the student's gender

-major : private String

String to hold the student's major

-cgpa : private Double

Double to hold the student's cgpa

-input : Scanner

In useScanner() method: Scanner object that will be used to retrieve input from the file

-input : ObjectInputStream

In useInputStreams() method: ObjectInputStream object that will be used to retrieve deserialized objects from the sequential access file

-studentInfo : Student

Object of class Student that is used to access instance variables through get methods.

Methods

Description

+ main (args : String[]) : void

Main method

+useScanner () : void

This method will have code to retrieve input from the file using Scanner class.

+useInputStream () : void

This method will have code to retrieve (deserialized) objects from the sequential access file using ObjectInputStream.

OutputToFile

Instance Variables

Description

-firstName : private String

String to hold the students first name

-lastName : private String

String to hold the student's last name

-age : private integer

Integer to hold the student's age

-gender : private String

String to hold the student's gender

-major : private String

String to hold the student's major

-cgpa : private Double

Double to hold the student's cgpa

-input : Scanner

Scanner object that will be used to scan input from the user

-output : Formatter

In useFormatter() method: Formatter object that will be used to create an output file and write data to that file.

-output : ObjectOutputStream

In useOutputStreams() method: ObjectOutputStream object that will be used to write serialized objects to a sequential access file.

-studentInfo : Student

Object of class Student that is used to access instance variables through get methods by accessing the Student constructor.

Methods

Description

+ main (args : String[]) : void

Main method

+useFormatter () : void

This method will have code to get user input and write to a file using format method.

+useOutputStream () : void

This method will have code to get user input and write (serialized) objects to a sequential access file using ObjectOutputStream.

Student

Instance Variables

Description

-firstName : private String

String to hold the students first name

-lastName : private String

String to hold the student's last name

-age : private integer

Integer to hold the student's age

-gender : private String

String to hold the student's gender

-major : private String

String to hold the student's major

-cgpa : private Double

Double to hold the student's cgpa

Methods

Description

constructor Student (firstName : String, lastName : String, age:integer, gender:String, major:String, cgpa:double ) : Student

Constructor that accepts six input parameters. The received parameters will be assigned to instance variables in the following order: firstName, lastName, age, gender, major and cgpa.

+ setFirstName (firstName : String) : void

Method to assign an input parameter (value) of type String to instance variable firstName

+ setLastName (lastName : String) : void

Method to assign an input parameter (value) of type String to instance variable lastName

+ setAge (age : integer) : void

Method to assign an input parameter (value) of type integer to instance variable age

+ setGender (gender : String) : void

Method to assign an input parameter (value) of type String to instance variable gender

+ setMajor (major : String) : void

Method to assign an input parameter (value) of type String to instance variable major

+ setCgpa (cgpa : double) : void

Method to assign an input parameter (value) of type double to instance variable cgpa

+ getFirstName ( ) : String

Method returning current value of instance variable firstName

+ getLastName ( ) : String

Method returning current value of instance variable lastName

+ getAge ( ) : integer

Method returning current value of instance variable age

+ getGender ( ) : String

Method returning current value of instance variable gender

+ getMajor ( ) : String

Method returning current value of instance variable major

+ getCgpa ( ) : double

Method returning current value of instance variable cgpa

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

Making Databases Work The Pragmatic Wisdom Of Michael Stonebraker

Authors: Michael L. Brodie

1st Edition

1947487167, 978-1947487161

More Books

Students also viewed these Databases questions

Question

How wide are Salary Structure Ranges?

Answered: 1 week ago