Kapitel 5: Methoden

Lösungen

LÖSUNG AUFGABE 5.01: Kuchen backen

public class MyKaraIO extends KaraIO {
    
    public void act() {
        drawRectangle(21, 4);
        
        stop();
    }

    public void drawRectangle(int width, int height) {
        int i = 0;
        while (i < height) {
            putLeafs(width);
            turnAround();
            multiMove(width);

            // Zur nächsten Zeile gehen.
            turnRight();
            move();
            turnRight();

            i = i + 1;
        }
    }

    public void putLeafs(int count) {
        int i = 0;
        while (i < count) {
            putLeaf();
            move();
            i = i + 1;
        }
    }

    public void multiMove(int steps) {
        int i = 0;
        while (i < steps) {
            move();
            i = i + 1;
        }
    }

    public void turnAround() {
        turnLeft();
        turnLeft();
    }
}

LÖSUNG AUFGABE 5.02: Kerzen auf Kuchen

public class MyKaraIO extends KaraIO {
    
    public void act() {
        drawRectangle(21, 4);
        drawCandles(10);
        
        stop();
    }

    public void drawCandles(int count) {
        int i = 0;
        while (i < count) {
            move();
            turnLeft();
            putLeafs(3);
            turnAround();
            multiMove(3);
            turnLeft();
            move();
            i = i + 1;
        }
    }

    public void drawRectangle(int width, int height) {
        // ...
    }

    public void putLeafs(int count) {
        // ...
    }

    public void multiMove(int steps) {
        // ...
    }

    public void turnAround() {
        // ...
    }
}

LÖSUNG AUFGABE 5.03: Kerzen nach Alter

public class MyKaraIO extends KaraIO {

    public void act() {
        drawRectangle(21, 4);

        int age = intInput("Wie alt ist Deine Grossmutter?");
        drawCandles(age / 10);
        
        stop();
    }

    public void drawCandles(int count) {
        // ...
    }

    public void drawRectangle(int width, int height) {
        // ...
    }

    public void putLeafs(int count) {
        // ...
    }

    public void multiMove(int steps) {
        // ...
    }

    public void turnAround() {
        // ...
    }
}

LÖSUNG AUFGABE 5.04: Torte

public class MyKaraIO extends KaraIO {

    public void act() {
        int age = intInput("Wie alt ist Deine Grossmutter?");
        drawLayers(age / 10 - 5);
        
        stop();
    }

    public void drawLayers(int layers) {
        int currentWidth = 21;

        int i = 0;
        while (i < layers) {
            drawRectangle(currentWidth, 2);
            move();
            move();

            currentWidth = currentWidth - 4;
            i = i + 1;
        }
    }

    public void drawRectangle(int width, int height) {
        // ...
    }

    public void putLeafs(int count) {
        // ...
    }

    public void multiMove(int steps) {
        // ...
    }

    public void turnAround() {
        // ...
    }
}

Comments