Java Reference
In-Depth Information
P ROJECTS
1.
Define a pair of companion classes modeled after the class Student , as given in Segment C.2 of Appendix C.
Project 5 in Chapter 7 asked you to write a class KnapsackItem of items that would be placed in a list. Design
and implement a pair of companion classes for such knapsack items.
2.
Project 6 in Chapter 7 asked you to write a class Activity of activities that would be placed in a list. Design and
implement a pair of companion classes for such activities.
3.
4.
Chapter 13 describes an implementation of the ADT list that uses an instance of the class Vector to represent the
entries in the list. Write a clone method for this implementation and demonstrate that it works. Note that Vector
implements the interface Cloneable .
5.
Revise the linked implementation of the ADT sorted list, as given in Chapter 16, according to the suggestions
about cloning given in Segment 30.31. That is, the method add should place a clone of the desired entry, instead of
the entry itself, into the sorted list. Additionally, the method getEntry should return a clone of the desired entry,
instead of returning the entry itself.
6.
Suppose that you wanted to implement a deep copy for the class FamilyMember defined in Exercise 15 in this
chapter.
a. What difficulty will arise?
b. Implement a clone method for FamilyMember that makes a deep copy.
7.
Define three classes— A , B , and C —such that C is a subclass of B , and B is a subclass of A . Each class should define
a clone method and have at least one data field that is a mutable, cloneable object.
A NSWERS TO S ELF -T EST Q UESTIONS
1.
public ImmutableName(Name aName)
{
first = aName.getFirst();
last = aName.getLast();
} // end constructor
2.
// Create an object of the class Name
Name derek = new Name("Derek", "Greene");
// Convert the object to an immutable object; don't change its data fields
ImmutableName derekI = derek.getImmutable();
// Add the immutable object to the sorted list nameList
SortedListInterface<ImmutableName> nameList = new SortedList<ImmutableName>();
nameList.add(derekI);
3.
// Create an object of the class ImmutableName
ImmutableName lila = new ImmutableName("Lila", "Bleu");
// Convert the object to a mutable object; don't change its data fields
Name changer = lila.getMutable();
// Change the last name of the new object
changer.setLast("Greene");
// Convert the revised mutable object to an immutable object
ImmutableName unchanger = changer.getImmutable();
4.
a.
No. The clone y has a name object that is distinct from x 's name object, because a deep copy was made.
(See Figure 30-7.)
b.
Yes. Both objects share one name object. (See Figure 30-8.)
Search WWH ::




Custom Search