Class Turtle
- java.lang.Object
-
- Turtle
-
public class Turtle extends Object
This class represents a "turtle" that can be asked to change directions and move around the screen. It's similar to the concept of the Logo turtle, though with only four possible headings. The version below has been modified for use in a 161 lab: It has a copy constructor that the original didn't, and there are getter methods for providing information about the turtle's state.- Version:
- 2.0
- Author:
- Brad Richards
-
-
Constructor Summary
Constructors Constructor Description Turtle()
This constructor sets up our "turtle" and leaves it facing north.Turtle(int initialDistance)
The second constructor allows the user to specify how far our turtle should move each time forward() is called: It takes a parameter containing this extra information, and uses an assignment statement to store a copy of that value in our distance field.Turtle(Turtle other)
This copy constructor takes another Turtle object for inspiration, and sets "us" up to look just like them.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description boolean
equals(Turtle other)
Compares our state with that of another turtle.void
faceEast()
Change the turtle's state so that we're heading east.void
faceNorth()
Change the turtle's state such that we'll head "north" (up the screen) the next time forward() is called.void
faceSouth()
Change the turtle's state so that we're heading south.void
faceWest()
Change the turtle's state so that we're heading west.void
forward()
Move in the direction we're heading, by the number of pixels stored in the distance field.Circle
getCircle()
A "getter" method that returns a reference to our Circle object.int
getXDirection()
A "getter" method for providing the turtle's xDirection.int
getYDirection()
A "getter" method for providing the turtle's yDirection.void
setDistance(int newDistance)
The setDistance method can be used to change how far the turtle moves each time forward() is called.
-
-
-
Constructor Detail
-
Turtle
public Turtle()
This constructor sets up our "turtle" and leaves it facing north. It arbitrarily decides that we should move in 20-pixel increments.
-
Turtle
public Turtle(int initialDistance)
The second constructor allows the user to specify how far our turtle should move each time forward() is called: It takes a parameter containing this extra information, and uses an assignment statement to store a copy of that value in our distance field. Notice the sneaky trick it's using to avoid duplicating the code above: Java lets you call one constructor from another by using the name "this" in place of a method name. The first line below is calling the default (no-input) constructor above to do most of the setup, and then just changing the distance.- Parameters:
initialDistance
- The value to use for distance.
-
Turtle
public Turtle(Turtle other)
This copy constructor takes another Turtle object for inspiration, and sets "us" up to look just like them. We have to take special care to be sure that we don't share a Circle object with them -- we want our Circle to be independent from theirs, but to take on the same *values* as their Circle.- Parameters:
other
- A reference to another Turtle we want to copy.
-
-
Method Detail
-
equals
public boolean equals(Turtle other)
Compares our state with that of another turtle. We consider the two to be equal if our X and Y directions both match, our distance is the same as theirs, and our two Circles are at the same location.- Returns:
- True if our state matches that of the other turtle, false otherwise.
-
getXDirection
public int getXDirection()
A "getter" method for providing the turtle's xDirection.- Returns:
- Our xDirection value.
-
getYDirection
public int getYDirection()
A "getter" method for providing the turtle's yDirection.- Returns:
- Our yDirection value.
-
getCircle
public Circle getCircle()
A "getter" method that returns a reference to our Circle object.- Returns:
- A reference to our Circle.
-
forward
public void forward()
Move in the direction we're heading, by the number of pixels stored in the distance field. The direction is represented by a pair of fields, each of which is expected to be either 0, 1, or -1. We can figure out how far to move in each dimension by multiplying the direction by distance.
-
setDistance
public void setDistance(int newDistance)
The setDistance method can be used to change how far the turtle moves each time forward() is called. It takes this information as a parameter, and stores a copy of the value into our distance field using an assignment statement.- Parameters:
newDistance
- The new value to use for distance.
-
faceNorth
public void faceNorth()
Change the turtle's state such that we'll head "north" (up the screen) the next time forward() is called. There's no movement along the X axis involved in moving north, so xDirection is set to 0. The yDirection field is set to -1 since movement up the screen is in the negative direction.
-
faceSouth
public void faceSouth()
Change the turtle's state so that we're heading south.
-
faceWest
public void faceWest()
Change the turtle's state so that we're heading west.
-
faceEast
public void faceEast()
Change the turtle's state so that we're heading east.
-
-