CS1026 Computer Science Fundamentals I (Java)

Lab 3: Using Turtles


Objectives
Preparation

Turtle tips (to be used in the exercises)


Exercise 0: Setting up the book's classes in the lab

Follow the instructions given here to download and setup the book's classes on your account in the lab.


Exercise 1: Basic turtle operations

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
  1. What are the coordinates of the turtle at the end?

  2. Which direction is the turtle facing at the end?

  3. 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
  1. Now type in the Java statements that will tell you the coordinates and direction of turtle1 and of turtle2.

  2. Type in the Java statement(s) to move turtle1 to the same coordinates as turtle2.


Exercise 2: Practicing drawing

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.


Exercise 3: Adding a new method

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.
  1. 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).

  2. 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

Compile Turtle.java and fix any syntax errors until it compiles correctly.

Hints


Exercise 4: Trying methods from the Interactions pane

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.


Exercise 5: Using the new method in a complete Java program

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 */
  }
}