cs106a – Lecture #2: Programming & Control Structures

Prerequisites to the second lecture is reading the chapters 1 to 3 of “Karel the Robot learns Java”.

Karel knows four basic commands (or methods):

move()
turnLeft()
pickBeeper()
putBeeper()

To run feed Karel commands, first import Karels basic commands, create a class derived from Karel and put the commands into a run() method:

import stanford.karel.*;
public class BeeperPickingKarel extends Karel {
   public void run() {
      move();
      pickBeeper();
      move();
   } 
}

Define new functions to simplify your code. E.g. instead of turning right by turning left three times, you can create a new function to encapsulate the turning process:

/**
 * before
 */
public void run() {
   turnLeft();
   turnLeft();
   turnLeft();
} 

/**
 * after
 */
public void run() {
   turnRight();
}

private void turnRight() {
   turnLeft();
   turnLeft();
   turnLeft();
} 

By defining the turnRight() method as private you can hide the information about that method and only the class itself may use it.

If all your future Karel versions will want to use this new method, you do not need to add it every time again and again. You can create a subclass of Karel, e.g. the NewImprovedKarel class which holds the additional (now public, because you want to use it in its subclasses) function:

public class NewImprovedKarel extends Karel {
    public void turnRight() {
       turnLeft();
       turnLeft();
       turnLeft();
    }
}

And inherit it by sub classing this new class:

public class BeeperTotingKarel extends NewImprovedKarel { 
   ...
}

Decomposition is another way of making your code more readable by splitting a problem into logical junks, e.g.

move();
fillPothole();
move();

Where

  • Each subproblem should perform a conceptually simple task.
  • Each subproblem should perform a task that is as general as possible, so that it can
    be used in several different situations.

To “solve problems” Karol supports control statements namely conditional statements to bind the execution of statements to a certain condition, and iterative statements to execute statements repeatedly.

Conditional statements test if a certain condition (e.g. frontIsClear()) is given and only if true execute its set of statements:

if (conditional test) {
   // statements to be executed only if the condition is true
}

// or

if (conditional test) {
   // statements to be executed if the condition is true
} else {
   // statements to be executed if the condition is false
}

Iterative statements repeat its set of statements either by a “given number” of times or till or while a certain condition is met:

for (int i = 0; i < count; i++) { 
   // statements to be repeated
}

while (test) { 
   // statements to be repeated
}

Code for this lecture as well as the used worlds are available on github.

FacebooktwitterredditpinterestlinkedintumblrmailFacebooktwitterredditpinterestlinkedintumblrmail

Leave a Reply

Your email address will not be published.