Skip to main content
Logo image

Section 8.5 More turtles

We first used the Turtle library that is built into this book in SectionΒ 3.2Β APIs and Libraries. Now that we’ve learned more about classes we can use the classes in the Turtle library, Turtle and World to look at some concrete examples of some of the topics we’ve discussed in this unit.
Then we will practice using classes from the Turtle library in a set of coding exercises and challenges.

Subsection 8.5.1 Turtle basics

Like any class the Turtle class is a blueprint for making objects that each represent one turtle we can use to draw lines on a graphical world. It defines attributes for graphical turtles like their color and position and methods to make the turtles move. As a refresher, here’s a simple program that makes one Turtle and calls a few methods on it to draw a couple lines.

Activity 8.5.1.

Try clicking the run button below to see what the following program does.
The following video walk through how the program creates a World object and a Turtle object assigning them to the variables habitat and yertle
Figure 8.5.1. Turtle Memory Model
This code also demonstrates the use of the dot operator to invoke an object’s method. The dot allows us to get a particular method from an object, such as forward or turnLeft, and then we can call that method to cause the object to do whatever the code in the method specifies.
For example, yertle.forward() asks the turtle referenced by the variable yertle to go forward. The forward method doesn’t take any arguments so it moves the turtle forward a default amount of one hundred pixels.
The Turtle class gives us an example of overloaded methods, which we learned about in SubsectionΒ 8.4.5Β Overloaded methods. It defines two forward methods; the no-args version used above and another that takes an int arguments specifying the number of pixels to move. For example to have the turtle move forward fifty pixels instead of one hundred, we can use yertle.forward(50);. Try making that change in the code above and then run it again to see the change.

Activity 8.5.2.

In the code below, yertle goes forward and then turns left. Can you change the code to make yertle go forward twice and then turnRight?

Subsection 8.5.2 Creating multiple objects

Remember that a class is a blueprint from which we can make as many instances as we want. The code below creates two turtle objects and stores references to them in the variables yertle and myrtle. They are both added to the same World so when we run the code we see two turtles drawing.

Activity 8.5.3.

After running the code once to see what yertle and myrtle draw, can you add another turtle object to the code below? You will need to declare a new variable, construct a new Turtle and then call some methods on the new turtle to make it draw something.

Subsection 8.5.3 Overloaded constructors

As we saw in SubsectionΒ 8.3.3Β Overloading constructors, a class can define more than one constructor class, creating overloaded constructors. Both the World and Turtle classes have overloaded constructors in order to allow us to create instances with reasonable default values for some of their attributes and also to specify things ourselves.
The World class has two constructors, one that takes no arguments and one that takes two int arguments that specify the size of the world and thus the size the image that will be created. The no-argument constructor creates a world with a default size of 640x480 pixels.
Two World constructors
Figure 8.5.2. Two World constructors
Here are two lines of code that each use one of the constructors:
World world1 = new World(); // creates a default size 640x480 world
World world2 = new World(300,400); // creates a 300x400 world

Activity 8.5.4.

Which of these is valid syntax for creating and initializing a World object?
  • World w = null;
  • This declares a variable w that refers to a World object, but it doesn’t create a World object or initialize it.
  • World w = new World;
  • You must include parentheses () to call a constructor.
  • World w = new World();
  • Correct, use the new keyword followed by the class name and parentheses to create a new object and call the constructor.
  • World w = World();
  • You must use the new keyword to create a new object.
  • World w = new World(300,500);
  • Correct, this constructor call creates a new World object with the size 300x500 pixels.

Activity 8.5.5.

Which of these is overloading the constructor?
  • When a constructor takes one argument.
  • For a constructor to be overloaded there must be more than one constructor.
  • When a constructor takes more than one argument.
  • For a constructor to be overloaded there must be more than one constructor.
  • When one constructor is defined in a class.
  • For a constructor to be overloaded there must be more than one constructor.
  • When more than one constructor is defined in a class.
  • Overloading means that there is more than one constructor. The parameter lists must differ in either number, order, or type of parameters.
The Turtle class also also has multiple constructors. They all take at least a World argument in order to have a place to draw the turtle but one of them takes two int arguments to specify the initial x and y position of the Turtle in the World. The constructor that does not take these int arguments puts the Turtle in the middle of the World.
Turtle t1 = new Turtle(world1); // put turtle in center of image
Turtle t2 = new Turtle(50, 100, world1); // put turtle at 50,100
In order to understand how to position a new Turtle in the world we need to know how the x and y values are used which may be surprising since the Turtle world does not use the Cartesian coordinate system with (0,0) in the middle the screen like we may be used to from math class or languages like Snap! Instead, (0,0) is at the top left corner of the screen and x increases to the right and y increases towards the bottom of the screen.
Most computer graphics systems use this coordinate system which is a carryover from when computers dealt only with text being printed line by line, where it made sense to think of row zero, column zero as where the first character would come out.
Figure 8.5.3. The coordinate (0,0) is at the top left of the Turtle world.

Note 8.5.4.

Notice that the order of the arguments matter. The three-argument Turtle constructor takes the arguments x, y, and world in that order. If we put the World argument first Java will complain because there is no constructor that takes a Worldfollowed by two int values. Worse yet, if we swap the x and y values, the code will compile and probably run fine, i.e. it won’t crash, but our Turtle will not be positioned where we intended. So we need to be extra careful when a constructor or method takes multiple arguments of the same type to make sure we’re passing them in the right order.

