Sie sind auf Seite 1von 4

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

Spring 2012

Advanced Programming Lab Reflection and Serialization

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

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


Discover the secrets of the Java Serialization API article from Object Serialization tutorial from Java Serialization tutorial from

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 = "time.ser"; if(args.length > 0) { filename = args[0]; } // Create an object PersistentTime time = new PersistentTime(); // Serialize the object instance and save it in a file.

System.out.println("Current time is saved into " + filename); } } Hint : the current time could be get using the following method. Calendar.getInstance().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 = "time.ser"; if(args.length > 0) { filename = args[0]; } // Deserialize the previously saved // PersistentTime object instance.

// print out restored time System.out.println("Previously serialized time: " + time.getTime()); // print out the current time System.out.println("Current time: " + Calendar.getInstance().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

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 provides definitions for a simple node. defines a very simple collection of list operations. implements, adding a few additional functions. extends 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 with those implemented in a. Be sure all methods specified in are implemented. b. Does implement any additional public or private methods? If so, identify which new methods are defined. c. Temporarily delete the isin method from and recompile. Explain any error message(s) you encounter. (When you are done with this part, restore to its original form.) 3. Review the testing section of, and describe a likely testing strategy that led to this sequence. 4. Review, and explain how the insert method works. 5. While testing in includes printing after each list insertion, testing in performs a sequence of insertions before printing. Do you think this abbreviated testing for 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
System.out.println(a); System.out.println(a.toString());

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 :
a.Delete (5);

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