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. Create two classes, A and B, with default constructors (empty argument lists) that announce themselves. Inherit a new class called C from A, and create a member of class B inside C. Do not create a constructor for C. Create an object of class C and observe the results.
  2. Modify Exercise 1 so that A and B have constructors with arguments instead of default constructors. Write a constructor for C and perform all initialization within C’s constructor. Create a simple class. Inside a second class, define a reference to an object of the first class. Use lazy initialization to instantiate this object.
  3. Inherit a new class from class Detergent. Override scrub( ) and add a new method called sterilize( ). Take the file Cartoon.java and comment out the constructor for the Cartoon class. Explain what happens.
  4. Take the file Chess.java and comment out the constructor for the Chess class. Explain what happens.
  5. Prove that default constructors are created for you by the compiler. Prove that the base-class constructors are (a) always called and (b) called before derived-class constructors.
  6. Create a base class with only a nondefault constructor, and a derived class with both a default (no-arg) and nondefault constructor. In the derived-class constructors, call the base-class constructor.
  7. Create a class called Root that contains an instance of each of the classes (that you also create) named Component1, Component2, and Component3. Derive a class Stem from Root that also contains an instance of each “component.” All classes should have default constructors that print a message about that class.
  8. Modify Exercise 10 so that each class only has nondefault constructors.
  9. Add a proper hierarchy of dispose( ) methods to all the classes in Exercise 11.
  10. Create a class with a method that is overloaded three times. Inherit a new class, add a new overloading of the method, and show that all four methods are available in the derived class.
  11. In Car.java add a service( ) method to Engine and call this method in main( ).
  12. Create a class inside a package. Your class should contain a protected method. Outside of the package, try to call the protected method and explain the results. Now inherit from your class and call the protected method from inside a method of your derived class.
  13. Create a class called Amphibian. From this, inherit a class called Frog. Put appropriate methods in the base class. In main( ), create a Frog and upcast it to Amphibian and demonstrate that all the methods still work.
  14. Modify Exercise 16 so that Frog overrides the method definitions from the base class (provides new definitions using the same method signatures). Note what happens in main( ).
  15. Create a class with a static final field and a final field and demonstrate the difference between the two.
  16. Create a class with a blank final reference to an object. Perform the initialization of the blank final inside all constructors. Demonstrate the guarantee that the final must be initialized before use, and that it cannot be changed once initialized.
  17. Create a class with a final method. Inherit from that class and attempt to override that method.
  18. Create a final class and attempt to inherit from it. Prove that class loading takes place only once. Prove that loading may be caused by either the creation of the first instance of that class or by the access of a static member.
  19. In Beetle.java, inherit a specific type of beetle from class Beetle, following the same format as the existing classes. Trace and explain the output.

    [31] Don’t fall prey to the urge to prematurely optimize. If you get your system working and it’s too slow, it’s doubtful that you can fix it with the final keyword. However, Chapter 15 has information about profiling, which can be helpful in speeding up your program.

    Thinking in Java
    Prev Contents / Index Next

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