To gain an initial understanding of objects and methods in Java using the Turtle class.
To gain more practice creating, compiling and running programs with Dr Java.
To practice invoking methods from the Interactions pane of Dr Java.
Go over the notes: topic 3, including the creation of new object methods.
Turtle tips (to be used in the exercises)
the method moveTo(...)
sends the turtle to a given
location
the method setPenWidth(...)
changes the width of
the pen
the methods penUp()
and penDown()
move the pen up and down
Follow the instructions given here to download and setup the book's classes on your account in the lab.
In the Interactions pane, type (or copy-paste) the following sequence of Java statements and answer the questions below.
World worldObj = new World(); Turtle turtle1 = new Turtle(worldObj); System.out.println(turtle1); turtle1.forward(100); turtle1.turnRight(); turtle1.forward(100); turtle1.turnRight(); turtle1.forward(100); turtle1.turnRight(); turtle1.forward(100); System.out.println(turtle1);Questions
What are the coordinates of the turtle at the end?
Which direction is the turtle facing at the end?
What statement would you now type in the Interactions pane to make the turtle face in the same direction as it started?
Reset the Interactions Pane (click on Reset in DrJava) and type the following sequence of statements in the Interactions pane:
World worldObj = new World(); Turtle turtle1 = new Turtle(worldObj); turtle1.turn(45); turtle1.forward(120); Turtle turtle2 = new Turtle(60,60,worldObj); turtle2.turn(135); turtle2.forward(240);Questions
Now type in the Java statements that will tell you the coordinates and direction of turtle1 and of turtle2.
Type in the Java statement(s) to move turtle1 to the same coordinates as turtle2.
Reset the Interactions Pane and type the following sequence of statements in the Interactions pane:
World worldObj = new World(); Turtle turtle1 = new Turtle(worldObj); int length = 40; turtle1.turn(-30); turtle1.forward(length); turtle1.turn(180); turtle1.forward(length); turtle1.turn(-120); turtle1.forward(length); turtle1.turn(180); turtle1.forward(length); turtle1.turn(150);Admire the picture.
A method is a sequence of Java statements whose purpose is to
perform a given task. In this exercise, you will add two new method
definitions to the Turtle class: one that draws a square, and one that
draws a figure V. The Turtle class is in the
file Turtle.java
, which is one of the files in your
folder BookClasses that you set up in Exercise 0.
In DrJava, open the file Turtle.java. It will appear in the Definitions pane. Scroll down in the Definitions pane until you see the line
/////////////////// methods ///////////////////////You will type your new method definitions just below this line.
Type the code of the method public void
drawSquare(int width)
as defined in the lecture notes. Compile
Turtle.java
(fix any syntax errors until it
compiles correctly).
You will now write a new method for the Turtle class whose
task is to create the figure produced in Exercise 2. This method will
be called drawVee and it will have the header
public void drawVee (int length)
where the parameter
length indicates the length of the lines in the figure. Using the
method drawSquare
as a sample, type in the definition for
the method drawVee
right after the code for drawSquare
Hints
Check the code of the drawSquare
method to see
which kinds of statements should be included in the method
definition. You only need to include statements similar to the ones
from Exercise 2.
Remember that the variable width
is a parameter
to the method; again, check drawSquare
method to see how
the parameter is used.
You cannot use a specific turtle name in your method definition
because the method has to work for any turtle object that it is
invoked on. So, you need to use the special Java keyword this
, which
refers to the object that the method will be invoked on.
You will now test your methods drawSquare
and drawVee
from the
Interactions Pane. First reset the Interactions pane. Then create a
new World object and a Turtle object and call the method drawSquare
.
Reset the interactions Pane, create and new Wolrd and a new Turtle.
Call your new
method drawVee
once, to draw a figure V; then, move the
turtle to another place, and draw another V, with pen width 10. The
turtle should lift the pen between the two V's.
You will now write a complete Java program that makes a turtle draw
two V's on the screen, by using the drawVee
method. Type the
following program into the Definitions pane, adding statements to
create a World object and a single Turtle object, and make the turtle
draw two V's, as you did in the previous exercise.
public class TurtleDrawVs { public static void main(String[] args) { /* your code goes here */ } }