0% found this document useful (0 votes)
17 views4 pages

Java Serialization and Reflection Lab

This document describes an advanced programming lab on reflection and serialization in Java. It includes two parts: Part 1 involves serializing and deserializing a time object to save and retrieve the current time. Students are provided code templates to serialize and deserialize a time object. They are asked to modify the code to add a new field and observe errors during deserialization. Part 2 covers reflection and interfaces in Java. Students study code examples of a list implementation using interfaces. Exercises involve compiling and running the code, comparing interface methods, and using reflection to get class names and method names.

Uploaded by

deardeago
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views4 pages

Java Serialization and Reflection Lab

This document describes an advanced programming lab on reflection and serialization in Java. It includes two parts: Part 1 involves serializing and deserializing a time object to save and retrieve the current time. Students are provided code templates to serialize and deserialize a time object. They are asked to modify the code to add a new field and observe errors during deserialization. Part 2 covers reflection and interfaces in Java. Students study code examples of a list implementation using interfaces. Exercises involve compiling and running the code, comparing interface methods, and using reflection to get class names and method names.

Uploaded by

deardeago
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Cairo University Faculty of Engineering Computer Engineering Department CMP (N) 306

Spring 2012

Advanced Programming Lab Reflection and Serialization

Prerequisites
In this lab, it is assumed that student is able to: - Write a simple program in Java as a startup.

Objectives
By the end of this lab, the student should be able to: - Learn about the Reflection and Serialization in Java

Resources

Discover the secrets of the Java Serialization API article from [Link] Object Serialization tutorial from [Link] Java Serialization tutorial from [Link]

Part 1: Serialize and Deserialize an object A. You are asked to use the concept of serialization to save the current time and use the concept of deserialization to read it back when it is needed. You may use the following template as a guide.
// import the required classes public class SerializeTime{ private Date time; public static void main(String [] args){ String filename = "[Link]"; if([Link] > 0) { filename = args[0]; } // Create an object PersistentTime time = new PersistentTime(); // Serialize the object instance and save it in a file.

[Link]("Current time is saved into " + filename); } } Hint : the current time could be get using the following method. [Link]().getTime();

B. To deserialize the time , you may fill the following template:


// import the required classes public class DeserializeTime { public static void main(String [] args) { String filename = "[Link]"; if([Link] > 0) { filename = args[0]; } // Deserialize the previously saved // PersistentTime object instance.

// print out restored time [Link]("Previously serialized time: " + [Link]()); // print out the current time [Link]("Current time: " + [Link]().getTime()); } }

C. In this part , you do the following: - Use your previous code to serialize the time class PersistentTime. - Modify the class to include a new variable ; for instance you may add :
private String aNewField;

Try to run the deserialization code and report what happen and why?

Part 2: Reflection

Objectives
By the end of this lab, the student should be able to: - Deal with interfaces in java - Know how to implement interfaces - Know how to extend any class in java PreLab

The following is the implementation of a List using interface stored in [Link]:


[Link] provides definitions for a simple node. [Link] defines a very simple collection of list operations. [Link] implements [Link], adding a few additional functions. [Link] extends [Link] by changing the insert method to maintain an ordered list.

You are required to study these classes and identify the relations among them. Exercise 1: 1. Copy these files to your account, compile them (in the order listed above), and then run both SimpleList and OrderedList. 2. Compare the methods defined in [Link] with those implemented in [Link]. a. Be sure all methods specified in [Link] are implemented. b. Does [Link] implement any additional public or private methods? If so, identify which new methods are defined. c. Temporarily delete the isin method from [Link] and recompile. Explain any error message(s) you encounter. (When you are done with this part, restore [Link] to its original form.) 3. Review the testing section of [Link], and describe a likely testing strategy that led to this sequence. 4. Review [Link], and explain how the insert method works. 5. While testing in [Link] includes printing after each list insertion, testing in [Link] performs a sequence of insertions before printing. Do you think this abbreviated testing for [Link] is sufficiently complete? Briefly explain your answer.

Exercise 2: 1. Add the following lines to the testing part and explain the output to the lab instructor
[Link](a); [Link]([Link]());

2. Add a new Method to the previous project to delete any element from the List? Show where is the best place to add it and why? Example :
[Link] (5);

3. Using the reflection concept : a. Get the class name ? b. Print the names of all the declared methods in OrderedList class

You might also like