Exercises

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

  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



Comments

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

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • On-demand Event Event Date: September 10, 2014 Modern mobile applications connect systems-of-engagement (mobile apps) with systems-of-record (traditional IT) to deliver new and innovative business value. But the lifecycle for development of mobile apps is also new and different. Emerging trends in mobile development call for faster delivery of incremental features, coupled with feedback from the users of the app "in the wild." This loop of continuous delivery and continuous feedback is how the best mobile …

  • A modern mobile IT strategy is no longer an option, it is an absolute business necessity. Today's most productive employees are not tied to a desk, an office, or a location. They are mobile. And your company's IT strategy has to be ready to support them with easy, reliable, 24/7 access to the business information they need, from anywhere in the world, across a broad range of communication devices. Here's how some of the nation's most progressive corporations are meeting the many needs of their mobile workers …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds