Turtles Assignment 1

Table of Contents



Note: You can make your Turtles stop flickering if you rename the method named "paint" in View.java to "update". Sorry!

You should log on to the computer using your own logon, not "student."

The program you will be working with in the first few assignments is called "Turtles." It is a Java applet in which creatures called "turtles" can move around on a canvas, optionally drawing lines behind them. You can download it from MrZ/Turtles.zip.

Do this now: Download and extract the Turtles project zip file from the website. Save it in your personal space, not just on the desktop.

The project that you downloaded contains a JCreator workspace file and a JCreator project. Double-click on either to open JCreator.

To see the applet running, select "Run" or "Execute" or whatever that menu item is that I can't remember at the moment, from the "Build" menu (the icon is two blue circles). You should see a "turtle" appear near the center of the screen, make a quick sketch, and disappear. You can play around a bit with the various controls.

1. Changing and Compiling the Souce Code

Quit the turtles applet. Back in JCreator, in the file list ont he left of the screen, is a directory called "src". Open this now. You will see lots of different files. The "soure code" files that were used to make this applet are called Turtles.java, Turtle.java, View.java, and Controller.java. Each file represents a different type of "object" in the applet. The "Turtles" object represents the applet itself, what you see on the screen. The "View" object represents the particular part of the applet that draws the canvas and turtles on the screen. The "Turtle" object describes what a turtle is. The "Controller" object gives the instructions for what sort of turtles the applet should make and what they should do. This is the file that you will be editing for this assignment.

Do this now: Double-click the file "Controller.java" to open it.

Much of what you see in this file probably looks strange and incomprehensible if you haven't seen programming code before. In outline view, this is what the file does:

(Various introductory stuff)
Begin describing an object type called "Controller" {
   Describe what to do when the Controller is told to run {
      This is where the instruction to create and move turtles are.
   }
}

Scroll down to where the file says:

