This book is now obsolete Please use CSAwesome instead.

19.1. Compute with Turtles: Intro to Objects

In the 1960s, an educational programming language called Logo was developed. It is best known for teaching programming with turtles! The turtles were graphical or robotic turtles that were controlled with simple commands like go forward or turn right. Here’s a photo of a robot turtle from the 1960s. The turtle had a pen attached to it. The student-programmers would steer the robot around using simple commands to create drawings with their code.

Children playing with a Logo turtle robot that can draw with a pen

Figure 1: Children playing with a Logo turtle robot that could draw with a pen

Today, we can play with virtual turtles in a graphical world. Below is a sample Java program that works with Turtle objects. Try clicking the run button button below to see what the following program does.

Let’s try another TurtleTest that has the World class embedded in the activecode.

In the program above there were two turtle objects yertle and myrtle. The hidden Java code defined a complicated class called Turtle. A class in programming defines a new abstract type. When you create objects in code, you create new variables of that class data type. Here, yertle and myrtle are 2 objects created from the class Turtle.

Can you add another turtle object to the code above? You can make up a variable name for your turtle and add in a line like the following in the main method:

Turtle yourTurtleName = new Turtle(world);

The class Turtle defines fields (data or properties) and methods (behaviors or functions) that each turtle can use. The dot operator (.) is used to run an object’s method. You can think of the . as an ‘s, for example run yertle’s forward method. The parentheses () after method names are there in case you need to give the method arguments (some data) to do its job, for example go forward 100 pixels.

Here is a class diagram that shows some of the fields and methods inherited from the SimpleTurtle class in the class Turtle.

Turtle class diagram

Figure 2: Turtle Class Diagram

Try some of the methods above. To change the pen color, try something like: yertle.setColor(Color.red); This uses the Color class in Java. Can you make yertle draw a square? How about a triangle? You’ll need 60 degree interior angles for an equilateral triangle. Then, try drawing a letter A using the code below.

turtle drawing A

Figure 3: Turtle drawing A

The following code uses a turtle to draw a capital A, but the lines are mixed up. Drag the code blocks to the right and put them in the correct order to draw the A in the order shown by the numbers in the picture above. Click on the “Check Me” button to check your solution. It may help to act out the code pretending you are the turtle. Remember that the angles you turn depend on which direction you are facing, and the turtle begins facing up. You can also try this code in the Java program above to see what the turtle will do.

Use the area below to have a turtle draw your initials with two different colors. Be patient with yourself because this may take a lot of trial and error to get the correct angle values.

Vocabulary:

Make sure you know the following vocabulary words after this lesson:

You have attempted of activities on this page