1 00:00:13,218 --> 00:00:16,489 This presentation is delivered by the Stanford Center for Professional 2 00:00:16,489 --> 00:00:23,489 Development. 3 00:00:26,039 --> 00:00:27,150 Alrighty, 4 00:00:27,149 --> 00:00:30,459 welcome back to CS106A. 5 00:00:30,460 --> 00:00:31,960 If you're 6 00:00:31,960 --> 00:00:32,649 stuck in the back, 7 00:00:32,649 --> 00:00:34,739 just come on down, have a seat. 8 00:00:34,740 --> 00:00:37,900 Originally, I thought maybe we would have slightly fewer people today than last 9 00:00:37,899 --> 00:00:40,369 time, but that appears not to be the case. 10 00:00:40,369 --> 00:00:42,299 So while we're waiting, 11 00:00:42,299 --> 00:00:44,769 everyone loves babies, so 12 00:00:44,770 --> 00:00:49,510 I decided to put - that's Karel, the Robot, the early days. 13 00:00:49,509 --> 00:00:53,349 No, this actually - my son, and 14 00:00:53,350 --> 00:00:54,750 yeah, I know. 15 00:00:54,750 --> 00:00:57,340 He's a little bit older now but, like, 16 00:00:57,340 --> 00:01:01,249 he's got these little robot pajamas and he runs around all the time. So I'm like, oh, 17 00:01:01,249 --> 00:01:03,510 it's Karel, The Robot, and my wife looks at me like, 18 00:01:03,509 --> 00:01:06,700 it's your son. But 19 00:01:06,700 --> 00:01:09,990 that's a whole different issue. Anyway, a few administrative announcements before we 20 00:01:09,989 --> 00:01:10,599 start. 21 00:01:10,599 --> 00:01:14,369 A couple things, there are four more handouts today, just because we like consistency. 22 00:01:14,370 --> 00:01:18,359 Four last time, there's four this time. They're all in the back. If you didn't already pick them up, 23 00:01:18,359 --> 00:01:21,900 you can pick them up after class, but they have all the information about 24 00:01:21,900 --> 00:01:24,910 downloading Eclipse, which is the environment that you're going to use for 25 00:01:24,909 --> 00:01:28,149 programming in this class, both for Karel and for Java. There 26 00:01:28,150 --> 00:01:33,520 is also a special handout in there just on using Karel, so it talks about how Karel 27 00:01:33,519 --> 00:01:36,560 works in the Eclipse environment, and so you can get all set up with that. 28 00:01:36,560 --> 00:01:39,980 And so after today, you'll know how Karel works and you'll have the environment to 29 00:01:39,980 --> 00:01:43,228 use it. So surprisingly enough, you also get your first assignment today, 30 00:01:43,228 --> 00:01:44,840 which is actually in two parts. So 31 00:01:44,840 --> 00:01:48,509 the assignment, the real assignment is the programming part, 32 00:01:48,509 --> 00:01:49,578 which is due 33 00:01:49,578 --> 00:01:52,479 on Friday of next week, October 5th. 34 00:01:52,480 --> 00:01:56,560 There is also an email part to it, where the email part is, we ask all of you kind 35 00:01:56,560 --> 00:01:59,478 folks to send an email to Ben, the head TA, 36 00:01:59,478 --> 00:02:01,650 myself, and also your section leader. 37 00:02:01,650 --> 00:02:04,830 So Ben and I get a whole bunch of mail, your section leader will hopefully get a little bit 38 00:02:04,829 --> 00:02:08,240 less mail. But you won't actually know your section leader until after you go 39 00:02:08,240 --> 00:02:10,310 to your first section next week, 40 00:02:10,310 --> 00:02:13,848 which is why that part of the assignment is not actually due until basically one 41 00:02:13,848 --> 00:02:17,009 minute before midnight on Sunday. So I didn't make it midnight, just because then there's 42 00:02:17,009 --> 00:02:18,268 always confusion, 43 00:02:18,269 --> 00:02:19,459 midnight, which night? 44 00:02:19,459 --> 00:02:23,118 So 11:59 p.m. on Sunday, October 7th is when you should send 45 00:02:23,118 --> 00:02:25,740 that email. That's not really the critical part of the assignment. 46 00:02:25,740 --> 00:02:28,740 The critical part of the assignment is the programming problems, which are actually 47 00:02:28,740 --> 00:02:32,058 all due in class on Friday. Okay, 48 00:02:32,058 --> 00:02:35,228 and then there's one last assignment, or one last handout, which is about how you actually 49 00:02:35,229 --> 00:02:36,449 submit your work in this class. 50 00:02:36,449 --> 00:02:39,319 You'll submit your work both electronically and in hard copy. The 51 00:02:39,318 --> 00:02:42,609 electronic copy, so that your section leader can run it and verify it. 52 00:02:42,610 --> 00:02:45,840 The hard copy is so that they can actually write comments on it, and then you'll do 53 00:02:45,840 --> 00:02:47,549 interactive grading with them as well. A 54 00:02:47,549 --> 00:02:50,389 couple other quick announcements. The website, in case you weren't here on 55 00:02:50,389 --> 00:02:53,528 Monday and you don't know what handouts I'm talking about, and you don't know 56 00:02:53,528 --> 00:02:54,598 where to get them, 57 00:02:54,598 --> 00:02:58,829 go to the class website, cs106A.stanford.edu. There will be 58 00:02:58,830 --> 00:03:01,810 electronic copies in PDF format of all the handouts there, so you can get caught 59 00:03:01,810 --> 00:03:03,618 up if today is your first day. 60 00:03:03,618 --> 00:03:08,408 You also need to sign up for a section. As we mentioned last time, section signups start 61 00:03:08,408 --> 00:03:12,688 tomorrow, 5:00 p.m. Sign up early if you want to have the most flexibility in 62 00:03:12,688 --> 00:03:13,889 terms of time, because 63 00:03:13,889 --> 00:03:16,189 look around, there's a whole bunch of people in this class. 64 00:03:16,189 --> 00:03:19,568 It's going to fill up quickly. So if you have constraints, sign up quickly, 65 00:03:19,568 --> 00:03:23,238 and the place you sign up is 66 00:03:23,239 --> 00:03:28,019 CS 198, not CS 106A, but CS198.stanford.edu/section. The 198 67 00:03:28,019 --> 00:03:32,838 folks are the folks who lovingly run all of the sections and coordinate the whole program. Last, but not 68 00:03:32,838 --> 00:03:35,649 least, just a word on the readings that I didn't mention last time. 69 00:03:35,649 --> 00:03:38,778 On the syllabus, you'll notice that pretty much for every day, or most days in 70 00:03:38,778 --> 00:03:39,508 the quarter, 71 00:03:39,508 --> 00:03:41,439 there is some sort of reading associated with it. 72 00:03:41,439 --> 00:03:44,650 That's the reading assignment that you should have done by that day's lecture, because that's what we will cover in that day's lecture. So for today 73 00:03:44,650 --> 00:03:47,819 it 74 00:03:47,818 --> 00:03:51,489 should be the first three chapters of the Karel book, or the course 75 00:03:51,490 --> 00:03:54,590 reader. And if you're like, oh my God, I'm already three chapters behind, don't worry, it's like 76 00:03:54,590 --> 00:03:57,598 20 pages or something. It's pretty lightweight, 77 00:03:57,598 --> 00:04:00,679 but you should do the readings by that time in class. So any questions about anything 78 00:04:00,679 --> 00:04:03,699 we covered last time, which is mostly logistics, or any 79 00:04:03,699 --> 00:04:07,608 of the sort of administrivia? 80 00:04:07,609 --> 00:04:08,319 All righty, 81 00:04:08,318 --> 00:04:10,759 then let's get started on the real content. So 82 00:04:10,759 --> 00:04:11,769 you remember from 83 00:04:11,770 --> 00:04:15,620 the time before, we talked a little bit about Karel, the Robot, and here is Karel in that 84 00:04:15,620 --> 00:04:17,989 world that I showed you before. And there were 85 00:04:17,988 --> 00:04:21,858 avenues that run north-south, and streets that run east-west, and little beepers in 86 00:04:21,858 --> 00:04:25,769 the world. And Karen can face different directions, and there's walls. And so now, we 87 00:04:25,769 --> 00:04:29,228 want to think about how do we actually program Karel. How do we get this little guy 88 00:04:29,228 --> 00:04:32,019 to do something interesting in the world? Okay, 89 00:04:32,019 --> 00:04:36,588 and it turns out there are four commands that Karel understands, okay, 90 00:04:36,588 --> 00:04:39,168 and those are pretty straightforward. So here they are. You're going to get all 91 00:04:39,168 --> 00:04:42,799 of Karel's vocabulary in, like, one minute. There's a 92 00:04:42,800 --> 00:04:45,199 command called move, and move 93 00:04:45,199 --> 00:04:47,629 basically moves Karel one spot forward 94 00:04:47,629 --> 00:04:51,329 in the direction he's facing. So if he's on corner one, one, and he moves one spot 95 00:04:51,329 --> 00:04:55,039 forward, he's facing east. So he'll move over to two, one, basically. So 96 00:04:55,040 --> 00:04:58,020 one corner forward in the direction he's facing. 97 00:04:58,019 --> 00:05:03,248 Karel also started being a good democrat, knows how to turn left. 98 00:05:03,249 --> 00:05:08,660 So he turns left. This is a lower case T, this is an upper case L. So turning left 99 00:05:08,660 --> 00:05:12,640 changes his direction by 90 degrees in the left hand of whichever way he's 100 00:05:12,639 --> 00:05:14,968 facing. So if he's facing east now, and he turns left, 101 00:05:14,968 --> 00:05:16,360 he will be facing north. 102 00:05:16,360 --> 00:05:19,689 And then he can turn left again and he'll be facing west. Okay, 103 00:05:19,689 --> 00:05:23,239 now, question? [Inaudible]? 104 00:05:23,238 --> 00:05:27,468 There is no space here. These are all one word. Good question, and 105 00:05:27,468 --> 00:05:29,969 I love it when you make it easy. All right, 106 00:05:29,970 --> 00:05:31,460 so besides turning left, 107 00:05:31,459 --> 00:05:34,680 there's also B-person Karel's world, and if he couldn't do 108 00:05:34,680 --> 00:05:37,560 anything with beepers, they wouldn't be interesting. So he can pick up beepers, 109 00:05:37,560 --> 00:05:40,329 which interestingly enough is called pickBeeper, 110 00:05:40,329 --> 00:05:43,949 again lower case P, upper case B, all one word, 111 00:05:43,949 --> 00:05:46,949 and there is putBeeper. 112 00:05:46,949 --> 00:05:50,639 And what these do is pickBeeper, if Karel happens to be on a corner that has 113 00:05:50,639 --> 00:05:54,689 a Beeper on it, he picks up that Beeper, and stuffs it in his Beeper bag, in which case 114 00:05:54,689 --> 00:05:58,419 the count in his Beeper bag goes up by one. Or if it's infinite, it remains infinite, 115 00:05:58,418 --> 00:05:58,848 because 116 00:05:58,848 --> 00:06:02,748 he's got a real big Beeper bag, and sometimes he can infinitely many Beepers in 117 00:06:02,749 --> 00:06:03,660 there. It's just - 118 00:06:03,660 --> 00:06:06,620 we can talk about the infinity of Karel's Beeper bags some other time, but if 119 00:06:06,620 --> 00:06:08,989 you're interested, it's fascinating. And he 120 00:06:08,988 --> 00:06:12,709 can also putBeeper, which means he can go to a corner and be like, "Hey, I'm just feeling 121 00:06:12,709 --> 00:06:13,470 happy. 122 00:06:13,470 --> 00:06:17,130 I'm going to put a Beeper down, say, on that corner right there." Except you can't 123 00:06:17,129 --> 00:06:20,069 throw the beepers, right. Karel's kind of limited. He's got, you know, if you look at him 124 00:06:20,069 --> 00:06:22,029 he's got no arms. He's got legs. 125 00:06:22,029 --> 00:06:24,978 No arms, so basically all he can do is he sort of like drops the little Beeper 126 00:06:24,978 --> 00:06:27,699 where he's at. So this puts it on the corner that he's at. 127 00:06:27,699 --> 00:06:30,980 And these things are what we refer to as 128 00:06:30,980 --> 00:06:32,460 methods. 129 00:06:32,459 --> 00:06:33,438 Okay, 130 00:06:33,439 --> 00:06:36,780 methods are basically some instruction that we can call, 131 00:06:36,779 --> 00:06:40,718 okay, or use, like move, or turn left, or pickBeeper, or putBeeper. 132 00:06:40,718 --> 00:06:43,569 And what we say is Karel responds to this method. 133 00:06:43,569 --> 00:06:47,960 What we're doing is we're invoking, or we're calling a particular method on Karel, 134 00:06:47,959 --> 00:06:51,089 and he takes some action, which is basically what that method specifies to do. Okay, 135 00:06:51,089 --> 00:06:53,248 so 136 00:06:53,249 --> 00:06:55,479 if we kind of think about this program, or if 137 00:06:55,478 --> 00:06:58,568 we kind of think about this world, maybe we want Karel to do something. So here's the 138 00:06:58,569 --> 00:07:01,908 initial configuration of the world. Maybe what we want Karel to do is pick up this 139 00:07:01,908 --> 00:07:02,968 beeper, 140 00:07:02,968 --> 00:07:06,908 drop it off at this corner, and end up at this corner facing to the east. 141 00:07:06,908 --> 00:07:10,110 And you can think about how we might do that with some of the instructions we have. So 142 00:07:10,110 --> 00:07:13,968 what we want the final configuration of the world to look like, and I'll just 143 00:07:13,968 --> 00:07:17,509 put Karel at blinding speed. You can actually control Karel's speed here with this 144 00:07:17,509 --> 00:07:19,059 little slider. 145 00:07:19,059 --> 00:07:22,770 So I'm going to just show you the end configuration by running Karel 146 00:07:22,769 --> 00:07:26,448 so fast you can't even see it. It's just - he's blindingly fast. 147 00:07:26,449 --> 00:07:28,530 He's just that good, okay. 148 00:07:28,529 --> 00:07:31,629 And so that's what we want to get to. Notice he's still facing east, but he's 149 00:07:31,629 --> 00:07:34,308 picked up that beeper that was on that corner at two, one, 150 00:07:34,309 --> 00:07:37,360 and moved it over to the corner at four, two. Okay, 151 00:07:37,360 --> 00:07:39,740 and so how might we do this? Right, 152 00:07:39,740 --> 00:07:43,439 so one thing we could consider is, what was the initial state of the world again? 153 00:07:43,439 --> 00:07:46,610 So I'm just going to run this program again, and here I have a clip set up, which is 154 00:07:46,610 --> 00:07:49,080 your handout number five explains how to get this. 155 00:07:49,079 --> 00:07:52,498 But if I want to run a program, there is these two little running person icons, 156 00:07:52,499 --> 00:07:53,759 or as you might notice, 157 00:07:53,759 --> 00:07:58,399 we even have our own menu in Eclipse, because Stanford's just that much fun, okay. 158 00:07:58,399 --> 00:08:02,369 So under the Stanford menu, we have these two options for run. Import project 159 00:08:02,369 --> 00:08:04,479 is explained in the handout, is what you'll use. 160 00:08:04,478 --> 00:08:07,500 We'll give you some initial stuff for Karel that you'll start with, 161 00:08:07,500 --> 00:08:11,879 and you'll use import project to get it into Eclipse. But once it's there, we can run it, 162 00:08:11,879 --> 00:08:13,080 and so what we're going to do 163 00:08:13,079 --> 00:08:14,240 is say run 164 00:08:14,240 --> 00:08:18,280 this particular Karel program. So it's get 165 00:08:18,279 --> 00:08:21,538 - and we'll go through all this - it's get and go. And this is our first Karel program 166 00:08:21,538 --> 00:08:22,579 that we want to run, 167 00:08:22,579 --> 00:08:24,709 and it's sort of, here's the initial world again, 168 00:08:24,709 --> 00:08:26,049 all big and happy. 169 00:08:26,050 --> 00:08:28,889 So I'm going to shrink this down a little bit, so we can see it over here on 170 00:08:28,889 --> 00:08:29,848 the side, 171 00:08:29,848 --> 00:08:33,668 while we think about what commands Karel would need to execute to kind of get to that 172 00:08:33,668 --> 00:08:35,490 final state we just talked about. 173 00:08:35,490 --> 00:08:36,779 And over here, 174 00:08:36,779 --> 00:08:38,728 happily enough, let me resize a little bit, 175 00:08:38,729 --> 00:08:42,288 is our first file, which is empty right now, that we're going to write our first 176 00:08:42,288 --> 00:08:43,119 Karel program. 177 00:08:43,119 --> 00:08:46,449 So what commands might we consider Karel actually doing from the list you have 178 00:08:46,448 --> 00:08:46,998 here, 179 00:08:46,999 --> 00:08:52,779 to sort of affect what we want to happen in the world? [Inaudible]. 180 00:08:52,779 --> 00:08:55,529 Yeah, so we want to move. Okay, 181 00:08:55,529 --> 00:08:57,949 and then what do we want to do? [Inaudible]. 182 00:08:57,950 --> 00:09:02,110 PickBeeper, then move. 183 00:09:02,110 --> 00:09:05,430 Notice at that point we could have actually done something else. This is part of the art of 184 00:09:05,429 --> 00:09:07,969 programming. There's actually many ways to solve the problem. We're just going to 185 00:09:07,970 --> 00:09:09,019 happen to pick one. 186 00:09:09,019 --> 00:09:13,620 So we've moved. We've picked the beeper. We've moved again, and now what do we do? Go to the left. 187 00:09:13,620 --> 00:09:16,970 Turn left. What happens if we turn left? [Inaudible]. 188 00:09:16,970 --> 00:09:20,500 Right, then what are we going to do? Move. Move. 189 00:09:20,500 --> 00:09:22,870 And now what do we want? [Inaudible]. 190 00:09:22,870 --> 00:09:26,679 Yeah, conceptually what we'd like to do is turn right, right? 191 00:09:26,679 --> 00:09:28,489 At this point, Karel's like, 192 00:09:28,490 --> 00:09:29,629 "No, man. 193 00:09:29,629 --> 00:09:30,689 I'm just, you know, 194 00:09:30,690 --> 00:09:33,820 I'm left wing. What can I do? I turn left. That's what I do." And you kind of think about it, and you 195 00:09:33,820 --> 00:09:34,489 say, 196 00:09:34,489 --> 00:09:38,048 "That's all right, because really the world is just one big spectrum, and if you go 197 00:09:38,048 --> 00:09:40,939 around far enough to one side, you end up on the other." 198 00:09:40,940 --> 00:09:43,399 So if we make you turn left three times, 199 00:09:43,399 --> 00:09:46,330 that's equivalent to essentially turning 90 degrees to the right. 200 00:09:46,330 --> 00:09:48,420 So here, we turn left, 201 00:09:48,419 --> 00:09:49,599 turn left, 202 00:09:49,600 --> 00:09:51,759 and turn left. 203 00:09:51,759 --> 00:09:54,159 All right, now what do we do? Move. 204 00:09:54,159 --> 00:09:57,879 Move. So we sort of go up the step. 205 00:09:57,879 --> 00:10:02,820 PutBeeper and 206 00:10:02,820 --> 00:10:04,160 move to the last spot. 207 00:10:04,159 --> 00:10:07,419 Now, at this point we'd like to think, oh good times, we can just run this and it's 208 00:10:07,419 --> 00:10:10,199 a Karel program, and life is happy. 209 00:10:10,200 --> 00:10:12,610 In fact, this is not a valid Karel 210 00:10:12,610 --> 00:10:14,528 program. What this is, is an algorithm. 211 00:10:14,528 --> 00:10:17,850 This is a recipe for doing something, and we'll talk in a lot more detail about 212 00:10:17,850 --> 00:10:19,399 different algorithms on Friday. 213 00:10:19,399 --> 00:10:22,120 But the way you can think of distinguishing between an algorithm and a 214 00:10:22,120 --> 00:10:22,649 program 215 00:10:22,649 --> 00:10:26,419 is an algorithm is essentially the recipe for doing something. 216 00:10:26,419 --> 00:10:30,689 The program is something that is valid syntactically according to the rules 217 00:10:30,690 --> 00:10:32,650 of the language. And so 218 00:10:32,649 --> 00:10:36,429 Karel actually has some specific rules to its language that we have to apply to 219 00:10:36,429 --> 00:10:39,519 these statements to make them look a little bit different, so they follow the 220 00:10:39,519 --> 00:10:41,119 valid syntax of Karel. 221 00:10:41,119 --> 00:10:43,079 And that's what we're going to do now. We're just going to kind of go through 222 00:10:43,078 --> 00:10:45,120 this step by step, okay. 223 00:10:45,120 --> 00:10:47,610 So one of the things we'd like to be able to do 224 00:10:47,610 --> 00:10:50,539 is, first of all, we want these to be valid commands. Right now, they're not valid 225 00:10:50,539 --> 00:10:54,259 commands. To turn a command into a valid command, or what we would refer to as a 226 00:10:54,259 --> 00:10:55,948 method call in Karel, 227 00:10:55,948 --> 00:10:59,258 after the name of the command we put an open paren and a close 228 00:10:59,259 --> 00:11:00,009 paren, 229 00:11:00,009 --> 00:11:01,789 and then a semicolon. Okay, 230 00:11:01,789 --> 00:11:06,149 so move, open paren, close paren, semicolon is actually the valid move 231 00:11:06,149 --> 00:11:09,338 command, or move method invocation for Karel, 232 00:11:09,339 --> 00:11:11,970 and we do that for all these things. So to save a little bit of time, I'm just 233 00:11:11,970 --> 00:11:13,269 going to copy this 234 00:11:13,269 --> 00:11:16,470 and just go through and paste until the cows come home. 235 00:11:16,470 --> 00:11:17,899 236 00:11:17,899 --> 00:11:18,549 237 00:11:18,549 --> 00:11:20,399 Let's do a little paste there. Oh, it 238 00:11:20,399 --> 00:11:21,149 didn't copy. 239 00:11:21,149 --> 00:11:24,580 All right, let's try that again. We do 240 00:11:24,580 --> 00:11:26,290 a little paste, a little paste. 241 00:11:26,289 --> 00:11:29,889 So we're going to go and turn all of these into valid 242 00:11:29,889 --> 00:11:33,009 Karel commands. Okay, so now you might think, okay they're all valid Karel commands. 243 00:11:33,009 --> 00:11:35,529 Are we ready to actually run this Karel program? 244 00:11:35,529 --> 00:11:39,740 And it turns out no, we're not yet ready to run - this is not yet a valid Karel 245 00:11:39,740 --> 00:11:40,310 program. 246 00:11:40,309 --> 00:11:43,539 What makes it a valid Karel program is we need a few more things. 247 00:11:43,539 --> 00:11:47,500 One of those things is, we need to tell Karel where to start running. 248 00:11:47,500 --> 00:11:49,350 And you look at this and you're like, 249 00:11:49,350 --> 00:11:52,149 what does that mean? Like, doesn't he just start at the top? Does Karel start somewhere in 250 00:11:52,149 --> 00:11:53,009 the middle? 251 00:11:53,009 --> 00:11:56,399 No, what it really means is that we're going to do is encapsulate a set of 252 00:11:56,399 --> 00:11:59,379 instructions inside something that tells Karel, 253 00:11:59,379 --> 00:12:02,639 hey, this is what you're going to execute. And the way we do that is we 254 00:12:02,639 --> 00:12:03,480 create 255 00:12:03,480 --> 00:12:06,800 something called a run method, and the syntax for that 256 00:12:06,799 --> 00:12:09,409 looks a little bit archaic, but basically it says, 257 00:12:09,409 --> 00:12:10,829 public, 258 00:12:10,830 --> 00:12:11,689 void, 259 00:12:11,688 --> 00:12:12,418 run, 260 00:12:12,418 --> 00:12:16,000 open paren, close paren, and then we put a brace. And 261 00:12:16,000 --> 00:12:20,570 that first brace says everything that you will now see until the closing brace 262 00:12:20,570 --> 00:12:23,379 is all part of what we refer to as the body 263 00:12:23,379 --> 00:12:24,958 for this thing called run. 264 00:12:24,958 --> 00:12:28,328 So we come down here to the bottom and we put a closing brace, and everything 265 00:12:28,328 --> 00:12:31,739 between the open brace and the close brace is referred to the body, 266 00:12:31,740 --> 00:12:34,909 or what's actually executed when we do a run. Okay, 267 00:12:34,909 --> 00:12:38,759 so get to know where the brace keys on your keyboard are. They will come in very 268 00:12:38,759 --> 00:12:41,490 hand in programming, because you'll use them all the time. 269 00:12:41,490 --> 00:12:43,649 One thing we also do with this is 270 00:12:43,649 --> 00:12:45,169 we're going to actually 271 00:12:45,169 --> 00:12:46,439 insert some tabs 272 00:12:46,440 --> 00:12:48,660 to make it easier for the human to read 273 00:12:48,659 --> 00:12:50,169 this program. Okay, 274 00:12:50,169 --> 00:12:51,909 so when we put in tabs, 275 00:12:51,909 --> 00:12:54,879 suddenly everything that's inside the body of run 276 00:12:54,879 --> 00:12:57,919 gets tabbed in, and when I look at it visually, it's much more appealing. I can 277 00:12:57,919 --> 00:13:01,419 just tell all the things that are actually inside run. Okay, 278 00:13:01,419 --> 00:13:05,878 so we tab those in there and now we have the body of run is inside the braces. 279 00:13:05,879 --> 00:13:09,269 What we've actually done is created a new method. 280 00:13:09,269 --> 00:13:14,539 Well, run is here, is we defined for Karel a new method called run. 281 00:13:14,539 --> 00:13:18,279 And run is a very special method because that's where Karel begins running. What 282 00:13:18,279 --> 00:13:20,000 he says is when he starts up, 283 00:13:20,000 --> 00:13:23,080 he comes into the world, he's like a brand new baby. He's like, "Hey, I'm here. I'm 284 00:13:23,080 --> 00:13:24,910 in the world. What do I do?" And 285 00:13:24,909 --> 00:13:29,209 he's preprogrammed to go find the method called run 286 00:13:29,210 --> 00:13:32,730 and start executing from the beginning of it. So he does executed in a top down manner, 287 00:13:32,730 --> 00:13:34,810 but he needs to have this run thing. 288 00:13:34,809 --> 00:13:38,989 And so you might see them say, "All right, we're ready to go. Fire up Karel, right. I'm slipping a 289 00:13:38,990 --> 00:13:42,120 shrimp on the barbie." I'm sitting here barbequing while Karel's going and moving the 290 00:13:42,120 --> 00:13:43,169 beeper. It's 291 00:13:43,169 --> 00:13:45,329 like, no, we're not quite there yet. You're 292 00:13:45,330 --> 00:13:48,620 like, oh, what else do we need? Okay, so 293 00:13:48,620 --> 00:13:51,500 one thing that we need is we need to take Karel. 294 00:13:51,500 --> 00:13:55,289 Karel is actually implemented in Java. If you've done some Java programming before, 295 00:13:55,289 --> 00:13:57,559 some of the syntax already looks familiar to you. 296 00:13:57,559 --> 00:13:59,829 If you haven't done any Java before, 297 00:13:59,830 --> 00:14:02,550 it's perfectly fine. You don't need to know anything 298 00:14:02,549 --> 00:14:03,358 about Java. 299 00:14:03,359 --> 00:14:06,778 What you do need to know about Karel is that Karel is defined in what we refer 300 00:14:06,778 --> 00:14:08,629 to as a class. 301 00:14:08,629 --> 00:14:12,299 Karel is sort of a class of robot. There's kind of like the Karel 1000, kind of 302 00:14:12,299 --> 00:14:13,949 comes of the assembly line, 303 00:14:13,950 --> 00:14:17,540 and what we'd like to do is take those Karel 1000s and sort of mold 304 00:14:17,539 --> 00:14:19,230 them into our own version, 305 00:14:19,230 --> 00:14:20,620 which does some 306 00:14:20,620 --> 00:14:22,830 set of instructions that we'd like it to do. 307 00:14:22,830 --> 00:14:24,530 So in order to sort of say, 308 00:14:24,529 --> 00:14:28,529 "I want to create my own version of Karel," what we do is we create a class, and 309 00:14:28,529 --> 00:14:29,899 again we say public, 310 00:14:29,899 --> 00:14:33,438 and we say class now, instead of void. So far, all you need to worry about, the 311 00:14:33,438 --> 00:14:35,908 stuff that's in purple, public void, and public class, 312 00:14:35,908 --> 00:14:39,460 is just kind of the standard syntax of Karel. 313 00:14:39,460 --> 00:14:43,240 And later on, all of this stuff will make sense what it actually means, but right 314 00:14:43,240 --> 00:14:47,399 now just think of it as standard syntax, and just put it there, and life is good. 315 00:14:47,399 --> 00:14:50,340 Public class, we now need to give this Karel 316 00:14:50,340 --> 00:14:51,930 program, in some sense, a name, 317 00:14:51,929 --> 00:14:55,049 and what we're going to name it, for lack of any better name, 318 00:14:55,049 --> 00:14:58,609 is we could call it our Karel program, because we did it together. It's 319 00:14:58,610 --> 00:15:02,409 kind of a collective process. So our Karel program, 320 00:15:02,409 --> 00:15:05,569 and at this point we're not done. We need to actually say - you might have noticed after I type 321 00:15:05,570 --> 00:15:08,510 this why suddenly all this red showed up on the side of the editor. And 322 00:15:08,509 --> 00:15:12,990 it's like, oh, bad times, dogs and cats sleeping together, like what's going to happen? 323 00:15:12,990 --> 00:15:14,330 Well, what happens is 324 00:15:14,330 --> 00:15:18,700 we're creating our Karel program and it says, "Okay, you're creating Karel program," 325 00:15:18,700 --> 00:15:22,129 but it doesn't even know anything about the basic Karel 1000. It 326 00:15:22,129 --> 00:15:24,450 needs to somehow know that this Karel program 327 00:15:24,450 --> 00:15:28,430 is creating a version of the Karel 1000. We'll call it the Karel 328 00:15:28,429 --> 00:15:29,329 1001, 329 00:15:29,330 --> 00:15:34,300 and the way we specify this is we say that our Karel program extends Karel, 330 00:15:34,299 --> 00:15:38,599 which is sort of the basic Karel that actually exists and has these predefined 331 00:15:38,600 --> 00:15:39,310 instructions in it. 332 00:15:39,309 --> 00:15:42,569 So we say public Karel, our Karel program extends 333 00:15:42,570 --> 00:15:43,190 Karel, 334 00:15:43,190 --> 00:15:48,280 and now we need to, again, encompass what our Karel, our particular class Karel is 335 00:15:48,279 --> 00:15:51,879 going to be running. So we put this inside braces and we say, "Yeah, this whole 336 00:15:51,879 --> 00:15:56,220 run thing is part of our Karel program." So we put another brace at the end, 337 00:15:56,220 --> 00:16:00,250 and to make it more readable, we go through one more time. Don't want 338 00:16:00,250 --> 00:16:01,489 to put a tab there, 339 00:16:01,489 --> 00:16:02,899 and tab everything 340 00:16:02,899 --> 00:16:06,399 so that it's nicely readable by a person. Okay, 341 00:16:06,399 --> 00:16:07,689 so all this stuff 342 00:16:07,690 --> 00:16:10,420 now gets tabbed over one more spot, 343 00:16:10,419 --> 00:16:14,399 and even this brace over here gets tabbed one more spot. So now you can see, 344 00:16:14,399 --> 00:16:17,909 everything here is part of the run method, because it's nicely tabbed in. We 345 00:16:17,909 --> 00:16:20,029 can tell that, and all this stuff 346 00:16:20,029 --> 00:16:24,829 here is all part of the body of run, and all of it's encapsulated inside our Karel 347 00:16:24,830 --> 00:16:29,270 program. [Inaudible]? 348 00:16:29,269 --> 00:16:32,370 Why is there a space, you mean like down here?] Yeah. 349 00:16:32,370 --> 00:16:34,929 Just for readability. We don't actually need it. There doesn't need to be a space, 350 00:16:34,929 --> 00:16:38,589 but that's a good question. 351 00:16:38,590 --> 00:16:40,670 Oh, nice bank. I like that. 352 00:16:40,669 --> 00:16:44,069 Feel free to bank. If someone - you're on the rebound and I'll set you up for the 353 00:16:44,070 --> 00:16:47,680 rebound, just because you were the first one to do it. All right, so, 354 00:16:47,679 --> 00:16:51,449 and you might say, "Okay, now we've got our Karel program. We've got our run method. Are we ready for 355 00:16:51,450 --> 00:16:52,690 Karel?" Okay, 356 00:16:52,690 --> 00:16:55,890 and you think we're ready for Karel, and someone's calling, saying, "You're ready 357 00:16:55,889 --> 00:16:58,000 for Karel. Do it." 358 00:16:58,000 --> 00:17:01,179 What I would actually encourage you to do is take a moment and turn off your cell phones, unless 359 00:17:01,179 --> 00:17:04,720 your doctor. I actually would tell this to my students all the time. I had one student who was a 360 00:17:04,720 --> 00:17:06,189 doctor once, and he says, "Actually, 361 00:17:06,189 --> 00:17:08,200 your class is when I'm on call, 362 00:17:08,200 --> 00:17:12,380 and if my phone rings, that means, like, someone's dying. So can I keep it on?" And I 363 00:17:12,380 --> 00:17:13,970 was like, hmm, 364 00:17:13,970 --> 00:17:14,870 yeah, okay. 365 00:17:14,869 --> 00:17:18,509 So he got an exception, but other than that, I'd encourage you to turn your 366 00:17:18,509 --> 00:17:22,828 cell phones, unless you have a really cool ring. Then you can leave it on. All right, 367 00:17:22,828 --> 00:17:25,960 so at this point, you might say, "Oh, we're ready to run it," but 368 00:17:25,960 --> 00:17:29,630 even now, we're not ready to run it. And the reason why we're not ready to run it is, 369 00:17:29,630 --> 00:17:33,390 where does this Karel thing come from to begin with, right? Someone, somewhere 370 00:17:33,390 --> 00:17:34,159 had to say, 371 00:17:34,159 --> 00:17:37,090 "Hey, I'm going to build the original version of Karel that you're now 372 00:17:37,089 --> 00:17:37,789 extending." 373 00:17:37,789 --> 00:17:42,079 And that actually comes from a place called Stanford.Karel. So what we do 374 00:17:42,079 --> 00:17:44,889 is we add a line called import, 375 00:17:44,890 --> 00:17:50,250 Stanford.Karel.*, and we put a little semicolon at the end of it. 376 00:17:50,250 --> 00:17:51,888 And what this tells us is, 377 00:17:51,888 --> 00:17:56,488 hey, go and get everything that Stanford's previously defined about Karel. And when we get 378 00:17:56,489 --> 00:17:59,389 to Java, this'll become much more clear, but for right now, all you can think 379 00:17:59,388 --> 00:18:03,148 of that import's meaning is, go get me all the standard Karel stuff that 380 00:18:03,148 --> 00:18:04,649 Stanford's already done for me. 381 00:18:04,650 --> 00:18:08,600 And that defines this thing called Karel, which now we're extending in our Karel 382 00:18:08,599 --> 00:18:11,009 program. Uh huh? What is 383 00:18:11,009 --> 00:18:14,970 the semicolon for? The semicolons always come at the end of the line and Karel's syntax, what they mean 384 00:18:14,970 --> 00:18:17,460 is this is the end of one statement. 385 00:18:17,460 --> 00:18:20,460 So at the end of the import, for example, that says import the stuff, 386 00:18:20,460 --> 00:18:21,730 that's the end of a statement, 387 00:18:21,730 --> 00:18:28,160 or for move, or pickBeeper, or turn left, that's the end of a statement. So we put a semicolon. Uh huh? Do the stars 388 00:18:28,160 --> 00:18:29,580 mean anything? Do stars mean anything? 389 00:18:29,579 --> 00:18:33,699 Yeah, that star at the end of Stanford.Karel.* is actually important, 390 00:18:33,700 --> 00:18:36,860 and in a couple weeks you'll understand why. For right now, it just means get 391 00:18:36,859 --> 00:18:43,859 everything associated with Stanford.Karel. Uh huh? So then, is the run program [inaudible]? 392 00:18:45,140 --> 00:18:48,929 It's all part of run. So when Karel starts up, and you'll see that in just a second, 393 00:18:48,929 --> 00:18:50,019 we start - 394 00:18:50,019 --> 00:18:53,240 we go to run and we just start executing the statements from run. 395 00:18:53,240 --> 00:18:56,230 So let's actually run this and see what happens, and I'll take a couple more questions in a 396 00:18:56,230 --> 00:18:59,829 second. So we go up to our little run icon here. Actually, before we do that, let me 397 00:18:59,829 --> 00:19:02,369 cancel this, because we already have a running program over here. 398 00:19:02,369 --> 00:19:06,239 So let's quit out of the running program and come here, and say run. 399 00:19:06,240 --> 00:19:09,509 And sometimes if you actually happen to write multiple Karel 400 00:19:09,509 --> 00:19:10,900 programs, and you hit the run, then 401 00:19:10,900 --> 00:19:13,610 this guy sort of looks like he's running with smoke all around him, which means he's 402 00:19:13,609 --> 00:19:14,659 running really fast. 403 00:19:14,660 --> 00:19:17,170 That means run the last program I was running. 404 00:19:17,170 --> 00:19:19,130 The guy without smoke around him means, 405 00:19:19,130 --> 00:19:22,550 run one of the programs that I tell you to run, and it might give you a list if you 406 00:19:22,549 --> 00:19:27,099 have multiple programs. Here we're going to pick first Karel program, okay. 407 00:19:27,099 --> 00:19:30,649 So yes, we'll go ahead and save the resource because we forgot to save our file, 408 00:19:30,650 --> 00:19:34,030 and here is life. And we'll run it slowly, and so we start the program, and 409 00:19:34,029 --> 00:19:37,649 there goes Karel. Woo-hoo! 410 00:19:37,650 --> 00:19:41,230 Congratulations. You are now all computer programs. Okay, because you got Karel to 411 00:19:41,230 --> 00:19:42,860 do what you wanted to do, and you're like, "Oh, that's so 412 00:19:42,859 --> 00:19:44,699 good. Let's do it again." 413 00:19:44,700 --> 00:19:47,710 And so you're like, "Yeah, let me start the program again." 414 00:19:47,710 --> 00:19:50,259 Wah-wah-wah, thanks for playing. 415 00:19:50,259 --> 00:19:51,170 What happened? 416 00:19:51,170 --> 00:19:55,410 It tried to execute the same program again, but the world was in a different 417 00:19:55,410 --> 00:19:59,070 state, right? Karel was facing that wall, and the first thing we said is, "Hey, 418 00:19:59,069 --> 00:20:00,929 Karel, move." And he's like, 419 00:20:00,930 --> 00:20:04,200 "Okay." He's a little happy, scrappy robot. He doesn't know anything about the world, 420 00:20:04,200 --> 00:20:07,590 and he just, wham, into the wall. Thanks for playing. 421 00:20:07,589 --> 00:20:09,899 Dead Karel. All right, 422 00:20:09,900 --> 00:20:14,290 no, because Karel's blocked, okay, and you get this little bug on top of Karel. It's like 423 00:20:14,289 --> 00:20:18,389 Alfred Hitchcock, the birds, he's just being, like, harangued by this little bug. 424 00:20:18,390 --> 00:20:21,270 But what that means is Karel tried to do something 425 00:20:21,269 --> 00:20:24,259 in the world that the world would not allow him to do, and this is what happens 426 00:20:24,259 --> 00:20:27,180 if you try to walk through a wall, or try to pick up a beeper from a corner where it 427 00:20:27,180 --> 00:20:30,279 doesn't exist, or you try to put down a beeper and your beeper bag happens to be 428 00:20:30,279 --> 00:20:30,950 empty, 429 00:20:30,950 --> 00:20:32,379 that's the kind of error you'll get. 430 00:20:32,378 --> 00:20:35,628 So if you want to start this program again, you need to load world - and we 431 00:20:35,628 --> 00:20:37,329 happen to have first Karel program, 432 00:20:37,329 --> 00:20:41,609 you need to reload your world and then you can start it again from that point. Okay, 433 00:20:41,609 --> 00:20:48,609 so any questions about our first Karel program? Uh huh? [Inaudible]? 434 00:20:49,640 --> 00:20:52,840 Yeah, he's moving basically from one grid point to the next grind point. 435 00:20:52,839 --> 00:20:58,339 So he just moves one step at a time. Uh huh? 436 00:20:58,339 --> 00:21:03,419 Is Karel case sensitive? Karel is case sensitive, yes. Everything you type will be case sensitive. My 437 00:21:03,420 --> 00:21:06,730 throwing is case sensitive as well, and evidently, that was the wrong case. All 438 00:21:06,730 --> 00:21:09,460 right, so something else we'd like to be able to do with Karel, and a few people 439 00:21:09,460 --> 00:21:10,680 already pointed out is, 440 00:21:10,680 --> 00:21:14,060 "Hey, we created this thing called run. That's kind of cool. Can I create other 441 00:21:14,059 --> 00:21:14,690 stuff?" 442 00:21:14,690 --> 00:21:18,140 And as a matter of fact, you can, because that's just how snazzy Karel is. 443 00:21:18,140 --> 00:21:20,538 And one thing, you might look at this and be like, "Ooh, ooh, 444 00:21:20,538 --> 00:21:23,148 ooh, I know what I want to create." What would you want to create if you could 445 00:21:23,148 --> 00:21:25,250 create a new command? 446 00:21:25,250 --> 00:21:31,289 Turn right. Turn right. Right? That's a social, right? Everyone wants to turn right. All right, 447 00:21:31,289 --> 00:21:34,000 except the person who actually said, "Turn right." So 448 00:21:34,000 --> 00:21:38,259 what we can do is we can have a similar syntax that allows us to create new 449 00:21:38,259 --> 00:21:40,859 methods in Karel, like the run method 450 00:21:40,859 --> 00:21:43,428 actually is the place where Karel will always start. 451 00:21:43,429 --> 00:21:46,690 But we can create new methods that we could use, and so maybe we want to create a 452 00:21:46,690 --> 00:21:49,460 turn right. So we could say void, sorry, 453 00:21:49,460 --> 00:21:52,329 here we'd say 454 00:21:52,329 --> 00:21:53,699 private void. 455 00:21:53,700 --> 00:21:56,900 Voice - sometimes typing is bad, especially when your hands are a little 456 00:21:56,900 --> 00:22:01,130 chalky. So run - you'll notice run is always public void run. 457 00:22:01,130 --> 00:22:03,830 For all the other commands that we're going to do as far as Karel's concerned, we're 458 00:22:03,829 --> 00:22:07,579 going to use private void, and the name of the command. So we'll call this turn 459 00:22:07,579 --> 00:22:09,049 right, 460 00:22:09,049 --> 00:22:10,999 and this command will have a body, 461 00:22:10,999 --> 00:22:14,610 and what is the body of the turn right command going to be? Turn left. 462 00:22:14,609 --> 00:22:17,439 Turn left three times. So I'm just going to copy from here 463 00:22:17,440 --> 00:22:21,880 and paste into - and the indenting even works out right. Rock on, right? 464 00:22:21,880 --> 00:22:25,460 And what I've now done - I'll go ahead and save this - is I've created a new command 465 00:22:25,460 --> 00:22:27,470 for Karel called turn right. 466 00:22:27,470 --> 00:22:31,430 So anywhere else in my program, I can now say, "Turn right," like here are these three 467 00:22:31,430 --> 00:22:32,009 turn lefts. 468 00:22:32,009 --> 00:22:34,379 I can replace them. 469 00:22:34,380 --> 00:22:35,820 Let me just get rid of them, 470 00:22:35,819 --> 00:22:37,139 and I'll replace them 471 00:22:37,140 --> 00:22:38,659 with a turn right, 472 00:22:38,659 --> 00:22:40,429 and I'll appropriately indent that. 473 00:22:40,429 --> 00:22:44,259 And what that means is, as you're executing the program, you move, you pickBeeper, you move, you 474 00:22:44,259 --> 00:22:45,308 turn left, you move. 475 00:22:45,308 --> 00:22:46,879 When you get to turn right, 476 00:22:46,880 --> 00:22:49,220 it says, "Let me pause 477 00:22:49,220 --> 00:22:51,089 where I saw that turn right. 478 00:22:51,089 --> 00:22:56,119 I will go to the turn right method body and run all the instructions that are 479 00:22:56,119 --> 00:22:59,969 in that body, and then I will return back to where I left off. 480 00:22:59,970 --> 00:23:04,250 So this program is exactly equivalent to the program we wrote before, now in terms 481 00:23:04,250 --> 00:23:09,460 of something called turn right, and just to prove that to you, we'll go ahead and run, 482 00:23:09,460 --> 00:23:10,960 and there it is, 483 00:23:10,960 --> 00:23:13,340 Karel does the equivalent of turn right and life is good. Okay, so 484 00:23:13,339 --> 00:23:18,539 we've created a new command called turn right. Question? [Inaudible]? 485 00:23:18,539 --> 00:23:20,259 The location's not important. 486 00:23:20,259 --> 00:23:23,240 You can put it anywhere. I like to put it at the end, because that's convention in the 487 00:23:23,240 --> 00:23:27,930 book, but you can put it anywhere. Uh huh? [Inaudible]? 488 00:23:27,930 --> 00:23:31,509 Why is run always public? Because that's where things start, and for Karel to know where it is, 489 00:23:31,509 --> 00:23:34,750 like private's all the stuff you're sort of like, eh, I'm keeping it to myself. 490 00:23:34,750 --> 00:23:38,210 But Karel's like - he's, like, looking around. He's like, "Where do I run? What do I run? And 491 00:23:38,210 --> 00:23:41,519 so you got to say, "Oh, it's public. It's available to the whole world." 492 00:23:41,519 --> 00:23:44,579 And in a couple weeks when we get into Java, you'll see public and private rear their 493 00:23:44,579 --> 00:23:51,579 ugly heads again and it'll all be clear. Uh huh? [Inaudible]? Pardon? 494 00:23:53,579 --> 00:23:56,129 If you made that a public [inaudible]? It would still run. It would be fine. 495 00:23:56,130 --> 00:23:59,450 It's just good programming style. See, there's all these things that we kind of 496 00:23:59,450 --> 00:24:02,460 will eventually get to, but we got to start somewhere. So we sort of start right 497 00:24:02,460 --> 00:24:03,419 in the middle 498 00:24:03,419 --> 00:24:05,890 and we're going to build up from there, and eventually we'll go back and fill in 499 00:24:05,890 --> 00:24:10,549 some of the pieces underneath. Uh huh? Do you have to [inaudible]? 500 00:24:10,548 --> 00:24:14,450 Run is where - has to be called run. That's where Karel always knows where to 501 00:24:14,450 --> 00:24:17,590 start, but everything else could be something else. Like, instead of turn right, I 502 00:24:17,589 --> 00:24:20,109 don't know, what's the French word for turn right, 503 00:24:20,109 --> 00:24:25,129 or the French two words for turn right? Anyone speak French? [Inaudible]. 504 00:24:25,130 --> 00:24:25,640 Exactly. 505 00:24:25,640 --> 00:24:29,880 We could have called it that if we wanted to, and then rather then turn right we would use 506 00:24:29,880 --> 00:24:33,419 that, and it would be fine. So all the other things besides run, we can name 507 00:24:33,419 --> 00:24:36,850 them whatever we want as long as we're consistent throughout. All right, 508 00:24:36,849 --> 00:24:39,799 so we could also - you might say something else might be kind of fun to do is to 509 00:24:39,799 --> 00:24:42,599 have a command called turn around, for example, 510 00:24:42,599 --> 00:24:46,939 which turns Karel around 90 degrees. So 511 00:24:46,940 --> 00:24:49,759 we'll just do a little copy and paste magic 512 00:24:49,759 --> 00:24:53,579 and have turn around here. We might not actually use it in this program, but just to 513 00:24:53,579 --> 00:24:54,148 have, 514 00:24:54,148 --> 00:24:57,648 and turn around is actually just two turn lefts. It turns Karel around, and if you really 515 00:24:57,648 --> 00:25:01,209 wanted to do it, you could do it as two turn rights, but that's kind of six turn lefts, 516 00:25:01,210 --> 00:25:04,539 and that's not really a good idea. Okay, so 517 00:25:04,539 --> 00:25:08,388 because turn right and turn around are such common things to do, 518 00:25:08,388 --> 00:25:11,918 we actually have a souped up version of Karel. It's sort of like Karel with the training 519 00:25:11,919 --> 00:25:13,320 wheels taken off, 520 00:25:13,319 --> 00:25:17,279 and he's bad, and he's buff, and he knows everything Karel knows, all four 521 00:25:17,279 --> 00:25:18,109 methods, 522 00:25:18,109 --> 00:25:21,678 plus two more, which is turn right and turn around built in. 523 00:25:21,679 --> 00:25:25,240 And that's super Karel. So if we get rid of these and we say, "Hey, our Karel program, 524 00:25:25,240 --> 00:25:26,799 instead of extending Karel, 525 00:25:26,799 --> 00:25:29,869 is going to extend the super Karel model." Okay, 526 00:25:29,869 --> 00:25:33,019 oh, I know, it's dangerous, but it's okay. 527 00:25:33,019 --> 00:25:35,190 Super Karel, we've gotten rid of 528 00:25:35,190 --> 00:25:38,850 the turn right definition and the turn around definition, but if we run 529 00:25:38,849 --> 00:25:41,869 this now, 530 00:25:41,869 --> 00:25:43,899 start the program, 531 00:25:43,900 --> 00:25:45,009 Karel 532 00:25:45,009 --> 00:25:51,400 actually will do the right thing. 533 00:25:51,400 --> 00:25:54,750 All right, let me quit out of this, unless my computer 534 00:25:54,750 --> 00:25:59,079 decides to freeze, and sometimes it decides to freeze, and that would be bad. All right, 535 00:25:59,079 --> 00:26:02,710 so at this point you're Karel programmers. You're rocking. You know all the Karel's 536 00:26:02,710 --> 00:26:05,679 methods. That's all he knows, at least for the time being, 537 00:26:05,679 --> 00:26:07,720 and you know how to actually make 538 00:26:07,720 --> 00:26:09,960 new commands by 539 00:26:09,960 --> 00:26:13,600 creating new definitions that have a body and the syntax around them to create new commands. 540 00:26:13,599 --> 00:26:15,009 All right, 541 00:26:15,009 --> 00:26:16,829 so now what we're going to do 542 00:26:16,829 --> 00:26:19,339 is I want to ask you a philosophical question. I 543 00:26:19,339 --> 00:26:22,538 know, I guess you're like, this is a computer science class, why do we have to 544 00:26:22,538 --> 00:26:23,480 do philosophy in 545 00:26:23,480 --> 00:26:24,980 here? Because it's good for you, 546 00:26:24,980 --> 00:26:26,519 and the philosophical question is 547 00:26:26,519 --> 00:26:30,129 what is the downfall of the modern college student? Anyone 548 00:26:30,130 --> 00:26:33,050 know? You're 549 00:26:33,049 --> 00:26:33,878 like, what does that 550 00:26:33,878 --> 00:26:34,579 mean? 551 00:26:34,579 --> 00:26:35,470 You're like, I 552 00:26:35,470 --> 00:26:38,960 haven't fallen down yet. That's good. Hopefully it won't be that way. 553 00:26:38,960 --> 00:26:42,419 But there is something, probably in your everyday life, that's just sitting there 554 00:26:42,419 --> 00:26:45,050 niggling. Uh huh? 555 00:26:45,049 --> 00:26:50,038 Procrastination. Procrastination? It's related to that. What do you do to procrastinate in the morning? Sleep. Sleep. Oh, 556 00:26:50,038 --> 00:26:57,038 sorry, yeah, and how do you prolong that? [Inaudible]. Snooze. 557 00:26:58,019 --> 00:27:01,720 And the thing about snooze is, it's amazing how quickly you can do math in the 558 00:27:01,720 --> 00:27:04,799 morning. I don't know about you, but my alarm has a nine-minute snooze on it. Why it's nine, 559 00:27:04,799 --> 00:27:07,599 I don't know, but it came out of the factory with a nine-minute snooze. And so you're, 560 00:27:07,599 --> 00:27:09,819 like, lying there. I remember when I was a student, like, 561 00:27:09,819 --> 00:27:13,099 the dreaded, like, 10:00 a.m. class, 9:00 a.m. class. Heaven forbid you ever 562 00:27:13,099 --> 00:27:15,319 have a 9:00 a.m. class, 563 00:27:15,319 --> 00:27:18,519 and you're sitting there, and, like, the alarm goes off, and you're like, 564 00:27:18,519 --> 00:27:21,410 yeah, not going to brush my teeth today. Snooze. 565 00:27:21,410 --> 00:27:24,540 And then you think about it a little bit and nine minutes later, it goes off, and you're 566 00:27:24,539 --> 00:27:27,920 like, okay, it must be, like, 8:09 a.m. now. And then the math kicks in. You're 567 00:27:27,920 --> 00:27:31,539 like, if I don't take a shower, that's like two more whacks on the snooze bar, snooze, snooze. 568 00:27:31,539 --> 00:27:34,579 And now you're up to like 8:27 a.m. You're just doing 569 00:27:34,579 --> 00:27:37,759 this all while you're, like, have asleep, right. It's like calculus when you're fully awake, 570 00:27:37,759 --> 00:27:39,509 yeah, impossible. Snooze bar, 571 00:27:39,509 --> 00:27:43,430 you could have, like, your brain removed in a lobotomy and you could still figure it out. 572 00:27:43,430 --> 00:27:46,650 But what the snooze bar actually gives you is a way of doing something a 573 00:27:46,650 --> 00:27:49,480 certain number of times when you want to be able to do it a certain number of 574 00:27:49,480 --> 00:27:52,288 times. And so you might say, "Hey, Karel, 575 00:27:52,288 --> 00:27:54,269 sometimes rather than just, like, 576 00:27:54,269 --> 00:27:57,349 telling you turn, left turn, left turn, left three times, can I just tell 577 00:27:57,349 --> 00:28:00,469 you to turn left three times? Is there some way of doing that?" And in fact, in Karel 578 00:28:00,470 --> 00:28:01,179 there is, and 579 00:28:01,179 --> 00:28:04,030 it's something called a for-loop, and the syntax for 580 00:28:04,029 --> 00:28:05,638 this 581 00:28:05,638 --> 00:28:07,409 looks like this, 582 00:28:07,410 --> 00:28:08,029 for, 583 00:28:08,029 --> 00:28:11,069 just the word for, paren, 584 00:28:11,069 --> 00:28:15,139 int I equals zero. And so this might look a little bit strange, and if you've 585 00:28:15,140 --> 00:28:18,090 done some programming before, it won't look strange, and if you haven't done any programming, 586 00:28:18,089 --> 00:28:21,278 this is just the syntax. Just use it and feel good about it. 587 00:28:21,278 --> 00:28:26,559 This is int I equals zero, semicolon, 588 00:28:26,559 --> 00:28:30,250 I is less than the number you want to count up to. So if I want to do something 589 00:28:30,250 --> 00:28:34,490 three times, I'd have a three here, but in general this could be any number that 590 00:28:34,490 --> 00:28:35,950 you want. 591 00:28:35,950 --> 00:28:41,809 Then another semicolon, and then an I followed by a plus and a plus with no spaces, 592 00:28:41,809 --> 00:28:44,079 and then a close paren. 593 00:28:44,079 --> 00:28:47,349 And then after all that, you have a brace, an 594 00:28:47,349 --> 00:28:51,889 open brace, and there's a close brace down here, and everything that goes here is 595 00:28:51,890 --> 00:28:55,549 the body of what we refer to as the for-loop, 596 00:28:55,548 --> 00:28:59,929 and that body gets executed however many times you say here. 597 00:28:59,929 --> 00:29:03,519 It can be one instruction. It can be more than one instruction. So turn left 598 00:29:03,519 --> 00:29:05,730 could actually be defined 599 00:29:05,730 --> 00:29:07,980 - or turn right could be defined 600 00:29:07,980 --> 00:29:12,230 as for I equals zero, I less than three, I 601 00:29:12,230 --> 00:29:13,149 plus, plus, 602 00:29:13,148 --> 00:29:16,979 turn left. And what this will do is it will execute this three times. I could have 603 00:29:16,980 --> 00:29:20,450 multiple instructions in here if I want to, and it would execute that whole set 604 00:29:20,450 --> 00:29:21,750 sequentially three times, 605 00:29:21,750 --> 00:29:24,679 but this is the equivalent of turn right. Okay, 606 00:29:24,679 --> 00:29:28,019 question? [Inaudible]? 607 00:29:28,019 --> 00:29:30,049 So the spaces are, 608 00:29:30,049 --> 00:29:30,569 sorry, 609 00:29:30,569 --> 00:29:32,579 there is a space here. 610 00:29:32,579 --> 00:29:34,829 Well, you don't actually need a space there, 611 00:29:34,829 --> 00:29:38,449 but there are no spaces in here. So you need to have this space for sure. 612 00:29:38,450 --> 00:29:41,509 The rest of these spaces are basically optional, but we like to kind of space 613 00:29:41,509 --> 00:29:43,759 them out for it to make sense. 614 00:29:43,759 --> 00:29:47,538 You definitely should not have any spaces in the I plus, plus. There's no 615 00:29:47,538 --> 00:29:51,220 spaces in there. Uh huh? Is all of this syntax in the course reader? 616 00:29:51,220 --> 00:29:54,190 It's all in the course reader, yeah. Yeah, 617 00:29:54,190 --> 00:29:56,330 so you don't need to worry about - if you want to take notes, 618 00:29:56,329 --> 00:29:59,549 that's great, but it's all kind of in the course reader if you want to follow along. So 619 00:29:59,549 --> 00:30:02,909 you can do something any number of times that you want, right, and it'll just count up. So 620 00:30:02,910 --> 00:30:04,019 this could be, like, 621 00:30:04,019 --> 00:30:05,099 if you wanted to, 622 00:30:05,099 --> 00:30:08,740 you could say, "Hey, turn left six times," and that's still the equivalent of turn right. Or 623 00:30:08,740 --> 00:30:14,880 we can do the equivalent of snooze, which is that. Right, 624 00:30:14,880 --> 00:30:17,179 so basically what does snooze do? 625 00:30:17,179 --> 00:30:19,960 Nothing. You just sit there, and you spin around a few times, and you're kind of like 626 00:30:19,960 --> 00:30:24,140 in the same spot, facing the same direction when you were done. Okay, but 627 00:30:24,140 --> 00:30:27,369 anytime you want to do something some number of times, you can actually do it 628 00:30:27,368 --> 00:30:30,608 using this for construct. And this is the way you should remember it. You're always just going 629 00:30:30,608 --> 00:30:33,058 to use this syntax in 630 00:30:33,058 --> 00:30:36,308 Karel, and the only thing that's going to change is this number of times that you 631 00:30:36,308 --> 00:30:37,410 want to do something. 632 00:30:37,410 --> 00:30:38,690 All right, 633 00:30:38,690 --> 00:30:40,039 so besides that 634 00:30:40,039 --> 00:30:43,750 there's also sometimes, though, where you don't know how many times beforehand 635 00:30:43,750 --> 00:30:46,890 you want to do something. And you might say, "Huh, that's kind of weird. When would I not 636 00:30:46,890 --> 00:30:49,120 know how many times I want to do something?" 637 00:30:49,119 --> 00:30:53,009 Well, let's say I'm Karel, and I'm standing here, and there's a wall over there. 638 00:30:53,009 --> 00:30:56,420 And I know there's a wall somewhere over there, because my world is finite. It has to 639 00:30:56,420 --> 00:31:00,470 end at some point, but one told me how many steps it is to that wall. So if I say, 640 00:31:00,470 --> 00:31:04,829 "Hey, I'm going to take ten steps to that wall using a for-loop." One, two, three, 641 00:31:04,829 --> 00:31:06,349 four, five, six, 642 00:31:06,349 --> 00:31:10,909 and then I'm like, oh, dead Karel, right, and that's a bad time. So 643 00:31:10,910 --> 00:31:14,090 sometimes, we just don't know when the program starts, and what we want to say is, 644 00:31:14,089 --> 00:31:15,809 "Do something 645 00:31:15,809 --> 00:31:19,359 while some condition is true," like, keep going forward as long as there's no wall 646 00:31:19,359 --> 00:31:23,459 in front of you. And when there's a wall in front of you, stop going forward. All right, 647 00:31:23,460 --> 00:31:24,929 so the way we do that 648 00:31:24,929 --> 00:31:28,710 is something called a while-loop. So 649 00:31:28,710 --> 00:31:32,058 at this point, hopefully you're intimately familiar with all of Karel's 650 00:31:32,058 --> 00:31:32,930 instructions. 651 00:31:32,930 --> 00:31:35,590 And the way the while-loop works, 652 00:31:35,589 --> 00:31:38,449 while, because that's what you want to do something. You want to do something while 653 00:31:38,450 --> 00:31:40,620 there is some condition that's true, 654 00:31:40,619 --> 00:31:43,668 is the syntax looks like this, while, 655 00:31:43,669 --> 00:31:45,038 and then a paren, 656 00:31:45,038 --> 00:31:48,849 and inside here we're going to have some condition. 657 00:31:48,849 --> 00:31:51,539 Okay, so I'm going to put a squiggly line under it. So you don't put the word condition. We're 658 00:31:51,539 --> 00:31:53,319 going to put a condition in there in just a second, 659 00:31:53,319 --> 00:31:54,599 a close paren, 660 00:31:54,599 --> 00:31:56,019 and then we're going to have 661 00:31:56,019 --> 00:31:59,440 some body inside braces, open brace, close brace. 662 00:31:59,440 --> 00:32:01,610 Now, where do these conditions come from? 663 00:32:01,609 --> 00:32:05,439 So one thing we can think about is the notion of moving until we get to a wall. 664 00:32:05,440 --> 00:32:09,269 And so Karel, it turns out, has some things that he can sense about his 665 00:32:09,269 --> 00:32:12,139 world, which are conditions you can check to see if there's something going on in 666 00:32:12,140 --> 00:32:13,009 this world. 667 00:32:13,009 --> 00:32:15,369 One of the things you can check is 668 00:32:15,369 --> 00:32:17,629 front is 669 00:32:17,630 --> 00:32:19,540 clear, 670 00:32:19,539 --> 00:32:22,700 and that's an open paren and a close paren, and so 671 00:32:22,700 --> 00:32:24,528 there's actually two parens. 672 00:32:24,528 --> 00:32:27,869 Lower case F, capital I, capital C, all one word. 673 00:32:27,869 --> 00:32:30,558 And so what that asks Karel to do is say, 674 00:32:30,558 --> 00:32:34,109 "Where you're standing right now is your front clear," which means is there a wall 675 00:32:34,109 --> 00:32:37,089 right in front of the direction you're facing. If 676 00:32:37,089 --> 00:32:38,869 your front is clear, 677 00:32:38,869 --> 00:32:42,119 so while your front is clear, what it does, it basically checks if the front is clear, 678 00:32:42,119 --> 00:32:44,769 and that's true, then it executes the body of 679 00:32:44,769 --> 00:32:45,690 the while loop. 680 00:32:45,690 --> 00:32:48,538 So we could say, for example, move 681 00:32:48,538 --> 00:32:51,750 here. And so what it's going to do is, while my front is clear I move and it's still 682 00:32:51,750 --> 00:32:54,659 clear, and it's still clear, and I just keep doing this. 683 00:32:54,659 --> 00:32:56,499 And at some point, I move along, 684 00:32:56,499 --> 00:33:00,068 and my front is no longer clear at this point. Front is clear is no longer 685 00:33:00,068 --> 00:33:02,750 true, and so I stop. Okay, 686 00:33:02,750 --> 00:33:04,349 and basically at 687 00:33:04,349 --> 00:33:07,889 that point I stop executing this loop and I just pick up from 688 00:33:07,890 --> 00:33:09,700 right after the close brace. So I'm 689 00:33:09,700 --> 00:33:12,659 after I'm done executing a loop, whether it's a for-loop or a while-loop, I just 690 00:33:12,659 --> 00:33:15,280 pick up executing after that close brace. Okay, 691 00:33:15,279 --> 00:33:19,759 so I could actually turn this, for example, into a new method, 692 00:33:19,759 --> 00:33:21,349 private, void, 693 00:33:21,349 --> 00:33:24,759 walk to wall, or we'll call it move to wall like that. 694 00:33:24,759 --> 00:33:28,869 695 00:33:28,869 --> 00:33:32,398 And then anytime I basically want to tell Karel, "Just keep moving forward until you hit 696 00:33:32,398 --> 00:33:34,738 a wall," I just essentially 697 00:33:34,739 --> 00:33:38,479 invoke, or call the move to wall method, and it will just get me up until I get to a 698 00:33:38,479 --> 00:33:39,269 wall. 699 00:33:39,269 --> 00:33:42,048 Now you might wonder, hey, where are all these conditions and what are all the 700 00:33:42,048 --> 00:33:44,869 conditions? So if we can get the overhead for just a second, 701 00:33:44,869 --> 00:33:45,869 I'll show you. 702 00:33:45,869 --> 00:33:49,009 Page 18 of your reader. 703 00:33:49,009 --> 00:33:51,509 704 00:33:51,509 --> 00:33:56,289 So on page 18, there's the lovely table one. 705 00:33:56,289 --> 00:33:59,409 Oh, lovely table one, and these are all the conditions you can check in Karel. 706 00:33:59,410 --> 00:34:02,620 There's front is clear, left is clear, right is clear. So you can sort of check to your 707 00:34:02,619 --> 00:34:03,448 left and right. 708 00:34:03,449 --> 00:34:05,740 There's no behind is clear. 709 00:34:05,740 --> 00:34:07,980 Important for babies, not for Karel. 710 00:34:07,980 --> 00:34:11,760 There's beepers present, which means is there a beeper, at least one on the corner 711 00:34:11,760 --> 00:34:13,129 that you're at. Are 712 00:34:13,128 --> 00:34:14,548 there beepers in your bag? 713 00:34:14,548 --> 00:34:18,298 Is your bag not empty? Are you facing north, east, south, or west? So you can 714 00:34:18,298 --> 00:34:19,679 check all those conditions. 715 00:34:19,679 --> 00:34:23,459 You can also check the opposite of all the conditions, like front is blocked. 716 00:34:23,458 --> 00:34:25,739 So you can actually check, like while your front 717 00:34:25,739 --> 00:34:28,969 is blocked you might want to turn left, because you want 718 00:34:28,969 --> 00:34:32,588 to keep turning until eventually your front gets cleared up, and then maybe you go 719 00:34:32,588 --> 00:34:33,668 do something else. 720 00:34:33,668 --> 00:34:37,099 Or your left is blocked, or your right is blocked. So these are all - I'm not going 721 00:34:37,099 --> 00:34:39,869 to go through them all, but they're all sort of obvious. They sort of give you 722 00:34:39,869 --> 00:34:43,009 the obvious information that you would expect about Karel's world, and they're all in your 723 00:34:43,010 --> 00:34:45,659 course reader on page 18. All right, 724 00:34:45,659 --> 00:34:47,818 so given that you have these 725 00:34:47,818 --> 00:34:49,628 conditions you can check. 726 00:34:49,628 --> 00:34:52,498 Sometimes you want to do something while some condition is true, and as soon as 727 00:34:52,498 --> 00:34:55,578 this condition is no longer true, you stop executing. 728 00:34:55,579 --> 00:34:59,209 There's also sometimes that rather than doing something over and over, you just 729 00:34:59,208 --> 00:35:01,349 want to say, let me check some condition 730 00:35:01,349 --> 00:35:05,449 and if it's true, then do this one thing. So I'm not going to do it as a loop, but 731 00:35:05,449 --> 00:35:08,028 I'm just going to done thing. Okay, 732 00:35:08,028 --> 00:35:11,699 and that's something called an if statement, and the way an 733 00:35:11,699 --> 00:35:13,188 if statement works 734 00:35:13,188 --> 00:35:16,078 is it's if, 735 00:35:16,079 --> 00:35:20,329 space, open paren, some condition, again I'll put this condition in squigglies, 736 00:35:20,329 --> 00:35:21,429 737 00:35:21,429 --> 00:35:26,329 open brace, close brace, and this is the body. And it says, if some condition is true, 738 00:35:26,329 --> 00:35:30,179 then do what it's in the braces. And if it's not true, just completely skip over this 739 00:35:30,179 --> 00:35:33,478 and keep executing from right after the braces. 740 00:35:33,478 --> 00:35:36,668 So you might want to say, "Hey, I only want to pick up a beeper if I know a beeper exists in 741 00:35:36,668 --> 00:35:39,969 the world." So I sort of want to have a notion of having a safe 742 00:35:39,969 --> 00:35:43,889 pickup of a beeper. So I could say if 743 00:35:43,889 --> 00:35:44,990 beeper 744 00:35:44,989 --> 00:35:45,778 745 00:35:45,778 --> 00:35:49,668 is present, which means there is a beeper, at least one on the corner that I'm at, then it's 746 00:35:49,668 --> 00:35:50,719 safe 747 00:35:50,719 --> 00:35:52,509 to pick a beeper. 748 00:35:52,509 --> 00:35:55,369 Because if I didnft necessarily check before, 749 00:35:55,369 --> 00:35:57,969 and I didn't know there was a beeper there for sure, I might try to pick up 750 00:35:57,969 --> 00:36:01,208 a beeper on some corner, and there isn't a beeper, and Karel blows up, and life 751 00:36:01,208 --> 00:36:02,509 is unhappy, and 752 00:36:02,510 --> 00:36:06,229 tears well in your eye, and the whole deal. All right, so 753 00:36:06,228 --> 00:36:07,578 if statement, 754 00:36:07,579 --> 00:36:10,778 that's just what we refer to it as kind of as an if statement. It's all 755 00:36:10,778 --> 00:36:11,670 lower case. 756 00:36:11,670 --> 00:36:16,130 Now, sometimes what happens in life is you don't want to just say, "If 757 00:36:16,130 --> 00:36:18,140 something is true, then 758 00:36:18,139 --> 00:36:21,868 essentially what you want to do is this." You want to say, "If that thing is false, 759 00:36:21,869 --> 00:36:24,640 then I want to do something different." Okay, 760 00:36:24,639 --> 00:36:28,208 so you might want to say, "Hey, if there's a beeper present, I'm going to pick I up. 761 00:36:28,208 --> 00:36:31,928 But if there isn't one there, oh, Karel feels a little sad and he's going to put a 762 00:36:31,929 --> 00:36:36,329 beeper down to, like, plant a new tree because he wants to fight global warming." 763 00:36:36,329 --> 00:36:38,180 So else 764 00:36:38,179 --> 00:36:41,179 and then we're going to have another body here, 765 00:36:41,179 --> 00:36:42,288 putBeeper, 766 00:36:42,289 --> 00:36:45,839 assuming that Karel has beepers in his bag. So what this says is, if 767 00:36:45,838 --> 00:36:47,190 there's a beeper present, 768 00:36:47,190 --> 00:36:48,809 then pick it up. 769 00:36:48,809 --> 00:36:51,340 If there is not a beeper present, it doesn't 770 00:36:51,340 --> 00:36:55,130 try to pick a beeper, it skips this part and does the part that's referred 771 00:36:55,130 --> 00:36:56,380 to by the else. 772 00:36:56,380 --> 00:36:57,590 So we'll put a beeper. 773 00:36:57,590 --> 00:37:02,700 So it will only execute one of these bodies, and which body it does depends on whether or 774 00:37:02,699 --> 00:37:05,599 not there are beepers present. If it's true, it's this one, if it's false, if that's one. Question? Is that a 775 00:37:05,599 --> 00:37:10,818 space 776 00:37:10,818 --> 00:37:14,380 or a tab [inaudible]? These are spaces here. So these are required to be spaces, and the syntax is also 777 00:37:14,380 --> 00:37:21,380 all in book too. So you can see where the spaces are there as well. Uh huh? Can you make multiple [inaudible]? 778 00:37:22,679 --> 00:37:25,470 For the time being, you should just check one condition at a time. 779 00:37:25,469 --> 00:37:29,368 Now, you can actually sort of, what we refer to as nest, 780 00:37:29,369 --> 00:37:30,650 these kind of statements. 781 00:37:30,650 --> 00:37:34,280 So you could say, "If beeper is present," you might say, "Hey, if there's a beeper 782 00:37:34,280 --> 00:37:37,420 present there, that's some marker that I should move forward if there is not a wall in front 783 00:37:37,420 --> 00:37:38,389 of me." 784 00:37:38,389 --> 00:37:42,359 So what I want to do is, if there is a beeper present, what I now want to do 785 00:37:42,360 --> 00:37:46,509 is check to see if I can move forward. So if 786 00:37:46,509 --> 00:37:47,548 front 787 00:37:47,548 --> 00:37:52,528 is clear, 788 00:37:52,528 --> 00:37:55,478 move. 789 00:37:55,478 --> 00:37:58,798 And this is what we refer to as a nested expression. Notice how I sort of tabbed it 790 00:37:58,798 --> 00:38:02,858 in a little bit, so that it's kind of nested inside this other guy. 791 00:38:02,858 --> 00:38:05,288 What it means if check to see if beepers are present. 792 00:38:05,289 --> 00:38:07,410 If they're not present, then it skips 793 00:38:07,409 --> 00:38:08,518 this whole body 794 00:38:08,518 --> 00:38:10,189 and it just puts a beeper down. 795 00:38:10,190 --> 00:38:13,400 If there is a beeper present, it's going to executed this body. Well, what is the 796 00:38:13,400 --> 00:38:16,749 body? It's another statement that says, "Is your front clear?" 797 00:38:16,748 --> 00:38:19,608 If your front is clear, you say, "Hey, I was on a beeper, and my front is clear. 798 00:38:19,608 --> 00:38:22,088 Now, I'll take a step forward." But 799 00:38:22,088 --> 00:38:25,748 if there was a beeper present, and my front was clear, or was not clear, I'm 800 00:38:25,748 --> 00:38:27,448 not going to take a step forward, 801 00:38:27,449 --> 00:38:30,858 in which case it doesn't do the if part. It finishes up 802 00:38:30,858 --> 00:38:34,130 and there's nothing else for it to do here. So it's done with this whole part and 803 00:38:34,130 --> 00:38:37,670 it's done with the whole if-else statement. Okay, 804 00:38:37,670 --> 00:38:39,289 any questions about that, 805 00:38:39,289 --> 00:38:41,079 what's actually going on there, right? 806 00:38:41,079 --> 00:38:43,900 If beepers are present, you know you're only going to execute this part. This 807 00:38:43,900 --> 00:38:46,528 part is now a goner, because that's the else part, and 808 00:38:46,528 --> 00:38:47,909 beepers present was true. 809 00:38:47,909 --> 00:38:51,059 So to do this part, you come in here and you say, "Is front clear?" 810 00:38:51,059 --> 00:38:54,629 No, front's not clear. I have some wall in front of me. Oh, okay, well I'm not going to do the 811 00:38:54,628 --> 00:38:56,719 move. Is there an else over here? No. 812 00:38:56,719 --> 00:39:00,219 This else is not attached here. This else is attached to this if that says, oh, there's no 813 00:39:00,219 --> 00:39:02,889 else. Okay, there's nothing else 814 00:39:02,889 --> 00:39:05,608 I should do. Are there any other statements over here that I should 815 00:39:05,608 --> 00:39:06,248 execute? 816 00:39:06,248 --> 00:39:09,209 No, I'm done with the whole body for this part. So it's done 817 00:39:09,210 --> 00:39:14,659 with the entire statement and it continues executing from down there. Okay, 818 00:39:14,659 --> 00:39:19,588 so any questions about that? 819 00:39:19,588 --> 00:39:22,849 Uh huh? So is it every brace always comes in pairs? Every brace comes in pairs. You always have an open brace and there's going to 820 00:39:22,849 --> 00:39:27,318 be some close brace that's going to define the body. Oh, that was the double. All right, 821 00:39:27,318 --> 00:39:28,639 so what we're going to do 822 00:39:28,639 --> 00:39:31,998 is we're going to put this all together in a big program. 823 00:39:31,998 --> 00:39:35,408 Okay, so we're going to use all these things in a big program, and this program, 824 00:39:35,409 --> 00:39:38,890 I'll show you, is basically, Karel is going to run a little steeplechase. You're 825 00:39:38,889 --> 00:39:41,558 basically sort of like running hurdles, okay. 826 00:39:41,559 --> 00:39:45,869 So we're going to open up another program. Don't worry about all the details right now. 827 00:39:45,869 --> 00:39:49,440 I'm just going to run this so I can show you what it looks like. 828 00:39:49,440 --> 00:39:53,679 So you can see what the world looks like and what we want Karel to do. So 829 00:39:53,679 --> 00:39:55,930 we're running, we're running, 830 00:39:55,929 --> 00:39:58,538 steeplechase. 831 00:39:58,539 --> 00:40:01,910 So what Karel's going to do here 832 00:40:01,909 --> 00:40:05,949 is they're the world that is always nine avenues long, and he knows that. It's 833 00:40:05,949 --> 00:40:07,930 always nine avenues long, 834 00:40:07,929 --> 00:40:11,699 which means there are, at most, eight steeples in the world, or eight hurdles. 835 00:40:11,699 --> 00:40:16,018 The reason why we call them steeples as opposed to hurdles is because some of them are big. 836 00:40:16,018 --> 00:40:20,138 Some of them are just ginormous, and so what Karel needs to do is he needs to start 837 00:40:20,139 --> 00:40:20,690 here 838 00:40:20,690 --> 00:40:25,079 and end up over here, and run around every hurdle he encounters. 839 00:40:25,079 --> 00:40:28,089 So how are we going to do that using the stuff that we've learned, and putting it 840 00:40:28,088 --> 00:40:29,909 together into a larger program? 841 00:40:29,909 --> 00:40:32,899 Well, the first thing we're going to do is we need to start with our friend, the 842 00:40:32,900 --> 00:40:33,769 run method. 843 00:40:33,769 --> 00:40:36,608 Right, so in the run method, 844 00:40:36,608 --> 00:40:39,960 all, Eclipse doesn't jump out in front of me. Right, so notice we have all the kind of other 845 00:40:39,960 --> 00:40:41,300 boilerplate that we want to have. 846 00:40:41,300 --> 00:40:44,690 We import Stanford Karel. We're going to have some public class we'll call 847 00:40:44,690 --> 00:40:45,340 steeplechase that 848 00:40:45,340 --> 00:40:49,269 extends super Karel. Right, all the stuff you've seen before as boilerplate. 849 00:40:49,268 --> 00:40:51,379 We're going to define the run method 850 00:40:51,380 --> 00:40:55,090 and what the run method is going to do is, if you have nine avenues you need 851 00:40:55,090 --> 00:40:58,269 to go through, that means intervening the avenues, there's at most 852 00:40:58,269 --> 00:40:59,329 eight steeples. 853 00:40:59,329 --> 00:41:02,969 So I want you to do something eight times. So we're going to have a for-loop that 854 00:41:02,969 --> 00:41:04,628 executes eight times. 855 00:41:04,628 --> 00:41:06,299 What are you going to do inside there 856 00:41:06,300 --> 00:41:09,289 Well, you have a choice. When you look at the world, 857 00:41:09,289 --> 00:41:13,950 either you're facing a steeple or you're not. So if we look over here, right, here 858 00:41:13,949 --> 00:41:17,558 Karel starts out facing a steeple, but after he jumps over it, hey it's clear. So he 859 00:41:17,559 --> 00:41:20,298 could actually move forward before jumping the next steeple. 860 00:41:20,298 --> 00:41:25,449 So that's essentially what we check for in the program. At every step, what we say is, 861 00:41:25,449 --> 00:41:28,539 "If your front is clear, there is no steeple for you to jump. 862 00:41:28,539 --> 00:41:31,049 So just move ahead. But 863 00:41:31,048 --> 00:41:34,070 if your front is not clear, you got some work to do buddy. 864 00:41:34,070 --> 00:41:36,179 So jump hurdle." "And you're like, 865 00:41:36,179 --> 00:41:39,298 jump hurdle. I've been sitting here for 45 minutes. You never told me about jump hurdle. 866 00:41:39,298 --> 00:41:40,608 Where is jump hurdle?" And I'm like, 867 00:41:40,608 --> 00:41:43,389 "Yeah, I didn't tell you about jump hurdle because we're going to write jump hurdle," and you're 868 00:41:43,389 --> 00:41:47,098 like, "Oh, yeah, new instruction that we create." So we're going to break our 869 00:41:47,099 --> 00:41:48,659 program down into smaller pieces. 870 00:41:48,659 --> 00:41:49,980 So what's jump hurdle about? 871 00:41:49,980 --> 00:41:52,059 How do we jump a hurdle? 872 00:41:52,059 --> 00:41:55,369 Well, think about what you want to do to jump a hurdle, and 873 00:41:55,369 --> 00:41:58,329 I'll put it in the simplest possible terms. You want to ascend the hurdle. You want to go 874 00:41:58,329 --> 00:41:58,870 up. 875 00:41:58,869 --> 00:42:02,029 You want to move past the hurdle, and you want to descend the hurdle to come 876 00:42:02,030 --> 00:42:03,159 back down. You're like, 877 00:42:03,159 --> 00:42:04,949 "Yeah, thanks, Ron, that really helped a lot. 878 00:42:04,949 --> 00:42:08,829 How do I ascend and descend the hurdle, right?" So what we're doing is we're breaking the 879 00:42:08,829 --> 00:42:10,470 program down into more and more steps, 880 00:42:10,469 --> 00:42:12,750 and notice this is reading like English. 881 00:42:12,750 --> 00:42:16,449 And the important thing to realize in programming is programming is not just 882 00:42:16,449 --> 00:42:20,989 about writing a program that the computer understands. Programming is about writing 883 00:42:20,989 --> 00:42:22,868 programs that people understand. 884 00:42:22,869 --> 00:42:27,789 I can't stress that enough. That's huge. That's what programming style is all about. 885 00:42:27,789 --> 00:42:30,330 It's what good software engineering is all about. 886 00:42:30,329 --> 00:42:34,248 Writing a program that just works, that someone else can't read and understand, 887 00:42:34,248 --> 00:42:37,768 is actually really terrible software engineering. And I've seen software companies do this where 888 00:42:37,768 --> 00:42:41,458 some hotshot programmer comes along and they're like, "Oh, I'm great programmer." And they go and write some program to do 889 00:42:41,458 --> 00:42:43,128 something, 890 00:42:43,128 --> 00:42:47,018 and then it breaks. And then they come along and they say, "Hey, we need to actually get 891 00:42:47,018 --> 00:42:50,098 that program to do something slightly different, or we need to upgrade it. And it's 892 00:42:50,099 --> 00:42:53,440 written in terms of the code so badly that they can't do anything with it, 893 00:42:53,440 --> 00:42:56,130 and they throw it out completely, fire the programmer, 894 00:42:56,130 --> 00:42:59,289 and get a team of good software engineers to actually do it. I've actually 895 00:42:59,289 --> 00:43:02,429 seen that happen multiple times. Okay, so 896 00:43:02,429 --> 00:43:04,769 write programs for people to read, 897 00:43:04,768 --> 00:43:07,588 not just for computers to read. Both of them need to be able to read it, 898 00:43:07,588 --> 00:43:10,969 but it's far more important that a person reads it and understands it, and that 899 00:43:10,969 --> 00:43:14,488 the computer still executes it correctly. But that's the first major software 900 00:43:14,489 --> 00:43:16,039 engineering principle to think about. 901 00:43:16,039 --> 00:43:19,650 So how do we ascend and descend the hurdle? Move we knew about. Well, 902 00:43:19,650 --> 00:43:21,619 how do we ascend the hurdle? 903 00:43:21,619 --> 00:43:23,920 If you think about ascending the hurdle. 904 00:43:23,920 --> 00:43:28,048 Right, what that means is you're basically facing a wall, some wall that goes up 905 00:43:28,048 --> 00:43:30,028 some amount that you don't know. 906 00:43:30,028 --> 00:43:34,199 Well, how am I going to get up that wall? I'm going to turn left, so now I'm looking up 907 00:43:34,199 --> 00:43:38,489 the wall. And guess what? If I put my right hand out against that wall and 908 00:43:38,489 --> 00:43:42,068 keep moving up along the wall, I can eventually get to a point where I've gotten 909 00:43:42,068 --> 00:43:46,208 to the top of that wall. So I turn left to face north, 910 00:43:46,208 --> 00:43:48,548 while my right is blocked, which is why that 911 00:43:48,548 --> 00:43:49,298 wall is there. 912 00:43:49,298 --> 00:43:53,579 I keep moving all the way up until my right is no longer blocked. That means I've 913 00:43:53,579 --> 00:43:55,479 extended all the way up the hurdle. 914 00:43:55,478 --> 00:43:57,728 At this point, I'm still facing north. 915 00:43:57,728 --> 00:44:00,379 This guy's expecting me to face east, so I can 916 00:44:00,380 --> 00:44:03,898 move over the hurdle, right. If I'm facing north, I'm just going to step one more up. So I 917 00:44:03,898 --> 00:44:04,989 turn right at the end 918 00:44:04,989 --> 00:44:09,099 to now be way above the hurdle and be facing an open place where I can actually 919 00:44:09,099 --> 00:44:10,318 do that move. 920 00:44:10,318 --> 00:44:12,639 So now, I do the move. I'm on the other side of the hurdle. 921 00:44:12,639 --> 00:44:14,578 I need to descend the hurdle. 922 00:44:14,579 --> 00:44:17,829 Well, how do I descend the hurdle? 923 00:44:17,829 --> 00:44:21,149 Ah, this is where our friend, move to wall, that we just wrote, comes in handy. 924 00:44:21,148 --> 00:44:23,958 So I'll show you both of these at the same time. If I want to descend, 925 00:44:23,958 --> 00:44:28,219 I'm at the top of the hurdle, I turn right. So I'm looking south now, I'm looking down, 926 00:44:28,219 --> 00:44:31,670 and I just move all the way down to the wall. It hits the floor, 927 00:44:31,670 --> 00:44:34,969 and this is move to wall that we just wrote, right, while front is clear just 928 00:44:34,969 --> 00:44:36,949 moves. I move all the way down, 929 00:44:36,949 --> 00:44:41,139 and so when I move to wall and I get to the wall, I'm facing south. I want to turn left one 930 00:44:41,139 --> 00:44:44,239 last time so I'm facing forward to go again. Okay, 931 00:44:44,239 --> 00:44:47,389 so any questions about that? 932 00:44:47,389 --> 00:44:50,368 Let me run Karel, so we can see 933 00:44:50,369 --> 00:44:53,440 if he's feeling up to the challenge. So here he is. We'll start the program. 934 00:44:53,440 --> 00:44:54,719 There he goes. 935 00:44:54,719 --> 00:44:57,240 Notice what he's doing. He goes over every hurdle. 936 00:44:57,239 --> 00:44:59,419 If his front is blocked, he goes up 937 00:44:59,420 --> 00:45:02,480 and over. Here's one where you can see it. He goes all the way up until his right 938 00:45:02,480 --> 00:45:05,760 is clear. Then he moves and he comes all the way down until he hits the wall, 939 00:45:05,760 --> 00:45:08,950 and he keeps doing this. And if he does this eight times, he always ends up at 940 00:45:08,949 --> 00:45:15,949 the end because we know it's guaranteed to be nine avenues long. Uh huh? [Inaudible]? Yeah, 941 00:45:17,338 --> 00:45:21,168 good eye. So you might say, "Hey, before when Karel turned right and he was making 942 00:45:21,168 --> 00:45:23,958 three right and left turns, what's going on now?" 943 00:45:23,958 --> 00:45:25,928 It's super Karel, baby. 944 00:45:25,929 --> 00:45:29,309 We're loving it. Super Karel not only knows the three turn lefts, just turn right, 945 00:45:29,309 --> 00:45:31,079 but he's like, "Hey, 946 00:45:31,079 --> 00:45:33,250 forget turn left, man. I'm just turning right." 947 00:45:33,250 --> 00:45:37,389 So he knows, or she knows, or it knows, or whatever, that amorphous form in the sky 948 00:45:37,389 --> 00:45:44,389 knows. Uh huh? 949 00:45:46,929 --> 00:45:50,999 [Inaudible]? Good question, because that's not the specification of the program, right. 950 00:45:50,998 --> 00:45:54,268 So one question you could ask is, like, "Hey when I bought my word processor, I bought 951 00:45:54,268 --> 00:45:57,778 my word processor to, like, write essay with. So how come when I got my word 952 00:45:57,778 --> 00:45:59,298 processor," and this is a valid question to ask, 953 00:45:59,298 --> 00:46:02,989 "Why didn't it come with all the essays I needed to write at Stanford?" Right? I mean, 954 00:46:02,989 --> 00:46:06,499 it would be nice if it did, right. I'd go pay the $400. I'd be like, woo hoo, 955 00:46:06,498 --> 00:46:07,469 super Karel, baby. 956 00:46:07,469 --> 00:46:10,598 I got super word processor. Right, 957 00:46:10,599 --> 00:46:13,479 because that's not the specification of the program. The specification of the 958 00:46:13,478 --> 00:46:17,348 program is to go over the hurdles one by one. Now, 959 00:46:17,349 --> 00:46:20,219 just to see if - and you might say, "Oh, thatfs kind of interesting, but does it 960 00:46:20,219 --> 00:46:23,880 just work on this one world?" So let's consider another world, 961 00:46:23,880 --> 00:46:26,670 super steeplechase. So we can load a world, 962 00:46:26,670 --> 00:46:29,380 say you can just click load world, and there are some worlds that we'll provide to you in 963 00:46:29,380 --> 00:46:32,480 your starter project, which is all explained in the handout. Super 964 00:46:32,480 --> 00:46:34,528 steeplechase. So here's super steeplechase. 965 00:46:34,528 --> 00:46:36,548 Yeah, that might look familiar. 966 00:46:36,548 --> 00:46:39,318 More bars in more places, right. So 967 00:46:39,318 --> 00:46:43,418 here's Karel, right. This is like a world that 50 high. That's Karel down there and you're 968 00:46:43,418 --> 00:46:45,279 like, "Oh, man, you're really making," yeah, 969 00:46:45,280 --> 00:46:47,329 we're making him work. All right, so 970 00:46:47,329 --> 00:46:50,160 go little Karel. 971 00:46:50,159 --> 00:46:54,250 There he goes. Oh, oh, come on, give him a little encouragement. Go Karel, 972 00:46:54,250 --> 00:46:55,268 go Karel, 973 00:46:55,268 --> 00:46:58,218 go Karel. Aw, yeah, because you 974 00:46:58,219 --> 00:47:02,420 can just speed 975 00:47:02,420 --> 00:47:04,559 him up if your program's running slow, 976 00:47:04,559 --> 00:47:09,130 but there he goes, right. Huge steeples, he's done, same program works. 977 00:47:09,130 --> 00:47:12,980 So the important lesson that comes from that is to think about the generality of 978 00:47:12,980 --> 00:47:16,679 the program, right. You don't want to just write a program that works in 979 00:47:16,679 --> 00:47:20,359 one particular world, unless that's the only world you need to worry about. 980 00:47:20,360 --> 00:47:22,679 You want to generalize your program in a way 981 00:47:22,679 --> 00:47:26,209 so that it's using, in some sense, general principles that will apply to any world 982 00:47:26,208 --> 00:47:29,538 that meets your specifications. Right, it still 983 00:47:29,539 --> 00:47:33,180 needs to meet the specification. Our world is still nine avenues long and we know what the specification of the 984 00:47:33,179 --> 00:47:34,838 steeples are. All right, 985 00:47:34,838 --> 00:47:41,838 so any quick questions before we go? Uh huh? So if you had the [inaudible]? 986 00:47:42,829 --> 00:47:46,700 No, because it's still just nine avenues. The for-loop counter stays the same, right. 987 00:47:46,699 --> 00:47:50,308 It's that while-loop that's actually going up and down the hurdles, because it 988 00:47:50,309 --> 00:47:52,769 doesn't know how big the hurdles are. 989 00:47:52,768 --> 00:47:58,938 Any other questions? [Inaudible]? 990 00:47:58,938 --> 00:48:05,938 That it's nine avenues and the hurdles are just pools, basically, of any length. [Inaudible]. Yeah, 991 00:48:07,079 --> 00:48:09,849 all right, I will see you on Friday. If you have any more questions, come on down.