Java Reference
In-Depth Information
Answers to Self-Test Exercises
1. The invocation of
Thread.sleep
takes place inside a thread. Every action in Java
takes place inside some thread. Any action performed by a method invocation in
a specific thread takes place in that thread. Even if it does not know where it is, a
method's action takes place where the method invocation is; if you were lost and
yelled out, you might not know where you are but the yell would still be wher-
ever it is you are.
2. The class
Packer
inherits the method
start
from the base class
Thread
and it is
not overridden. The method
start
invokes the method
run
, but when
start
is
invoked by an object of the class
Packer
, it is the definition of
run
that is given in
the class
Packer
that is used, not the definition of
run
given in the definition of
Thread
. That is exactly what is meant by late binding or polymorphism.
3. A port is used to identify which program should receive data from the network.
Only one program may be bound to a specific port.
4. Each client connection may run in its own thread, requiring a large amount of
memory, disk space, or processor time.
5.
Persistence
means that a component's state can be saved so that the next time it is
used it remembers what state it was in.
6. The same one that is used for Swing and AWT.
7. SELECT Result.Title, Names.Author, Result.Author_ID
FROM Result, Names
WHERE Result.Author_ID = Names.Author_ID
Programming Projects
Many of these Programming Projects can be solved using AW's CodeMate.
To access these please go to:
www.aw-bc.com/codemate
.
1.
Write a GUI program that uses the methods in the
Graphics
class together with
threads to draw a smiley face, wait two seconds, draw a frowny face, wait two
seconds, and repeat starting with the smiley face. A separate thread should be
started to implement the two-second timer. The application should be respon-
sive and exit immediately if the user decides to close the window.
2.
Create a class named
TimerAlarm
that extends Thread and implements a timer.
Don't use the built-in Timer class for this exercise. Your class constructor should
take as input an integer named t representing time in milliseconds and an inter-
face object named
obj
that defines a method named
alarmAction( )
. You will
need to define this interface. Every
t
milliseconds the class should invoke
method
obj.alarmAction( )
. Add
pause( )
and
play( )
methods that disable
and enable the invocation of
alarmAction( )
. Test your class with code that
increments and prints out a counter.