Activity 8.5.6.

Which of these is valid syntax for creating and initializing a Turtle object in world1?
  • Turtle t = Turtle(world1);
  • You must use the new keyword to create a new Turtle.
  • Turtle t = new Turtle();
  • All turtle constructors take a world as an argument.
  • Turtle t = new Turtle(world1, 100, 100);
  • The order of the parameters matter, so this would cause a syntax error.
  • Turtle t = new Turtle(100, 100, world1);
  • This creates a new Turtle object in the passed world at location (100,100)

Activity 8.5.7.

Try changing the code below to create a World object with 300x400 pixels. Where is the turtle placed by default? What arguments do you need to pass to the Turtle constructor to put the turtle at the top right corner? Experiment and find out. What happens if you mix up the order of the arguments?

Subsection 8.5.4 Turtle exercises

Now we can practice more with the Turtle class and its methods. Here is a class diagram that shows some of the attributes and methods in the class Turtle.
Turtle class diagram
Figure 8.5.5. Turtle class diagram

Activity 8.5.8.

The following code uses a turtle to draw the digital number 7 (with just straight lines), but the code is mixed up. Drag the code blocks to the right and put them in the correct order to first draw the line going up (towards the top of the page) and then turn and draw a line to the left to make a 7. Remember that the turtle is facing the top of the page when it is first created. Click on the β€œCheck Me” button to check your solution.

Activity 8.5.9.

After solving the mixed up code problem above, type in the same code below to make the turtle draw a 7.

Activity 8.5.10.

Can you make yertle draw the number 8, as two squares on top of each other? If you want to be really fancy you can use penUp and penDown to put some space between the two squares. Maybe you can even draw an 8 as it would be displayed on a seven segment display.

Activity 8.5.11.

  1. Can you make yertle draw a square and change the pen color for each side of the square? Try something like: yertle.setColor(Color.red); This uses the Color class in Java which has some colors predefined like red, yellow, blue, magenta, cyan. You can also use more specific methods like setPenColor, setBodyColor, and setShellColor.
  2. Can you draw a triangle? The turnRight() method always does 90 degree turns, but you’ll need external angles of 120 degree for an equilateral triangle. Use the turn method which has a parameter for the angle of the turn in degrees. For example, turn(90) is the same as turnRight(). Try drawing a triangle with different colors.

Activity 8.5.12.

The following code uses a turtle to draw a simple house, but the lines are mixed up. Drag the code blocks to the right and put them in the correct order to first draw a square for the house and then a red triangle for the roof. Click on the β€œCheck Me” button to check your solution. You can type this code in the Active Code window above to see it in action.
simple house

Activity 8.5.13.

Try the code below that prints out the turtle’s current position using getXPos() and getYPos(). Can you make it go to position (0,0) or as close to it as possible?

Activity 8.5.14.

Fix the errors in the code below so that it prints out the area of the space that the turtle occupies by multiplying its width and height. Remember that you have to do something with the values that the get methods return.

Subsection 8.5.5 Coding Challenge: Draw letters

Working in pairs, use the area below to have your turtle draw simple block-style letters for your first or last name initials using just straight lines.
It may help to act out the code pretending you are the turtle. Remember that which way you turn depends on which direction you are facing, and the turtle begins facing north (towards the top of the page).

Project 8.5.15.

Have your turtle create a block drawing of the initials of your name. Use just straight lines.

Subsection 8.5.6 Coding Challenge: Turtle house

simple house
This creative challenge is fun to do collaboratively in pairs. Design a house and have the turtle draw it with different colors below. Can you add windows and a door? Come up with your own house design as a team.
To draw a window, you will need to call penUp and moveTo to walk the turtle into position without drawing, for example:
t.penUp();
t.moveTo(120,200);
t.penDown();
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. When planning your coordinates for the house, remember that the turtle starts at the center of the screen (150,150) and the top left corner is (0,0).

Project 8.5.16.

Draw a Turtle House! Make sure you use forward, turn, penUp, penDown, moveTo methods as well as different colors. Have fun!

Subsection 8.5.7 Summary

  • (AP 1.12.A.1) A class defines a new data type (a classification). It is the formal implementation, or blueprint, of the attributes and behaviors of the objects of that class.
  • (AP 1.12.A.1) An object is a specific instance of a class with defined attributes. Objects are declared as variables of a class type.
  • (AP 1.12.B.1) A variable of a reference type holds an object reference, which can be thought of as the memory address of that object.
  • An attribute or instance variable is data the object knows about itself. For example a turtle object knows the direction it is facing or its color.
  • A behavior or method is something that an object can do. For example a turtle object can go forward 100 pixels.
  • (AP 1.12.A.2) A class hierarchy can be developed by putting common attributes and behaviors of related classes into a single class called a superclass. Classes that extend a superclass, called subclasses, can draw upon the existing attributes and behaviors of the superclass without replacing these in the code. This creates an inheritance relationship from the subclasses to the superclass. Designing and implementing inheritance relationships are outside the scope of the AP Computer Science A course and exam.
  • (AP 1.12.A.2) All classes in Java are subclasses of the Object class.
You have attempted of activities on this page.