CSE 341 -- Programming Languages

Autumn 2002

Department of Computer Science and Engineering, University of Washington

Steve Tanimoto (instructor)

Assignment 4

Version 1.0 of November 17 (Subject to Change)

Java Traffic Lights and Turtles 

Due date and time: Thursday, November 21, 2002 (at the beginning of section).


 

Title: Java Traffic Lights and Turtles

Purposes:

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.
 
  • Allow the turtle to move in 3 dimensions. To do this, keep track of a z coordinate of the turtle. Display the drawing using "orthographic projection". You may need to come up with new conventions for turning right and left, and for heading up and down relative to the current course. (Perhaps think of an airplane with yaw and pitch.)
  • Implement variables having one-letter names that can store numbers. Assume the letters are always capitals, like X, not x. Assume all numbers are represented as Java doubles but allow them to be input as either integers or decimal fractions. Numbers can be negative, using the minus sign "-". Use the syntax given in the Logo Tutorial: e.g.,
    MAKE "X 50
    PRINT :X
    
  • Implement simple arithmetic expressions that can be used on statements such as MAKE and PRINT statements. For example,
    PRINT 5 * :X
    
    Handle the operations +, *, -, and /.
  • Implement IF where the condition can be a comparison using < or =. For example,
    IF :I < 10 PRINT :I
    
  • Implement function definition and call, (with arguments) using the TO syntax shown in the LOGO tutorial.
  • Allow functions to work recursively.
  • Provide a way for the turtle's pen color to be changed.

  •  

    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.