Turtlegraphics with Java
HomeAufgabenDruckenJava-Online

Methods

Complex programs can be split into shorter and more simple program parts with the help of methods. In addition to this, methods enable the programmer to reuse certain program sections without retyping the whole code. This program technique can be practices with the help of simple examples.

The first example uses the method square to enable the Turtle to draw a square.

 
// Tu9.java

import ch.aplu.turtle.*;

public class  Tu9
{
  Turtle joe = new Turtle();
  
  public Tu9()
  {
    double = 180;
    joe.setPos(-50-100);
   
    while (a > 5)
    {
      for (int = 0; i < 4; i++)
      {
        joe.forward(a);
        joe.right(90);
      }
    joe.left(10);
    a = * 0.9;
   }
  }

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

Explaining the program code:

void square() A method is defined by the key word void, the method's name and the parameter brackets. If the method does not need any parameter the brackets are left empty.
square() The method square() is called and executed.

 

Methods with parameters

A method can have one or more parameter. In the following example Tu10.java the method square() receives the parameter Color c. This causes the Turtle to draw the 36 squares with different, randomly chosen colors.

 
// Tu10.java

import ch.aplu.turtle.*;

public class  Tu10
{
  Turtle joe = new Turtle();
  
  public Tu10()
  {
    joe.hideTurtle();
    double = 5;
    while (a < 200)
    {
      joe.forward(a);
      joe.right(70);
      a++;
    }
  }

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

In the example Tu10a.java the method square has the parameter side:

 
// Tu10a.java

import ch.aplu.turtle.*;

public class  Tu10a
{
  Turtle joe = new Turtle();

  public Tu10a()
  {
    joe.hideTurtle();
    for (int k = 0; k < 10; k++)
    {
      for (int i = 0; i < 4; i++)
      {
        joe.forward(100);
        joe.right(90);
      }
      joe.left(36);
    }
  }

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

 

Explaining the program code:

int  r  = (int)(Math.random() * 255) returns a random number between 0 and 255
Color  c  = new Color(r, g, b) Color c is defined with three randomly chosen red-, green and blue-components
square(c) This calls and executes the method square with the parameter c
void square(Color  c) The method square with the parameter Color c
double  side  = 180
void square (double  s)
The parameter in the methods brackets does not need the have the same name as the variable
side  =  side  * 0 . 90 The length of the side is decreased by 90%


Further examples with two alternatives:

Even though the use of methods without any parameter seems easier, it can be of great importance when dealing with more complex problems, such as recursions.

Using methods without parameters:

 
// Tu11.java

import ch.aplu.turtle.*;

public class  Tu11
{
  Turtle = new Turtle();

  public Tu11()
  {
    t.speed(-1);
    for (int = 0; k < 8; k++)
    {
      petal();
      t.right(45);
    }
  }

  void arc()
  {
    for(int = 0; i < 30; i++)
    {
      t.forward(4);
      t.right(3);
    }
  }

  void petal()
  {
    arc();
    t.right(90);
    arc();
  }

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

Using methods with parameters:

 
// Tu11a.java

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

public class  Tu11a
{
  Turtle = new Turtle();

  public Tu11a()
  {
    t.speed(-1);
    t.setPenColor(Color.red);
    t.fillToPoint(0, 0);
    for (int = 0; k < 8; k++)
    {
      petal();
      t.right(45);
    }  
  }
  
  void arc()
  {
    for(int = 0; i < 30; i++)
    {
      t.forward(4);
      t.right(3);
    }
  }  
  
  void petal()
  {
    arc();
    t.right(90);
    arc();  
  }  

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

Explaining the program code:
joe.setPos(x, 0) ; Sets the Turtle to the specified coordinates
for (int  x  = - 140 ; x  < 200 ; x  =  x  + 75) The first flower is drawn at the coordinates (-140, 0), the second at (-65, 0), etc.