Turtlegraphics for Smartphones and Tablets

Bern University of Teacher Education  
HomeStart Online EditorPrintAndroid GamesJava-Online

Variables


Variables are storage regions, where files that will be used during the program can be saved. Before the first use, the name of the variables and its type need to be declared. The type declaration sets the value sector of the variable (int, double usw.). With the declaration, the variable can be initialised throught the indication of the starting value.

Edit source with online-compiler

Install App on smartphone or tablet

QR-Code

download sources (Tu9.zip).

// Tu9.java

package app.tu9;

import turtle.*;

public class Tu9 extends Playground
{
  void main()
  {
    double = 10;
    while (a < 200)
    {
      fd(a);
      rt(70);
      a = + 2;
    }
  }
}
 

Explanatation to the program code:

double a = 10 Variable a is declared as int and supplied with the starting value 10
a = a  + 2 After the loop cycle, the value of the variables is aumented by 2
fd(a) Turtle-coordinates are actually doubles, but also int can be used


In the following example double-variables are used, since decimal numbers are necessary when calculating the square side.

Edit source with online-compiler

Install App on smartphone or tablet

QR-Code

download sources (Tu9a.zip)

// Tu9a.java

package app.tu9a;

import turtle.*;

public class Tu9a extends Playground
{
  void main()
  {
    double = 200;
    setPos(-50-50);
    while (a > 5)
    {
      for (int = 0; i < 4; i++)
      {
        fd(a);
        rt(90);
      }
      lt(10);
      a = * 0.9;
    }
  }
}
 

Explanatations to the program code:

a = a * 0.9 After each loop cycle the value of the variables is reduced to 90%