THE Java™ Programming Language, Fourth Edition

(Jeff_L) #1

Exercise 3.7: Override equals and hashCode for ColorAttr. 101


Exercise 3.8: Make Vehicle and PassengerVehicle into Cloneable types.


Which of the four described attitudes should each class take toward cloning? Is the simple copying done by
Object.clone correct for the clone methods of these classes? 107


Exercise 3.9: Write a Garage class whose objects can hold up to some number of Vehicle objects in an
array. Make Garage a Cloneable type, and write a proper clone method for it. Write a Garage.main
method to test it. 107


Exercise 3.10: Make your LinkedList class (from the exercises in Chapter 2) Cloneable, with clone
returning a new list that refers to the same values as the original list, not clones of the values. In other words,
changes to one list should not affect the other list, but changes to the objects referenced by the list would be
visible in both lists. 107


Exercise 3.11: Find at least one security hole in SortDouble that would let a sorting algorithm cheat on its
metrics without getting caught. Fix the security hole. Assume that the sorting algorithm author doesn't get to
write main. 114


Exercise 3.12: Write a general-purpose SortHarness class that can sort any object type. How would you
provide a way to represent ordering for the objects in a general way, given that you cannot use < to compare
them? 114


Exercise 4.1: Rewrite your solution to Exercise 3.6 on page 99 using an interface for EnergySource
instead of an abstract class. 131


Exercise 4.2: Rewrite your solution to Exercise 3.12 on page 114 using an interface if you didn't write it that
way in the first place. 132


Exercise 4.3: Should the LinkedList class from previous exercises be an interface? Rewrite it that way
with an implementation class before you decide. 132


Exercise 4.4: Design a collection class hierarchy using only interfaces. 132


Exercise 4.5: Think about whether the following types should be represented as interfaces, abstract classes, or
concrete classes: (a) TReeNode to represent nodes in an N-ary tree; (b) treeWalker to walk the tree in a
particular order (such as depth-first or breadth-first); (c) Drawable for objects that can be drawn by a
graphics system; (d) Application for programs that can be run from a graphical desktop. 132


Exercise 4.6: What changes in your assumptions about each of the problems in Exercise 4.5 would make you
change your answers? 132


Exercise 5.1: Consider the Attr class and Attributed interface from Chapter 4. Should one of these be a
nested type of the other? If so, which way makes the most sense? 136


Exercise 5.2: Create a version of BankAccount that records the last ten actions on the account. Add a
history method that returns a History object that will return Action objects one at a time via a next
method, returning null at the end of the list. Should History be a nested class? If so, should it be static or
not? 138


Exercise 6.1: Define simple enums for the days of the week and traffic light colors. 152


Exercise 6.2: Redo your solution to Exercise 2.17 to use an enum to represent the TURN_LEFT and

Free download pdf