2017年7月18日 星期二

CS106A - Programming Methodology - Lecture 2

methods
syntax
Algorithm
Program

move
turnLeft
pickBeeper
putBeeper




import stanford.karel.*;

public class karelprogramname extends karel {

  public void run () {

      methods ();
        methods ();
        methods ();
        methods ();

    }

    private void turnRight () {

        turnLeft ();
        turnLeft ();
        turnLeft ();

    }

}

"superkarel" with turnAround and turnRight

public class karelprogramname extends superkarel {

}

FOR LOOP do something for a few times

for (int i=0; i<3; i++) {

    turnLeft ();

}

WHILE LOOP do something while the certain condition

while (frontIsClear()) {

    move ();

}

private void moveToWall () {

    while (frontIsClear()) {

        move ();

    }
}

Conditions in Karel
Karel can test the following conditions:

frontIsClear()     frontIsBlocked()
leftIsClear()      leftIsBlocked()
rightIsClear()     rightIsBlocked()
beepersPresent()   noBeepersPresent()
beepersInBag()     noBeepersInBag()
facingNorth()      notFacingNorth()
facingEast()       notFacingEast()
facingSouth()      notFacingSouth()
facingWest()       notFacingWest()

IF STATEMENT only do the thing one time (while loop as a loop would do it multiple times as long as the condition is true)

if (beepersPresent()) {

    pickBeeper ();

} else {

    putBeeper ();

}


沒有留言:

張貼留言