CS 161 Lab B - Turtle Graphics

Due Fri Jan 30 at the start of class

Overview

In the late 60s, Seymour Papert at MIT developed a robot "turtle" to help teach young children to program. This robot has a small pen attached to it, which would draw a trail (a line) on a piece of paper as the robot moved and turned. Children could then program the robot's movement in order to have it draw a particular picture. This method of producing drawings is known as turtle graphics, versions of which can be found in pretty much every programming language.

In this lab, you will program a Java version of the robot to draw some simple images. This will give you a chance to continue practice writing Java code and calling methods on Java objects.

This lab will be completed in pairs. You should review the pair programming guidelines before you get started.

Objectives

Necessary Files

You will need to download and extract the Turtles BlueJ project from the Turtles.zip file. This project includes Turtle class that has already been written for you, so you have a Turtle to work with. You will need to create (simple) classes as well.

The Turtle

Below is a list of some of the methods that the turtle supports:

    /**
     * Moves the turtle forward the specified distance (in pixels)
     */
    public void forward(int distance)
    
    /**
     * Turns the turtle to the left the specified number of degrees
     */
    public void left(int angle)

    /**
     * Turns the turtle to the right the specified number of degrees
     */
    public void right(int angle)
    
    /**
     * Puts the turtle's pen down.
     */
    public void penDown()
    
    /**
     * Lifts the turtle's pen up.
     */
    public void penUp()
    
    /**
     * Sets the color of the turtle's pen, using the name of the color. Can either be:
     * "red", "green", "blue", "cyan", "magenta", "yellow", "pink", "gray", "black", "white"
     */
    public void setPenColorByName(String color)        

    /**
     * Sets the turtle's speed. 0.5 is default, 1 is fastest, 0 is slowest.
     */
    public void setSpeed(double speed)

Assignment Details

Drawing 1: A Bowtie

  1. Let's start by drawing something following the instructions below (this is the same process you used in the last lab):
    Create a Turtle object named myTurtle
    Set myTurtle's pen color to "blue"
    Turn myTurtle left by 90 degrees
    Move myTurtle forward by 300
    Turn myTurtle right by 135 degrees
    Move myTurtle forward by 425
    Turn myTurtle left by 135
    Move myTurtle forward by 300
    Turn myTurtle left by 135
    Move myTurtle forward by 425

    That should be straightforward, but incredibly tedious to do by hand. Can you image how long it would take to draw something more sophisticated than a bowtie? Or what happens if you screw up?! Luckily, we can program the computer to do all that work for us.

  2. Go back to the BlueJ project window and create a new Bowtie object in the workbench. When you right-click to view it's methods, you'll notice that it doesn't have any! So let's add a method: double-click on the Bowtie class to view the source code. As you can see, besides the class declaration and a few comments, not much has been provided for you (thanks Joel).
  3. First, fill in your names next to the @author tag (e.g., @author Joel & David), and add in a quick comment describing the class.
  4. Now the fun part: you need to create a new method for the object. We'll call it draw():
      /**
       * REPLACE THIS: Fill in a brief bescription of what method does
       */
      public void draw()
      {
        //add statements to draw the bowtie here
    
      }
    • The first few lines are a block comment that describes the method. Recall that comments are completely ignored by the Java compiler, so they only serve the purpose of informing people reading the code. Go ahead and fill in a brief sentence about what this method will perform when it is called.
    • The next line is the all-important method signature. It says that the method is:
      • public and so is visible to users.
      • void and so will not return any values
      • draw() is the method's name, followed by a list of parameters. In this case, no parameters are required.
    • Everyelse is known as the method body. There is where the algorithm goes--the step-by-step instructions how to solve the problem!
  5. This is a good time to switch drivers!
  6. The body of the void draw() method should run the algorithm you just executed. This means you'll need to translate from the natural language description to the Java language description.
    • To instantiate (create) a new object of class ClassName called objectName, you can write:
      ClassName objectName = new ClassName();
    • To call a method methodName() on an object, you can write:
      objectName.method(param1, param2, ...);

      where param1, param2, ... are the parameters separated by commas. Note that the parameter list may be empty.

  7. To get a feel for the translation process, we've gotten things started for you by providing code to create the Turtle and set its color. Your job is to complete the remaining translation and finish filling in the draw() method.
    Algorithm in Natural Language Algorithm in Java (unfinished)
    Create a Turtle object named myTurtle
    Set myTurtle's pen color to "blue"
    Turn myTurtle left by 90 degrees
    Move myTurtle forward by 300
    Turn myTurtle right by 135 degrees
    Move myTurtle forward by 425
    Turn myTurtle left by 135
    Move myTurtle forward by 300
    Turn myTurtle left by 135
    Move myTurtle forward by 425
    Turtle myTurtle = new Turtle();
    myTurtle.setPenColorByName("blue");

    Helpful hint! Try writing only a few method calls at a time, and then compile and test your method. Get used to this "write a little; test it; write a little more" workflow---it will make your life much easier in the future!

  8. After you've implemented the method and successfully compiled your program, instantiate a new Bowtie object and call its draw() method. The drawing should automatically appear, and you've just completed your first method in Java!

