Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

In this lab we will deserialize a Java object that contains data about a movie.

ID: 3716631 • Letter: I

Question

 In this lab we will deserialize a Java object that contains data about a movie. The serialized object contains a String containing the movie's title, a String containing the name of the movie's director, and an int containing the year the movie was released.

 Import the attached Movie.java file into an Eclipse project. Make sure the Movie class is in a package named lab6. Next, download the file in the "Lab 6 serialized file" folder. Finally, create a class that contains a main method that deserializes this file and displays the title, director, and year using the Movie class's getter methods.

 Upload the source file with the main method that deserializes the file, and upload a text file that contains the title, director, and year from the serialized file.
 In this lab we will deserialize a Java object that contains data about a movie. The serialized object contains a String containing the movie's title, a String containing the name of the movie's director, and an int containing the year the movie was released.

 Import the attached Movie.java file into an Eclipse project. Make sure the Movie class is in a package named lab6. Next, download the file in the "Lab 6 serialized file" folder. Finally, create a class that contains a main method that deserializes this file and displays the title, director, and year using the Movie class's getter methods.

 Upload the source file with the main method that deserializes the file, and upload a text file that contains the title, director, and year from the serialized file.

Explanation / Answer

Movie.java

import java.io.Serializable;

public class Movie implements Serializable {

private String title;

private String director;

private int year;

public Movie(String title, String director, int year) {

this.title = title;

this.director = director;

this.year = year;

}

public String getTitle() {

return title;

}

public String getDirector() {

return director;

}

public int getYear() {

return year;

}

}

________________

Lab6.java

import java.io.FileInputStream;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.ObjectInputStream;

import java.io.ObjectOutputStream;

import java.util.Scanner;

public class Lab6 {

public static void main(String[] args) {

Movie m1 = null;

String filename = "SerialFile.ser";

String title,director;

int year;

/*

* Creating an Scanner class object which is used to get the inputs

* entered by the user

*/

Scanner sc = new Scanner(System.in);

System.out.print("Enter the Movie title :");

title=sc.nextLine();

System.out.print("Enter the nam eof Director :");

director=sc.nextLine();

System.out.print("Enter the Year :");

year=sc.nextInt();

Movie m = new Movie(title,director,year);

// Serialization

try

{

//Saving of object in a file

FileOutputStream file = new FileOutputStream(filename);

ObjectOutputStream out = new ObjectOutputStream(file);

// Method for serialization of object

out.writeObject(m);

out.close();

file.close();

System.out.println("Movie Object has been serialized");

}

catch(IOException ex)

{

System.out.println("IOException is caught");

}

// Deserialization

try {

// Reading the object from a file

FileInputStream file = new FileInputStream(filename);

ObjectInputStream in = new ObjectInputStream(file);

// Method for deserialization of object

m1 = (Movie) in.readObject();

in.close();

file.close();

System.out.println("Movie Object has been deserialized ");

System.out.println("Movie Name = " + m1.getTitle());

System.out.println("Director = " + m1.getDirector());

System.out.println("Year = " + m1.getYear());

}

catch (IOException ex) {

System.out.println("IOException is caught");

}

catch (ClassNotFoundException ex) {

System.out.println("ClassNotFoundException is caught");

}

}

}

__________________

Output:

Enter the Movie title :Titanic
Enter the nam eof Director :James Cameron
Enter the Year :1996
Movie Object has been serialized
Movie Object has been deserialized
Movie Name = Titanic
Director = James Cameron
Year = 1996

_______________Thank You

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote