TurtleMob
class, which keeps track of a couple of Turtle
instances in much the same way as a CircleDrawer
managed a pair of Circle objects. But since each of our Turtle
instances contains a Circle, we actually have a three-level hierarchy. To help you keep track of the fields and methods in each of the classes, I've generated the documentation for you.
Turtle
and TurtleMob
that you'll fill in later.
xDirection
, yDirection
, and distance
fields all hold the same values as the other Turtle's fields do, but don't forget about the Circle field, shell
. We need to make sure that our new Turtle instance has its own, separate Circle object, but that "our" circle has the same state as theirs. (Hint, there's already a copy constructor in the Circle class. See if you can use that to help you out here.)
original
.
original
as an input.
equals
method in the Turtle class. We'll declare two Turtle instances to be the same if the xDirection, yDirection, and distance values match, and their Circle objects are at the same locations. Think carefully about how to retrieve all of the information you need. How do you compare "our" xDirection to theirs? How do you compare our Circle's x position to their Circle's x position?
equals
finds them to be the same. Move one of the Turtles and try again to make sure equals
now detects that they're not the same.
TurtleMob
class and take a look around. You'll discover pretty quickly that it's not much of a mob. The class only manages two Turtle objects, but it can ask them both to move in various ways. Go ahead and create a TurtleMob
instance and use advance
to move it — that much should be working already.
TurtleMob
isn't very big, but we can create a secret weapon: We can write a copy constructor that can set up a new TurtleMob
object that looks just like an existing Mob. We could use such a constructor to "clone" an existing Mob and quickly double our numbers. A couple more copies and we're starting to look more like a mob of turtles! Finish the copy constructor in TurtleMob
. Note that we only have two fields to worry about, but that each of those fields is pretty complex — each is a Turtle instance, which has information about the direction it's facing and how far it moves, but also contains a complete Circle object, which then has its own state. Your copy constructor needs to make sure that our new pair of Turtles is identical in every way to the existing pair of Turtles. (Hint: Don't forget that we already added a copy constructor to the Turtle class! Think about how you can use that here to make your life easier.) As always, test your code before proceeding.
equals
method to the TurtleMob
class as well. It should only return true if our first turtle is an exact match for their first turtle, and our second matches their second turtle. (If only we had some easy way to test Turtles for equality... Wait, we do!)
TurtleMob
, finish the definition of sameSpot()
. It's supposed to return if both of the turtles in a given Mob are at the same position. (We're not comparing one Mob to another Mob, we're comparing "comet" to "cupid" here.)
TurtleMob
's advance
method. (Double-click on the left margin of the screen where the stop sign is shown below to set the breakpoint.) Then create a TurtleMob
like you normally would and call its advance method. The execution should pause before running the selected line of code. Experiment with the "step" and "step into" buttons. Do a "step" once, for example, and verify that comet moves forward. On the next one, try a "step into" and see what happens.
TurtleMob
so that we can control the direction of our "mob". Right now it always heads south, and there's no way to change that.
TurtleMob
class called comeTogether
that causes the two turtles (comet and cupid) to park themselves on the same spot. We can't tell a Turtle to move to a particular position, so the only way to do this is to figure out how far one turtle needs to move in the X and Y directions to get to the other, then ask it to turn and advance as necessary to get it there.
TurtleMob
that prints a message summarizing the relative positions of the two turtles. For example, it might print out something like "comet is to the north and west of cupid".