Start Here

Media Resources

File Conversion

Instance variables and assignment operators

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

Instance variables

The following video explains the drawing of bigger and bigger squares that we saw in the last lesson.

Here's a summary of what you should know about instance variables:

  • The side variable is not a local variable or a parameter. It is a new kind of variable called an instance variable or field.
  • Instance variables belong to an object. They are not local to a method.
  • In its declaration, the instance variable has a visibility modifier like the a method has.
  • Instance variables can be inherited just methods.
  • To use the instance variable, we can precede it with this., e.g. this.side.
  • Method calls are followed by (). Variables are not. That include instance variables.
  • You can pass an instance variable as an argument to a method call, the same way you can with local variables.
  • You can assign values to instance variables the same way you assign values to local variables or parameters.
  • In the Turtle class from turtle-v5, the width variable is used a local variable in drawSquare2() and as a paramter in drawSquare(int width). The side variable is declared as a local variable

Assignment operators

The following video shows you how to use all the assignment operators in Java.

In addition to = and += Java also has these assignment operators: -=, *=, /=, %= for assigning the difference, product, quotient, or remainder of a variable and a value to the variable. Examples:

int x = 10;	// local variable x is now declared and has value 10
x = 3;		// now x has the value 2
x += 7;		// now x has the value 10 again
x -= 6;		// now x has the value 4, since 10 - 6 is 4
x *= 5;		// now x has the value 20, since 4 times 5 is 20
x /= 4;		// now x has the value 5, since 20 divided by 4 is 5
x %= 2;		// now x has the value 1, since 3 divided by 2

Modify your act method to look like this:

    /**
     * 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.poly(this.side, 95);
        this.side += 5;
    }    

Here are some other poly calls to try:

  • this.poly(this.side, 90);
  • this.poly(this.side, 120);
  • this.poly(this.side, 117);

Try calling poly with some other arguments. If you find a design you like, you might want to save it to a PNG file with a name that includes the arguments you used. For example, if you like the design made by this.poly(this.side, 117), you could write the image to a file called poly-side-117.png.

What happens if you try this.poly(1, 1); in the act method?

Hacker Challenges

  • Add an instance variable called angle. Change you act method so that it calls poly but changes the angle of the turtle's turn each time instead of changing the length of the side.
  • Modify the act method so that it draws a square, turns the turtle a little, draws a bigger square, turns the turtle a little, and so on. Use the version of drawSquare that takes a parameter.

Use the quizlet below to check your understanding of instance variables, and the new assignment operators you learned about.