
Control Flow Chris Piech CS106A, Stanford University Piech, CS106A, Stanford University CS106A Piech, CS106A, Stanford University Karel the Robot s * While Karel is in Java, when you program your Karel assignment we ask that you stick to the concepts in the course reader Piech, CS106A, Stanford University First Challenge 3 + + + + 2 + + + + 1 + + + + 1 2 3 4 Piech, CS106A, Stanford University Anatomy of a Program import stanford.karel.*; public class OurKarelProgram extends Karel { public void run() { move(); pickBeeper(); move(); turnLeft(); move(); turnRight(); This is the program's move(); source code putBeeper(); move(); } private void turnRight() { turnLeft(); turnLeft(); turnLeft(); } } Piech, CS106A, Stanford University Anatomy of a Program import stanford.karel.*; public class OurKarelProgram extends Karel { public void run() { move(); pickBeeper(); move(); turnLeft(); move(); This piece of the program's turnRight(); source code is called a method. move(); putBeeper(); move(); } private void turnRight() { turnLeft(); turnLeft(); turnLeft(); } } Piech, CS106A, Stanford University Anatomy of a Program import stanford.karel.*; public class OurKarelProgram extends Karel { public void run() { move(); pickBeeper(); x move(); This line of code gives the turnLeft(); name of the method move(); (here, run) turnRight(); move(); putBeeper(); move(); } private void turnRight() { turnLeft(); turnLeft(); turnLeft(); } } Piech, CS106A, Stanford University Anatomy of a Program import stanford.karel.*; public class OurKarelProgram extends Karel { public void run() { move(); pickBeeper(); move(); This line of code gives the turnLeft(); name of the method move(); (here, turnRight) turnRight(); move(); putBeeper(); move(); } private void turnRight() { turnLeft(); turnLeft(); turnLeft(); } } Piech, CS106A, Stanford University Anatomy of a Program import stanford.karel.*; public class OurKarelProgram extends Karel { public void run() { move(); pickBeeper(); move(); turnLeft(); move(); turnRight(); This is called an import statement. It move(); tells Java what Karel is. putBeeper(); move(); } private void turnRight() { turnLeft(); turnLeft(); turnLeft(); } } Piech, CS106A, Stanford University Anatomy of a Program import stanford.karel.*; public class OurKarelProgram extends Karel { public void run() { move(); pickBeeper(); move(); This is called a turnLeft(); move(); code block turnRight(); move(); putBeeper(); move(); } private void turnRight() { turnLeft(); turnLeft(); turnLeft(); } } Piech, CS106A, Stanford University Anatomy of a Program import stanford.karel.*; public class OurKarelProgram extends Karel { public void run() { move(); pickBeeper(); move(); turnLeft(); move(); turnRight(); move(); putBeeper(); move(); } private void turnRight() { turnLeft(); turnLeft(); turnLeft(); } } Piech, CS106A, Stanford University Anatomy of a Program import stanford.karel.*; public class OurKarelProgram extends Karel { public void run() { move(); pickBeeper(); move(); turnLeft(); move(); turnRight(); move(); putBeeper(); move(); } private void turnRight() { turnLeft(); turnLeft(); turnLeft(); } } Piech, CS106A, Stanford University Anatomy of a Program import stanford.karel.*; public class OurKarelProgram extends Karel { public void run() { move(); pickBeeper(); move(); turnLeft(); move(); turnRight(); move(); putBeeper(); move(); } private void turnRight() { turnLeft(); turnLeft(); turnLeft(); } } Piech, CS106A, Stanford University Anatomy of a Program import stanford.karel.*; public class OurKarelProgram extends Karel { public void run() { move(); pickBeeper(); move(); turnLeft(); move(); turnRight(); move(); putBeeper(); move(); } private void turnRight() { turnLeft(); turnLeft(); turnLeft(); } } Piech, CS106A, Stanford University Anatomy of a Program import stanford.karel.*; public class OurKarelProgram extends Karel { public void run() { move(); pickBeeper(); move(); turnLeft(); move(); turnRight(); The run method is “public” so that move(); Eclipse can call it. putBeeper(); move(); } private void turnRight() { turnLeft(); turnLeft(); turnLeft(); } } Piech, CS106A, Stanford University Anatomy of a Program import stanford.karel.*; public class OurKarelProgram extends Karel { public void run() { move(); pickBeeper(); move(); turnLeft(); move(); turnRight(); The turnRight method is “private” to move(); indicate it is only visible to our current putBeeper(); program. move(); } private void turnRight() { turnLeft(); turnLeft(); turnLeft(); } } Piech, CS106A, Stanford University Method Definition This adds a new command to Karels vocabulary private void name() { statements in the method body } Piech, CS106A, Stanford University Decomposition of Hard Tasks Piech, CS106A, Stanford University Today’s Goal 1. Code using loops and conditions 2. Trace programs that use loops and conditions Piech, CS106A, Stanford University Today’s Route You are here Control Flow While For Loops Loops Piech, CS106A, Stanford University For loops, While loops, If/Else statements Piech, CS106A, Stanford University Place 99 beepers? 99 Piech, CS106A, Stanford University Place 99 beepers public class Place99Beepers extends SuperKarel { public void run() { move(); for(int i = 0; i < 99; i++) { putBeeper(); } move(); } } This “for loop” repeats the code in its “body” 99 times Piech, CS106A, Stanford University Place Beeper Square public class BeeperSquare extends SuperKarel { public void run() { for(int i = 0; i < 4; i++) { putBeeper(); move(); turnLeft(); } } } Piech, CS106A, Stanford University Place Beeper Square public class BeeperSquare extends SuperKarel { public void run() { for(int i = 0; i < 4; i++) { putBeeper(); move(); turnLeft(); } } } Piech, CS106A, Stanford University Place Beeper Square public class BeeperSquare extends SuperKarel { public void run() { for(int i = 0; i < 4; i++) { putBeeper(); move(); turnLeft(); } } } Piech, CS106A, Stanford University Place Beeper Square public class BeeperSquare extends SuperKarel { public void run() { for(int i = 0; i < 4; i++) { putBeeper(); move(); turnLeft(); } } } Piech, CS106A, Stanford University Place Beeper Square public class BeeperSquare extends SuperKarel { public void run() { for(int i = 0; i < 4; i++) { putBeeper(); move(); turnLeft(); } } } Piech, CS106A, Stanford University Place Beeper Square public class BeeperSquare extends SuperKarel { public void run() { for(int i = 0; i < 4; i++) { putBeeper(); move(); turnLeft(); } } } Piech, CS106A, Stanford University Place Beeper Square public class BeeperSquare extends SuperKarel { public void run() { for(int i = 0; i < 4; i++) { putBeeper(); move(); turnLeft(); } } } Piech, CS106A, Stanford University Place Beeper Square public class BeeperSquare extends SuperKarel { public void run() { for(int i = 0; i < 4; i++) { putBeeper(); move(); turnLeft(); } } } Piech, CS106A, Stanford University Place Beeper Square public class BeeperSquare extends SuperKarel { public void run() { for(int i = 0; i < 4; i++) { putBeeper(); move(); turnLeft(); } } } Piech, CS106A, Stanford University Place Beeper Square public class BeeperSquare extends SuperKarel { public void run() { for(int i = 0; i < 4; i++) { putBeeper(); move(); turnLeft(); } } } Piech, CS106A, Stanford University Place Beeper Square public class BeeperSquare extends SuperKarel { public void run() { for(int i = 0; i < 4; i++) { putBeeper(); move(); turnLeft(); } } } Piech, CS106A, Stanford University Place Beeper Square public class BeeperSquare extends SuperKarel { public void run() { for(int i = 0; i < 4; i++) { putBeeper(); move(); turnLeft(); } } } Piech, CS106A, Stanford University Place Beeper Square public class BeeperSquare extends SuperKarel { public void run() { for(int i = 0; i < 4; i++) { putBeeper(); move(); turnLeft(); } } } Piech, CS106A, Stanford University Place Beeper Square public class BeeperSquare extends SuperKarel { public void run() { for(int i = 0; i < 4; i++) { putBeeper(); move(); turnLeft(); } } } Piech, CS106A, Stanford University Place Beeper Square public class BeeperSquare extends SuperKarel { public void run() { for(int i = 0; i < 4; i++) { putBeeper(); move(); turnLeft(); } } } Piech, CS106A, Stanford University Place Beeper Square public class BeeperSquare extends SuperKarel { public void run() { for(int i = 0; i < 4; i++) { putBeeper(); move(); turnLeft(); } } } Piech, CS106A, Stanford University Place Beeper Square public class BeeperSquare extends SuperKarel { public void run() { for(int i = 0; i < 4; i++) { putBeeper(); move(); turnLeft(); } } } Piech, CS106A, Stanford University Place Beeper Square public class BeeperSquare extends SuperKarel { public void run() { for(int i = 0; i < 4; i++) { putBeeper(); move(); turnLeft(); } } } Piech, CS106A, Stanford University Exciting! Piech, CS106A, Stanford University Aside: Super Karel public class BeeperSquare extends SuperKarel { public void run() { for(int i = 0; i < 4; i++) { putBeeper(); move(); turnLeft(); } } } Piech, CS106A, Stanford University Aside: Super Karel public class BeeperSquare extends SuperKarel { public void run() { // super karel has a few more commands turnRight(); turnAround(); paintCorner(BLUE); putBeeper(); move(); } } Piech, CS106A, Stanford University Next task Piech, CS106A, Stanford University Place Beeper Line Try and solve it! Piech, CS106A, Stanford University Place Beeper Line import stanford.karel.*; public class BeeperLine extends SuperKarel { public void run() { for(int i = 0; i < 4; i++) { putBeeper(); move(); } } } Piech, CS106A, Stanford University Piech, CS106A, Stanford University Place Beeper
Details
-
File Typepdf
-
Upload Time-
-
Content LanguagesEnglish
-
Upload UserAnonymous/Not logged-in
-
File Pages110 Page
-
File Size-