Objectives:
World
worldObj = new World();
Turtle turtle1 = new Turtle(worldObj);
You should have a frame containing your world
with a turtle in the middle.
Chapter
3 discusses using objects by executing "methods". Look at the
documentation for the class Turtle located on the course website. You will see that it "inherits"
from SimpleTurtle, meaning that it has all the methods of that class plus
special features of its own. Check out the methods available from
SimpleTurtle.
We can use a method with
objectName.method(parameterList);
For example,
turtle1.forward(20);
moves the turtle forward 20 steps. Try out the methods
that move, and turn the turtle. Also try the methods penUp and penDown.
We can
define our own methods. On p57 of your text you are given a method for
the Turtle class which will tell the turtle to draw a square of any size
given by the parameter width. Type
in this new method into the Turtle class (including the comments which describe the method to a
human reader). Save your changes and compile. Then try it out in
the Interactions pane by creating a new turtle and telling the turtle
to draw a square of size 200. Use this same method again to draw a
square of size 50.
| Assignment: Write a Turtle method called stackedSquares which draws the following:
Hint: Your methods should invoke or use the method draswSquare three times. (Using code that is already written in the class is always a good thing!) |
| Assignment: Write a Turtle method which draws an equilateral triangle. The method should take a parameter which is the length of the sides. |
| Assignment: Write a Turtle method which draws a star. The method should take a parameter which is the length of the sides.
Hint: The interior angles at each point are 36 degrees. |
| Assignment: Write a Turtle method which draws both of your initials in block, squared off letters. This method mostly likely will not have any parameters. |
| Assignment: Edit MyProgram.java so that instead of printing a message it now, creates a World object, creates a Turtle object, and then the turtle draws your initials. Do this by simply USING the method that you wrote above (rather than rewriting that code). Hint: This should be really short and easy! |