Follow Techotopia on Twitter

On-line Guides
All Guides
eBook Store
iOS / Android
Linux for Beginners
Office Productivity
Linux Installation
Linux Security
Linux Utilities
Linux Virtualization
Linux Kernel
System/Network Admin
Programming
Scripting Languages
Development Tools
Web Development
GUI Toolkits/Desktop
Databases
Mail Systems
openSolaris
Eclipse Documentation
Techotopia.com
Virtuatopia.com
Answertopia.com

How To Guides
Virtualization
General System Admin
Linux Security
Linux Filesystems
Web Servers
Graphics & Desktop
PC Hardware
Windows
Problem Solutions
Privacy Policy

  




 

 

Thinking in Java
Prev Contents / Index Next

Exercises

Solutions to selected exercises can be found in the electronic document The Thinking in Java Annotated Solution Guide, available for a small fee from www.BruceEckel.com.

  1. Demonstrate a second level of aliasing. Create a method that takes a reference to an object but doesn’t modify that reference’s object. However, the method calls a second method, passing it the reference, and this second method does modify the object.
  2. 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 reference 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.
  3. 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.
  4. Change CheckCloneable.java so that all of the clone( ) methods catch the CloneNotSupportedException rather than passing it to the caller.
  5. Using the mutable-companion-class technique, make an immutable class containing an int, a double, and an array of char. 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.
  6. Starting with Snake.java, create a deep-copy version of the snake.
  7. Implement the Collection interface in a class called CloningCollection by using a private ArrayList to provide the container functionality. Override the clone( ) method so that CloningCollection performs a “conditional deep copy”; it attempts to clone( ) all the elements it contains, but if it cannot it leaves the reference(s) aliased. href="TIJ319_002.htm">[116] In C, which generally handles small bits of data, the default is pass by value. C++ had to follow this form, but with objects pass by value isn’t usually the most efficient way. In addition, coding classes to support pass by value in C++ is a big headache.

    [117] This is not the dictionary spelling of the word, but it’s what is used in the Java library, so I’ve used it here, too, in some hopes of reducing confusion.

    [118] You can apparently create a simple counter-example to this statement, like this:

    public class Cloneit implements Cloneable {
      public static void main (String[] args) 
      throws CloneNotSupportedException {
        Cloneit a = new Cloneit();
        Cloneit b = (Cloneit)a.clone();
      }
    }

    However, this only works because main( ) is a method of Cloneit and thus has permission to call the protected base-class method clone( ). If you call it from a different class, it won’t compile.

    [119] Except for the poor avocado, which has been reclassified to simply “fat.”

    [120] C++ allows the programmer to overload operators at will. Because this can often be a complicated process (see Chapter 10 of Thinking in C++, 2nd edition, Prentice Hall, 2000), the Java designers deemed it a “bad” feature that shouldn’t be included in Java. It wasn’t so bad that they didn’t end up doing it themselves, and ironically enough, operator overloading would be much easier to use in Java than in C++. This can be seen in Python (see www.Python.org) which has garbage collection and straightforward operator overloading.

    [121] Doug Lea, who was helpful in resolving this issue, suggested this to me, saying that he simply creates a function called duplicate( ) for each class.

    Thinking in Java
    Prev Contents / Index Next

 
 
   Reproduced courtesy of Bruce Eckel, MindView, Inc. Design by Interspire