// SquareTurtle Demo Program // this program illustrates two different ways of drawing squares with a Turtle // 1. adding the static method to the client program and passing a Turtle object // as a parameter. // The call statement drawSquare(donny); passes donny // as an actual parameter. donny plugs in for anyTurtle which is a formal // parameter in the static method. // 2. typing out a whole class named SquareTurtle that includes a drawSquare // instance method. // Because leo is declared & instantiated as a SquareTurtle, // the call statement leo.drawSquare(); passes leo as an implied // parameter to the drawSquare method that is found in the SquareTurtle class. // The optional keyword this is used to refer to leo since he is implied // parameter that was passed. // Notice how the class SquareTurtle extends the Turtle class // This is called inheritance and it saves time by making all of the methods // in Turtle (and SimpleTurtle) available for use by SquareTurtle's! // For certain tasks, "static" methods are preferable. But usually it is better // to create a whole new class that contains an "instance" method. The // drawSquare method is much more reusable BY OTHER CLIENT PROGRAMMERS // if it is packaged into a separate class that can be distributed and even // posted online as external class folders and jar files. Static methods are // not very reusable since the other client programmer would need access to // the source code of the original client program in order to copy and paste // the code. In the real world, client programmers do not share their client // program source code with other client programmers. But class files like // SquareTurtle are distributed or even sold along with their API documentation // to client programmers. ////////////////// CLIENT PROGRAM //////////////////////////////////////////// public class SquareTurtleDemo { public static void main(String[] args) { World earth = new World(); Turtle donny = new Turtle(50, 250, earth); SquareTurtle leo = new SquareTurtle(100, 100, earth); drawSquare(donny); // not a great way to draw a square leo.drawSquare(); // better way to draw a square } // draw a square public static void drawSquare(Turtle anyTurtle) { anyTurtle.penDown(); anyTurtle.forward(); anyTurtle.turnRight(); anyTurtle.forward(); anyTurtle.turnRight(); anyTurtle.forward(); anyTurtle.turnRight(); anyTurtle.forward(); } } //////////////// SQUARE TURTLE CLASS ///////////////////////////////////////// // pretend this is in a different java source file named SquareTurtle.java // if the keyword public is removed, you can be lazy and type multiple classes // in the same file as a client program that has a main method. I only do this // to make it easier to post examples on my website and to save paper when // printing worksheets. class SquareTurtle extends Turtle { // constructor (required, we will study constructors later) public SquareTurtle(int x, int y, ModelDisplay display) { super(x, y, display); // super means that you are calling a constructor } // in the parent class (Turtle) // draw a square public void drawSquare() { this.penDown(); this.forward(); this.turnRight(); this.forward(); this.turnRight(); this.forward(); turnRight(); // using the keyword this is optional forward(); } }