Name:___________________________________
Answer the following questions:
Turtle
class).
When creating a new object we usually create a reference to hold it. For example:
World myWorld = new World();
The reference myWorld
is a
mechanism that we can use to refer to the new
World
object created by the statement
new World();
Why doesn't it make sense to create an object without a reference to store it? For
example, just the command: new World();
Turtle
object we pass a
reference to a world to its class constructor.
For example, look at the following code:
World larrysWorld = new World(); Turtle larryTheTurtle = new Turtle(larrysWorld);
When we execute the statement
new Turtle(larrysWorld);
we are passing a
reference to the World
created on the previous line. Why do we do this?
What happens when you try to create a new Turtle
object and you do not pass a reference to a World
object to its constructor?
turtle1.forward(20);
Means that you want the Turtle
object refered
to by turtle1
to move forward 20 units.
Why doesn't it make sense to make a call like this:
forward(20);
turtle1.turn(-45);
and turtle1.turn(45);
?
drawRectangle
method for the Turtle
class
(similar to the drawSquare
method on page 57 of your book) that takes two arguments: a length and
width, and draws a rectangle of that specified length and width.
Below is the header of the method you'll write:
public void drawRectangle(int length, int width) { }
drawRectangle
method for a given
Turtle
object to draw a rectangle of length 100 and width 50: