Exercises


  1. Using the RingTest and Ring classes from the previous lesson, introduce an assert into the add method that rejects a null argument. Make sure you write a test! Don't forget to enable assertions before running your tests.

  2. Create an annotation @Dump. This annotation can be applied to any field in a class. Then create a ToStringer class which, when passed an object, will spit out a dump of each field that has been marked for use in the toString method.

  3. Modify the dump annotation to allow for sorting the fields by adding an optional order parameter. The order should be a positive integer. If a field is not annotated, it should appear last in the list of fields.

  4. Add another parameter for dump called quote, which should be a boolean indicating whether or not to surround the value with quotes. This is useful for objects whose toString representation might be empty or have leading or trailing spaces.

  5. Add an outputMethod field to the @Dump annotation. This specifies the method for toStringer to use in order to get a printable representation of the field. Its value should default to toString. This is useful for when you have an object with a toString representation you cannot change, such as an object of a system class type.

  6. Change the outputMethod annotation to outputMethods and have it support a String array of method names. The ToString code should construct a printable representation of the object by calling each of these method names in order and concatenating the results. Separate each result with a single space. (You might consider adding another annotation to designate the separator character.)



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