Exercises
Posted
on March 1st, 2001
| Bruce Eckel's Thinking in Java | Contents | Prev | Next |
- 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.
- 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.
- Change CheckCloneable.java so that all of the clone( ) methods catch the CloneNotSupportedException rather than passing it to the caller.
- 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.
- Starting with Snake.java, create a deep-copy version of the snake.

Comments
There are no comments yet. Be the first to comment!