|
|
|
|
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.
- Prove that the fields in an interface are implicitly static
and final.
- 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.
- In c07:Sandwich.java, create an interface called FastFood
(with appropriate methods) and change Sandwich so that it also implements
FastFood.
- 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.
- Modify Exercise 5 by creating an abstract class and inheriting that
into the derived class.
- 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.
- Change Exercise 6 in Chapter 7 so that Rodent is an
interface.
- In Adventure.java, add an interface called CanClimb,
following the form of the other interfaces.
- Write a program that imports and uses Month.java.
- Following the example given in Month.java, create an enumeration of
days of the week.
- 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.
- 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.
- Repeat Exercise 13 but define the inner class within a scope within a
method.
- Repeat Exercise 13 using an anonymous inner class.
- Modify HorrorShow.java to implement DangerousMonster and
Vampire using anonymous classes.
- 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.
- 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.
- 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.
- Repeat Exercise 19 using an anonymous inner class.
- Create a class containing a nested class. In main( ), create an
instance of the inner class.
- Create an interface containing a nested class. Implement this
interface and create an instance of the nested class.
- 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.
- Create a class with an inner class. In a separate class, make an instance
of the inner class.
- 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.
- Repair the problem in WindError.java.
- 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.
- In GreenhouseControls.java, add Event inner classes that turn
fans on and off. Configure GreenhouseController.java to use these new
Event objects.
- 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.
- 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.
|
|
|