AP Computer Science Homework Set 14 Serialization

whooshribbitSoftware and s/w Development

Dec 2, 2013 (3 years and 6 months ago)

98 views

Page
1

AP Computer Science



Unit 14

Programs

http://thecubscientist.com

AP Computer Science

Homework Set 14

Serialization


1.

When saving objects using serialization, only the state of the objects is
saved (i.e. the instance variables). The methods or behavior of these objects is
not saved. When you restore or deserialize an o
bject, how you restore the
behavior of the object?


Answer:









2.

What is the difference between saving a file using serialization vs. saving
to a text file?


Answer:



















Page
2

AP Computer Science



Unit 14

Programs

http://thecubscientist.com


3.

The process of serialization requires that you create a FileOu
tputStream
object (connection stream) and an ObjectOutputStream object (chain stream)
before you actually write the object to a file. Think about the two parts of an
extension cord; the cord and the plug. Using this analogy, what do the
FileOutputStream an
d the ObjectOutputStream objects correspond to?


Answer:






4.

The FileOutputStream and ObjectOutputStream are separate objects that
in the process of serialization are linked together. Why did the designers of Java
make them two different objects as opp
osed to just one object? Use the extension
cord example in your explanation and think about electrical outlets when
“traveling abroad”.


Answer:












5.

If you want to use serialization to save an object that contains instance
variables that are refe
rences to OTHER objects, what special steps need to be
taken to ensure that these other objects are serialized as well?


Answer:






Page
3

AP Computer Science



Unit 14

Programs

http://thecubscientist.com


6.

Using the FileOutputStream and ObjectOutputStream requires the use of
try
-
catch blocks because these objects can thro
w exceptions. Search the Java 5
API and name two exceptions that each of these objects “throw”.


Answer:









7.

What is the purpose of the “transient” keyword and when would you want
to use it?


Answer:









8.

Marking an instance variable as “trans
ient” can cause a complicated
issues since it is possible that this instance variable may refer to other objects,
which would NOT be saved in the object graph. Describe the options for solving
this “problem”.


Answer:






Page
4

AP Computer Science



Unit 14

Programs

http://thecubscientist.com

9.

Since static variables are NO
T saved during the serialization process, what
value does an object get when it is deserialized? What problems might this
cause?


Answer:




Page
5

AP Computer Science



Unit 14

Programs

http://thecubscientist.com

10.

True or false time…

a.

The interface “Serializable” has one method serialize() that need
s

to
be implemented.

b
.

In order for an object graph to be completely serialized, all objects
in the graph must be “serializable”.

c.

Any class can be serializable as long as it implements the
“Serializable”interface.

d.

When a serializable object is “deserialized” its construc
tor is
executed during the process of recreating the object.

e.

During the serialization process, ONE copy of the class is saved so
that during the deserialization process the JVM knows what class
type to return.

f.

When

saving files using serialization
, y
ou
MUST

use try
-
catch
blocks.


Answer:





Page
6

AP Computer Science



Unit 14

Programs

http://thecubscientist.com

11.

The process of “deserialization” is almost the exact reverse process of
serialization except for one step. What is the one extra step and why is it needed?


Answer:





12.

What is the main advantage of using

a java.io.File object instead of a
String when saving text files?


Answer:





13.

What does it mean to “parse” a String?


Answer:






Page
7

AP Computer Science



Unit 14

Programs

http://thecubscientist.com

14.

Examine the GameCharacter class on page 445. Which one of the changes
(listed as “a”, “b”, and “c” as comments) in

this updated GameCharacter class
would cause a VersionID error during a subsequent deserialization process?


public class GameCharacter
extends Character
implements
Serializable

// (a) GameCharacter is now derived from



// a new superclass


{


int power;


String type;


String[] weapons;


String name;


//
(b
) adding a name instance variable




public GameCharacter(int p, String t, String[] w)


{


power = p;


type = t;


weapons = w;



}




public int getPower(
int p
)

// (c ) changing parameter for


{

// getPower() method


return power

* p
;


}




public String getType()


{


return type;


}




public String getWeapons(
)


{


String weaponList = "";


for (int i = 0; i < weapons.length; i++)


{


weaponList += weapons[i] + " ";


}


return weaponList;


}


}



Answer: