Chapter 2: Program Flow

Solutions

SOLUTION TASK 2.01

TASK 2.01 - Solution


SOLUTION TASK 2.02

TASK 2.02 - Solution


SOLUTION TASK 2.03

  1. Describe with words what the following code does.
    Removes a leaf if there is one, puts a leaf if there is no leaf.

  2. Sketch it as a flowchart.

TASK 2.03 - Solution


SOLUTION TASK 2.04

  1. Describe with words what the following code does.
    If Kara is on a leaf he makes one step forward.
  2. Sketch it as a flowchart.

TASK 2.04 - Solution


SOLUTION TASK 2.05

  1. Describe what happens when you run the program.
    Removes the leaf if there is a tree on the left, otherwise, just makes a move.
  2. Draw a corresponding flowchart.

TASK 2.05 - Solution


SOLUTION TASK 2.06: Around Tree II

public void act() {
    if (treeFront()) {
        goAroundTree();
    } else {
        move();
    }

    if (onLeaf()) {
        removeLeaf();
        stop();
    }
}

public void goAroundTree() {
    turnLeft();
    move();
    turnRight();
    move();
    move();
    turnRight();
    move();
    turnLeft();
}

SOLUTION TASK 2.07: Nested Conditions

public void act() {
    if (treeLeft()) {
        move();
    } else {
        if (onLeaf()) {
            removeLeaf();
            move();
        } else {
            move();
        }
    }
}

SOLUTION TASK 2.08: Afraid of Tunnel

public void act() {
    if (treeLeft() && treeRight()) {
        putLeaf();
        stop();
    } else {
        move();
    }
}

SOLUTION TASK 2.09: Leaf at Tree

public void act() {
    if (treeLeft() || treeRight()) {
        putLeaf();
        move();
    } else {
        move();
    }

    if (onLeaf()) {
        stop();
    }
}

SOLUTION TASK 2.10: Put Leaf Track

public void act() {
    if (!onLeaf()) {
        putLeaf();
    }

    if (!treeFront()) {
        move();
    } else {
        stop();
    }
}

SOLUTION TASK 2.11: Round Trip

public void act() {
    if (onLeaf()) {
        removeLeaf();
    } else {
        if (!treeFront()) {
            move();
        } else {
            if (!treeLeft()) {
                turnLeft();
                move();
            } else {
                turnRight();
                move();
            }
        }
    }
}

SOLUTION TASK 2.12: Kara Plays Pacman

public void act() {
    if (!treeFront()) {
        removeLeaf();
        findNextLeaf();
    } else {
        removeLeaf();
        stop();
    }
}

public void findNextLeaf() {
    // look for leaf in front
    move();
    if (!onLeaf()) {
        // no leaf in front, go back and look left
        turnAndGoBack();
        turnRight();
        move();
        if (!onLeaf()) {
            // no leaf left; leaf must be on right side
            turnAndGoBack();
            move();
        }
    }
}

public void turnAndGoBack() {
    turnLeft();
    turnLeft();
    move();
}

SOLUTION TASK 2.13

<tr>
  <td>b.</td>
  <td><pre>while (treeRight()) {

move(); }

<tr>
  <td>c.</td>
  <td><pre>while (treeLeft() || treeRight()) {

move(); }

<tr>
  <td>d.</td>
  <td><pre>if (treeLeft()) {

move(); } while (treeLeft() && treeRight()) { move(); }

<tr>
  <td>e.</td>
  <td><pre>while (!treeFront) {

if (treeLeft()) { move(); } }

# Code Description Steps
a.
while (treeLeft()) {
  move();
}
Move as long as ther is a tree on the left. 4
Move as long as ther is a tree on the right. 0
Move as long there is a tree either on the right or on the left side. 5
First, move if there is a tree on the left side. Then move as long as there is a tree on the right and on the left side.

4
As long as thre is no tree in front of Kara: If there is a tree on the left, make a step.

Warning: never-ending loop

4

SOLUTION TASK 2.14: Around Tree III

public void act() {
    while (!onLeaf()) {
        if (treeFront()) {
            goAroundTree();
        } else {
            move();
        }
    }

    // Found leaf --> eat it
    removeLeaf();

    stop();
}

public void goAroundTree() {
    turnLeft();
    move();
    turnRight();
    move();

    while (treeRight()) {
        move();
    }

    turnRight();
    move();
    turnLeft();
}

SOLUTION TASK 2.15: Climbing Up

public void act() {
    while (treeFront()) {
        oneStepUp();
    }

    stop();
}

public void oneStepUp() {
    turnLeft();
    move();
    turnRight();
    move();
}

SOLUTION TASK 2.16: Kara as Guard

public void act() {
    makeOneStep();
}

public void makeOneStep() {
    if (!treeRight()) {
        // no tree right --> go right
        turnRight();
        move();
    } else {
        // there is a tree right
        if (!treeFront()) {
            // no tree in front --> move
            move();
        } else {
            // trees right and front
            if (!treeLeft()) {
                // no tree left --> go left
                turnLeft();
                move();
            } else {
                // trees right, front and left: dead end
                turnLeft();
                turnLeft();
                move();
            }
        }
    }
}