Drawing 2: Your Initials

  1. For the second drawing, you should program the turtle to draw out the first initials of you and your partner. So if Joel and David were working together, we'd program the turtle to draw
    J + D
  2. This is a good time to switch drivers!
  3. You should write this program in a new class to represent your drawing. Click on the New Class button and name your class InitialDrawing.
  4. You'll need to delete most of the sample code that BlueJ provides--everything except the class declaration. You'll then need to add in a public void draw method declaration. Your class should thus look like:
  5. Inside the body of the draw() method, you'll need to instantiate a new Turtle object; see the above section for details. You can then use BlueJ to create a new red InitialDrawing object box, and then right-click on that to call your draw() method.
  6. You should program your turtle using the same tricks and techniques you worked on for drawing the bowtie. Be sure to write a bit of your code, then compile and run to test your program!
  7. You are welcome to get creative with this--using different colors, decorations, etc. But make sure you have time to complete the whole lab!

Drawing 3: The House

  1. Your final task will be to write a program that instructs a turtle to draw a simple picture of a house:
  2. This is a good time to switch drivers!
  3. You should write this program in a new class to represent your house drawing. Click on the New Class button and name your class HouseDrawing.
  4. The pitch (or angle) of the roof should be 45 degrees, with the tallest point being lined up to the center of the house. Turning the turtle in the right directions should not be a problem, but calculating the distance that needs to be drawn can present some challenges. If you go too far or stop too short (see below), the pitch will be skewed to one side:

    The trick to solving this problem is to observe that the side of the roof is the hypoteneuse, or the longest side (C), of a right triangle, where the other two sides (A) and (B) have equal length:

    Luckily, we know the Pythagorean Theorem. Can you use it to solve for the hypotenuse? You will need to get the square root of a number, and Java provides this useful method:

    Math.sqrt(n)
    where n is a number. You can use this to calculate the length of the roof, and then plug that number into the forward() method as follows:
      double roofLength = Math.sqrt(???); //this is a double because it may not be a whole number
      turtle.forward((int)roofLength);
    (We need to put the (int) in front of the roofLength to turn it back into a whole number that the forward() method expects).

Submission

  1. Make sure both of your names are on the classes you've created (HouseDrawing and InitialDrawing). If your names aren't on your assignment, we can't give you credit!
  2. Right-click on the project folder you downloaded, then:
    • If using Linux, select Compress...
    • If using Windows, select Send to and then Zip file
    • If using Mac, select Compress ... items

    This will let you take the selected folder (or files) and generate a new compressed .zip file.

  3. Navigate to the course on Moodle (Lecture Section A), (Lecture Section B). Upload your .zip file to the Lab B Submission page. Remember to click "Save Changes"! You may submit as often as you'd like before the deadline; we will grade the most recent copy.
    • Only one partner needs to submit the assignment!
  4. While you're on Moodle, remember to fill out the Lab B Partner Evaluation. Both partners need to submit evaluations.

Extensions

An OPTIONAL note about color:

Grading

This assignment will be graded out of 10 points.