CSE 341 -- Programming LanguagesAutumn 2002 |
Department of Computer Science and Engineering, University of WashingtonSteve Tanimoto (instructor) |
Assignment 4Version 1.0 of November 17 (Subject to Change) |
Java Traffic Lights and TurtlesDue date and time: Thursday, November 21, 2002 (at the beginning of section).
|
Instructions:
Part A (Individual Work)
Read Chapters 1 through 6, and 8 of Learning Java. Chapters 15, 16, and 22
are recommended, too, since they may help you with this assignment
and/or with your project implementations.
Write a Java applet that displays a traffic light.
There should be a button next to the traffic light
labeled "next state". When the user clicks on the
button, the state of the light should be advanced
by one. The light should have three states:
green, yellow, and red. The states are visited
cyclically, with green following red, etc.
Remember to let your applet's paint method do all
the rendering of graphics. It can check a state
variable to find out the current phase of the light.
Place either a .jar file or the relevant class files
on a UW web server of your choice (e.g., abstract.cs.washington.edu).
Create a suitable web page to host the applet.
Turn in your applet and code by
turning in (by a method to be announced) the following two items.
A complete URL to your working applet page.
Java source code for your applet. If you have multiple
files, please post all of them on the same page, unless
they are very large, in which case you can use additional
replies to the "Assignment 4, Part 1" post.
B. (Optional)
Add the following features to your traffic light applet:
1. Autonomy, so that the traffic light changes by itself with
a cycle consisting of 3 seconds of green, 1 second of yellow,
and 4 seconds of red. 2. A walk/don't-walk sign that normally
reads as "Don't walk". 3. A button (for pedestrians) to request
a walk phase in the next traffic light cycle. The sign should
read as "Walk" for 2 seconds before the light turns green,
and the light should stay red for those extra 2 seconds.
Once the walk phase has ended, no new walk phase should happen
unless the button is pressed again. If you want to get fancy,
make the walk phase 3 seconds long, with the first second showing
"Walk" and the next two seconds showing a flashing "Don't Walk".
You can also make the pedestrian button "illuminated", which mean
it lights up when pressed to show that a walk phase is pending;
it's light goes out when the walk phase begins.
To achieve the timing needed in the extra credit option,
you should use a separate Java thread,
and call the Thread method sleep for the appropriate numbers
of milliseconds in between changes to the phase of the traffic
light.
Part II (Group Work)
In this part, turn in your solutions using the
same method as in Part I. Only one
solution per team is required for each problem.
"Turtle graphics" is a type of drawing capability originally
developed as part of the Logo language at the Bolt, Beranek, and Newman
Corporation in the 1970s. For more information about turtle graphics
and Logo, see this
tutorial.
Implement a turtle-graphics programming facility with the
following features:
Extra credit:
Implement one or more of the following additional features.
MAKE "X 50
PRINT :X
PRINT 5 * :X
Handle the operations +, *, -, and /.
IF :I < 10 PRINT :I
Turtle Graphics Commands to be Handled in Part II.
Tool Recommendations
If you are new to Java, the best platform to use is probably
BlueJ. See our syllabus page for a link to the BlueJ home page,
with free download available.
Whether or not you are new to Java, you will need to use
the Java Development Kit (JDK) from Sun Microsystems. BlueJ requires
that it already be installed. And you can also used the JDK without
any other tool like BlueJ. The recommended version of the JDK is 1.4,
since this is the version that will be used by the graders.
If you don't use the same version as the graders, then there is
some possibility that your program will not run when they evaluate it.
Documentation and Style
Provide a separate plain ASCII text file that documents your program.
First describe the set of possible programs that can be written in
your system. What is the intended range of applications? What
are some features that are NOT handled?
Give a list of the language constructs such as particular functions
that your system provides. For each construct, show an example
of its use.
Provide an implementation description.
This should include the following: (a) how your code is structured,
(b) the inheritance hierarchy for the classes you define, (c) a
description of each interface you have defined, (d) what the purpose
of each class is.
Use conventional
Java programming style.