Turtle Graphics with Java

Bern University of Teacher Education  
HomeStart Online-EditorPrintAndroid-TurtlegraphicsJava-Online

Variables

The use of variables in Java is not simple. Depending on which type of variable different rules need to be followed.


Variables of the basic data type (Simple variables)

Variables of the basic data type are memory in which data used during the execution of a program can be stored. The following data types are used:
int Integers between -231 and 231-1
long Integers between -263 and 263-1
double Decimal number with double precision
boolean Can only have the value of 1 (true) and 0 (false)
char Characters (e.g. a key of the keyboard)

Before using a simple variable, its name, data type and value must be set. This is called declaration and initialization of the variable.

The declaration of a variable assigns the name and the data type and saves the necessary space in the memory.
The initialization assigns a value to the variable.

The declaration and the initialization can done in one step:
int i = 0;
double x = 3.14;
boolean odd = true;

The following rules need to be obeyed when assigning a variable name:


Reference variables

Reference variables are declared with the key word new, e.g.
Turtle john = new Turtle()
This generates a new object as an instance of the class Turtle. The difference between a simple and a reference variable is that the reference variable does not save a certain amount of space in the memory but simply refers to a class as an object. If for example the properties of the Turtle class are changed, the properties of john are changed automatically, too.


Instance variables, local variables and visibility

Variables can be declared in each class and also in each block (defined by curly brackets). By default each variable is only visibly inside the block in which it is defined (local variables). Variables which are defined outside the constructor-block are called instance variables and are visible throughout the whole class. Local variables should be prefered to instance variables.


Examples

In the following example the variable a is used to draw squares with different sizes.

Run this example (Tu12.java)


// Tu12.java
import ch.aplu.turtle.*;

class Tu12
{
  Tu12()
  {
    Turtle joe = new Turtle();

    double a = 180;
    joe.hideTurtle();
    while (> 5)
    {
      for (int i = 0; i < 4; i++)
      {
        joe.forward(a);
        joe.right(90);
      }
    joe.left(10);
    a = a * 0.9;
   }
  }

  public static void main(String[] args)
  {
    new Tu12();
  }
}
 

Test this example in the Online-Editor

The next two example shows the difficulty of the visibility of instance and local variables. The first example, Tu13a, does not compile correctly, because the variable i is declared in the block of the for-loop and so only visible there. It is a local variable and can not be seen by the block after the loop (joe.forward(10 * i)). If i had been declared outside the for-loop, as in the second example Tu13, the program would compile and run without any error.

// Tu13a.java
import ch.aplu.turtle.*;

class Tu13a
{
  Tu13a()
  {
    Turtle joe = new Turtle();

    for (int i = 0; i < 8; i++) //local variable
    {
      joe.forward(10 * i);
      joe.right(90);
    }

    joe.left(180);
    joe.forward(10 * i);  //error
  }

  public static void main(String[] args)
  {
    new Tu13a();
  }
}
 

Run this example (Tu13.java)

// Tu13.java
import ch.aplu.turtle.*;

class Tu13
{
  Tu13()
  {
    Turtle joe = new Turtle();

    int i;                  //instance variable
    for (= 0; i < 8; i++)
    {
      joe.forward(10 * i);
      joe.right(90);
    }

    joe.left(180);
    joe.forward(10 * i);
  }

  public static void main(String[] args)
  {
    new Tu13();
  }
}
 

Test this example in the Online-Editor


In this last example the Turtle joe is not declared as an instance variable, but as a local variable inside the constructor. If the method square(Turtle t) did not receive the Turtle as a parameter it would not compile correctly, because the variable joe is local and so only visible in the constructor. But since the square-method does receive it as a parameter it compiles and runs perfectly.

Run this example (Tu14.java)

// Tu14.java
import ch.aplu.turtle.*;

class Tu14
{
  Tu14()
  {
    Turtle joe = new Turtle(); //local variable

    for (int k = 0; k < 10; k++)
    {
      quadrat(joe);
      joe.left(36);
    }
  }

  void quadrat(Turtle t) //parameter
  {
    t.hideTurtle();
    for (int i = 0; i < 4; i++)
      {
        t.forward(100);
        t.right(90);
      }
  }

  public static void main(String[] args)
  {
    new Tu14();
  }
}
 

Test this example in the Online-Editor

Either the variable joe is declared as an instance variable outside the constructor or the method square() must receive the parameter (Turtle t). All actions inside the square-method are using the parameter t. When calling the square-method with square(joe) inside the constructor the parameter joe will be sent to it.


Exercises