Download presentation
Presentation is loading. Please wait.
1
SSEA Computer Science: CS106A
Dr. Cynthia Lee Lecturer in Computer Science Stanford
2
Goals for this Course Learn how to harness computing power to solve problems. To that end: Explore fundamental techniques in computer programming Develop good software engineering techniques Gain familiarity with the Java programming language
3
Goals for today Learn how to harness computing power to solve problems. To that end: Look at a very simple model of computation (Karel), and see how we build from these basic building blocks to surprisingly elaborate results. Observe that in doing that building, we need to be organized in our design and encapsulate ideas into layers of units.
4
Meet Karel the Robot
6
Karel's World 3 . . . . . 2 . . . . . 1 . . . . . 1 2 3 4 5
7
Karel's World 3 . . . . . 2 . . . . . 1 . . . . . 1 2 3 4 5
8
Each row is called a street.
Karel's World 3 . . . . . 2 . . . . . 1 . . . . . Each row is called a street. 1 2 3 4 5
9
Karel's World 3 . . . . . 2 . . . . . 1 . . . . . 1 2 3 4 5
10
Each column is called an avenue.
Karel's World 3 . . . . . 2 . . . . . 1 . . . . . 1 2 3 4 5 Each column is called an avenue.
11
Karel's World 3 . . . . . 2 . . . . . 1 . . . . . 1 2 3 4 5
12
The intersection of a street and an avenue is a corner.
Karel's World 3 . . . . . 2 . . . . . 1 . . . . . The intersection of a street and an avenue is a corner. 1 2 3 4 5
13
Karel's World 3 . . . . . 2 . . . . . 1 . . . . . 1 2 3 4 5
14
Karel cannot move through walls.
Karel's World 3 . . . . . 2 . . . . . 1 . . . . . Karel cannot move through walls. 1 2 3 4 5
15
Karel's World 3 . . . . . 2 . . . . . 1 . . . . . 1 2 3 4 5
16
Karel's World 3 . . . . . 2 . . . . . 1 . . . . . 1 2 3 4 5
17
Beepers mark locations in Karel's world.
3 . . . . . 2 . . . . . 1 . . . . . 1 2 3 4 5 Beepers mark locations in Karel's world.
18
Karel's World 3 . . . . . 2 . . . . . 1 . . . . . 1 2 3 4 5
19
. . . . . . . . . . . . . . . Karel's World Karel Commands 3 2 1 1 2 3
4 5
20
. . . . . . . . . . . . . . . Karel's World move Karel Commands 3 2 1
4 5
21
. . . . . . . . . . . . . . . Karel's World move Karel Commands 3 2 1
4 5
22
. . . . . . . . . . . . . . . Karel's World move Karel Commands 3 2 1
4 5
23
. . . . . . . . . . . . . . . Karel's World move Karel Commands 3 2 1
4 5
24
. . . . . . . . . . . . . . . Karel's World move Karel Commands 3 2 1
4 5
25
. . . . . . . . . . . . . . . Karel's World move pickBeeper
Karel Commands 3 . . . . . move pickBeeper 2 . . . . . 1 . . . . . 1 2 3 4 5
26
. . . . . . . . . . . . . . . Karel's World move pickBeeper
Karel Commands 3 . . . . . move pickBeeper 2 . . . . . 1 . . . . . 1 2 3 4 5
27
. . . . . . . . . . . . . . . Karel's World move pickBeeper turnLeft
Karel Commands 3 . . . . . move pickBeeper 2 . . . . . turnLeft 1 . . . . . 1 2 3 4 5
28
. . . . . . . . . . . . . . . Karel's World move pickBeeper turnLeft
Karel Commands 3 . . . . . move pickBeeper 2 . . . . . turnLeft 1 . . . . . 1 2 3 4 5
29
. . . . . . . . . . . . . . . Karel's World move pickBeeper turnLeft
Karel Commands 3 . . . . . move pickBeeper 2 . . . . . turnLeft 1 . . . . . 1 2 3 4 5
30
. . . . . . . . . . . . . . . Karel's World move pickBeeper turnLeft
Karel Commands 3 . . . . . move pickBeeper 2 . . . . . turnLeft putBeeper 1 . . . . . 1 2 3 4 5
31
. . . . . . . . . . . . . . . Karel's World move pickBeeper turnLeft
Karel Commands 3 . . . . . move pickBeeper 2 . . . . . turnLeft putBeeper 1 . . . . . 1 2 3 4 5
32
Wait, why are we learning something with such limited rules?
Weird mix of extreme scarcity and extreme abundance in the way we thinking about resources in computer science.
33
Nairobi, Kenya: Akirachix Mention Telerivet, Medic Mobile
Source:
35
This piece of the program's source code is called a method.
import stanford.karel.*; public class OurKarelProgram extends Karel { public void run() { move(); pickBeeper(); turnLeft(); putBeeper(); } This piece of the program's source code is called a method.
36
This line of code gives the name of the method (here, run)
import stanford.karel.*; public class OurKarelProgram extends Karel { public void run() { move(); pickBeeper(); turnLeft(); putBeeper(); } This line of code gives the name of the method (here, run)
37
import stanford.karel.*;
public class OurKarelProgram extends Karel { public void run() { move(); pickBeeper(); turnLeft(); putBeeper(); } The inside of the method is is called the body of the method and tells Karel how to execute the method.
38
import stanford.karel.*;
public class OurKarelProgram extends Karel { public void run() { move(); pickBeeper(); turnLeft(); putBeeper(); } This part of the program is called a class definition. You'll discuss classes later in CS106A.
39
This is called an import statement. It tells Java what Karel is.
import stanford.karel.*; public class OurKarelProgram extends Karel { public void run() { move(); pickBeeper(); turnLeft(); putBeeper(); } This is called an import statement. It tells Java what Karel is.
40
Improving our Program At this point, do the following:
1. Go back and replace “turnLeft() x3” with a turnRight method. 2. Mention that the name is completely irrelevant by renaming turnRight to something like chewBubbleGum. (Joke about “you could tell a 3-year old that toasters are called republicans and they'd believe you) 2. Introduce SuperKarel, which already knows how to turnRight. 3. Introduce the next problem: Karel Runs Races in a 4x4 world. Solve it by doing runOneStretch() x4, and having runOneStretch be move() x3
41
The for loop
42
for (int i = 0; i < N; i++) { … statements to repeat N times … }
1. Go back and replace runOneSide x4 and move x3 with the appropriate for loops. People are often confused about why the turnLeft() is outside of the for loop – explain this. Also, try to deflect questions about what the parts of the for loop mean – we'll cover that later on. 2. Mention that Karel is totally oblivious to his world – put Karel into a 7 x 7 world and he crashes, or an empty 10 x 10 world and he still runs around. 3. Motivate: Karel needs to learn about his world.
43
The while loop
44
while (condition) { } … statements to repeat when condition holds …
Some of Karel's Conditions: frontIsClear() frontIsBlocked() beepersPresent() beepersInBag() facingNorth() facingSouth() See the Karel reader (Page 18) for more details.
45
while (condition) { } … statements to repeat when condition holds …
Some of Karel's Conditions: frontIsClear() frontIsBlocked() beepersPresent() beepersInBag() facingNorth() facingSouth() See the Karel reader (Page 18) for more details. 1. Mention that we're going to use frontIsClear here. 2. Switch Karel so that he runs until he hits a wall and proceeds from there. 3. Put two “move” statements into the while loop and note that Karel smacks into the wall on a 4 x 4 world. Why? Because the condition is only tested at the bottom, not continuously. 3. Try changing the for loop that says to run four times with a while loop that moves while front is clear. Talk about infinite loops. Joke about “great civilizations will rise and fall and Karel will still run. Archaeologists will say that Karel must have served some religious purpose.” 4. Hurdle Jumping Karel: Motivate the problem and solve it using a combination of while and for loops. 5. Note that Karel seems to “trip” over empty barriers.
47
Karel condition methods
Karel has some commands that are not meant to be complete statements, but rather are used to ask questions: Test Opposite What it checks frontIsClear() frontIsBlocked() Is there a wall in front of Karel? leftIsClear() leftIsBlocked() Is there a wall to Karel’s left? rightIsClear() rightIsBlocked() Is there a wall to Karel’s right? beepersPresent() noBeepersPresent() Are there beepers on this corner? beepersInBag() noBeepersInBag() Any there beepers in Karel’s bag? facingNorth() notFacingNorth() Is Karel facing north? facingEast() notFacingEast() Is Karel facing east? facingSouth() notFacingSouth() Is Karel facing south? facingWest() notFacingWest() Is Karel facing west? This is Table 1 on page 18 of the Karel course reader.
48
The while loop and the Fencepost Bug
49
Exercise: Sweeper Recall we wrote a “RoombaKarel" that picks up all beepers in front of Karel in a straight line. We started with a version with a slight bug… . . . . . ...
50
A tricky bug Our code fails when there is a beeper at the final corner. Changing the order of statements is likely to make it fail when there is a beeper at the first corner. It must work for both. Karel has to take six steps… 1 . . . . . . . 1 2 3 4 5 6 7 …but has to check/sweep seven corners.
51
Fencepost problem loop { place a post. place a post. loop {
fencepost problem: One with repeated statements, where some statements should be repeated n times and some n-1 times. Like creating a fence with 5 posts and 4 wires between the posts. Incorrect: Correct: loop { place a post. place a post. loop { place some wire. place some wire. } place a post. }
52
Fencepost loop public void run() { safePickup(); // post
To solve a fencepost problem, perform the task that needs to happen n times (the "post") once outside the loop. If necessary, invert the steps inside the loop ("wire", then "post"). public void run() { safePickup(); // post while (frontIsClear()) { move(); // wire safePickup(); // post } public void safePickup() { if (beepersPresent()) { pickBeeper();
53
Extra Topics
54
Private methods private method: An advanced concept. Public methods can be called by other classes/programs. Private ones cannot. Not really relevant for our Karel programs, which always have 1 class. The book examples always use private methods. (except run) Your methods on HW1 can be either public or private. (Up to you) public void run() { safePickup(); while (frontIsClear()) { move(); } private void safePickup() { if (beepersPresent()) { pickBeeper();
55
Advanced: Colors import stanford.karel.*;
The SuperKarel class has an additional method paintCorner that sets Karel's current position to be a given color. Valid colors are BLACK, BLUE, CYAN, DARK_GRAY, GRAY, GREEN, LIGHT_GRAY, MAGENTA, ORANGE, PINK, RED, WHITE, YELLOW, and null (no color) import stanford.karel.*; public class RedStripe extends SuperKarel { public void run() { while (frontIsClear()) { paintCorner(RED); move(); }
56
Suggested reading The following code examples in the Karel reader may prove useful: Ch. 4: BeeperCollectingKarel; collects all beepers from towers of arbitrary heights Ch. 5: MazeRunningKarel, finds a beeper in an arbitrary maze by following the "right-hand rule" Ch. 5: DoubleBeepers, doubles the number of beepers in a corner (harder than it sounds!)
Similar presentations
© 2024 SlidePlayer.com Inc.
All rights reserved.