Turtlegraphics with Java
HomeAufgabenDruckenJava-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.
// Tu12.java

import ch.aplu.turtle.*;

public class  Tu12
{
  Turtle = new Turtle();

  public Tu12()
  {
    double = 140;
    while (s > 5)
    {
      square(s);
      t.left(180);
      s = * 0.9; 
    }
  }

  void square(double size)
  {
    for (int = 0; i < 4; i++)
    {
      t.forward(size);
      t.right(90);
    }
  }

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

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.

// Tu13.java

import ch.aplu.turtle.*;
import java.awt.Color;

public class  Tu13
{
  Turtle = new Turtle();

  public Tu13()
  {
    t.hideTurtle();
    t.clear(Color.blue);
    t.setPenColor(Color.yellow);
    for (int = 0; i < 50; i++)
    {
      double = -180 + 360 * Math.random();
      double = -180 + 360 * Math.random();
      star(x, y);
    }
  }

  void star(double x, double y)
  {
    t.setPos(x, y);
    t.fillToPoint(x, y);
    for (int = 0; i < 6; i++)
    {
      t.forward(10);
      t.right(140);
      t.forward(10);
      t.left(80);
    }
  }

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


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.


// Tu14.java

import ch.aplu.turtle.*;
import java.awt.Color;

public class  Tu14
{
  Turtle = new Turtle();

  public Tu14()
  {
    t.speed(-1);
    t.wrap();
    t.fillToPoint(0, 0);
    for (int = 0; i < 200; i++)
    {
      Color = new Color((int)(Math.random() * 255),
                          (int)(Math.random() * 255), 255);
      t.setPenColor(c);
      if (Math.random() < 0.5)
        t.right(90);
      else
        t.left(90);
      t.forward(60 * Math.random());
    }
    t.hideTurtle();
  }

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

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.