/** * CustomTurtleRunner * @author John Doe Per 1 */ import java.awt.Color; import java.util.List; ////////////////// CLIENT PROGRAM //////////////////////////////////////////// public class CustomTurtleRunner { public static void main(String[] args) { World earth = new World(); // SECTION A System.out.println("\n\nSECTION A"); SquareTurtle leonardo = new SquareTurtle(10, 10, earth); SquareTurtle donatello = new SquareTurtle(50, 500.0, "donny", 10, 150, earth); SquareTurtle michelangelo = new SquareTurtle(500.0, "mick", 10, 300, earth); System.out.println("other constructor test: " + leonardo.toString()); System.out.println("other constructor test: " + donatello.toString()); System.out.println("other constructor test: " + michelangelo.toString()); // SECTION B System.out.println("\n\nSECTION B"); System.out.println("a dashed line with blue and red"); leonardo.setPenColor(Color.BLUE); leonardo.turnRight(); leonardo.forward(); leonardo.penUp(); leonardo.forward(); leonardo.penDown(); leonardo.setPenColor(Color.RED); leonardo.forward(); // SECTION C System.out.println("\n\nSECTION C"); System.out.println("leonardo's health: " + leonardo.getHealth()); leonardo.setHealth(99); System.out.println("after setHealth modifier - leonardo's health: " + leonardo.getHealth()); System.out.println("leonardo's speed: " + leonardo.getSpeed()); leonardo.setSpeed(999.9); System.out.println("after setSpeed modifier - leonardo's speed: " + leonardo.getSpeed()); System.out.println("leonardo's nickname: " + leonardo.getNickname()); leonardo.setNickname("leo"); System.out.println("after setNickname modifier - leonardo's nickname: " + leonardo.getNickname()); // SECTION D System.out.println("\n\nSECTION D"); System.out.println("two squares of different colors appear"); donatello.drawSquare(); michelangelo.drawSquare(Color.RED); System.out.println("leonardo's health = " + leonardo.getHealth()); leonardo.exerciseWithTrainer(donatello); System.out.println("leonardo's health after exercising with donatello = " + leonardo.getHealth()); leonardo.forwardSlowly(50); // SECTION E System.out.println("\n\nSECTION E"); System.out.println("all turtles turned black"); SquareTurtle.turtlePlague(earth); System.out.println("the average of all turtles' health is " + SquareTurtle.averageHealth(earth)); // SECTION F System.out.println("\n\nSECTION F"); michelangelo = donatello; donatello.setHealth(1); System.out.println("michelangelo's health = " + michelangelo.getHealth() + " and donatello's health = " + donatello.getHealth()); michelangelo = new SquareTurtle(11, 500.0, "michelangelo", 200, 300, earth); System.out.println("after breaking aliasing, michelangelo's health = " + michelangelo.getHealth() + " and donatello's health = " + donatello.getHealth()); }// end of main method }// end of CustomTurtleRunner class //////////////// CUSTOM TURTLE CLASS ///////////////////////////////////////// class SquareTurtle extends Turtle { // there are technical reasons why this class does not have a default constructor // "other" constructors public SquareTurtle(int x, int y, ModelDisplay display) { super(x, y, display); // super calls a constructor in parent class (Turtle) myHealth = 100; mySpeed = 500.0; myNickname = ""; } public SquareTurtle(int health, double speed, String nickname, int x, int y, ModelDisplay display) { super(x, y, display); myHealth = health; mySpeed = speed; myNickname = nickname; } public SquareTurtle(double speed, String nickname, int x, int y, ModelDisplay display) { super(x, y, display); myHealth = 100; // health is set to default 100 mySpeed = speed; myNickname = nickname; } // accessor methods public int getHealth() { return myHealth; } public double getSpeed() { return mySpeed; } public String getNickname() { return myNickname; } // modifier methods public void setHealth(int health) { if (health >= MAX_HEALTH) { myHealth = MAX_HEALTH; } else if (health <= MIN_HEALTH) { myHealth = MIN_HEALTH; } else { myHealth = health; } } public void setSpeed(double speed) { mySpeed = speed; } public void setNickname(String nickname) { myNickname = nickname; } // interesting methods public void drawSquare() { penDown(); forward(); turnRight(); forward(); turnRight(); forward(); turnRight(); forward(); } public void drawSquare(Color color) { Color currentColor = getPenColor(); // keep track of pen's current color setPenColor(color); penDown(); forward(); turnRight(); forward(); turnRight(); forward(); turnRight(); forward(); setPenColor(currentColor); // return pen to original color } public void exerciseWithTrainer(SquareTurtle trainer) { int num = trainer.getHealth(); // trainer's health if (myHealth + num <= MAX_HEALTH) // or if (this.myHealth + num <= MAX_HEALTH) { setHealth(myHealth + num); } else { setHealth(MAX_HEALTH); } } public void forwardSlowly(int distance) { for (int i = 0; i < distance; i++) { forward(1); try { java.lang.Thread.sleep(1001 - (int) mySpeed); } catch (InterruptedException ex) {} } } // static methods public static void turtlePlague(World earth) { List listOfTurtles = earth.getTurtleList(); // collecting all turtles in World SquareTurtle[] arrayOfTurtles = (SquareTurtle[]) listOfTurtles.toArray(new SquareTurtle[0]); for (int i = 0; i < arrayOfTurtles.length; i++) { arrayOfTurtles[i].setShellColor(Color.BLACK); } } public static double averageHealth(World earth) { int numTurtles = 0; // number of SquareTurtles in World int healthTotal = 0; // sum of all SquareTurtles' healths List listOfTurtles = earth.getTurtleList(); // collecting all turtles in World SquareTurtle[] arrayOfTurtles = (SquareTurtle[]) listOfTurtles.toArray(new SquareTurtle[0]); for (int i = 0; i < arrayOfTurtles.length; i++) { healthTotal += arrayOfTurtles[i].getHealth(); numTurtles++; } return (double) healthTotal / numTurtles; } // instance variables private int myHealth; // health of SquareTurtle, an integer between 0 and 100 private double mySpeed; // speed of SquareTurtle, an integer between 0.0 and 1000.0 private String myNickname; // class constants public static final int MAX_HEALTH = 100; // upper limit of a SquareTurtle's health public static final int MIN_HEALTH = 0; // SquareTurtle cannot have negative health }// end of SquareTurtle class