Lab 2: Turtle Graphics, Classes & Methods

Instructions: Follow the steps given in each section below. For each question, click the radio button next to the most correct answer. When you are finished, type your name and your partner at the end of the document, write down your answers for your records, then press the Submit button. You may submit just one lab response. If you submit more than one, only the first one counts for a grade.

Contents

A. Purpose

This lab is designed to introduce you to classes and methods, and to see why they are useful. By the end of the lab, you should be able to write a simple class that extends the Turtle class.

B. Turning to the Left

  1. The turtle can only turn between 0 and 180 degrees to the right. To turn to the left, therefore, requires two turnRight() instructions. For example, this program makes the turtle face straight left and draw a line. Download the program, then compile and run it on your computer.
  2. DrawStraightLineLeft.java Download (Help)
    // Draw a line straight left
    
    import turtlegraphics.*;
    
    public class DrawStraightLineLeft
    {
      public static void main(String[] args)
      throws TurtleException
      {
        Turtle aTurtle = new Turtle();
    
        // Face left by using two turnRight() instructions
        aTurtle.turnRight(180);  // Face down
        aTurtle.turnRight(90);   // Face left
    
        // Draw a line
        aTurtle.move(300);
      }
    }
    DrawStraightLineLeft.java Download (Help)

    Question
    1
    What does the turtle draw when you run program DrawStraightLineLeft?

    1. A vertical line in the right half of the drawing window.
    2. A line drawn at an angle in the center of the drawing window.
    3. A horizontal line in the left half of the drawing window.
    4. A vertical line in the left half of the drawing window.
    5. A horizontal line in the right half of the drawing window.

  3. Here's an example of a program that uses only right turns to draw a square. Download the file, compile it and run it.
  4. DrawSquare.javaDownload (Help)
    // Draw a square
    
    import turtlegraphics.*;
    
    public class DrawSquare
    {
      public static void main(String[] args)
      throws TurtleException
      {
        Turtle aTurtle = new Turtle();
    
        // Draw first edge
        aTurtle.move(300);
        aTurtle.turnRight(90);
    
        // Draw second edge
        aTurtle.move(300);
        aTurtle.turnRight(90);
    
        // Draw third edge
        aTurtle.move(300);
        aTurtle.turnRight(90);
    
        // Draw fourth edge
        aTurtle.move(300);
        aTurtle.turnRight(90);
      }
    }
    DrawSquare.javaDownload (Help)

    Question
    2
    What does the turtle draw when you run program DrawSquare?

    1. A square in the lower right part of the drawing window.
    2. A square in the upper left part of the drawing window.
    3. A square in the upper right part of the drawing window.
    4. A square in the center of the drawing window.
    5. A square in the lower left part of the drawing window.

  5. Open DrawSquare.java with the editor (File|Open), and change the program so it draws a square with left turns instead of right turns.
  6. Question
    3
    After you have made the changes to program DrawSquare, what does the turtle draw when you run the program?

    1. A square in the lower right part of the drawing window.
    2. A square in the center of the drawing window.
    3. A square in the lower left part of the drawing window.
    4. A square in the upper right part of the drawing window.
    5. A square in the upper left part of the drawing window.

  7. Using two turnRight()instructions every time you want the turtle to turn left is tedious, especially when the left turn angle is something other than 90 degrees. Therefore, we're going to learn how to create a new instruction for turning left.
  8. We won't be able to add this new instruction to the those in the Turtle class; that class is fixed. But we can do the next best thing -- we can make a new class with all the instructions of the Turtle class plus more.

C. The SmartTurtle Class

  1. We can write a new class that extends the Turtle class with new instructions. We'll call this new class the SmartTurtle class. Add this code to the program you have already, between the import statement and the class DrawSquare line.
  2. class SmartTurtle extends Turtle
    {
    }
    

  3. Compile the program. You should not get any error messages.
  4. Now change the turtle definition from
  5.     Turtle aTurtle = new Turtle();

    to

        SmartTurtle aTurtle = new SmartTurtle();
    

    (This changes the turtle from a regular Turtle to a SmartTurtle turtle.)

  6. Save and compile the program. You should not get any error messages. Try running the program -- it should run the same as before. This example shows that a SmartTurtle turtle can do everything a regular turtle can do.

