Bruce Eckel's Thinking in Java Contents | Prev | Next

Exercises

  1. Create a class myString containing a String object that you initialize in the constructor using the constructor’s argument. Add a toString( ) method and a method concatenate( ) that appends a String object to your internal string. Implement clone( ) in myString. Create two static methods that each take a myString x handle as an argument and call x.concatenate(“test”), but in the second method call clone( ) first. Test the two methods and show the different effects.
  2. Create a class called Battery containing an int that is a battery number (as a unique identifier). Make it cloneable and give it a toString( ) method. Now create a class called Toy that contains an array of Battery and a toString( ) that prints out all the batteries. Write a clone( ) for Toy that automatically clones all of its Battery objects. Test this by cloning Toy and printing the result.
  3. Change CheckCloneable.java so that all of the clone( ) methods catch the CloneNotSupportedException rather than passing it to the caller.
  4. Modify Compete.java to add more member objects to classes Thing2 and Thing4 and see if you can determine how the timings vary with complexity – whether it’s a simple linear relationship or if it seems more complicated.
  5. Starting with Snake.java, create a deep-copy version of the snake.
4

Contents | Prev | Next