CS 226: Data Structures, Program #1

Turtle Tongue

Assigned: Monday, September 13, 2003
Due: Monday, September 20, 2003 (UNGRADED)

(Reminder: Please do not look at each other's source code except for any designated project partner. For the graded Project #1 which builds on this program, we will be actively looking for programs that are "too similar", and I do not wish to find any.)


This program is the first component of the Turtle Commander project, an interpreter which takes in commands from the keyboard control a "turtle graphics" drawing interface. The turtle has some state, like a position, orientation, color, etc. When you move the turtle, it can draw lines. In this first assignment, you will be parsing lines of text containing turtle commands.


Your parser should recognize the following commands:

move <distance>
turn <degrees_clockwise>
color <red> <green> <blue>
pen up|down
autodisplay on|off

Each command token should be case insensitive. <distance> is a positive floating point number. <degrees_clockwise> is a signed floating point number. <red>, <green>, and <blue> are floating point numbers in the range [0.0, 1.0].


You should create classes which implement the TurtleCommand  interface:
interface TurtleCommand {
public void print();
Each command should get its own class, but you should derive them from a single class. The interface will be expanded in future assignments, and you may then benefit from some inherited functions. The print function should output something like this:
COMMAND: color   PARAMETER #1: 1.0   PARAMETER #2: 0.0   PARAMETER #3: 0.5
In other words, the print function should make it clear that you have parsed the individual commands into their components.

You should also create a TurtleTongue class which contains the main program. The program should perform the parsing of commands, line by line. For each command, it should instantiate the proper class and call the print function. You do not need to store these commands yet. When you encounter the exit command, you should exit the program after printing it.

Reading User Input

The user input is a sequence of valid tokens, with a carriage return terminating the expression and other white space ignored. To simplify the parsing of user input, you may use the StreamTokenizer or the StringTokenizer class (or you can do it by manual parsing of characters, but you will probably find this much more painful). The StreamTokenizer actually has a mechanism for distinguishing between numeric and string tokens. The StringTokenizer just breaks the string into substrings, and you have to do your own determination of string versus numeric values. You can set up the input for reading as follows:
java.io.BufferedReader in = new java.io.BufferedReader(new java.io.InputStreamReader(System.in));
StreamTokenizer tok = new StreamTokenizer(in);
After initializing tok to accept the correct set of input tokens, you can get tokens from the user input by calling
tok.nextToken( );
For testing purposes, you may find it handy to redirect a text file containing turtle commands to the standard input stream rather than typing the commands each time you run the program. Under a windows-type command prompt, you would do this as:
type <commandfile> | java TurtleTongue
Under unix, that would be:
cat <commandfile> | java TurtleTongue


You may use the java.io and java.lang class libraries. In general, you may not use classes from the java.util library unless you receive permission to do so for some particular class. Standard exceptions to this rule are StringTokenizer and the Random (which may be useful for testing some of your programs and does not jeopardize the goals of the class).

Submission - Read  this Carefully

Your submission should include a set of .java files and a README.txt file with any information you want to tell the grader about running your program (even though this one is ungraded). If you are working with a designated project partner, you should specify the submission ids of the team members (you only need to submit the program under a single persons submission id). Your main program class should be called TurtleTongle, and thus it should be compilable using "javac *.java" and runnable with the command "java TurtleTongue". You should submit your program as a single .zip (zip archive) file. This file should have no directory structure in it -- just the actual files, so extracting them should result in all your original files placed in the current directory.

To submit, you should follow these steps:
  1. Create the .zip file for submission
  2. Make a new, empty directory
  3. Copy the submission file to the new directory
  4. Extract all the files from your submission file into the new directory
  5. Verify that you can compile and run the program on your test cases in the new directory
  6. Go to the submissions page
  7. Submit the .zip file, making sure that the file appears in the directory listing displayed in the browser after submission (the file size listed could be slightly different on the recipient SunOS machine from that of a Windows  OS machine, but they should be very similar)
  8. Also in the submissions page, View the submission to be certain that the file is there (you should see basically the same directory listing you saw at the end of step 7).
If anything goes wrong with steps 7 or 8, try repeating them (resubmitting your file overwrites any previous submission and time stamp). If you cannot successfully submit your assignment, immediately send e-mail to the professor and the head TA explaining the problem (with all details so we can hopefully fix it) and including the submission file as an e-mail (MIME) attachment. It is your responsibility to verify that the assignment is received. If we do not have an assignment or record of its submission, it will receive zero points.


Although this initial program is not graded, the complete multipart project will eventually be graded based on the following general criteria:
Compiling and running
Proper execution on a number of our test cases (with valid and invalid inputs)
Overall implementation correctness
Comments, style, and readability (informative,  but not extraneous comments are appreciated)
If a project does not compile, you can expect to receive very few points, if any. This should encourage you to take care in your submission process.

Final Words of Encouragement

Good luck with your first assignment! Although it is ungraded, we hope you will take it seriously and submit it on time (it's also a good chance to try out the submission system). There will be a follow up assignment after the deadline, so avoid getting behind. Remember, if you have problems understanding the assignment or how to carry it out, please contact the TA  CA, or professor. We want to help all of you succeed, so please do seek help when you need it.

September 13, 2004