public void run() {

This is a description of a method of the Controller, a thing that the Controller object knows how to do when asked. Everthing from the curly bracket "{" after "run()" to the opposite curly bracket "{" many lines later is a list of instructions to be followed when the Controller is told to run().

Looking at the lines of code in this method, you will see that they separate into two types: code, which is written in the Java language, and comments which start with "//" and have ordinary English text describing what the code around them means.

Reading the comments and looking at the code near them should give you a good idea of what a Turtle can do. The run() method currently creates a new turtle and names it "t". Then it sends a bunch of messages to "t" to tell it how to move. One such message looks like this:

// Move the turtle forward
t.move();

The syntax for sending a message to an object is to write the name of the variable that refers to the object, then put a period and the name of the method to "call". Each method call ends with a pair of parentheses; we'll understand later in this assignment why this is. Every line of Jva code ends with a semicolon (";").

We'd like to change the Controller so that the turtle does something a bit different - we want it to draw the same shape, but a bit higher on the screen. To do this, we'll have the turtle, before all the drawing that it does now, move up a few steps. We'll also have it lift the pen first, so that it doesn't leave a line leading up to where it starts drawing.

Do this now: In Controller.java, enter the following before the first move() instruction:

t.penUp();
t.move();
t.move();
t.move();

Do this now: Save the file. Compile it by selecting "Compile file" from the "Build" menu.

To "compile" your code means to translate it into Java instructions. You don't need to know the details at the moment. Just remember that in order to see your changes to the code have an effect, you need to recompile (compile again) the source file(s) you changed.

Now that you've compiled your source file, you want to see whether your changes had the desired effect. Run the applet again.

Do this now: Does the turtle draw what we wanted it to? What did we forget to do? Change this and recompile.

You don't need to hand anything in for this part. Go on to the next part.

2. Writing some Basic Instructions

There are six basic Turtle methods that you will need or this part. If you have a turtle that is referred to by the variable "t", then you could use the following commands:

t.move(); // Move forward one step.
t.rt(); // Make a 90 degree right turn.
t.lt(); // Make a 90 degree left turn.
t.penUp(); // Lift the pen (stop drawing).
t.penDown(); // Start drawing again.
t.die(); // Disappear.

Take a look at the sketch to the right. This is the logo of Turtles.inc, the company where you've scored your first programming job. Your boss asks you to make the Turtles Java applet draw this logo on the company's webpage. Your first assignment, therefore, is to rewrite run() in Controller.java so that the turtle produces this sketch. Remember to have the turtle die() when it is done.

When you have finished rewriting Controller.java, compile it and run the applet. Fix any mistakes you might notice.

Just as you're finishing up that first assignment, your boss comes back and tells you that Bill from marketing (who everyone knows is a self-promoting jerk) had lunch with the vice president of sales and convinced him, with lots of complicated sounding artsy babble, that the company logo would be more compelling if it were more symmetric. So now, you have to remake the whole thing. Furthermore, you need to have it be drawn by two turtles, which, after moving to their starting positions, will follow each other step for step.

The new logo is shown below in various stages of completion. The two turtles and thier work are colored red and blue to make it easier for you to see what each did. You don't have to follow exactly how I did it here, as long as your turtles are synchronized in their motions. Your logo should be drawn simply in black; you don't know how to do color yet.
Ready to start drawing.Done with square.Almost done.Completed logo.

When you have finished rewriting Controller.java, compile it and run the applet. Make sure that the turtles do what you wanted it to. When you have all the bugs worked out, fill out all the information in the header at the top of the Controller.java file; then print it out and hand it in to me. You may want to save a copy of Controller.java as something like ControllerPart2.java, so you can look back at it later once you've changed Controller for the next part.

3. Sending Parameters to a Method

Your boss stopped by again while you were working on that last assignment, and when he saw this assignment page, he said, "Wow, I really like how this Mr. Z. made the turtles draw their two parts in color. Could we do something like that?"

So, yet again, you have to change your program. This is what it's like being a programmer in the real world.

I told you a while ago that you would learn eventually what the "()" parentheses after every method call were there for. Usually, when you tell someone to do something like "Move forward," they'll want more information about how to do it; for example, they might want to know how far to move. To send extra information to a method, we include that information in the parentheses; this is called a "parameter" (you might also hear it called an "argument"). Each method defines what sorts of parameters it accepts, and in what order. Parameters are separated by commas.

In order to change the pen color, you send a Color object to the penDown() method. Before you can use Color objects, you need to "import" the class java.awt.Color; you do this by adding an "import" command at the top of your source file, right under the package declaration. Here is an example of what this looks like:

(Near the top of Controller.java)
package turtles;

import java.awt.Color;

(Various other lines)
penDown(Color.RED); // Have the turtle start drawing, in red.

The possible colors are BLACK, BLUE, CYAN, DARK_GRAY, GRAY, LIGHT_GRAY, MAGENTA, ORANGE, PINK, RED, WHITE, YELLOW. If you're interested, there are instructions near the end of this assignment for creating a Color object representing some color other than these basic ones.

Change two penDown() instructions in your Controller.java from part 2 so that the two turtles draw in red and blue. Compile and make sure that it works.

The other turtle methods can also use parameters. All the movement methods can be sent a decimal number (called a "double" in programming terms, for reasons you don't need to know at the moment) to tell them how big the movement is. Use them as shown below:

t.move(2.5); // Move forward two and a half steps.
t.rt(60.); // Make a 60 degree right turn.
t.lt(120.); // Make a 120 degree left turn.

Use these methods to make some new sketch that makes use of the fact that you can draw angled lines and lines of different length. You might also want to include multiple colors in your sketch. When you are done, change the assignment information at the top of Controller.java to say "part 3", and print it out and hand it in. Again, you may want to save a backup copy of Controller.java.

4. Extras

4.1. Other Colors

Suppose you wanted to make a turtle draw in purple. How would you do this?

You have to pass a Color object to penDown() to get it to change color. If there isn't a ready-made Color object (like Color.RED) available to you, then you'll have to make your own. That will mean making a Color variable, making a new color object and assigning it to that variable, and then passing that variable as a parameter to penDown().

Here's an example of code that does this:

Color purple; // Variable to refer to our new color.

// Create a new color with red = 150, green = 0, and blue = 255.
purple = new Color(150, 0, 255);

t.penDown(purple); // Pass that color to t's penDown() method.

The only complicated part is what you do to instantiate the new Color object. I pass three parameters to new Color(). Each is an integer between 0 and 255. They tell Color how much red, green, and blue, respectively, to mix together to get the new Color I am making. So, for example, I could reproduce the color yellow with "new Color(255, 255, 0)".

4.2. The other Turtles code

If you're interested, you might like to look over the rest of the Turtles applet code, in the three other source files. You probably won't understand everything that I do there, but you might pick up some interesting ideas, and get a sense of what sorts of things you can do in Java. I think that, in particular, you might like to see how the Turtle class implements its moving methods.

One way to check whether you understand what's going on in someone else's code is to try changing something, and then see whether your change had the effect that you expected.

When doing this, it's good practice to copy the line you're changing and comment it, so that you can go back to the old version if you break somethign badly.

Here are some things you might be able to figure out how to do:Hint: All of these things can be done without changing the file Turtles.java.

4.3. Some Timesavers

Programmers are always looking for ways to do less work. Here are some ways you can combine statements in order to get something done with less typing.

Creating a variable and assigning it to a new object can be done in one line. In fact, you can create multiple variables of the same type and assign them all to new objects, all in one line.

Turtle t = new Turtle(), u = new Turtle();

If you're just using an object once, you don't need to assign it to a variable. You can just put the new statement, creating the object, wherever you need that object.

t.penDown(new Color(150, 0, 255));

The other thing you might have noticed is that your code often involves repeating the same thing a number of times. For example, drawing a square is done by move() and rt() repeated four times. In the next lesson, you will learn how to write code that repeat itself, so that you won't have to copy and paste the same chunk of code four times.

back to Computer Programming Assignments.