Overview

We’ve now had a chance to explore the Turtle class and its basic capabilities. However, what happens if we aren’t satisfied with its current capabilities and want to add new ones to a class? In this reading we’ll look at how to add new methods to a class, focusing for now on modifying the Turtle class. We’ll also get an early glimpse at our next topic, pictures, by learning how to create and display pictures.

Required Reading

Sections 3.5-3.7 (pp. 50-74) from the course textbook.

Learning Objectives

BASIC Learning Objectives

Each student will be responsible for learning and demonstrating proficiency in the following objectives PRIOR to the class meeting. The reading quiz will test these objectives.

  1. Identify each component of a method declaration.
  2. Describe what it means for a method to have a return type of void.
  3. Add a method (e.g. drawSquare) to the Turtle class using DrJava.
  4. In DrJava, create a turtle and have it perform a method that you added to the Turtle class (e.g. drawSquare).
  5. Write a line of code that declares and creates a new Picture object based on a picture to be selected with the file chooser.

ADVANCED Learning Objectives

The following objectives should be mastered by each student DURING and FOLLOWING the class session through active work and practice.

  1. Identify common errors in Java code and use computing terminology to describe them.
  2. Given the description of a method, identify the appropriate return type for the method.

Pre-class Exercises

These exercises are geared towards mastering the BASIC learning objectives listed above. You are expected to submit them before class and it is highly recommended that you complete them before attempting the reading quiz.

  1. I wrote a method named drawRectangle that makes a turtle draw a rectangle of a given width and height. The header for that method is given below.

    public void drawRectangle(int width, int height)

    If I have a turtle variable named satTheTurtle, write a Java expression that makes this turtle draw a 30x50 rectangle. Note that you don’t have to write the code inside of the drawRectangle method, you should simply call that method with the correct parameters (like you would call e.g. the forward method).

  2. In the body of my drawRectangle method, would I need to create a new turtle object? Explain why or why not.

  3. For each of the following, write a Java statement that performs that action.