Turtlegraphics for Smartphones and Tablets

Bern University of Teacher Education  
HomeStart Online EditorPrintAndroid GamesJava-Online

Use methods


Complex programs should be fragmented with the aid of methods into smaller, easier part-programs Such part-programs can also be used several times. A method declaration has a methodhead consisting of the name of the method and the type of the return value (void by methods without return value) as well as an instruction block. The method is called by the method's name, folow by a bracket pair.We show this programming technique with a simple example:

 
The method arc() draws a quarter circle


 
In order to draw a leaf, we write a method petal() ("petal"). In this, the method arc() is called twice

In the main program, the method e petal() is called in a for-loop to draw all 8 leaves of the flower.

Edit source with online-compiler

Install App on smartphone or tablet

QR-Code

download sources (Tu10.zip)

// Tu10.java

package app.tu10;

import turtle.*;

public class Tu10 extends Playground
{
  public void main()
  {
    for (int = 0; k < 8; k++)
    {
      petal();
      rt(45);
    }  
  }
  
  void arc()
  {
    for(int = 0; i < 30; i++)
    {
      fd(4);
      rt(3);
    }
  }  
  
  void petal()
  {
    arc();
    rt(90);
    arc();  
  }  
}
 

Explanations to the program code:

void arc() This method is declared with the keyword void, followed by the method's name and an empty bracket pair
arc() The call of the method arc()

 


If the above mentioned program is extended by two lines, you will receive a red, filled flower.

Edit source with online-compiler

Install App on smartphone or tablet

QR-Code

download sources (Tu10a.zip)

// Tu10a.java

package app.tu10a;

import turtle.*;

public class Tu10a extends Playground
{
  public void main()
  {
    for (int = 0; k < 8; k++)
    {
      petal();
      rt(45);
    }  
  }
  
  void arc()
  {
    setPenColor(RED);
    for(int = 0; i < 30; i++)
    {
      fd(4);
      rt(3);
    }
  }  
  
  void petal()
  {
    fillToPoint(getX(), getY());
    arc();
    rt(90);
    arc();  
  }  
}


 

Explanations to the program code:

fillToPoint(getX(), getY()) Fills the figure continuously from the current point (starting point of the flower)

 

Methodes with parameters


Methodes can include paramters that allow the transfer of values to other methods. Like variables, also the parameters have to own a certain type. By using the method square(double s) squares with various side lenghts can be drawn.

Edit source with online-compiler

Install App on smartphone or tablet

QR-Code

download sources (Tu11.zip).

// Tu11.java

package app.tu11;

import turtle.*;

public class Tu11 extends Playground
{
  public void main()
  {
    double = 140;
    while (s > 5)
    {
      square(s);
      lt(180);
      s = * 0.9; 
    }
  }

  void square(double s)
  {
    for (int = 0; i < 4; i++)
    {
      fd(s);
      rt(90);
    }
  }
}
 

Explanatations to the program code:

void square(double s) The parameter s is of the type double
square(s) With the method's call, the parameter has to be indicated