Java Reference
In-Depth Information
this, you will need to add a constructor, a private instance variable, and a method
called
getDelay( )
. After making these changes, what improvements do you see? On
your own, can you think of other improvements? (Hint: Try using ordinal values to
switch light colors rather than relying on a
switch
statement.)
The improved version of the traffic light simulation is shown here. There are two ma-
jor improvements. First, a light's delay is now linked with its enumeration value,
which gives more structure to the code. Second, the
run( )
method no longer needs to
use a
switch
statement to determine the length of the delay. Instead,
sleep( )
is passed
tlc.getDelay( )
, which causes the delay associated with the current color to be used
automatically.