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 a = 180; joe.setPos(-50, -100); while (a > 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 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. |
// Tu10.java import ch.aplu.turtle.*; public class Tu10 { Turtle joe = new Turtle(); public Tu10() { joe.hideTurtle(); double a = 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% |
Using methods without parameters:
// Tu11.java import ch.aplu.turtle.*; public class Tu11 { Turtle t = new Turtle(); public Tu11() { t.speed(-1); for (int k = 0; k < 8; k++) { petal(); t.right(45); } } void arc() { for(int i = 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 t = new Turtle(); public Tu11a() { t.speed(-1); t.setPenColor(Color.red); t.fillToPoint(0, 0); for (int k = 0; k < 8; k++) { petal(); t.right(45); } } void arc() { for(int i = 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. |