# Turtle Art

All the following exercises are intended to be implemented in the Greenfoot scenario "turtle-v3". Download the scenario here and open it in Greenfoot before continuing.

## Tips on Adding and Copying Code

This video covers some common problems that students have when they are creating or copying snippets of code.

## Draw a Square

Open up the editor for the Turtle class. You can do this by double-clicking on the Turtle class in the "Actor classes" area of Greenfoot, or right-click (or control-click) on the Turtle class and select "Open editor".

Locate the cursor outside any method definition but inside the class definition of Turtle.

Use the "Insert method" selection from the Edit menu to insert a new method.

Modify the method to look like the following:

``` /** * Method to draw a square with width and height of 30 pixels. */ public void drawSquare() { } ```

Make sure your project still compiles. Resolve any compiler errors.

Using calls to turnRight, forward, and turnLeft, add code to your drawSquare method so it will make the turtle draw a square.

Compile your program. Resolve any compiler errors.

Add an instance of the Turtle class to TurtleWorld and invoke the drawSquare method to make sure it works.

## Draw a Square Using a Variable for Width

In algebra, variables are letters that can stand for any number. In programming, variables are words that can store many different kinds of values, including numbers. One kind of variable defined in Java is the local variable. Local variables are defined inside a method. The local variables in a method are "born" when the method is invoked and "die" when the method terminates.

``` /** * Method to draw a square with a width and height determined by a variable. */ public void drawSquare2() { int width = 30; } ```

When we write `int width = 30;`, we are telling the compiler, "Create an `int` (integer) variable named `width`, and assign it the value 30". This statement is called an assignment statement

Notice that the equal sign has a special meaning in programming languages like Java. In Java, we call it an assignment operator since it assigns a value to a variable.

The equal sign always assigns the value on its right to the variable on its left. In our example, `width` is the variable to left of the equal sign, and 30 is the value to its right.

Once a variable has been assigned a value, we can use the variable anyplace we would use that value. Since the variable `width` has a value of 30, we can use it anyplace where we could use the value 30. For example, we could write `this.forward(width);` instead of `this.forward(30);`

You will add code to this method to make it draw a square like the one you drew in drawSquare. However, in all your code after the assignment statement, use the variable `width` in place of the value 30. You may want to copy and paste the statements from your drawSquare method and replace each 30 with `width`.

Compile your program. Resolve any compiler errors.

Add an instance of the Turtle class to TurtleWorld and invoke the drawSquare method to make sure it works.

## Draw a Square Using a Width as a Parameter

Some methods, like `forward`, need information in order to do their jobs. The `forward` method needs a value of type `int` to tell it how many pixels to move the turtle. The values passed to a method are called arguments. We use a list of variables called parameter in a method definition to store the arguments that are passed to our method.

Copy and paste drawSquare2 in a new location inside your Turtle class and modify the comment and method signature as shown below. Don't change the body of your method.

``` /** * Method to draw a square with a width and height of some passed amount. * * @param width number of pixels to use for width and height of square */ public void drawSquare(int width) { // Leave your statements from drawSquare2 here } ```

What happens when you try to compile? You should see a squiggly red line under the statement `int width = 30;` and when you hover your mouse over the statement, you should see a message like "variable width is already defined in method drawSquare(int)". Once `width` has been defined as a parameter, it can't be defined again as a local variable.

Remove the statement `int width = 30;` from your method so that we use the parameter `width` instead of the local variable `width`.

Compile your program. Resolve any compiler errors.

Add an instance of the Turtle class to TurtleWorld and invoke the drawSquare method to make sure it works. Notice that you when you right-click (or control-click) on the turtle you now have two drawSquare methods to choose from. Why? Which one is the one you just wrote? In general, Java uses the name of the method and its parameter list to tell one method from another.

Use your new version of drawSquare to draw squares of different sizes.

Use the quizlet below to check your understanding of local variables, parameters, and the assignment statement.