# Return Values

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

## Methods we can use like integers

This video shows how some methods can be used like integers to make an interesting design.

NOTE: Arithmetic operators +, -, *, /, and % don't assign values like the assignment operators +=, -=, *=, /= and %=. Consider these statements:

```x + y; // The addition is carried out but the result is not assigned to x or y, so nothing changes w = x + y; // Assigns the sum of x and y to w without changing x or y x += y; // Assigns the value of x + y to x x = x + y // Assigns the value of x + y to x ```

Notice that the last two statements do exactly the same thing.

Try using some combination of the instance variables `side` and `angle` and local variables x, y and r to make an interesting pattern. When you've made a pattern you like, save it to a PNG file.

Try the following `act` method:

``` /** * Act - do whatever the Turtle wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { int x = this.getX(); this.turn(x*this.getY()); this.forward(this.side); this.turn(this.getRotation()-x); this.forward(this.side); } ```

Add this method to your Turtle class:

``` /** * Multiplies a given number by two. * * @param number The integer to be doubled. * @return Twice the value of number. */ public int timesTwo(int number) { return number * 2; } ```

What do you think the `timesTwo` method does? Let's take a closer at its different pieces:

• Its method signature is `public int timesTwo(int number)`
• Its visibility modifier is `public`
• Its return type is `int`. This means that you can use this method like an integer.
• The body of the method has only one statement in it: `return number * 2;`. The `return` statement determines what integer value will be returned by the method
• The method name is `timesTwo`
• It has one parameter. The name of the parameter is `number`. The type of the parameter is `int`. This means that we need to provide one argument when we call `timesTwo`, and that argument must be an integer.

Try using `timesTwo` in your `act` method. Here are some ideas:

``` /** * Act - do whatever the Turtle wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { this.forward(this.side); this.turn(7); this.side = twoTimes(this.side); } ``` ``` /** * Act - do whatever the Turtle wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { int x = this.getX(); int y = this.getY(); int z = this.timesTwo(y); int r = this.getRotation(); this.turn(x*z); this.forward(4); this.turn(r-x); this.forward(4); } ```

See what designs you can make using `twiceTwo`.

We saw that you can use this.getX() like an integer. Can you use it as a variable? In other words, can you compile a statement like this: `this.getX() = 7;`. Try adding that line to your `act` method. What happens?

### Hacker Challenge

Write a method called `drawRectangle` that uses `twiceTwo` to draw a rectangle whose length is twice its width.

### Bonus Video

This video shows you how to add color to your turtle drawings.

Try coloring some other designs with `setColor` using numbers you get from `getX`, `getY`, and `getRotation`.

The instance variables `this.side` and `this.angle` are `int` values. Try passing them as parameters to `setColor`.

Can you use `twiceTwo` to create new values to pass to `setColor`?

## Special Review Quizlet

Here's a quizlet set with all the quizlet items we've covered so far. See what you remember.