Lab 1: Turtle Graphics

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 (if you had one) 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 review programming in Java. By the end of the lab, you should be familiar with the turtle graphics statements and how to use them.

B. Skeleton Program

A Java program follows this skeleton. You substitute code for the italicized words in the skeleton.

// Comment describing the program

import statements; public class ProgramName { public static void main(String[] args) { statements; } }

For example, here's the first program from Chapter 2 of Wu.

/*

    Chapter 2 Sample Program: Displaying a Window

    File: Ch2Sample1.java
	
*/

import javax.swing.*;

public class Ch2Sample1 {

  public static void main (String[] args) {

    JFrame myWindow;

    myWindow = new JFrame ();
	
	myWindow.setSize (300, 200);
	myWindow.setTitle ("My First Java Program");
	myWindow.setVisible (true);
  }
}

C. Skeleton for a Turtle Graphics Program

  1. Many of our first programs will use a special programming system called turtle graphics. In turtle graphics, the program controls a make-believe turtle on the screen. As the turtle moves around, it leaves a line behind. (The turtle graphics package is not standard Java, but should work on any computer that has a graphics display. You can download the package to your own computer from the C SC 160 web site.)
  2. Here is the skeleton for a program that uses turtle graphics.
  3. // Comment describing the program
    

    import turtlegraphics.*;

    public class ProgramName { public static void main(String[] args) throws TurtleException { Turtle turtleName = new Turtle(); statements; } }

  4. Here's an example of a turtle graphics program. Click the Download link below to copy the program to your computer. (Click the Help link for more information about copying programs to your computer.) Make sure to save the program as FirstProgram.java, and compile it (use javac FirstProgram.java).
  5. FirstProgram.javaDownload (Help)
    // What does this display?
    
    import turtlegraphics.*;
    
    public class FirstProgram
    {
      public static void main(String[] args)
      throws TurtleException
      {
        Turtle myTurtle = new Turtle();
        for (int i = 1; i <= 20; i++)
        {
          myTurtle.move(i * 40);
          myTurtle.turnRight(120);
        }
      }
    }
    FirstProgram.javaDownload (Help)

  6. When you run the program, you should see this on the screen.
  7. Image of turtle drawing

  8. Notice the little ^ in the middle of the screen -- that's the turtle. The statements in the program tell the turtle to move around and draw stuff.
  9. Make the turtle move one step by clicking the Single Step button once with your mouse.
  10. Question
    1
    What did the turtle do when you just pressed the Single Step button?

    1. Moved a little to the right and down, leaving a line behind.
    2. Drew several squares.
    3. Moved a little up, leaving a line behind.
    4. Turned around and faced the opposite direction.
    5. Turned to the right, but stayed in the same position.

  11. Make the turtle move again by clicking Single Step. (Watch this one closely, or you'll miss it.)
  12. Question
    2
    What did the turtle do when you just pressed the Single Step button?

    1. Moved a little up, leaving a line behind.
    2. Turned to the right, but stayed in the same position.
    3. Moved a little to the right and down, leaving a line behind.
    4. Turned around and faced the opposite direction.
    5. Drew several squares.

  13. Make the turtle move again by clicking Single Step.
  14. Question
    3
    What did the turtle do when you just pressed the Single Step button?

    1. Turned around and faced the opposite direction.
    2. Moved a little up, leaving a line behind.
    3. Moved a little to the right and down, leaving a line behind.
    4. Turned to the right, but stayed in the same position.
    5. Drew several squares.

  15. You can make the turtle take one step at a time like this, but it can get tedious. The Continuous button makes the turtle run at full speed until the program has no more statements. Click the Continuous button to make the turtle follow the rest of the program's instructions.
  16. Question
    4
    What did the turtle draw?

    1. Several pentagons.
    2. A triangle-shaped spiral.
    3. Interlocking pentagons.
    4. Interlocking squares.
    5. Swirling squares.

  17. When you are done with the program, you can press the Exit button to close the Turtle Graphics window. (The X button in the upper right corner does not close a Turtle Graphics window.) Press the Exit button.
  18. Run the program again, and press the Single Step button exactly 10 times.
  19. Question
    5
    What did the turtle do on the 10th press of the Single Step button?

    1. Turned to face the bottom left corner, but stayed in the same position.
    2. Turned to face the top left corner, but stayed in the same position.
    3. Moved a little to the left, leaving a line behind.
    4. Moved a little to the right, leaving a line behind.
    5. Turned to face the top right corner, but stayed in the same position.

  20. Run the program to completion.
  21. Before exiting the program, see what happens if you change the size of the Turtle Graphics window by maximizing it or dragging its edges.
  22. Question
    6
    What happens when you change the size of the window?

    1. The drawing disappears.
    2. The size of the drawing stays the same.
    3. The size of the window is fixed.
    4. The drawing gets bigger when the window gets smaller, and vice-versa.
    5. The drawing grows and shrinks with the window size.

  23. Exit the program.

D. The Turtle

  1. The first thing a turtle graphics program must have is a turtle. This statement in the first program creates a turtle called myTurtle.
  2.     Turtle myTurtle = new Turtle();

  3. You can call the turtle anything you like, as long as it starts with a letter and has letters and digits after that (no spaces!).
  4. As an experiment, rename myTurtle to your own first and last name, with no spaces. (You will also need to change the other two uses of the turtle's name.) Compile the program; you should not get an error message (unless your name has characters that are not letters and digits!)
  5. Now try putting // in front of the turtle line, making it a comment. To the compiler, the effect is the same as if the line were not there. Compile the program.
  6. Question
    7
    Does the program compile?

    1. Yes, but the turtle's drawing window is not the correct size.
    2. Yes, there is no change.
    3. Yes, but the program doesn't run the same way.
    4. No, the compiler displays an error message.
    5. Yes, but the color of the turtle is different.

  7. Now remove the // and put a space in the turtle's name and compile the program.
  8. Question
    8
    Does the program compile?

    1. Yes, there is no change.
    2. Yes, but the program doesn't run the same way.
    3. Yes, but the turtle's drawing window is not the correct size.
    4. No, the compiler displays an error message.
    5. Yes, but the color of the turtle is different.

  9. Now try renaming the turtle to something that starts with a digit, such as 344Turtle, and compile the program.
  10. Question
    9
    Does the program compile?

    1. Yes, but the turtle's drawing window is not the correct size.
    2. No, the compiler displays an error message.
    3. Yes, but the program doesn't run the same way.
    4. Yes, but the color of the turtle is different.
    5. Yes, there is no change.

E. The Turtle and its World

The turtle is an imaginary creature that moves around on the computer screen. We control the turtle with instructions in a Java program, such as move and turnRight. As it moves, the turtle draws lines with an imaginary pen. If the turtle's pen is down, the turtle leaves a line behind when it moves.

If the turtle lifts its pen, it can move without drawing a line. To draw a dotted line, for example, we have the turtle move a little and pick up its pen. We then move it a little more and put its pen down. We continue moving the turtle this way, alternately moving with the pen up and then down.

The turtle's world is the drawing window (the one labeled "Turtle Graphics") on the computer screen. The turtle can move anywhere within this drawing window. The turtle measures distance in turtle steps (t-steps for short) , where one t-step is the smallest distance the turtle can move. The distance from the left side of the drawing window to the right is 1200 t-steps, and the distance from the top to the bottom is also 1200 t-steps. (These measurements hold no matter how big or small the window is.)

To make the turtle draw something, you write instructions in a Java program and compile the program. When you run the program, the turtle will move around the screen according to the instructions in the program.

The turtle starts in the center of its world, facing straight up, with its pen down.

F. Turtle Statements

There are four turtle graphics statements. (We've seen two in the first program.)

The move Instruction

The move instruction tells the turtle to move in the direction it's facing. If the turtle's pen is down, the turtle draws a line as it moves. When you use move, you need to tell the turtle how far to go. Here's how to move the turtle myTurtle 300 t-steps, for example.

myTurtle.move(300);

You should try to think of this statement as a request to the turtle myTurtle to move 300 t-steps. Try to imagine myTurtle as an object that can respond to requests. (That's what it is, after all. Later in the course, we'll learn more about objects and object-oriented programming...)

The reason you should think of myTurtle.move(300) as a request (instead of a command) is that the turtle may decide not to follow your request. For example, the turtle will refuse to move off the drawing window -- so be careful when the turtle is near an edge. The turtle won't move zero or a negative number of t-steps, either.

When the turtle refuses to follow a request, it gives up on the program. The computer stops the program and displays an error message.

The turnRight Instruction

To make the turtle face a different direction, ask the turtle to turnRight. The turtle will turn any number of degrees from 0 to 180, and will refuse to turn any amount outside that range.

To turn the turtle myTurtle around so it faces the opposite direction, for example, you'd write this in a program.

myTurtle.turnRight(180);

The penUp Instruction

Use the penUp instruction to ask the turtle to pick up its pen. When the turtle's pen is up, it won't leave a line behind as it moves. The turtle refuses to pick up its pen if it's already up.

Here's how to ask the turtle myTurtle to pick up its pen.

myTurtle.penUp();

The penDown Instruction

The penDown instruction is the opposite of penUp. The penDown instruction asks the turtle to put its pen down, so it leaves a line behind as it moves. The turtle refuses to put its pen down if it's already down.

Here's how to ask the turtle myTurtle put down its pen.

myTurtle.penDown();

G. Putting it All Together

As a simple example, we'll have the turtle draw a square. Let's think through the process before we write the program. The easiest way to design a turtle graphics program is to imagine it from the turtle's point of view. From this point of view, the process of drawing a square consists of these steps.

You might have noticed that we don't need the last turn. However, it's good practice to leave the turtle in the same position and direction in which it started.

Before writing the program, we need to decide how big to make the square. Remember that the drawing window is 1200 t-steps across and 1200 t-steps down. The turtle starts in the middle of the drawing window, so we'd better make the size of the square less than 600-- let's use 500.

  1. Here's the program. Notice that it follows the steps above. Click the Download link below to copy the program to your computer. Compile and run the program on your computer.
  2. DrawSquare.javaDownload (Help)
    // This program draws a square on the screen
    
    import turtlegraphics.*;
    
    public class DrawSquare
    {
      public static void main(String[] args)
      throws TurtleException
      {
        Turtle myTurtle = new Turtle();
        
        myTurtle.move(500);
        myTurtle.turnRight(90);
        myTurtle.move(500);
        myTurtle.turnRight(90);
        myTurtle.move(500);
        myTurtle.turnRight(90);
        myTurtle.move(500);
        myTurtle.turnRight(90);
      }
    }
    DrawSquare.javaDownload (Help)

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

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

  3. Using the editor's cut and paste commands, modify this program so it draws two distinct squares (that is, so they are both visible when the program finishes). Hint: Duplicate the eight instructions with move and turnRight in them.
  4. Question
    11
    Could you simply copy and paste the eight instructions to make the turtle draw two distinct squares?

    1. Yes, after copying the original eight instructions and pasting eight more instructions after the original eight instructions, two squares were visible.
    2. No, at least one instruction had to be changed so that both squares are visible.

H. Turtle Graphics Errors

Each of the four turtle graphics instructions can give an error message:

  1. Write a turtle graphics program that makes the turtle move off the screen. Run the program and examine the error message in the console window. (Note: Turtle errors show up when you run the program. You won't see any compile-time error messages.)

    Question
    12
    What error message displays in the console window?

    1. Exception in thread "main" TurtleException: Illegal move
    2. Exception in thread "main" TurtleException: Move too far
    3. Exception in thread "main" TurtleException: Invalid move
    4. Exception in thread "main" TurtleException: Move offscreen
    5. Exception in thread "main" TurtleException: *** ERROR ***

  2. Write a turtle graphics program that makes the turtle turn less than 0 degrees. Run the program and examine the error message in the console window.
  3. Question
    13
    What error message displays in the console window?

    1. Exception in thread "main" TurtleException: Illegal degrees
    2. Exception in thread "main" TurtleException: *** ERROR ***
    3. Exception in thread "main" TurtleException: Degrees invalid
    4. Exception in thread "main" TurtleException: Degrees too small
    5. Exception in thread "main" TurtleException: Degrees too large

  4. Write a turtle graphics program that makes the turtle turn more than 180 degrees. Run the program and examine the error message in the console window.
  5. Question
    14
    What error message displays in the console window?

    1. Exception in thread "main" TurtleException: Degrees invalid
    2. Exception in thread "main" TurtleException: *** ERROR ***
    3. Exception in thread "main" TurtleException: Degrees too large
    4. Exception in thread "main" TurtleException: Illegal degrees
    5. Exception in thread "main" TurtleException: Degrees too small

  6. Write a turtle graphics program that makes the turtle put down its pen when the pen is already down. Run the program and examine the error message in the console window.
  7. Question
    15
    What error message displays in the console window?

    1. Exception in thread "main" TurtleException: Invalid pen movement
    2. Exception in thread "main" TurtleException: Pen already up
    3. Exception in thread "main" TurtleException: Pen must be up first
    4. Exception in thread "main" TurtleException: Pen already down
    5. Exception in thread "main" TurtleException: *** ERROR ***

  8. Write a turtle graphics program that makes the turtle pick up its pen when the pen is already up. Run the program and examine the error message in the console window.
  9. Question
    16
    What error message displays in the console window?

    1. Exception in thread "main" TurtleException: Pen already up
    2. Exception in thread "main" TurtleException: Pen already down
    3. Exception in thread "main" TurtleException: Pen must be down first
    4. Exception in thread "main" TurtleException: Invalid pen movement
    5. Exception in thread "main" TurtleException: *** ERROR ***


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.