/** * Class that represents a turtle that * follows another turtle */ public class TurtleFollower extends Turtle { /** Copy-pasted from Turtle. * Constructor that takes the model display * @param modelDisplay the thing that displays the model */ public TurtleFollower (ModelDisplay modelDisplay) { // let the parent constructor handle it super(modelDisplay); } private Turtle myLeader; public void setLeader(Turtle aTurtle){ myLeader = aTurtle; } public void step(){ this.turnToFace(myLeader); this.forward(10); } public static void main(String [] args){ World earth = new World(); int size = 500; TurtleFollower t1 = new TurtleFollower(earth); t1.penUp(); t1.forward(size); t1.turnRight(); t1.forward(size); t1.setHeading(0); t1.penDown(); TurtleFollower t2 = new TurtleFollower(earth); t2.penUp(); t2.forward(size); t2.turnLeft(); t2.forward(size); t2.setHeading(270); t2.penDown(); TurtleFollower t3 = new TurtleFollower(earth); t3.penUp(); t3.forward(-size); t3.turnLeft(); t3.forward(size); t3.setHeading(180); t3.penDown(); TurtleFollower t4 = new TurtleFollower(earth); t4.penUp(); t4.forward(-size); t4.turnRight(); t4.forward(size); t4.setHeading(90); t4.penDown(); t1.setLeader(t2); t2.setLeader(t3); t3.setLeader(t4); t4.setLeader(t1); for (int i = 0; i < 100; i++){ t1.step(); t2.step(); t3.step(); t4.step(); try {Thread.sleep(300);} // See page 88 -- about 1/3 of a second catch(Exception e){System.err.println("Something went wrong!");} } } }