CS 101 Turtle Contract

Turtles are cousins of buggles. They still move in a 2D plane, but there are no grid lines, and they themselves are invisible. They can move forward, backward, left, and right, and can leave trails with a pen in their bellies.

Instances of the Turtle class represent whimsical creatures that inhabit an instance of the TurtleWorld class. Conceptually, every turtle has four abstract state variables:

Constructor Methods

Turtle ()
Returns a new turtle at position (0.0,0.0) whose heading is 0 (toward the right), whose color is Color.RED, and whose pen is down.

Instance Methods

public void forward(int n)
public void forward(double n)
Moves this turtle forward by length n in the direction of its current heading.

public void backward (int n)
public void backward (double n)
Moves this turtle backward by length n in the direction of its current heading.

public void left (int angle)
public void left (double angle)
Turns this turtle left by angle degrees.

public void right (int angle)
public void right (double angle)
Turns this turtle right by angle degrees.

public void penUp ()
Raises this turtle's pen. When the pen is raised, the turtle leaves no trail when it moves.

public void penDown ()
Lowers this turtle's pen. When the pen is lowered, the turtle leaves a trail when it moves.

public Point getPosition ()
Returns a point that indicates the current position of this turtle in turtleworld.

public void setPosition (Point p)
Changes the position of this turtle to be the point p.

public void setPosition (int x, int y)
public void setPosition (double x, double y)
Changes the position of this turtle to be (x,y).

public double getHeading ()
Returns the heading of this turtle.

public void setHeading (int d)
public void setHeading (double d)
Changes the heading of this turtle to be the angle d.

public Color getColor ()
Returns the color of this turtle.

public void setColor (Color c)
Changes the color of this turtle to be the color c.

public String toString ()
Returns a string representation of this turtle.


Back to Computer Science 101 Home
Department of Computer Science