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

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

  




 

 

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. Prove that the fields in an interface are implicitly static and final.
  2. Create an interface containing three methods, in its own package. Implement the interface in a different package. Prove that all the methods in an interface are automatically public.
  3. In c07:Sandwich.java, create an interface called FastFood (with appropriate methods) and change Sandwich so that it also implements FastFood.
  4. Create three interfaces, each with two methods. Inherit a new interface from the three, adding a new method. Create a class by implementing the new interface and also inheriting from a concrete class. Now write four methods, each of which takes one of the four interfaces as an argument. In main( ), create an object of your class and pass it to each of the methods.
  5. Modify Exercise 5 by creating an abstract class and inheriting that into the derived class.
  6. Modify Music5.java by adding a Playable interface. Move the play( ) declaration from Instrument to Playable. Add Playable to the derived classes by including it in the implements list. Change tune( ) so that it takes a Playable instead of an Instrument.
  7. Change Exercise 6 in Chapter 7 so that Rodent is an interface.
  8. In Adventure.java, add an interface called CanClimb, following the form of the other interfaces.
  9. Write a program that imports and uses Month.java.
  10. Following the example given in Month.java, create an enumeration of days of the week.
  11. Create an interface with at least one method, in its own package. Create a class in a separate package. Add a protected inner class that implements the interface. In a third package, inherit from your class and, inside a method, return an object of the protected inner class, upcasting to the interface during the return.
  12. Create an interface with at least one method, and implement that interface by defining an inner class within a method, which returns a reference to your interface.
  13. Repeat Exercise 13 but define the inner class within a scope within a method.
  14. Repeat Exercise 13 using an anonymous inner class.
  15. Modify HorrorShow.java to implement DangerousMonster and Vampire using anonymous classes.
  16. Create a private inner class that implements a public interface. Write a method that returns a reference to an instance of the private inner class, upcast to the interface. Show that the inner class is completely hidden by trying to downcast to it.
  17. Create a class with a nondefault constructor (one with arguments) and no default constructor (no “no-arg” constructor). Create a second class that has a method that returns a reference to the first class. Create the object to return by making an anonymous inner class that inherits from the first class.
  18. Create a class with a private field and a private method. Create an inner class with a method that modifies the outer class field and calls the outer class method. In a second outer class method, create an object of the inner class and call its method, then show the effect on the outer class object.
  19. Repeat Exercise 19 using an anonymous inner class.
  20. Create a class containing a nested class. In main( ), create an instance of the inner class.
  21. Create an interface containing a nested class. Implement this interface and create an instance of the nested class.
  22. Create a class containing an inner class that itself contains an inner class. Repeat this using nested classes. Note the names of the .class files produced by the compiler.
  23. Create a class with an inner class. In a separate class, make an instance of the inner class.
  24. Create a class with an inner class that has a nondefault constructor (one that takes arguments). Create a second class with an inner class that inherits from the first inner class.
  25. Repair the problem in WindError.java.
  26. Modify Sequence.java by adding a method getRSelector( ) that produces a different implementation of the Selector interface that moves backward through the sequence from the end to the beginning. Create an interface U with three methods. Create a class A with a method that produces a reference to a U by building an anonymous inner class. Create a second class B that contains an array of U. B should have one method that accepts and stores a reference to a U in the array, a second method that sets a reference in the array (specified by the method argument) to null, and a third method that moves through the array and calls the methods in U. In main( ), create a group of A objects and a single B. Fill the B with U references produced by the A objects. Use the B to call back into all the A objects. Remove some of the U references from the B.
  27. In GreenhouseControls.java, add Event inner classes that turn fans on and off. Configure GreenhouseController.java to use these new Event objects.
  28. Inherit from GreenhouseControls in GreenhouseControls.java to add Event inner classes that turn water mist generators on and off. Write a new version of GreenhouseController.java to use these new Event objects.
  29. Show that an inner class has access to the private elements of its outer class. Determine whether the reverse is true.

    [33] This approach was inspired by an e-mail from Rich Hoffarth. Item 21 in Joshua Bloch’s Effective Java (Addison-Wesley, 2001) covers the topic in much more detail.

    [34] Thanks to Martin Danner for asking this question during a seminar.

    [35] This is very different from the design of nested classes in C++, which is simply a name-hiding mechanism. There is no link to an enclosing object and no implied permissions in C++.

    [36] Roughly similar to nested classes in C++, except that those classes cannot access private members as they can in Java.

    [37] Thanks again to Martin Danner.

    [38] On the other hand, ‘$’ is a meta-character to the Unix shell and so you’ll sometimes have trouble when listing the .class files. This is a bit strange coming from Sun, a Unix-based company. My guess is that they weren’t considering this issue, but instead thought you’d naturally focus on the source-code files.

    [39] For some reason this has always been a pleasing problem for me to solve; it came from my earlier book C++ Inside & Out, but Java allows a much more elegant solution.

    Thinking in Java
    Prev Contents / Index Next

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