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.
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.
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.
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.
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.
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.
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.
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.
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?
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.
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.
public class Draw7
{
---
public static void main(String[] args)
{
---
World habitat = new World(300,300);
---
Turtle yertle = new Turtle(habitat);
---
yertle.forward();
---
yertle.turnLeft();
yertle.forward();
---
habitat.show(true);
---
} // end main
---
} // end class
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.
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.
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.
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.
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?
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.
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).
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.
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).
(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.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.