Exercises


  1. Use an anonymous inner class of type Comparable to sort the chessboard positions that you created in an earlier exercise.

  2. When you debug, you will often find that a toString method doesn't provide you with enough information. At times like this, it may be handy to have a utility method to expose, or "dump," information that is more internal.

    Create an object-dumper utility class that takes an object and lists every field name on the object and the current dump of those objects, recursively, using a hierarchical output format. Do not traverse into classes from java or javax packages. The utility should be able to display private fields. Mark static fields as such. For simplicity's sake, ignore fields from any superclass.

  3. Yet another clone exercise: Implement another version of the poor man's clone, this time using reflection operations. Obtain a Constructor object from the source object's class. Call newInstance to create the new object and copy each field's values from the source object to the new object. The class being cloned will need to provide a no-argument constructor. Build support for a shallow copy only.

  4. Create a Proxy class that delegates to the original object for every method it defines, except for toString. When the proxy receives the toString message, it delegates to the object dumper instead. The target object will need to implement an interface type that defines toString, since proxies work off an interface.



Agile Java. Crafting Code with Test-Driven Development
Agile Javaв„ў: Crafting Code with Test-Driven Development
ISBN: 0131482394
EAN: 2147483647
Year: 2003
Pages: 391
Authors: Jeff Langr

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net