The project you'll extend is modeled after the turtle made famous by the Logo programming language in the 1960s. The Logo turtle was displayed on a graphics screen, and could be given basic commands like "turn left 90 degrees" and "go forward 100". Ours will be simpler: We'll have commands to make our turtle face north, south, east, or west, and to move forward by a fixed amount.
figures
class, as well as a new Turtle
class. The Turtle
class is the only class you'll need to edit. (And, in fact, you should not modify any of the other classes.) Before you worry about writing any Java code, take a moment to play around with the existing program: You should be able to create instances of the Turtle class and call some methods on it, though it only knows how to face north at the moment which is kind of sad.
Turtle
class to see how it's been implemented. Make sure you spend some time looking at the fields and thinking about how a turtle instance remembers which direction it's heading.
faceSouth()
, faceWest()
, and faceEast()
methods to the Turtle
class. Please add them at the bottom of the class, where the comment instructs you to. (Grading will be easier if everyone puts their new code in the same places.) These will look a lot like faceNorth()
. Like faceNorth()
, none of them should have parameters — we don't need to supply any extra information when calling one of these methods. Test your new methods and make sure they work before proceeding.
Turtle
instance is created. That way users can customize their turtles to their needs. Add a second constructor just below the existing constructor. It should take a single parameter — an int
that specifies how far the turtle should move on each call to forward()
. (It's going to look enough like the existing constructor that it might make sense to copy-and-paste to make the second one, then do a bit of editing.) You should not need to add any new fields to the class to make this work.
Before proceeding, make sure your second constructor works. When you right-click on the Turtle
class, you should see two constructor options — the "default" constructor that doesn't require an input, and the one you added that takes a parameter. Make sure they both still work. Try creating multiple Turtle
instances, each with different speeds, to be sure.
setDistance
that takes a single parameter — an int
that specifies how far the turtle should move on each future forward
call — and changes the distance
field to take on the specified value. Please add this method just above the faceNorth
method, where the comment suggests. Test your new method before proceeding.
/** ... */
) comment at the top of the class containing your name and a sentence or two explaining what the project is about, and Javadoc-style comments above each of the methods in your class.
faceNorthwest
, for example). Or add methods to modify other aspects of a turtle object's state (e.g. its color or size). The Logo turtle had an option for drawing a line behind it as it moved. Can you find a way to have our turtle draw lines behind it? (Fair warning: That's pretty hard...)
faceSouth()
, faceWest()
, and faceEast()
methods work properly.
setDistance
method works as described above.