RPAD- 9/28

 Measuring programming progress by lines of code is like measuring aircraft building progress by weight.
– Bill Gates

 

Preface

Warm Up

Today’s Work

Once you’ve finished Assignments1 with Karel, install Eclipse on the computer you and your partner havn’t been using. You won’t be loading Assignments1 on this computer, but instead import Assignments1.5. With exception to NewspaperKarel, the end goal here is two write one program that can solve both the original program (from Assignments1) and the newly modified program tasks (in Assignments1.5). However, that’s a bit complicated. So, for right now, lets just focus on how to solve Assignments1.5 using loops, sub task, and conditionals.

We’ll talk about sub tasks, loops, conditionals, and other things that will aid you in solving Assignments1.5.

Programming Resources

Basic Commands
move();
turnLeft();
turnRight();
pickBeeper();
putBeeper();

Loops
for (int i = 0; i < #; i++) {
Type Commands in this Space
}

while (condition) {
Type Commands in this Space
}

Conditionals
if (condition) {
Type Commands in this Space
}

if (condition) {
Commands for Karel if Condition is met
} else {
Commands for Karel if Condition is NOT met
}

Conditions Karel can check on
frontIsClear()
frontIsBlocked()

leftIsClear()
leftIsBlocked()

rightIsClear()
rightIsBlocked()

beepersPresent()
noBeepersPresent()

beepersInBag()
noBeepersInBag()

facingNorth()
notFacingNorth()

facingEast()
notFacingEast()

facingSouth()
notFacingSouth()

facingWest()
notFacingWest()

SubTasks / SubClasses
Using a subclass, is basically like a function from lightbot. How do you create a sub task? It’s basically like writing a miniature program.

 private void Name of SubClass Here() {
Type Commands in this Space
 }

Programming Examples

Below you can see the main program, with a function command in it, followed by the defining of a function. Below is a complete program that would run.

/* File: CollectNewspaperKarel.java
 * ————————————–
* instructions instructions
 */
public class CollectNewspaperKarel extends SuperKarel {
 
 public void run() {
  functOne();
 }
 
 
 private void functOne() {
move();
turnRight();
 }
}

This examples below are not complete programs, just examples of code that could be contained within a program.

example of a while loop

while (frontIsClear()) {
 move();
 }

 

example of a while loop with an embedded/nested if statement

  while (frontIsClear()){
   if (noBeepersPresent()){
    putBeeper();
   }
   move();
  }

example of a for loop

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

example of an if-else statement

  if (frontIsClear()) {
move();
} else {
turnLeft();
}
Advertisements
This entry was posted in RPaD.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s