LÖSUNG AUFGABE 3.01
public void act() {
int count = 0;
while (!treeFront()) {
move();
if (onLeaf()) {
count = count + 1;
}
}
System.out.println("Das Resultat ist: " + count);
stop();
}
LÖSUNG AUFGABE 3.02
public class MyKara extends Kara {
boolean goingRight = true;
public void act() {
invertField();
if (treeFront()) {
if (goingRight) {
// Wir sind am rechten Rand.
turnAroundRight();
} else {
// Wir sind am linken Rand.
turnAroundLeft();
}
} else {
move();
}
}
public void turnAroundRight() {
if (treeRight()) {
// Wir sind in der Ecke rechts unten.
stop();
} else {
turnRight();
move();
turnRight();
goingRight = false;
}
}
public void turnAroundLeft() {
if (treeLeft()) {
// Wir sind in der Ecke links unten.
stop();
} else {
turnLeft();
move();
turnLeft();
goingRight = true;
}
}
public void invertField() {
if (onLeaf()) {
removeLeaf();
} else {
putLeaf();
}
}
}
LÖSUNG AUFGABE 3.03
public class MyKara extends Kara {
boolean goingRight = true;
int step = 0;
public void act() {
putLeafIfEvenStep();
if (treeFront()) {
if (goingRight) {
// Wir sind am rechten Rand.
turnAroundRight();
} else {
// Wir sind am linken Rand.
turnAroundLeft();
}
} else {
move();
step = step + 1;
}
}
public void turnAroundRight() {
if (treeRight()) {
// Wir sind in der Ecke rechts unten.
stop();
} else {
turnRight();
move();
turnRight();
goingRight = false;
step = step + 1;
}
}
public void turnAroundLeft() {
if (treeLeft()) {
// Wir sind in der Ecke links unten.
stop();
} else {
turnLeft();
move();
turnLeft();
goingRight = true;
step = step + 1;
}
}
public void putLeafIfEvenStep() {
if (step % 2 == 0) {
// Gerade Schrittnummer --> Lege ein Blatt.
putLeaf();
}
}
}
LÖSUNG AUFGABE 3.04
public class MyKara extends Kara {
int longestRow = 0;
public void act() {
while (!onLeaf()) {
if (treeFront()) {
countRow();
} else {
move();
}
}
System.out.println("Die Längste Baumreihe ist " + longestRow +
" Bäume lang");
stop();
}
public void countRow() {
int currentRow = 0;
turnLeft();
while (treeRight()) {
currentRow = currentRow + 1;
move();
}
// Gehe um die Baumreihe herum.
turnRight();
move();
move();
turnRight();
// Gehe zurück nach unten.
int i = 0;
while (i < currentRow) {
move();
i = i + 1;
}
turnLeft();
// Teste, ob die aktuelle Zeile länger ist.
if (currentRow > longestRow) {
longestRow = currentRow;
}
}
}
LÖSUNG AUFGABE 3.05
Siehe Lösungsszenario unter Downloads.