D. Creating Methods

  1. Let's add a method to the SmartTurtle class: one that makes the turtle turn 90 degrees to the left. Put this code between the braces after class SmartTurtle extends Turtle:
  2.     // turnLeft90: Turns the turtle 90 degrees to its left
        public void turnLeft90()
        throws TurtleException
        {
          this.turnRight(180);
          this.turnRight(90);
        }
    

  3. Right now, the main part of your program turns left 4 times, each time using 2 turnRight instructions. Change the main part of your program so it turns left using turnLeft90 instead of using turnRight. (You should have 4 turnLeft90 instructions in your program.)
  4. Compile your program and run it. The finished drawing should look the same as before.
  5. You can give any name (such as turnLeft90) you want to a method, as long as it starts with a letter and uses letters and digits after the first letter. However, it's best to give each method a name that describes what that method does. In this case, turnLeft90 describes the method's purpose succinctly. It's a Java convention to begin method names with a lower case letter, and to mark word beginnings (such as Left in turnLeft90 and Right in turnRight) with a capital letter. It's also a Java convention to begin class names (such as Turtle and SmartTurtle) with a capital letter.
  6. Is there a minimum length for method names? Try changing the name of the method to a single letter.
  7. Question
    4
    Does the program still compile and run?

    1. Yes
    2. No

  8. Is there a maximum length for method names? Try making the name of your method very long.
  9. Question
    5
    Is there an apparent maximum length? (If your editor won't let you type any more characters, then try compiling it with that many characters. If it still compiles, answer No.)

    1. No
    2. Yes

  10. Change the name of the method back to turnLeft90.
  11. The word this (in this.turnRight(180)) refers to the current turtle object. The reason we don't use aTurtle (or some other turtle name) is that turnLeft90 is an instruction for a class of turtles -- not a particular turtle. Try changing this.turnRight(xxx) to aTurtle.turnRight(xxx) in both lines of the turnLeft90 instruction. Compile the program.
  12. Question
    6
    Does the program compile?

    1. No
    2. Yes

  13. The word this is optional, because the default object is this. Try deleting aTurtle (and the following period) from both lines of the turnLeft instruction. When you compile the program, you should not get an error message.
  14. Add the method turnAround to SmartTurtle. This method should make the turtle turn around, facing exactly the opposite way it was facing before. Use turnAround in a program to make sure it works. (Be sure to include a descriptive comment.)
  15. Question
    7
    What statement goes inside your turnAround method?

    1. turnRight(180);
    2. turnRight(0);
    3. turnRight(270);
    4. turnRight(360);
    5. turnRight(90);

  16. A common error when using any class is to use different class names in the object definition. Change the turtle definition so it looks like this.
  17.     SmartTurtle aTurtle = new Turtle();  // Note "Turtle" after "new"

    Question
    8
    Does the compiler give an error message when you make this change?

    1. No, it doesn't give an error message, but it doesn't run at all.
    2. No, it compiles and runs fine.
    3. No, but it gives a warning and still compiles the program.
    4. Yes
    5. No, it compiles but doesn't run correctly.

  18. Try changing the definition to the other way, like this.
  19.     Turtle aTurtle = new SmartTurtle();  // Note "SmartTurtle" after "new"

    Question
    9
    Does the compiler give an error message when you make this change?

    1. Yes
    2. No, it compiles but doesn't run correctly.
    3. No, it doesn't give an error message, but it doesn't run at all.
    4. No, but it gives a warning and still compiles the program.
    5. No, it compiles and runs fine.

  20. It's important to use the same class name in both places. Change your program so it both class names are SmartTurtle.

    SmartTurtle aTurtle = new SmartTurtle();

E. Parameters

  1. A problem with turnLeft90 is that it can only turn 90 degrees to the left. To turn any other amount, we must create a new instruction. A better way is to use a parameter, that is, to make a turnLeft instruction that can turn any number of degrees -- just like turnRight.
  2. Add this method to the SmartTurtle class, either before or after turnLeft90.
  3.     // turnLeft: Turns the turtle the given degrees to its left
        public void turnLeft(int amount)
        throws TurtleException
        {
          this.turnRight(180);
          this.turnRight(180 - amount);
        }
    

  4. You can use turnLeft in the main part of your program just like turnRight. For example, to turn aTurtle 90 degrees to the left, write aTurtle.turnLeft(90). To turn it 30 degrees to the left, write aTurtle.turnLeft(30). Whatever number you put in parentheses goes into the parameter named amount inside the turnLeft instruction when the program runs.
  5. You can call the parameter anything you like, but it's best to make the parameter name as descriptive as possible. The word degrees is more descriptive than the fuzzy term amount. Change amount to degrees in the turnLeft instruction, and compile the program. You should not get any error messages.
  6. Change your program so it uses the new turnLeft instruction. You'll need to write turnLeft(90) wherever you currently have turnLeft90().
  7. By inspection or by trial and error, find out the range of angles that turnLeft can turn and write down this range.
  8. Question
    10
    What angles can the new turnLeft instruction turn?

    1. Between 1 and 179.
    2. Between 0 and 360.
    3. Between 0 and 180.
    4. Between 1 and 180.
    5. Between 0 and 179.

  9. Use the editor's cut and paste commands to switch the order of the turnLeft and turnLeft90 methods in the SmartTurtle class.
  10. Question
    11
    Does the order of the methods matter?

    1. No
    2. Yes, turnLeft must be first.
    3. Yes, turnLeft90 must be first.

  11. You can add any number of methods to the SmartTurtle class. For example, suppose you want a method that makes the turtle move backward. Here's one way to do it.
  12.     // moveBack: Makes the turtle move backward the given distance
        public void moveBack(int distance)
        throws TurtleException
        {
          this.turnRight(180);
          this.move(distance);
          this.turnRight(180);
        }
    

  13. Put moveBack in your SmartTurtle class, and try the new instruction in your program. For example, add this line just after the definition of the turtle object, then compile and run your program.
  14.     aTurtle.moveBack(500);

    Question
    12
    What does the turtle draw after adding this line?

    1. A square in the upper left part of the drawing window.
    2. A square in the center of the drawing window.
    3. A square in the lower right part of the drawing window.
    4. A square in the lower left part of the drawing window.
    5. A square in the upper right part of the drawing window.

  15. Add methods moveRight and moveLeft to your SmartTurtle class, and use them in a program to make sure they work. (Be sure to include a descriptive comment for each.)
  16. Add the methods drawSquare and drawEqTriangle to your SmartTurtle class. The drawSquare method should draw a square with sides of the given length. Likewise, drawEqTriangle should draw an equilateral (all sides the same length) triangle with sides of the given length.
  17. Question
    13
    What angle must the turtle turn between sides of an equilateral triangle?

    1. 60 degrees
    2. 120 degrees
    3. 30 degrees
    4. 180 degrees
    5. 90 degrees

  18. Save your work, either on a disk or your home directory (you'll want your SmartTurtle class for later).


Turn in this Assignment

First name:
Last name:
Partner's last name:

Write down your answers for your records, then press the Submit button below to turn in the lab assignment.