2 00:00:13,778 --> 00:00:17,050 This presentation is delivered by the Stanford Center for Professional 3 00:00:17,050 --> 00:00:24,050 Development. 4 00:00:25,039 --> 00:00:28,140 So a little bit of additional background on Karel before we dive into the real meat 5 00:00:28,140 --> 00:00:29,839 of things, okay? 6 00:00:29,839 --> 00:00:33,240 One thing you may have noticed is all the Karel code that youfve either started writing, 7 00:00:33,240 --> 00:00:35,700 if you're already working on assignment number one or all the code that we write 8 00:00:35,700 --> 00:00:36,560 in class 9 00:00:36,560 --> 00:00:40,940 is all in file that ends with dot Java. If you haven't noticed that before, 10 00:00:40,939 --> 00:00:43,118 you'll - now you know, it ends with dot Java. 11 00:00:43,118 --> 00:00:46,280 Because in fact, Karel is all implemented in Java. 12 00:00:46,280 --> 00:00:50,210 So one of the things you might be wondering is hey, I know a little Java; can I use 13 00:00:50,210 --> 00:00:53,710 some Java in conjunction with Karel? And the answer is no. 14 00:00:53,710 --> 00:00:57,160 For the purpose of the Karel assignments, you should just use the constructs that 15 00:00:57,159 --> 00:00:59,549 youfve been shown in class and in the Karel reader, 16 00:00:59,549 --> 00:01:02,729 just keep it to those constructs. That still gives you plenty of stuff to do with Karel, 17 00:01:02,729 --> 00:01:05,950 it's a good time, but keep it to that, and actually starting on Monday we're gonna, like, 18 00:01:05,950 --> 00:01:10,290 sort of leave Karel behind and say bye-bye, Karel, and I'll be, like, bye-bye, I love you, 19 00:01:10,290 --> 00:01:11,130 call 20 00:01:11,129 --> 00:01:12,118 me. 21 00:01:12,118 --> 00:01:16,328 But we'll get into Java, so if you know Java now, just use the Karel stuff in 22 00:01:16,328 --> 00:01:18,019 the Karel 23 00:01:18,019 --> 00:01:19,810 assignments that we actually do. All 24 00:01:19,810 --> 00:01:23,810 right, so with that said, any questions to start off with before we dive into 25 00:01:23,810 --> 00:01:30,810 something new? Um-hm? How do you 26 00:01:30,959 --> 00:01:33,928 stop the program? Like, I had a problem, 27 00:01:33,929 --> 00:01:38,760 it goes up into the corner and just kind of like it says an error message. And it kept looping there forever? Yeah, 28 00:01:38,760 --> 00:01:42,650 and I tried making, like, an empty program called Stop, but that didnft really work. No, just go 29 00:01:42,650 --> 00:01:47,230 up to the little icon - or the little thing in the top of the window that 30 00:01:47,230 --> 00:01:49,660 allows you to close the window and just close the window. 31 00:01:49,659 --> 00:01:51,688 Karel will be okay, he knows how to deal with that. 32 00:01:51,688 --> 00:01:55,789 As a matter of fact, that's - I am so glad you asked that question. It's 33 00:01:55,790 --> 00:01:59,270 just a wonderful thing, because it actually leads into the first topic that I want to talk about, 34 00:01:59,269 --> 00:02:01,659 which is common errors. 35 00:02:01,659 --> 00:02:04,649 And so there are some common errors that may come up, and these are good things to kind 36 00:02:04,650 --> 00:02:07,890 of know about. And one of them is the thing you just ran into. 37 00:02:07,890 --> 00:02:11,439 But before we actually talk about that at sort of the level of Karel, I wanna ask you a 38 00:02:11,439 --> 00:02:12,688 question: [inaudible] another one 39 00:02:12,688 --> 00:02:15,638 of these strange questions. How many people have actually ever 40 00:02:15,639 --> 00:02:19,149 read the instructions on a bottle of shampoo? 41 00:02:19,149 --> 00:02:22,389 A few folks - oh, wow. Man, that's more than - I was, like, 42 00:02:22,389 --> 00:02:26,088 youfve really gotta get out more often, right? 43 00:02:26,088 --> 00:02:31,468 But I'm surprised that many. And what do those instructions say? 44 00:02:31,468 --> 00:02:35,468 Rinse, right, that's where you rinse your hair, and then you lather, 45 00:02:35,468 --> 00:02:37,998 and then you repeat. 46 00:02:37,998 --> 00:02:41,680 And if anyone actually followed these instructions, you would still be in the 47 00:02:41,680 --> 00:02:43,209 shower now. 48 00:02:43,209 --> 00:02:46,789 As a matter of fact, you'd be in the shower for the rest of your life. Why? Because you 49 00:02:46,789 --> 00:02:49,019 rinse, you lather, and you repeat, 50 00:02:49,020 --> 00:02:51,420 which means you rinse, you later, and you repeat. 51 00:02:51,419 --> 00:02:54,129 And you just keep doing this, and it's like Karel just taking a shower, right? And he's 52 00:02:54,129 --> 00:02:56,870 like you keep telling me to rinse 53 00:02:56,870 --> 00:03:00,128 - to linse, rinse and lather together. 54 00:03:00,128 --> 00:03:02,438 Rinse, 55 00:03:02,438 --> 00:03:06,978 lather, and repeat, right? And this is what we refer to in program-speak 56 00:03:06,979 --> 00:03:10,129 as an infinite loop. 57 00:03:10,128 --> 00:03:14,088 Which as you can guess is a loop that keeps going forever. And this may come up in 58 00:03:14,088 --> 00:03:17,908 your Karel programs. Here is an example of an infinite loop. You might be standing 59 00:03:17,908 --> 00:03:20,429 somewhere and you might say while 60 00:03:20,429 --> 00:03:23,959 front is clear. 61 00:03:23,959 --> 00:03:27,259 Oh, well, if your front is clear, I want you to turn left, because, you know, I have 62 00:03:27,259 --> 00:03:31,340 this idea that eventually you'll turn and you'll be facing something and your front won't be 63 00:03:31,340 --> 00:03:32,289 clear anymore. 64 00:03:32,288 --> 00:03:34,089 And that might be a great idea to have, 65 00:03:34,090 --> 00:03:34,778 but what happens 66 00:03:34,778 --> 00:03:37,219 is if here's our friend Karel, 67 00:03:37,219 --> 00:03:43,539 happens to be standing in the world and guess what? There's no walls around him. 68 00:03:43,539 --> 00:03:45,048 What does he do? He rinse, 69 00:03:45,049 --> 00:03:48,979 lathers, and repeats, right? He just keeps sitting there, turning left 70 00:03:48,979 --> 00:03:52,579 on this corner, and his front will never become blocked so he'll never get out 71 00:03:52,579 --> 00:03:54,439 of that loop, okay? 72 00:03:54,438 --> 00:03:58,968 That's one of the places where the syntax of the program is perfectly valid, but 73 00:03:58,968 --> 00:04:02,618 in terms of our intention for what we wanted the program to do, it didnft work as 74 00:04:02,618 --> 00:04:05,508 intended. This is the more common thing that you're actually going to need to deal with fixing in 75 00:04:05,508 --> 00:04:06,399 your programs 76 00:04:06,400 --> 00:04:08,539 as opposed to syntax-type problems. 77 00:04:08,538 --> 00:04:11,358 As a matter of fact, I won't mention the name of the company but there was a 78 00:04:11,359 --> 00:04:14,209 company years and years ago that put out this advertisement that was, like, 79 00:04:14,209 --> 00:04:18,499 our processors are so fast they can execute an infinite loop in 2.5 80 00:04:18,499 --> 00:04:19,169 seconds. 81 00:04:19,168 --> 00:04:22,978 And you kind of looked at that and you were like ah, ha, ha, funny computer scientists. 82 00:04:22,978 --> 00:04:25,878 But the thing that was actually funny about it was the company that put out 83 00:04:25,879 --> 00:04:29,869 these chips, the chips actually had a flaw in them where if they executed things really fast they 84 00:04:29,869 --> 00:04:30,930 would warm up 85 00:04:30,930 --> 00:04:32,329 and then they would melt. 86 00:04:32,329 --> 00:04:35,918 So in fact, they would execute an infinite loop in 2.5 seconds, 87 00:04:35,918 --> 00:04:39,038 and then the processor would catch fire and it would stop. 88 00:04:39,038 --> 00:04:42,318 So sometimes, that happens. Hopefully, that won't happen to your machine. Don't worry, 89 00:04:42,319 --> 00:04:46,520 even if you get Karel going real fast, it's okay. If your processor melts, come 90 00:04:46,519 --> 00:04:50,359 talk to me, it's never happened. But it'll make for a wonderful story in class, and it'll cost 91 00:04:50,360 --> 00:04:52,899 you about $1,800.00. Fine. 92 00:04:52,899 --> 00:04:56,699 So another kind of error that comes up fairly often 93 00:04:56,699 --> 00:04:59,978 also has a little affectionate name to it. 94 00:04:59,978 --> 00:05:03,839 And this is something that we refer to in programming speak as 95 00:05:03,839 --> 00:05:10,839 an off-by-one bug. Or if you take the first letter of that, it becomes 96 00:05:11,810 --> 00:05:15,889 an OBOB. And sometimes you'll hear people refer to OBOB, and if you're name is 97 00:05:15,889 --> 00:05:17,259 Robert they're not saying, like, oh, Bob. No, that's 98 00:05:17,259 --> 00:05:20,539 just referring to the off-by-one bug. 99 00:05:20,540 --> 00:05:21,760 And what that means is 100 00:05:21,759 --> 00:05:26,000 you forgot to do something one more time than you really needed to, even though 101 00:05:26,000 --> 00:05:29,379 logically it didnft seem like you needed to. So the best way to understand 102 00:05:29,379 --> 00:05:31,279 that is with a little concrete example. 103 00:05:31,279 --> 00:05:34,709 So let me actually give you a concrete example by going over to the computer. 104 00:05:34,709 --> 00:05:36,628 So here's Karel sitting in his world, 105 00:05:36,629 --> 00:05:40,278 and one of the things we might wanna do with Karel - whoa - Karel in his world is to say 106 00:05:40,278 --> 00:05:40,750 that 107 00:05:40,750 --> 00:05:44,180 we actually want Karel to lay down a line of beepers, right? Just whatever 108 00:05:44,180 --> 00:05:47,538 row he's in, just lay down a line of beepers till you hit the wall. And that's a fairly 109 00:05:47,538 --> 00:05:49,829 common thing that you might wanna do, okay? 110 00:05:49,829 --> 00:05:53,060 Now one way we might be inclined to actually do this if we were to write 111 00:05:53,060 --> 00:05:54,149 some code 112 00:05:54,149 --> 00:05:55,759 is to say something like 113 00:05:55,759 --> 00:05:59,660 hey, I'm gonna, you know, write a new class, I'll call it fill row that extends 114 00:05:59,660 --> 00:06:02,750 super Karel. I import all the Karel stuff, so I'm doing all the crazy stuff that 115 00:06:02,750 --> 00:06:06,569 [inaudible] told me to do, and hey, I want to keep putting down beepers until I fill the 116 00:06:06,569 --> 00:06:07,080 row. 117 00:06:07,079 --> 00:06:10,899 So wouldnft it make sense to say while front is clear, right? There's no wall in 118 00:06:10,899 --> 00:06:11,680 front of me. 119 00:06:11,680 --> 00:06:12,848 Put down a beeper 120 00:06:12,848 --> 00:06:14,588 and move to the next corner. 121 00:06:14,588 --> 00:06:17,618 And as long as my front is clear I'll put down another beeper and keep taking 122 00:06:17,619 --> 00:06:20,159 another step and doing this over and over, and this seems like - 123 00:06:20,158 --> 00:06:23,680 hey, that seems like reasonable code to lay down a line of beepers. 124 00:06:23,680 --> 00:06:27,199 What happens when I do this? 125 00:06:27,199 --> 00:06:32,908 Yeah, so let's actually run it. Let's bring up our buddy Karel. 126 00:06:32,908 --> 00:06:36,588 Where'd you go, Karel? Here you are. And run that program. 127 00:06:36,588 --> 00:06:39,838 And so what Karel does, he puts down a beeper, his front is clear, so he moves, puts 128 00:06:39,838 --> 00:06:43,259 down a beeper, front is clear, puts down a beeper, front is clear, puts down a 129 00:06:43,259 --> 00:06:45,218 beeper, front is clear. He moves here, 130 00:06:45,218 --> 00:06:47,009 front is no longer clear. 131 00:06:47,009 --> 00:06:50,919 Before he puts down the beeper, he says hey, my front isnft clear anymore, I stop. 132 00:06:50,918 --> 00:06:54,589 He's off by one because there's one more thing that he needed to do, which 133 00:06:54,589 --> 00:06:57,019 was to put down another beeper in this last spot. 134 00:06:57,019 --> 00:07:00,339 This is another very common logical error that comes up - you're off by one 135 00:07:00,339 --> 00:07:03,829 in terms of what you're thinking, and so lo and behold if we go into the 136 00:07:03,829 --> 00:07:05,428 program over here 137 00:07:05,428 --> 00:07:08,258 and we look at this comment, and I'll tell you about comments in just a second, 138 00:07:08,259 --> 00:07:12,600 it says this is buggy, it's off by one. You need to add a final put beeper. 139 00:07:12,600 --> 00:07:15,710 And so what we do is we say hey, after you got out of your loop, your front is no 140 00:07:15,709 --> 00:07:16,649 longer clear, but 141 00:07:16,649 --> 00:07:19,228 you haven't put down a beeper on this corner yet. 142 00:07:19,228 --> 00:07:20,300 Put down 143 00:07:20,300 --> 00:07:22,460 a beeper, right? 144 00:07:22,459 --> 00:07:26,218 And if we save that puppy off and we close this off and just so we know that it's 145 00:07:26,218 --> 00:07:28,550 in fact running, 146 00:07:28,550 --> 00:07:32,239 doo doo doo, we run our little program 147 00:07:32,238 --> 00:07:34,218 and rock on, all right? 148 00:07:34,218 --> 00:07:36,278 No more off-by-one bug, life is good. 149 00:07:36,278 --> 00:07:39,699 So any questions about the OBOB or the off-by-one bug? 150 00:07:39,699 --> 00:07:40,980 That's another common thing. 151 00:07:40,980 --> 00:07:43,790 These are just a couple of common things I wanna show you, so, you if 152 00:07:43,790 --> 00:07:47,060 they come up in your programs you don't feel like oh, you're out there adrift, all 153 00:07:47,060 --> 00:07:50,500 alone. Like, these have been done millions of times by other very qualified 154 00:07:50,500 --> 00:07:51,100 programmers. 155 00:07:51,100 --> 00:07:53,588 Don't worry, it happens to everyone. All right, 156 00:07:53,588 --> 00:07:57,050 so one of the things I just mentioned is this thing called the comment. This thing 157 00:07:57,050 --> 00:07:59,829 that's up here in green. What's that all about? 158 00:07:59,829 --> 00:08:02,899 So as I mentioned to you last time, one of the key software engineering 159 00:08:02,899 --> 00:08:07,668 principles is to write programs that are understandable by people, not just by 160 00:08:07,668 --> 00:08:08,368 machines. 161 00:08:08,369 --> 00:08:12,729 And so what a comment is is a way of being able to put something in your 162 00:08:12,728 --> 00:08:17,088 program that another human being reading your program can actually read and understand, and 163 00:08:17,088 --> 00:08:21,899 it actually has no impact on the execution of the program at all. 164 00:08:21,899 --> 00:08:26,289 And so the way the comment works is it starts off with a slash and a star, 165 00:08:26,290 --> 00:08:30,939 and now everything you put - it can even span multiple lines - until you put a star 166 00:08:30,939 --> 00:08:35,090 and a slash is a comment. It's there just for the person to read, and it doesnft 167 00:08:35,090 --> 00:08:37,120 affect the program at all. 168 00:08:37,120 --> 00:08:39,980 And so what I would encourage you to do as part of good programming style is all 169 00:08:39,980 --> 00:08:43,810 of your programs up at the top should have a comment that says what the name of 170 00:08:43,809 --> 00:08:47,309 the file is and has a little bit of an explanation about what your program 171 00:08:47,309 --> 00:08:50,250 actually does. And if you're wondering how much of an explanation or how in-depth it 172 00:08:50,250 --> 00:08:52,509 should be, be it as in-depth as you want, 173 00:08:52,509 --> 00:08:55,850 but in the handout you got today you see examples of a bunch of programs, and they're 174 00:08:55,850 --> 00:08:58,730 all commented, so you can see examples of comments in there. 175 00:08:58,730 --> 00:09:02,259 There is also shorthand you can do. There is a comment that's just 176 00:09:02,259 --> 00:09:05,399 slash, slash, and that just means everything on the remainder of this line 177 00:09:05,399 --> 00:09:09,139 is a comment. So it actually has no close, in some sense, for the comment like this 178 00:09:09,139 --> 00:09:12,669 does explicitly. Everything else to the rest of the line, when you hit a return, that's 179 00:09:12,669 --> 00:09:15,289 how it knows it's done with the comment. So sometimes if you wanna have a 180 00:09:15,289 --> 00:09:16,358 one-liner comment somewhere, 181 00:09:16,359 --> 00:09:23,359 you can just put a slash, slash. [Inaudible.] 182 00:09:32,820 --> 00:09:35,950 Yeah, when you get assigned to a different section leader, you'll actually 183 00:09:35,950 --> 00:09:39,140 put your assignments in with your name and your section leader, and that's how 184 00:09:39,139 --> 00:09:42,460 it'll get differentiated. So wait until you get your first section, and then you'll see 185 00:09:42,460 --> 00:09:49,460 how submission stuff works. Um-hm? 186 00:09:53,389 --> 00:09:55,879 What about when you write private programs [inaudible] public ones, do those need comments, too? Oh, when you write, like, other methods? 187 00:09:55,879 --> 00:09:59,289 Yeah, you should comment those as well. So methods should also be commented as well, and I'll 188 00:09:59,289 --> 00:10:02,529 show you an example of that today, actually, so you can see an example of how we 189 00:10:02,529 --> 00:10:03,259 do that. 190 00:10:03,259 --> 00:10:07,019 So that's actually - and I love it when everyone just, you know, 191 00:10:07,019 --> 00:10:10,169 it's like ringers in the audience. You just lead me into the next topic, and that's a 192 00:10:10,169 --> 00:10:11,259 perfect example to say 193 00:10:11,259 --> 00:10:14,548 hey, let's actually look in, look at a program that we did last time, which was 194 00:10:14,548 --> 00:10:16,360 the steeplechase program, 195 00:10:16,360 --> 00:10:20,230 now with comments, okay? So up at the top - sometimes you need to click this little 196 00:10:20,230 --> 00:10:23,839 plus sign to expand the comment, because Eclipse has a tendency to sort 197 00:10:23,839 --> 00:10:26,609 of minimize the comments automatically. So just click the plus 198 00:10:26,609 --> 00:10:29,729 sign and that'll stand them out. Steeplechase, so we have a nice little comment here at 199 00:10:29,729 --> 00:10:31,439 the top of our program. 200 00:10:31,438 --> 00:10:34,899 Notice this is a Java file. As I mentioned before, it ends with dot Java, 201 00:10:34,899 --> 00:10:38,458 because Karel's actually implemented in Java. But you should use no features of 202 00:10:38,458 --> 00:10:39,349 Java 203 00:10:39,350 --> 00:10:43,399 other than what is in the Karel book. And if you're like hey, [inaudible] I don't 204 00:10:43,399 --> 00:10:48,730 know Java, hey, nothing for you to unlearn. You're good to go, because you just know Karel, all right? 205 00:10:48,730 --> 00:10:52,149 The other thing that's going on here is you'll notice that inside the program 206 00:10:52,149 --> 00:10:55,948 we have comments as well. So this method, the run method, it says to run a race that's 207 00:10:55,948 --> 00:10:59,169 nine avenues long. We need to forward or jump eight hurdles eight 208 00:10:59,169 --> 00:10:59,688 times. 209 00:10:59,688 --> 00:11:02,469 And that makes it explicit to the person, right? If we didnft have that comment, someone would 210 00:11:02,470 --> 00:11:03,399 come along and say 211 00:11:03,399 --> 00:11:06,669 hey, the race is nine avenues; why are you only iterating this eight times, right? And 212 00:11:06,669 --> 00:11:10,649 that's a natural question to ask. And so you put in comments to clarify things in the 213 00:11:10,649 --> 00:11:13,279 program, which are not obvious, okay? 214 00:11:13,279 --> 00:11:16,839 Another thing related to that is what's referred to as pre-conditions and 215 00:11:16,840 --> 00:11:21,139 post-conditions. What did you expect to be true before you called a particular 216 00:11:21,139 --> 00:11:24,028 method or before a particular method is invoked, 217 00:11:24,028 --> 00:11:25,860 and what's going to be true afterwards? 218 00:11:25,860 --> 00:11:29,639 So our friend Jump Hurdle here - remember Jump Hurdle from last time? We ascend the hurdle, 219 00:11:29,639 --> 00:11:30,750 which means we go up, 220 00:11:30,750 --> 00:11:34,000 we move to cross over the top of the hurdle, and we descend hurdle to come back 221 00:11:34,000 --> 00:11:35,970 down the other side? Well, 222 00:11:35,970 --> 00:11:39,590 what we needed for that to be true for this to actually work right is 223 00:11:39,590 --> 00:11:43,050 the precondition is you're facing east at the bottom of a hurdle. Which means 224 00:11:43,049 --> 00:11:47,029 the hurdle was right in front of you and you were facing east in front of it. 225 00:11:47,029 --> 00:11:50,610 That way, we know which way to turn and how to ascend the hurdle and how to get over it, 226 00:11:50,610 --> 00:11:54,060 and when we're done you're going to be facing east again. So you can assume, 227 00:11:54,059 --> 00:11:55,558 after this method is done, 228 00:11:55,558 --> 00:11:58,129 that you're facing east again and you're at the bottom 229 00:11:58,129 --> 00:11:59,420 of the world 230 00:11:59,419 --> 00:12:01,428 in the next avenue after the hurdle. 231 00:12:01,428 --> 00:12:05,808 So it makes clear, right, because we don't know what-all stuff is going on inside ascend hurdle 232 00:12:05,808 --> 00:12:09,159 and descend hurdle, but a programmer now doesnft need to trace 233 00:12:09,159 --> 00:12:11,459 through the execution of your program 234 00:12:11,460 --> 00:12:13,019 to figure out what's going on. 235 00:12:13,019 --> 00:12:15,889 This tells him what needs to be true before and what needs to be true 236 00:12:15,889 --> 00:12:17,850 after, and it helps you with debugging. 237 00:12:17,850 --> 00:12:20,480 Because if you're debugging your program and you call this 238 00:12:20,480 --> 00:12:24,389 jump hurdle method and you haven't satisfied the precondition, it's probably 239 00:12:24,389 --> 00:12:27,429 not gonna work right and that allows you to work backward to figure out what you 240 00:12:27,429 --> 00:12:28,120 need to do 241 00:12:28,120 --> 00:12:31,078 to fix things before you even call this method, 242 00:12:31,078 --> 00:12:34,059 okay? So any questions about that? 243 00:12:34,059 --> 00:12:37,500 So as we go further down, you'll notice - and this is all in your handout - all of the 244 00:12:37,500 --> 00:12:40,289 methods have pre-conditions and post-conditions that are commented in here. This 245 00:12:40,289 --> 00:12:43,838 is a good habit to get into, at least for your Karel programs, okay? 246 00:12:43,839 --> 00:12:46,669 Even if you don't explicitly put pre- and post-conditions, you should at least 247 00:12:46,668 --> 00:12:49,220 have some comment for every method, 248 00:12:49,220 --> 00:12:52,210 explaining what that method does. So that's the level of commenting that we actually 249 00:12:52,210 --> 00:12:53,290 want you to have. 250 00:12:53,289 --> 00:12:56,879 Notice it's also perfectly fine to have very short, like you know, three-line 251 00:12:56,879 --> 00:13:00,578 or in some cases even one-line methods is perfectly fine, 252 00:13:00,578 --> 00:13:04,479 because it helps - like here's a two-line method over here - it just helps break the 253 00:13:04,480 --> 00:13:05,909 program down, okay? 254 00:13:05,909 --> 00:13:09,059 And that's a process we refer to as decomposition. 255 00:13:09,059 --> 00:13:12,259 And so what's decomposition all about, right? We need to think of this in sort of more 256 00:13:12,259 --> 00:13:16,799 concrete terms that you may understand from your daily life. So 257 00:13:16,799 --> 00:13:20,109 besides reading the bottle of shampoo, another thing that we could talk about is 258 00:13:20,110 --> 00:13:23,470 what you did this morning. So if I were to say 259 00:13:23,470 --> 00:13:27,810 hey, what did you do before you got up for your - or before you went to your first 260 00:13:27,809 --> 00:13:31,379 class this morning? How did you get yourself ready this morning? What did you do? 261 00:13:31,379 --> 00:13:33,960 Anyone wanna volunteer what they did? I know it might be a little scary for 262 00:13:33,960 --> 00:13:35,450 some of you. 263 00:13:35,450 --> 00:13:42,450 Um-hm? What did you do this morning? Use the microphone, please. Pardon? 264 00:13:43,100 --> 00:13:43,350 Basically I 265 00:13:43,249 --> 00:13:48,980 basically rolled out of bed, and then - All right, so you woke up. Wow, did 266 00:13:48,980 --> 00:13:52,330 you even hit the snooze bar at all? 267 00:13:52,330 --> 00:13:57,620 Twice. Oh, good times. That's always good. That's why I set it early. She was good. I had a - my old roommate in college 268 00:13:57,620 --> 00:14:01,509 actually would play the snooze bar game, no joke, for two and a half hours. 269 00:14:01,509 --> 00:14:04,629 And I would be sitting there, I'd be in the room, like, I'd be awake, staring at the ceiling, and [inaudible] 270 00:14:04,629 --> 00:14:07,808 I'd know his snooze bar, and I'd be like Joe, 271 00:14:07,808 --> 00:14:12,649 why don't you just not set your alarm? And he'd be like no, man, I gotta set my alarm. And 272 00:14:12,649 --> 00:14:13,889 every 273 00:14:13,889 --> 00:14:16,740 day - anyway, I'm glad it's only twice for you. You can tell there's just, like, 274 00:14:16,740 --> 00:14:20,610 you know, like I got this little twitch from, like, 15 years ago from the snooze bar. 275 00:14:20,610 --> 00:14:23,159 But you woke up, and then what did you do? 276 00:14:23,159 --> 00:14:23,889 Okay, and 277 00:14:23,889 --> 00:14:27,299 then I brushed my teeth. Okay, you brushed teeth. 278 00:14:27,299 --> 00:14:28,229 What else? 279 00:14:28,230 --> 00:14:30,129 Go to the bathroom. 280 00:14:30,129 --> 00:14:30,990 Bathroom. 281 00:14:30,990 --> 00:14:33,620 I won't ask you for any more detail on that. Yeah. 282 00:14:33,620 --> 00:14:35,350 What else. That'd be awkward. 283 00:14:35,350 --> 00:14:39,360 Shower? Was shower in there, maybe? I shower later in the day. Oh, 284 00:14:39,360 --> 00:14:44,539 okay, that's fine. After [inaudible]. I'm just gonna stick shower in here. 285 00:14:44,539 --> 00:14:48,208 Okay. Just for good measure. But it's perfectly fine whenever you wanna take them. What 286 00:14:48,208 --> 00:14:50,639 else? And then clothes, [inaudible] 287 00:14:50,639 --> 00:14:53,610 clothes. Clothes. 288 00:14:53,610 --> 00:14:56,389 And then presumably you probably went out of your room 289 00:14:56,389 --> 00:14:59,590 or ate breakfast or - did you get up for breakfast? No. Yeah, 290 00:14:59,590 --> 00:15:04,060 I know, it's brutally early, isnft it? Yeah. Like, I think, like, I ate 291 00:15:04,059 --> 00:15:07,149 breakfast like once the whole time I was an undergrad, and it's because, like, I just 292 00:15:07,149 --> 00:15:10,689 stayed up the night before and I was, like, oh my god, there's a thing called breakfast, and I can actually 293 00:15:10,690 --> 00:15:12,950 go to it, so I went. 294 00:15:12,950 --> 00:15:15,940 But so this is an interesting thing, you give us sort of a breakdown of what you do. Thank you 295 00:15:15,940 --> 00:15:18,570 very much, what's your name, by the way? 296 00:15:18,570 --> 00:15:24,240 Jasmine. Thanks, Jasmine. I'll try to see if I can get to you, but - 297 00:15:24,240 --> 00:15:27,269 denied every time. So here's, you know, a standard set of things that we might 298 00:15:27,269 --> 00:15:28,539 think about doing. Now 299 00:15:28,539 --> 00:15:31,730 it turns out this is a very good list of things that you might want to consider 300 00:15:31,730 --> 00:15:34,039 doing. Good hygiene for the morning. 301 00:15:34,039 --> 00:15:35,169 But in fact, 302 00:15:35,169 --> 00:15:38,209 when you think about something like brushing your teeth, 303 00:15:38,210 --> 00:15:40,769 brushing your teeth - we all understand what brushing your teeth means. But if there's 304 00:15:40,769 --> 00:15:44,250 someone, like when I, you know, try to get my 1.5-year-old son to brush his 305 00:15:44,250 --> 00:15:44,919 teeth, 306 00:15:44,919 --> 00:15:47,870 I say hey, William, you need to brush your teeth, he has no idea what I'm talking 307 00:15:47,870 --> 00:15:48,990 about. 308 00:15:48,990 --> 00:15:51,649 Because brushing teeth for him involves 309 00:15:51,649 --> 00:15:55,079 getting the toothbrush - 310 00:15:55,080 --> 00:15:56,009 brush - 311 00:15:56,009 --> 00:15:58,500 putting toothpaste on it, 312 00:15:58,500 --> 00:16:02,200 and sort of moving it on your teeth, 313 00:16:02,200 --> 00:16:05,640 right? So there's a level at which I need to break this down. As a matter of fact, 314 00:16:05,639 --> 00:16:08,710 moving it on your teeth, if you've ever talked to a dentist, 315 00:16:08,710 --> 00:16:11,980 this is a whole involved process by itself. Like what angle you put it at and how 316 00:16:11,980 --> 00:16:13,959 much you move it and how much time [inaudible]. It's like whoa, man, 317 00:16:13,958 --> 00:16:17,979 I never thought there was that much to brushing your teeth, but evidently, there is. 318 00:16:17,980 --> 00:16:21,379 And so what happens is we start with a high-level description, and then each one 319 00:16:21,379 --> 00:16:24,409 of those steps in the high-level description we break down. 320 00:16:24,409 --> 00:16:28,429 And we keep breaking it down until we get to a level of detail, which is 321 00:16:28,429 --> 00:16:31,078 sufficient for us to be able to understand minutely. 322 00:16:31,078 --> 00:16:33,949 Or if we're doing this on the computer we get to a level of detail 323 00:16:33,950 --> 00:16:36,710 at which the computer understands it directly. 324 00:16:36,710 --> 00:16:39,879 What that means is we eventually get to a level of detail where it turns into things 325 00:16:39,879 --> 00:16:42,009 like put beeper or move 326 00:16:42,009 --> 00:16:43,979 or while something is true, 327 00:16:43,979 --> 00:16:47,359 right? So when we get to that level which we call primitives, that's when this process 328 00:16:47,359 --> 00:16:50,949 eventually stops. But this is essentially a process that's called 329 00:16:50,948 --> 00:16:53,919 stepwise refinement. 330 00:16:53,919 --> 00:16:57,000 Because what we're doing at each step in the process 331 00:16:57,000 --> 00:17:00,370 is we're coming up with some steps, and if those steps are not the primitives 332 00:17:00,370 --> 00:17:03,830 that we understand we need to refine them into more steps and refine them into 333 00:17:03,830 --> 00:17:06,480 more steps until we eventually get to the primitives, okay? 334 00:17:06,480 --> 00:17:10,259 So we start at the high level and we break things down. And the notion of 335 00:17:10,259 --> 00:17:13,779 breaking things down into their sub-pieces 336 00:17:13,779 --> 00:17:17,399 is something that we refer to as decomposition. 337 00:17:17,400 --> 00:17:22,659 Decomposition. Which just means taking something big and breaking it down into 338 00:17:22,659 --> 00:17:25,110 smaller pieces, which is something you should do with your program. You wanna 339 00:17:25,109 --> 00:17:27,449 break them down into smaller pieces, okay? 340 00:17:27,450 --> 00:17:28,980 And this whole process, 341 00:17:28,980 --> 00:17:32,620 the whole process itself is something we refer to as top-down 342 00:17:32,619 --> 00:17:37,139 design. And you'll hear this phrase referred to a lot, and what top-down design 343 00:17:37,140 --> 00:17:39,630 is is you start at the highest level, the top, 344 00:17:39,630 --> 00:17:41,120 and you go down from there. 345 00:17:41,119 --> 00:17:45,379 And that's to be contrasted with something that's referred to as bottom-up 346 00:17:45,380 --> 00:17:46,630 design. 347 00:17:46,630 --> 00:17:50,220 And what bottom-up design says is hey, I need to go do something in the 348 00:17:50,220 --> 00:17:52,799 world. Well, let me start at the primitives. I need to make Karel, 349 00:17:52,799 --> 00:17:55,960 you know, go to the end of the wall and put down beepers. What's the first thing I 350 00:17:55,960 --> 00:17:59,850 need to do? I need to put a beeper, and then I need to move. And it starts with the 351 00:17:59,849 --> 00:18:00,998 low-level stuff 352 00:18:00,999 --> 00:18:04,298 and this is actually the place most programmers, when they start out, 353 00:18:04,298 --> 00:18:07,119 this is what they begin doing is bottom-up design. People have actually done 354 00:18:07,119 --> 00:18:11,019 psychological studies. It takes about 100 hours of programming proficiency. 355 00:18:11,019 --> 00:18:14,288 In the average case, I think everyone at Stanford's above average so it'll take you 356 00:18:14,288 --> 00:18:18,528 less, to go from thinking bottom-up to thinking top-down. Guess what? It's a 10-week 357 00:18:18,528 --> 00:18:22,019 quarter. We might do about 100 hours of programming in here. Hopefully 358 00:18:22,019 --> 00:18:25,038 by the end of this class, everyone's doing top-down design. But if you can 359 00:18:25,038 --> 00:18:27,378 start from the very beginning doing top-down design, 360 00:18:27,378 --> 00:18:28,199 you're golden. 361 00:18:28,200 --> 00:18:30,259 In some cases this makes sense, 362 00:18:30,259 --> 00:18:33,808 but a lot of times this just makes your life easier, to think about the most 363 00:18:33,808 --> 00:18:36,048 abstract level and break down from there. 364 00:18:36,048 --> 00:18:38,058 And so if you think about what we did 365 00:18:38,058 --> 00:18:40,500 when we actually did the steeplechase program, 366 00:18:40,500 --> 00:18:43,970 this was top-down design, right? When we actually wrote this program together, 367 00:18:43,970 --> 00:18:45,440 when we went through it together, 368 00:18:45,440 --> 00:18:48,940 we didnft talk about Karel doing individual moves to begin with. We said 369 00:18:48,940 --> 00:18:50,710 hey, we need to run a steeplechase, 370 00:18:50,710 --> 00:18:53,129 and that either involves doing the move 371 00:18:53,128 --> 00:18:54,519 or jumping a hurdle. 372 00:18:54,519 --> 00:18:57,789 At this point, we've done enough step-wise refinement that we've gotten to the 373 00:18:57,789 --> 00:19:01,200 level of a primitive. We don't need to go any further because Karel immediately 374 00:19:01,200 --> 00:19:02,370 understands move. 375 00:19:02,369 --> 00:19:04,278 Jump hurdle, Karel doesnft understand. 376 00:19:04,278 --> 00:19:08,048 So once we've written this, we need to break it down into lower-level steps and say 377 00:19:08,048 --> 00:19:09,750 what does jump hurdle involve? 378 00:19:09,750 --> 00:19:12,558 And that's where we actually go down and define jump hurdle, 379 00:19:12,558 --> 00:19:15,839 and guess what? When we define jump hurdle, there might be some other things 380 00:19:15,839 --> 00:19:19,139 like ascending and descending a hurdle, which are the next steps down in the process 381 00:19:19,140 --> 00:19:21,390 that we need to define, and we just keep doing this, 382 00:19:21,390 --> 00:19:22,038 okay? 383 00:19:22,038 --> 00:19:25,650 So with that said, let's actually do top-down design together. Let's start 384 00:19:25,650 --> 00:19:29,300 another program from a blank slate and see if we can do top-down design together 385 00:19:29,299 --> 00:19:32,759 to see how this process might actually go for writing code, okay? 386 00:19:32,759 --> 00:19:36,059 So the problem that we wanna try to solve 387 00:19:36,059 --> 00:19:39,049 is we wanna - oh, and don't look at the code - we wanna try to get - and this is kind 388 00:19:39,049 --> 00:19:43,200 of a funky problem - it's Karel does math. If you didnft think Karel could do math, 389 00:19:43,200 --> 00:19:46,920 in fact Karel can do math. Karel's a lot smarter than we sometimes give him credit 390 00:19:46,920 --> 00:19:47,750 for. 391 00:19:47,750 --> 00:19:50,480 So this is a little problem called double beepers. 392 00:19:50,480 --> 00:19:53,089 So I don't know if you can see the number here, but there was a five here, 393 00:19:53,089 --> 00:19:55,619 which means there was five beepers on this corner. 394 00:19:55,619 --> 00:19:59,678 And when this program is done running, what we want to have happen is Karel 395 00:19:59,679 --> 00:20:03,200 is guaranteed to start in position 1-1 and there's guaranteed to be a pile of beepers 396 00:20:03,200 --> 00:20:05,720 in front of him and a space after him, okay? 397 00:20:05,720 --> 00:20:09,150 When he's done, he should return back to the same position and the pile of beepers 398 00:20:09,150 --> 00:20:13,190 should have exactly twice as many beepers on it as it had before, which 399 00:20:13,190 --> 00:20:17,360 means Karel begins with a beeper bag that's full of as many beepers as he 400 00:20:17,359 --> 00:20:20,250 needs, so it has infinite beepers in there so we can double the number of beepers. 401 00:20:20,250 --> 00:20:24,299 So if I speed up the program and just run it, you can see the final state. So let me 402 00:20:24,299 --> 00:20:26,529 just crank up the speed here 403 00:20:26,529 --> 00:20:27,918 and start the program, 404 00:20:27,919 --> 00:20:31,639 and it looks like nothing happened, but in fact this number changed from a five to 405 00:20:31,638 --> 00:20:33,279 a 10. It doubled the number of beepers. 406 00:20:33,279 --> 00:20:37,379 So two things should be going off in your head right now - one is thinking hey, 407 00:20:37,380 --> 00:20:38,530 that's kind of slick. 408 00:20:38,529 --> 00:20:41,648 How do I do that? What's the recipe for doing that? 409 00:20:41,648 --> 00:20:45,319 And the recipe for doing that, the sort of general approach you wanna 410 00:20:45,319 --> 00:20:48,269 take, is something that we call an algorithm, okay? 411 00:20:48,269 --> 00:20:53,519 So the notion of an algorithm - and this is just a fancy computer science term 412 00:20:53,519 --> 00:20:54,269 for 413 00:20:54,269 --> 00:20:58,869 an approach to something - actually comes from - anywhere know where the word 414 00:20:58,869 --> 00:21:02,629 algorithm comes from? 415 00:21:02,630 --> 00:21:04,530 416 00:21:04,529 --> 00:21:08,299 Nineteenth century Persian mathematician named, if I can pronounce it right, 417 00:21:08,299 --> 00:21:09,319 Al-Khwarizmi. 418 00:21:09,319 --> 00:21:11,889 419 00:21:11,890 --> 00:21:14,970 Al-Khwarizmi, 420 00:21:14,970 --> 00:21:18,569 which sounds like algorithm. There you go. 421 00:21:18,569 --> 00:21:21,470 422 00:21:21,470 --> 00:21:25,309 Yeah, add 1100 years to that pronunciation and it sounds like algorithm, all right? Because this is 423 00:21:25,309 --> 00:21:27,929 from the 19th century. It's your basic approach. As a matter of fact, there 424 00:21:27,930 --> 00:21:31,259 was a band at Stanford years and years ago called the Algorithms, where 425 00:21:31,259 --> 00:21:34,539 this was, like, rhythm, like hey, I got rhythm. Yeah. 426 00:21:34,539 --> 00:21:36,639 I won't say how 427 00:21:36,640 --> 00:21:39,220 good they were. Not very. All right. So 428 00:21:39,220 --> 00:21:42,509 you wanna think about your general approach and then you turn your general 429 00:21:42,509 --> 00:21:44,200 approach into 430 00:21:44,200 --> 00:21:47,279 thinking in terms of step-wise refinement. How are you gonna solve this problem 431 00:21:47,279 --> 00:21:51,410 from the top down? So let's actually start with a blank slate. Let's close this puppy out 432 00:21:51,410 --> 00:21:53,179 and we'll start with a blank slate, 433 00:21:53,179 --> 00:21:55,990 which we'll call, oh, Our Double Beepers. 434 00:21:55,990 --> 00:21:59,120 So I just gave you the boilerplate stuff to begin with. There's a little comment 435 00:21:59,119 --> 00:22:00,149 up at the top 436 00:22:00,150 --> 00:22:03,840 that says file double beepers. That should be called Our Double Beepers, 437 00:22:03,839 --> 00:22:06,230 actually, and Karel doubles the number of beepers on the corner directly in 438 00:22:06,230 --> 00:22:09,669 front of him in the world, he returns to his original position and orientation. 439 00:22:09,669 --> 00:22:13,730 So we have the public class stuff for Our Double Beepers, we have an [inaudible] super 440 00:22:13,730 --> 00:22:16,440 Karel. Now we're gonna run the run method, okay? 441 00:22:16,440 --> 00:22:19,980 So what do we wanna do to get Karel to double the number of beepers at a high 442 00:22:19,980 --> 00:22:26,980 level? What might we think about doing this? Hm. Um-hm? [Inaudible.] 443 00:22:31,579 --> 00:22:34,509 We need to figure out how many beepers there are. In order to do that, maybe we 444 00:22:34,509 --> 00:22:37,450 should move to the pile of beepers, right? It's one avenue in front of us. So we 445 00:22:37,450 --> 00:22:38,690 do a move. 446 00:22:38,690 --> 00:22:42,000 And now here's the part that almost seems like magic. I'm going to say move, hey, you're on this 447 00:22:42,000 --> 00:22:43,490 pile of beepers now. Why 448 00:22:43,490 --> 00:22:47,630 don't you, hey, double the beepers in the pile? 449 00:22:47,630 --> 00:22:49,680 And you're like, uh, [inaudible] can I do that? Sure, 450 00:22:49,680 --> 00:22:51,090 why not, 451 00:22:51,089 --> 00:22:53,889 right? You're gonna write the program, you can do whatever you want. So 452 00:22:53,890 --> 00:22:57,920 I would like to say move, double the beepers in the pile, and then actually 453 00:22:57,920 --> 00:23:01,380 what I wanna do is not move forward again but I wanna move backward, 454 00:23:01,380 --> 00:23:04,600 right? I wanna move - almost like Karel could go in reverse, that'd be kind 455 00:23:04,599 --> 00:23:08,569 of cool, to keep the same orientation but goes back. And you're like but [inaudible] you 456 00:23:08,569 --> 00:23:12,919 didnft tell me about move backward. Can super Karel do move backward? And I'm like, no. And you're 457 00:23:12,920 --> 00:23:16,990 like oh, okay, well, all right, well, I guess I need to write some of these, right? Let's 458 00:23:16,990 --> 00:23:19,420 actually do move backward, because that's the easier one. 459 00:23:19,420 --> 00:23:22,220 If I'm sitting on a particular corner and I'm facing east, 460 00:23:22,220 --> 00:23:25,150 what do I need to do to move backward one step 461 00:23:25,150 --> 00:23:27,850 and face the same direction? 462 00:23:27,849 --> 00:23:30,689 Yeah, I need - so let me define move backward here. 463 00:23:30,690 --> 00:23:34,960 Move backward. I need to turn around, which is a primitive that I can use. Then 464 00:23:34,960 --> 00:23:36,970 what do I do? Move. 465 00:23:36,970 --> 00:23:38,230 And then what do I do? 466 00:23:38,230 --> 00:23:41,960 I turn around again so I have the same orientation when I'm done as when I 467 00:23:41,960 --> 00:23:42,569 started. 468 00:23:42,569 --> 00:23:45,888 And now, guess what? Move backward is something I understand how to do, because 469 00:23:45,888 --> 00:23:49,498 I've broken it all down into primitives. I don't need to go any further in terms of 470 00:23:49,499 --> 00:23:52,470 the refinement for move backwards. 471 00:23:52,470 --> 00:23:55,940 But now there's the magical part, right? There's double beepers in pile, 472 00:23:55,940 --> 00:23:58,509 and that's something that might be a little bit more involved. So I come along and say 473 00:23:58,509 --> 00:24:00,240 okay, I need to define 474 00:24:00,240 --> 00:24:02,940 what double beepers in pile is. Double 475 00:24:02,940 --> 00:24:04,730 beepers in pile. 476 00:24:04,730 --> 00:24:07,579 At this point, I need to think about my algorithm. 477 00:24:07,579 --> 00:24:10,189 What's going to be my approach for doubling the number of beepers? What's 478 00:24:10,190 --> 00:24:13,528 a way that I could possibly think about that problem? Because Karel can't count, right? 479 00:24:13,528 --> 00:24:14,909 There is no way for Karel to just say 480 00:24:14,909 --> 00:24:17,260 hey, counting how many beepers are on this corner. 481 00:24:17,259 --> 00:24:24,259 What's one way he might approach it? Um-hm? [Inaudible.] 482 00:24:26,380 --> 00:24:31,200 That would be nice if we had a counter, but we don't have a counter, right? 483 00:24:31,200 --> 00:24:33,088 So Karel can't count. 484 00:24:33,088 --> 00:24:35,529 How is he potentially gonna do it? Um-hm, all the way in the back. 485 00:24:35,529 --> 00:24:39,559 Use your microphone, 486 00:24:39,559 --> 00:24:44,309 please. So he needs to pick up one beeper at a time and put it on the empty corner, and when he puts it on an empty corner, put two beepers for 487 00:24:44,309 --> 00:24:48,919 that one he puts down? Yeah. 488 00:24:48,920 --> 00:24:49,670 So why don't we - 489 00:24:49,670 --> 00:24:51,560 I know it's not gonna happen. 490 00:24:51,559 --> 00:24:54,509 Yeah, I know, everyone's just starting to cower now. I do realize that, I've been teaching for 491 00:24:54,509 --> 00:24:58,430 about 15 years and my shot has never improved, so you're just, you know, at 492 00:24:58,430 --> 00:25:00,670 the random candy mercy. 493 00:25:00,670 --> 00:25:03,950 Since we can't count, we need to do something beeper by beeper. So wouldnft it 494 00:25:03,950 --> 00:25:06,930 be interesting if we said hey, you're on that pile of beepers, 495 00:25:06,930 --> 00:25:08,929 pick up one beeper off the pile 496 00:25:08,929 --> 00:25:12,120 and take a step somewhere else and put two beepers down, 497 00:25:12,119 --> 00:25:15,649 and keep doing that over and over. Pick up a beeper, put two down. And eventually, 498 00:25:15,650 --> 00:25:18,109 you'll exhaust everything on this pile over here 499 00:25:18,108 --> 00:25:21,269 but you'll have twice as many on the next pile beside you. 500 00:25:21,269 --> 00:25:24,849 And then take that pile of twice as many beepers and move them all back to the 501 00:25:24,849 --> 00:25:25,959 place where you started, 502 00:25:25,960 --> 00:25:27,209 and then you're done. 503 00:25:27,209 --> 00:25:31,308 That's the algorithm. Let's write the code to do that. And you're like okay, [inaudible] let's write the code 504 00:25:31,308 --> 00:25:32,470 to do that. 505 00:25:32,470 --> 00:25:34,610 And I'm like yeah, rock on, this is gonna be easy, right? 506 00:25:34,609 --> 00:25:37,759 What we're gonna do is we're just gonna say you're on a pile of beepers. As 507 00:25:37,759 --> 00:25:40,849 long as there are still beepers for you to pick up - so I'm gonna have a while loop 508 00:25:40,849 --> 00:25:41,678 here - 509 00:25:41,679 --> 00:25:43,590 while beepers 510 00:25:43,589 --> 00:25:45,179 present. 511 00:25:45,180 --> 00:25:48,450 Well, what I want you to do is pick up a beeper, yeah, you know how to do that - life 512 00:25:48,450 --> 00:25:49,580 is good. 513 00:25:49,579 --> 00:25:51,329 Beeper, I always misspell beeper, I don't know why. 514 00:25:51,329 --> 00:25:54,169 I want you to put two beepers next door - 515 00:25:54,170 --> 00:25:59,350 put two beepers next door - and you're like oh, this is always the part, right, where you're 516 00:25:59,349 --> 00:26:02,740 thinking bottom-up design, you get uncomfortable 517 00:26:02,740 --> 00:26:06,390 calling methods that don't exist, right? You're just like oh, but that doesnft exist, right? And 518 00:26:06,390 --> 00:26:09,030 I'm like, it's cool, man, we're gonna write it. 519 00:26:09,029 --> 00:26:13,009 And then we hold hands and we sing Kumbaya and everything's good because at the end, 520 00:26:13,009 --> 00:26:15,990 Karel's gonna know what to do. So sometimes, you just write methods 521 00:26:15,990 --> 00:26:18,929 and they don't exist, and it's fine because you're the one that's gonna be 522 00:26:18,929 --> 00:26:21,809 writing them. So eventually, they'll exist, right? It's not like you're sitting there, 523 00:26:21,808 --> 00:26:25,210 like, lighting candles, making some magic incantation, hoping the code just shows 524 00:26:25,210 --> 00:26:27,890 up. And you're like, is it there yet? No, it's not there yet, 525 00:26:27,890 --> 00:26:30,890 right? You're gonna write it. I had a friend once who actually thought that's how we did 526 00:26:30,890 --> 00:26:34,259 computer science. I didnft bother to tell him that that wasn't true. 527 00:26:34,259 --> 00:26:37,359 But, you know, think about it. So we pick a beeper - yeah, I know, it's kind of mean, 528 00:26:37,359 --> 00:26:40,859 but that's all right. He does government now. 529 00:26:40,859 --> 00:26:43,758 We pick up two beepers and guess what? After we do that, 530 00:26:43,759 --> 00:26:47,389 when that's done we will have picked up all the beepers from that pile that 531 00:26:47,388 --> 00:26:50,689 we're standing on, and we would have put two next door for every beeper we picked 532 00:26:50,690 --> 00:26:51,370 up. 533 00:26:51,369 --> 00:26:54,369 So when we're done with this while loop, we know that the thing that's true 534 00:26:54,369 --> 00:26:57,859 is that there's no more beepers on that pile because beepers present is no 535 00:26:57,859 --> 00:26:58,908 longer true. 536 00:26:58,909 --> 00:27:02,549 So I have some pile next to me, say on the next corner, that has twice as many 537 00:27:02,548 --> 00:27:05,710 beepers on it. And so what do I wanna do? I just wanna move 538 00:27:05,710 --> 00:27:10,519 the pile next door back. 539 00:27:10,519 --> 00:27:11,849 And that's my function. You're like, oh, really? 540 00:27:11,849 --> 00:27:13,918 I can do that? Sure, why not? 541 00:27:13,919 --> 00:27:16,210 So what do we need to write now? Well, we need to 542 00:27:16,210 --> 00:27:20,200 know how to put two beepers next door. So let's go ahead and write that. 543 00:27:20,200 --> 00:27:24,340 [Inaudible] private void, put two 544 00:27:24,339 --> 00:27:25,509 beepers 545 00:27:25,509 --> 00:27:27,548 next door, right? 546 00:27:27,548 --> 00:27:30,750 Now if I'm standing on a particular corner and I'm facing east, let's say I wanna 547 00:27:30,750 --> 00:27:33,929 put two beepers on the space in front of me and come back to where I am 548 00:27:33,929 --> 00:27:35,340 still facing east, 549 00:27:35,339 --> 00:27:39,689 so I'm left in the same position I was when I started, how do I do that? 550 00:27:39,690 --> 00:27:42,539 I move. 551 00:27:42,539 --> 00:27:46,279 I need to put two beepers so I could put beeper, put beeper. And if you really 552 00:27:46,279 --> 00:27:49,490 wanted to you could put a four-loop here and say hey, [inaudible] you're doing something twice, ooh, 553 00:27:49,490 --> 00:27:54,529 ooh, can I do a four-loop and do four I equal into I equals, zero, I less than two, I++, 554 00:27:54,529 --> 00:27:56,369 put beeper? 555 00:27:56,369 --> 00:27:59,429 Sure, right? There's multiple ways of writing a program. I'm just going to put two put beepers because it's 556 00:27:59,430 --> 00:28:01,900 two lines. The four-loop would have been two lines too. 557 00:28:01,900 --> 00:28:04,450 Now what do I do? 558 00:28:04,450 --> 00:28:08,549 Ah, yeah, rock on. I move backward, right? Which allows me to move back one step 559 00:28:08,549 --> 00:28:10,349 and keep the same orientation. 560 00:28:10,349 --> 00:28:14,928 So I end up, my post-condition is essentially the same as my pre-condition, 561 00:28:14,929 --> 00:28:20,350 except I've moved one beeper over to the - or I put two beepers onto the next spot. And 562 00:28:20,349 --> 00:28:22,730 move backward, you're like hey, I already wrote that. 563 00:28:22,730 --> 00:28:25,950 So everything now is in terms of instructions that Karel will understand, so at 564 00:28:25,950 --> 00:28:27,630 this point I don't need to go any further. 565 00:28:27,630 --> 00:28:31,000 So I know how to put two beepers next door, so the only thing left to do in this 566 00:28:31,000 --> 00:28:31,369 program 567 00:28:31,369 --> 00:28:35,428 is to move the pile next door back to where I am now, okay? 568 00:28:35,429 --> 00:28:39,580 So what that means is how do I move that pile back to where I am? 569 00:28:39,579 --> 00:28:40,978 So I'll call this, 570 00:28:40,979 --> 00:28:45,649 for lack of - well, I know what the function name is, I'll just - or the method name. I'll 571 00:28:45,648 --> 00:28:48,899 just steal it from up here, because sometimes you just get tired of typing. All 572 00:28:48,900 --> 00:28:51,680 right, so I'll move the pile next door back. Well, 573 00:28:51,680 --> 00:28:56,190 the place that I call this, right, I call this up here. I'm not actually on the 574 00:28:56,190 --> 00:29:00,009 pile next door when I called it, right? My pre-condition is I am on the avenue before 575 00:29:00,009 --> 00:29:01,788 the pile. I'm facing the pile, 576 00:29:01,788 --> 00:29:04,759 but I'm not on the pile. So the first thing I need to do 577 00:29:04,759 --> 00:29:06,058 in terms of moving the 578 00:29:06,058 --> 00:29:07,389 pile next door back 579 00:29:07,390 --> 00:29:10,009 is I need to move over to where that pile is. 580 00:29:10,009 --> 00:29:13,279 So now I'm standing on that pile of twice as many beepers, and 581 00:29:13,279 --> 00:29:16,349 I wanna move all the beepers back one. So 582 00:29:16,349 --> 00:29:18,609 what could I do? 583 00:29:18,609 --> 00:29:22,750 I could pick beeper, but there's something I wanna do multiple times. 584 00:29:22,750 --> 00:29:26,049 I need to have a wild loop, right? And I'm gonna do something - what condition 585 00:29:26,049 --> 00:29:28,319 do I wanna test, right? 586 00:29:28,319 --> 00:29:31,779 If beeper is present, right? So as long as there's beepers present there's still more 587 00:29:31,779 --> 00:29:34,908 work for me to do because there's still more beepers I need to move back. 588 00:29:34,909 --> 00:29:36,790 So while there's beepers present, 589 00:29:36,789 --> 00:29:40,649 hey, why don't I move one beeper back? Move one 590 00:29:40,650 --> 00:29:43,809 beeper back. 591 00:29:43,808 --> 00:29:47,539 All right? Again, I haven't wrote that yet, that's fine. But when this loop is done, 592 00:29:47,539 --> 00:29:50,950 what I know is I've exhausted all of the beepers that are on this pile because every 593 00:29:50,950 --> 00:29:54,130 time there was beeper present I picked it up, I moved it back one spot, 594 00:29:54,130 --> 00:29:57,400 and hopefully that method brought me back, should bring me back to the place that 595 00:29:57,400 --> 00:29:59,269 I'm standing right now, and I just keep doing this 596 00:29:59,269 --> 00:30:01,079 until I've transferred all the beepers. 597 00:30:01,079 --> 00:30:03,809 So after I've transferred all the beepers, what do I need to do to satisfy 598 00:30:03,809 --> 00:30:07,289 my post-condition, which is I wanna go back to the place I just transferred all 599 00:30:07,289 --> 00:30:08,898 the beepers? 600 00:30:08,898 --> 00:30:10,719 I need to move 601 00:30:10,720 --> 00:30:11,240 backward. 602 00:30:11,240 --> 00:30:14,630 Because all the beepers were transferred one avenue back. 603 00:30:14,630 --> 00:30:18,049 So I just move backward to go back one avenue, okay? 604 00:30:18,048 --> 00:30:19,160 Now 605 00:30:19,160 --> 00:30:24,470 I'm almost done. I'm almost feeling good, but not quite. I need to do the move 606 00:30:24,470 --> 00:30:25,360 one 607 00:30:25,359 --> 00:30:26,799 beeper back. 608 00:30:26,799 --> 00:30:29,869 And at this point, I've gotten to such a simple level that maybe I could just do 609 00:30:29,869 --> 00:30:33,339 this. So if I'm standing on a pile of beepers facing east, how do I 610 00:30:33,339 --> 00:30:36,129 move one beeper back behind me, 611 00:30:36,130 --> 00:30:40,470 put it down, and come back to the spot that I'm at? 612 00:30:40,470 --> 00:30:43,950 Well, first let me pick a beeper, right? Because I need to actually move the beeper. I 613 00:30:43,950 --> 00:30:46,830 could turn around first, that's perfectly fine. There's multiple ways I can do things, 614 00:30:46,829 --> 00:30:50,058 but I'm gonna pick the beeper first. Then what do I 615 00:30:50,058 --> 00:30:53,250 do? Yeah, rather than turning around, let me just use move backward. That'll take me 616 00:30:53,250 --> 00:30:54,859 back one spot. 617 00:30:54,859 --> 00:30:56,449 Then I 618 00:30:56,450 --> 00:30:58,120 put beeper. 619 00:30:58,119 --> 00:30:59,589 And I 620 00:30:59,589 --> 00:31:02,038 move to move back to the spot that I'm at, 621 00:31:02,038 --> 00:31:05,759 right? Now I'm all in terms of things, what I refer to as primitives, 622 00:31:05,759 --> 00:31:06,220 right? 623 00:31:06,220 --> 00:31:08,740 Or instructions that I've previously defined. 624 00:31:08,740 --> 00:31:11,569 And there's nothing more for me to define, because every time I define 625 00:31:11,569 --> 00:31:13,490 some new method and I said hey, I'm just gonna call that, 626 00:31:13,490 --> 00:31:16,950 I went and wrote it and I just kept doing that over and over until I'd written 627 00:31:16,950 --> 00:31:23,950 everything I needed to do. Um-hm? Wouldn't you have to move backward twice [inaudible]? 628 00:31:28,039 --> 00:31:30,128 On move pile next door back? 629 00:31:30,128 --> 00:31:32,230 No, because move one beeper back, 630 00:31:32,230 --> 00:31:36,009 I pick up a beeper, I move backward, I drop it off, and I move forward. So I'm on 631 00:31:36,009 --> 00:31:40,879 the pile that has all the beepers. [Inaudible.] 632 00:31:40,880 --> 00:31:44,669 Yeah, and that's what I did up here, right? So after I doubled the beeper pile, then I 633 00:31:44,669 --> 00:31:48,059 moved backwards. So yeah, that's a good point, but you always wanna remember what 634 00:31:48,058 --> 00:31:51,148 other things are gonna happen after you execute all that, okay? 635 00:31:51,148 --> 00:31:58,148 And so now the question comes - ah? [Inaudible.] 636 00:32:01,660 --> 00:32:04,800 That's a good question. The only problem with doing that is Karel starts 637 00:32:04,799 --> 00:32:07,428 off with an infinite number of beepers in his bag, right? 638 00:32:07,429 --> 00:32:10,620 So if I pick up all five beepers at the corner that I'm at and then I take 639 00:32:10,619 --> 00:32:14,359 a step back, or I pick up all five beepers and now I wanna put them all down 640 00:32:14,359 --> 00:32:15,189 and double them, 641 00:32:15,190 --> 00:32:17,990 I don't know that there were five anymore, because after I pick up the five, I look 642 00:32:17,990 --> 00:32:19,650 in my beeper bag and I'm like 643 00:32:19,650 --> 00:32:22,440 oh, man, I had infinitely many beepers. I've got 644 00:32:22,440 --> 00:32:25,960 infinity plus five, and that looks like infinity, right? As a matter of fact, if you 645 00:32:25,960 --> 00:32:28,600 take CS103, which I encourage you to do because I teach it in the 646 00:32:28,599 --> 00:32:32,289 winter eventually, we talk about infinities, and we're like oh, infinities, and it all comes back 647 00:32:32,289 --> 00:32:34,819 to Karel. No, it actually doesnft come back to Karel at all. 648 00:32:34,819 --> 00:32:37,509 But infinity plus any number is 649 00:32:37,509 --> 00:32:40,529 within reason - yeah, if you're a number theorist, you're gonna argue about that - is 650 00:32:40,529 --> 00:32:41,549 still infinity. 651 00:32:41,549 --> 00:32:43,460 So that's why Karel can't count. 652 00:32:43,460 --> 00:32:47,519 If he had an empty beeper bag we could put all five in it and look inside and then we 653 00:32:47,519 --> 00:32:50,408 could put down five. But now we don't have five more beepers to double the number 654 00:32:50,409 --> 00:32:53,419 of beepers, which is why Karel had to start with infinitely many beepers, 655 00:32:53,419 --> 00:32:56,230 so no matter how many were in the initial pile he would always have enough 656 00:32:56,230 --> 00:32:57,190 beepers to double it. 657 00:32:57,190 --> 00:32:58,179 But that's a good point. 658 00:32:58,179 --> 00:33:01,419 So let's actually go ahead and run our program now. Let's save, and we'll go 659 00:33:01,419 --> 00:33:03,910 ahead and run Our Double Beepers. 660 00:33:03,910 --> 00:33:05,850 Doo, doo, doo, doo, doo. 661 00:33:05,849 --> 00:33:08,648 Operate just to make sure it actually works. 662 00:33:08,648 --> 00:33:11,819 And we'll sort of do it at slow speed. 663 00:33:11,819 --> 00:33:14,558 So there's our world, let's start the program. There's Karel, 664 00:33:14,558 --> 00:33:19,680 and you can see he's going [inaudible] beeper - whoa. He 665 00:33:19,680 --> 00:33:25,160 just didnft want me to explain that program at all. All right, Karel, I'll remember this. 666 00:33:25,160 --> 00:33:30,130 So for every beeper he picked up, he put two here, and now he's just transferring them all back. 667 00:33:30,130 --> 00:33:31,370 Go, Karel. 668 00:33:31,369 --> 00:33:35,919 And so now he's done with 10, and you'll notice he went back here after 10. Why? Because he 669 00:33:35,920 --> 00:33:38,700 needed to check to see if there were any more here, and there werenft, so he moved 670 00:33:38,700 --> 00:33:39,460 back 671 00:33:39,460 --> 00:33:40,480 and then he was done. 672 00:33:40,480 --> 00:33:43,710 So we just doubled the number of beepers. And this program, actually, notice the 673 00:33:43,710 --> 00:33:45,179 initial state of the world 674 00:33:45,179 --> 00:33:48,278 is now exactly the same as the ending state of the world, except we 675 00:33:48,278 --> 00:33:50,788 doubled the number of beepers. So we could actually run this again if we wanted to - 676 00:33:50,788 --> 00:33:52,599 let me speed it up a little bit - 677 00:33:52,599 --> 00:33:54,178 and we'll go from 10 beepers to 20 beepers. And 678 00:33:54,179 --> 00:33:57,580 there is Karel sort of doing his thing at faster speed, but you can 679 00:33:57,579 --> 00:33:59,288 see there's 20 beepers over there, 680 00:33:59,288 --> 00:34:02,429 now they all start coming back and saying, you'll be like, hey, [inaudible], how many times can I run it? 681 00:34:02,429 --> 00:34:05,360 Can I just run it over and over and I'll get, like, you know, 40 beepers and 80 beepers 682 00:34:05,359 --> 00:34:06,538 and 160 beepers? 683 00:34:06,538 --> 00:34:09,489 You just can't. I think eventually the program dies when you reach, like, 100 684 00:34:09,489 --> 00:34:12,158 and some-odd thousand beepers, because it's just like no more, man. 685 00:34:12,159 --> 00:34:14,079 No more, all right? 686 00:34:14,079 --> 00:34:18,109 But for all programs you write in this class, if you ever have more than 100,000 687 00:34:18,108 --> 00:34:23,219 beepers on a corner, there's probably something wrong, so you don't need to worry about it, okay? Now, 688 00:34:23,219 --> 00:34:26,608 that's - so congratulations, right? We just all wrote this together, and 689 00:34:26,608 --> 00:34:29,460 you just made Karel do math. As a matter of fact, Karel, 690 00:34:29,460 --> 00:34:34,050 in terms of raw computational power, can solve any arithmetic problem that 691 00:34:34,050 --> 00:34:35,519 any other program can. 692 00:34:35,518 --> 00:34:38,488 It just may take him a lot of beepers to do it. But theoretically, you can prove that. 693 00:34:38,489 --> 00:34:39,739 We're not gonna do it in this class, 694 00:34:39,739 --> 00:34:43,148 but if you're just kind of - you're like can Karel compute, like, square root and logarithm 695 00:34:43,148 --> 00:34:45,838 and he can integrate? Yeah, he can. 696 00:34:45,838 --> 00:34:50,318 It's just a whole lot of beepers, all right? 697 00:34:50,318 --> 00:34:51,358 So 698 00:34:51,358 --> 00:34:53,268 I could show you another program. 699 00:34:53,268 --> 00:34:57,449 Here's another little program I happened to write in the office. 700 00:34:57,449 --> 00:35:01,778 This is a program that happens to be called Do Your Thing, okay? 701 00:35:01,778 --> 00:35:04,958 And I'll show you the full program, I'll scroll it down here so you can see the full body of the 702 00:35:04,958 --> 00:35:05,509 program. 703 00:35:05,509 --> 00:35:11,329 And all I will ask you, what does this program do? Doo, doo, doo, doo, doo, doo, doo, doo, 704 00:35:11,329 --> 00:35:13,189 doo, doo, doo, doo, 705 00:35:13,188 --> 00:35:15,608 doo. 706 00:35:15,608 --> 00:35:20,009 This does example the same thing the code you just wrote did, okay? 707 00:35:20,009 --> 00:35:22,409 This is really bad software engineering, 708 00:35:22,409 --> 00:35:25,939 right? There is no decomposition, there's no comments, there's no indentation, 709 00:35:25,938 --> 00:35:28,568 there's no notion of what's going on in this program. If you looked at this program and 710 00:35:28,568 --> 00:35:29,188 I said, 711 00:35:29,188 --> 00:35:32,188 hey, instead of doubling the number of beepers, I want you to say triple the 712 00:35:32,188 --> 00:35:35,139 number of beepers, you'd have to go through this thing, and you might, because 713 00:35:35,139 --> 00:35:38,708 you just wrote it, say hey, I know that there's two put beepers in a row, so I put 714 00:35:38,708 --> 00:35:43,138 another put beeper in there and it'll probably work, right? Whereas 715 00:35:43,139 --> 00:35:46,179 say you're a software engineer and you just got your first job, and you can look at either 716 00:35:46,179 --> 00:35:49,679 that code, or you can come over here and look at the double beepers that's actually 717 00:35:49,679 --> 00:35:50,969 in your assignment, 718 00:35:50,969 --> 00:35:55,009 that has all the comments, like, hey, this doubles the number of beepers. And over here, 719 00:35:55,009 --> 00:35:58,748 guess what? This is the version that's in your handout. This is all the same code we 720 00:35:58,748 --> 00:36:02,658 just wrote except now with comments. Like for every beeper on the current corner, 721 00:36:02,659 --> 00:36:05,499 Karel places two beepers on the corner immediately ahead of him, 722 00:36:05,498 --> 00:36:08,728 and this says place two beepers on corner one avenue ahead. And you say hey, 723 00:36:08,728 --> 00:36:10,759 if I wanna triple the number of beepers, I change the 724 00:36:10,759 --> 00:36:13,858 comment from two to three and put another put beeper here, and I know 725 00:36:13,858 --> 00:36:16,989 exactly where it goes and it's easy to understand. 726 00:36:16,989 --> 00:36:19,028 That's why software engineering's important, 727 00:36:19,028 --> 00:36:21,918 because that kind of modification to code happens 728 00:36:21,918 --> 00:36:23,048 far more often 729 00:36:23,048 --> 00:36:25,139 than writing code from scratch - 730 00:36:25,139 --> 00:36:26,998 about 10 times more often. 731 00:36:26,998 --> 00:36:31,398 So if the code is bad to begin with, it gets 10 times more costly to go and 732 00:36:31,398 --> 00:36:33,199 make fixes to it, and that's something that's 733 00:36:33,199 --> 00:36:36,509 sort of well documented. Some people actually argue that the number is higher. But that's why 734 00:36:36,510 --> 00:36:40,040 it's important that when you write your program the first time, or subsequent 735 00:36:40,039 --> 00:36:43,309 revisions you make to it, you have good style because that's just way more important than 736 00:36:43,309 --> 00:36:46,358 just getting the program working. And I've seen people actually 737 00:36:46,358 --> 00:36:47,799 turn in programs 738 00:36:47,800 --> 00:36:50,960 called Do Your Thing. That's where I actually got the name, was I actually knew someone once 739 00:36:50,960 --> 00:36:54,480 who write a program called Do Your Thing. And I was, like, youfve gotta be kidding, 740 00:36:54,480 --> 00:36:56,019 right? 741 00:36:56,018 --> 00:36:58,828 And it was just like this, right? It was just a whole bunch of straight-line code, 742 00:36:58,829 --> 00:37:02,380 no decomposition. They were like, well, it works. And I'm like yeah, 743 00:37:02,380 --> 00:37:05,818 but that's not what software engineering is about. And we got into this - yeah, I won't call it an 744 00:37:05,818 --> 00:37:07,219 argument, I'll call it a discussion. 745 00:37:07,219 --> 00:37:10,829 And they got really adamant, because they were, like, but it works, so I must get an A. 746 00:37:10,829 --> 00:37:12,250 And I was like, you know, 747 00:37:12,250 --> 00:37:15,349 someday if you're teaching this class and you're willing to accept code like this, 748 00:37:15,349 --> 00:37:17,239 you can give yourself an A. 749 00:37:17,239 --> 00:37:18,489 I'm not, 750 00:37:18,489 --> 00:37:19,378 because 751 00:37:19,378 --> 00:37:23,239 this is just - anyone who sees this, not only in this class but outside of this 752 00:37:23,239 --> 00:37:24,838 class, will be horrendous code, 753 00:37:24,838 --> 00:37:28,248 and I don't want any people coming out of this class to go out into the industry and be like, 754 00:37:28,248 --> 00:37:31,468 hey, yeah, this is what [inaudible] told me to do, that's why I'm writing it this way, right? 755 00:37:31,469 --> 00:37:34,739 Because not only will they be, like, no, I'm sorry, you're fired, but find that 756 00:37:34,739 --> 00:37:38,369 clown [inaudible] and fire him, too, right? So 757 00:37:38,369 --> 00:37:40,219 don't write code like this. Is 758 00:37:40,219 --> 00:37:44,309 there any questions about this? Um-hm? [Inaudible.] 759 00:37:44,309 --> 00:37:47,439 And let me bring up the good code again, just so you don't get, you know, think 760 00:37:47,438 --> 00:37:50,949 like that other code is the good thing to do. When you're naming 761 00:37:50,949 --> 00:37:53,259 the private [inaudible] methods, do 762 00:37:53,259 --> 00:37:56,039 you - should the first letter be capitalized, or not? 763 00:37:56,039 --> 00:37:57,239 Ah, good question. 764 00:37:57,239 --> 00:38:01,929 So in terms of - someone asked before, all the words or methods that we 765 00:38:01,929 --> 00:38:03,588 define are all case sensitive. 766 00:38:03,588 --> 00:38:05,538 You'll notice that 767 00:38:05,539 --> 00:38:09,028 sometimes we use a naming convention. I'm actually bad because I mixed up the naming 768 00:38:09,028 --> 00:38:10,659 convention a little bit where 769 00:38:10,659 --> 00:38:14,568 the first name of the methods, we can either have a lower-case character or an 770 00:38:14,568 --> 00:38:17,570 upper-case character, it's up to you how you want to do it. 771 00:38:17,570 --> 00:38:21,349 Just be consistent. Sometimes, this notation is referred to as camel case, 772 00:38:21,349 --> 00:38:25,619 and the reason why it's referred to as camel case, the first letter of every word that 773 00:38:25,619 --> 00:38:29,230 actually appears sort of all stuck together is upper cased, and it's kind of like 774 00:38:29,230 --> 00:38:32,849 a camel with humps, because every once in a while you get this capital letter with a hump. 775 00:38:32,849 --> 00:38:36,329 So that when you - if you hear someone refer to oh, we use capital case or camel 776 00:38:36,329 --> 00:38:39,670 case notation, that's what they mean. It's all strung together as one word 777 00:38:39,670 --> 00:38:41,240 with the first 778 00:38:41,239 --> 00:38:43,739 letter of each word capitalized. 779 00:38:43,739 --> 00:38:46,669 Sometimes what people will do is they'll actually put in underscores. You can put in 780 00:38:46,670 --> 00:38:50,150 underscores between words and that's perfectly fine, too. When we get into 781 00:38:50,150 --> 00:38:51,670 Java, I'll actually spent 782 00:38:51,670 --> 00:38:54,389 probably like half of a lecture talking about the different conventions 783 00:38:54,389 --> 00:38:57,859 for all kinds of things, because when we get into the Java world there's actually a bunch of things 784 00:38:57,860 --> 00:39:00,009 besides just method names you need to worry about, 785 00:39:00,009 --> 00:39:02,568 and we use all different sorts of conventions for them. For right now, I'd 786 00:39:02,568 --> 00:39:06,458 just say pick one convention and stick with it, and the convention you can use is, 787 00:39:06,458 --> 00:39:09,208 you know, the two most common ones are either camel case all the way 788 00:39:09,208 --> 00:39:13,778 through or camel case except for the first character, which is lower case. 789 00:39:13,778 --> 00:39:20,778 Any other questions? Um-hm? [Inaudible.] Instructor: 790 00:39:25,949 --> 00:39:29,909 Yeah, so it starts at run and every time it encounters some method that it doesnft 791 00:39:29,909 --> 00:39:32,959 know is a primitive method, it just says hey, did you define it somewhere? 792 00:39:32,958 --> 00:39:35,379 And it goes to that definition. And guess what? If it encounters one that you didnft 793 00:39:35,380 --> 00:39:37,108 define, that's an error. 794 00:39:37,108 --> 00:39:39,909 And usually it'll tell you that when you try to run the program. It'll tell you 795 00:39:39,909 --> 00:39:43,900 that this thing doesnft actually exist. All righty? 796 00:39:43,900 --> 00:39:50,318 Any other questions about - um-hm? [Inaudible] as to what order you should put your [inaudible]? 797 00:39:50,318 --> 00:39:53,179 No, you can put them in whatever order you want. Usually I just put them in the 798 00:39:53,179 --> 00:39:56,469 order as I need to define them, right? So just like we did in class, I just keep going 799 00:39:56,469 --> 00:39:59,450 down, which means run starts at the top, and everything comes underneath it. 800 00:39:59,449 --> 00:40:02,649 A couple things related to that. One question that always comes up is 801 00:40:02,650 --> 00:40:06,329 how do I know how much to decompose, right? Like aren't there different ways I 802 00:40:06,329 --> 00:40:09,639 could decompose? And in fact, yeah, there are multiple different ways you can decompose. 803 00:40:09,639 --> 00:40:12,619 So let me just give you some sort of quickie guidelines about how you might 804 00:40:12,619 --> 00:40:15,038 think about decomposing, okay? 805 00:40:15,039 --> 00:40:17,940 In terms of decomposition, the thing that you really wanna think about 806 00:40:17,940 --> 00:40:19,999 is every one of your methods 807 00:40:19,998 --> 00:40:23,248 should basically solve one problem, okay? The 808 00:40:23,248 --> 00:40:27,018 only thing is that that problem can be at different levels. So when you think 809 00:40:27,018 --> 00:40:29,959 about decomposition, 810 00:40:29,960 --> 00:40:31,760 what are some good guidelines? 811 00:40:31,760 --> 00:40:36,749 One guideline is think about each method solving one problem, 812 00:40:36,748 --> 00:40:40,108 okay? That problem can be high-level, like double the number of beepers . 813 00:40:40,108 --> 00:40:41,889 That's a high-level single problem. 814 00:40:41,889 --> 00:40:43,449 When you get into doubling 815 00:40:43,449 --> 00:40:46,309 the number of beepers, you don't just wanna say oh, double the number of beepers 816 00:40:46,309 --> 00:40:47,259 again, because you're 817 00:40:47,259 --> 00:40:50,708 already at that level of decomposition. You need to go one step down and say, 818 00:40:50,708 --> 00:40:53,908 what are the things that make sense in terms of solving one problem at a time 819 00:40:53,909 --> 00:40:54,979 to break it down? 820 00:40:54,978 --> 00:40:56,519 Now a lot of times, people wonder 821 00:40:56,519 --> 00:41:00,230 but [inaudible], what does that mean in terms of lines of code? Because sometimes it's 822 00:41:00,230 --> 00:41:03,369 different for me, at least conceptually, to think about one problem. And here's just a 823 00:41:03,369 --> 00:41:04,159 rough 824 00:41:04,159 --> 00:41:04,618 guideline. 825 00:41:04,619 --> 00:41:09,160 It's not a guideline that always holds, but a rough guideline is most of your methods 826 00:41:09,159 --> 00:41:15,848 will probably be somewhere between one and 15 lines long, okay? 827 00:41:15,849 --> 00:41:17,419 And you're like, one line? 828 00:41:17,418 --> 00:41:20,759 Yeah, that's perfectly fine. It's perfectly fine for you to have a method that's 829 00:41:20,759 --> 00:41:22,929 just one line long that calls some other method, 830 00:41:22,929 --> 00:41:24,589 and you might say what's the purpose of that? 831 00:41:24,590 --> 00:41:29,509 The purpose of that is giving that thing that you call a different name, right? So 832 00:41:29,509 --> 00:41:32,670 remember when we did ascend hurdle, which basically brought you all the way down until 833 00:41:32,670 --> 00:41:35,619 you were facing a wall, and then it did one last turn at the end to get you facing the right 834 00:41:35,619 --> 00:41:36,599 direction? 835 00:41:36,599 --> 00:41:39,989 But basically all it was doing was move to wall. We could have actually defined ascend - or 836 00:41:39,989 --> 00:41:41,429 sorry, descend hurdle 837 00:41:41,429 --> 00:41:44,858 to be move to wall and then do the turn afterwards. 838 00:41:44,858 --> 00:41:48,598 That would have been perfectly fine, too, and then descend hurdle would have just been a 839 00:41:48,599 --> 00:41:49,410 one-line method. 840 00:41:49,409 --> 00:41:52,949 But it would have changed the concept, the way the programmer thinks 841 00:41:52,949 --> 00:41:53,599 about it, 842 00:41:53,599 --> 00:41:55,438 from being hey, I'm moving to a wall 843 00:41:55,438 --> 00:41:57,848 to be hey, I'm descending a hurdle. 844 00:41:57,849 --> 00:42:01,039 So that's another thing that kind of comes up in terms of the methods is you wanna 845 00:42:01,039 --> 00:42:06,079 think about them as having good names. The names should describe what 846 00:42:06,079 --> 00:42:09,140 the method is actually doing, what problem it's solving, 847 00:42:09,139 --> 00:42:12,779 because you want your programs to read like good English, right? When 848 00:42:12,780 --> 00:42:16,019 someone - again, programs are written for people. 849 00:42:16,018 --> 00:42:18,628 Computers execute them, but they're really written for people to be able to 850 00:42:18,628 --> 00:42:20,079 go and look at and modify, 851 00:42:20,079 --> 00:42:23,889 so good names explains what the program actually does, and every one of the 852 00:42:23,889 --> 00:42:27,279 methods that you have in terms of decomposition should also have some 853 00:42:27,280 --> 00:42:28,540 comments associated with it 854 00:42:28,539 --> 00:42:32,820 that explains either at some more detailed level what the method is doing, or 855 00:42:32,820 --> 00:42:34,548 if the method is 856 00:42:34,548 --> 00:42:37,210 very short and it's fairly self-explanatory, 857 00:42:37,210 --> 00:42:39,969 pre-conditions and post-conditions are sometimes a good idea as well. 858 00:42:39,969 --> 00:42:43,009 So think about these things for your assignment. When you're actually writing 859 00:42:43,009 --> 00:42:46,889 the code for your assignment, you wanna think about the sort of decomposition using 860 00:42:46,889 --> 00:42:50,159 the step-wise refinement process and make sure to 861 00:42:50,159 --> 00:42:53,588 comment - both a comment for your whole program and a comment for each method. 862 00:42:53,588 --> 00:42:54,719 Um-hm, question? 863 00:42:54,719 --> 00:42:59,149 The book mentioned procedural programming and decomposition under 864 00:42:59,150 --> 00:43:03,349 that. Is that what we're doing right now, or are we doing the - Well, there's a difference between procedural 865 00:43:03,349 --> 00:43:07,140 and object-oriented programming. Decomposition actually applies to both of them, 866 00:43:07,139 --> 00:43:10,879 but what we are doing is decomposition whether it's procedural or object-oriented. 867 00:43:10,880 --> 00:43:11,919 868 00:43:11,918 --> 00:43:13,588 They're just two different - 869 00:43:13,588 --> 00:43:15,739 come up to me after class and I'll set you up. 870 00:43:15,739 --> 00:43:18,149 So I did wanna show you one more thing, 871 00:43:18,150 --> 00:43:21,709 and the one thing I wanna show you is yet another interesting problem, 872 00:43:21,708 --> 00:43:23,669 which is called Clean-Up Karel. 873 00:43:23,670 --> 00:43:27,329 And what Clean-Up Karel was basically gonna do is Karel starts off, it's kind of 874 00:43:27,329 --> 00:43:30,598 like after - we should call it After-Fraternity Party Karel. It could 875 00:43:30,599 --> 00:43:32,900 be After-Sorority Party Karle, too. 876 00:43:32,900 --> 00:43:36,000 I don't wanna make any kind of claims. But basically what ends up 877 00:43:36,000 --> 00:43:36,940 happening 878 00:43:36,940 --> 00:43:39,329 is that Karel kind of comes into his world 879 00:43:39,329 --> 00:43:40,719 and it's a mess. 880 00:43:40,719 --> 00:43:43,798 He's just like what happened? When last I was in this world I just doubled the number 881 00:43:43,798 --> 00:43:47,038 of beepers and shown you I can do math. And now you bring me into this world 882 00:43:47,039 --> 00:43:51,528 and there's just crap everywhere, right? And we could call him Angry Karel, too, right? 883 00:43:51,528 --> 00:43:53,190 He's the one who's gotta live here. And there's, 884 00:43:53,190 --> 00:43:56,400 like, cups strewn all around and they're just on random corners, and the things that 885 00:43:56,400 --> 00:44:00,719 you know about Karel in this world is he starts at location 1-1 886 00:44:00,719 --> 00:44:04,398 and every corner of the world can have at most one beeper on it. It means it can 887 00:44:04,398 --> 00:44:05,868 either be empty, 888 00:44:05,869 --> 00:44:08,818 like some of these corners, or have one beeper on them. And what Karel needs to 889 00:44:08,818 --> 00:44:11,058 do is he needs to go through his whole 890 00:44:11,059 --> 00:44:14,189 world and clean it up, which means he needs to gather up all the beepers that are 891 00:44:14,188 --> 00:44:14,808 out there. 892 00:44:14,809 --> 00:44:18,309 And he can end up wherever he wants to end up, okay? 893 00:44:18,309 --> 00:44:20,979 Now when we kind of think about a problem like this, one thing we might be 894 00:44:20,978 --> 00:44:22,179 inclined to do 895 00:44:22,179 --> 00:44:25,768 is you say hey, if there's a whole world that I wanna clean up, 896 00:44:25,768 --> 00:44:29,488 I might wanna break this problem down into a series of steps. 897 00:44:29,489 --> 00:44:32,798 One step - and something that is, like, oh, I can go and write it right now because I know how to 898 00:44:32,798 --> 00:44:35,838 write it is maybe to clean up one row at a time, right? 899 00:44:35,838 --> 00:44:38,429 Because I probably know how to clean up one row, I probably just wanna keep 900 00:44:38,429 --> 00:44:42,460 moving until I get to a wall, and if every time I move there happens to be a beeper 901 00:44:42,460 --> 00:44:45,778 there, just pick it up. And so you get all excited and you go and you write the 902 00:44:45,778 --> 00:44:47,708 code to do that, 903 00:44:47,708 --> 00:44:49,009 and so you start off 904 00:44:49,009 --> 00:44:51,858 by having something like this - let me 905 00:44:51,858 --> 00:44:54,199 just focus on the top code up here - 906 00:44:54,199 --> 00:44:54,998 right, which is 907 00:44:54,998 --> 00:44:57,688 hey, I wanna clean a row. This thing just 908 00:44:57,688 --> 00:44:59,908 does 909 00:44:59,909 --> 00:45:02,999 not wanna stay on me. It's like no, you will not talk into the microphone. 910 00:45:02,998 --> 00:45:06,438 So cleaning up a row I could say hey, you know, [inaudible] told me about the OBOB 911 00:45:06,438 --> 00:45:10,808 too, he told me about the off-by-one bug. So before I even do my first move, 912 00:45:10,809 --> 00:45:13,839 I'm gonna see if there's a beeper where I'm standing now, and if there is, I'm gonna 913 00:45:13,838 --> 00:45:14,750 pick it up. 914 00:45:14,750 --> 00:45:17,130 And then I'll check if my front is clear, 915 00:45:17,130 --> 00:45:18,690 and if it is I know I can move 916 00:45:18,690 --> 00:45:21,400 and I should check to see if there's a beeper there, and I'll just keep doing 917 00:45:21,400 --> 00:45:24,139 this over and over until I've picked up all the beepers. 918 00:45:24,139 --> 00:45:26,538 And that's a perfectly fine thing to do, 919 00:45:26,539 --> 00:45:29,759 right? So now youfve written some method called clean row that can clean up a 920 00:45:29,759 --> 00:45:33,318 whole row, and you know youfve sort of dealt with the off-by-one bug because 921 00:45:33,318 --> 00:45:37,509 you sort of do one extra thing outside of your loop, either before or after. In this 922 00:45:37,509 --> 00:45:40,630 case, you happened to do it before just so you can see a different kind of 923 00:45:40,630 --> 00:45:42,088 formulation of it than doing it after, 924 00:45:42,088 --> 00:45:43,139 and that's great. 925 00:45:43,139 --> 00:45:46,269 The only thing is, now you need to figure out where clean row is gonna go in 926 00:45:46,268 --> 00:45:47,389 your program, 927 00:45:47,389 --> 00:45:52,318 okay? This is an example of sort of doing more, the notion of bottom-up as opposed 928 00:45:52,318 --> 00:45:55,599 to top-down design. It's easy to get excited, to say oh, there's something I 929 00:45:55,599 --> 00:45:59,798 know how to do, and just go write it. And if you happen to write the right thing 930 00:45:59,798 --> 00:46:04,188 that fits cleanly into the rest of your program, that's great. But sometimes it 931 00:46:04,188 --> 00:46:07,558 doesnft, and you might need to go back and modify it, so keep that in mind. But if you 932 00:46:07,559 --> 00:46:10,600 do this, that's perfectly okay. It turns out that in this case, 933 00:46:10,599 --> 00:46:14,150 that's a fine thing to do, to clean one row at a time. And so if we wanna think 934 00:46:14,150 --> 00:46:17,389 about how that fits into the larger program, 935 00:46:17,389 --> 00:46:18,979 here's kind of a funky thing. 936 00:46:18,978 --> 00:46:22,398 And let me just explain the algorithm to you, because the algorithm here is somewhat 937 00:46:22,398 --> 00:46:25,719 related to an algorithm, for example, on one of your programming assignments. 938 00:46:25,719 --> 00:46:28,979 I'm gonna clean up the first row, because I know that first row I always need to 939 00:46:28,978 --> 00:46:31,288 clean. So I'll just go clean up the first row, 940 00:46:31,289 --> 00:46:33,819 and now I wanna check to see am I done. 941 00:46:33,818 --> 00:46:37,628 Well, if I've just cleaned up a row, the only way I know I'm done is if the 942 00:46:37,628 --> 00:46:41,418 ceiling's above me, right? I've reached the end of my world. And so I'm facing east, so I 943 00:46:41,418 --> 00:46:44,308 check my left to see if there's a ceiling up there. 944 00:46:44,309 --> 00:46:48,339 And if there's a ceiling up there, then I'm done. But if there isnft a ceiling up 945 00:46:48,338 --> 00:46:50,619 there, then I need to do more work. So 946 00:46:50,619 --> 00:46:54,419 while my left is clear, right, there's no ceiling up there, I'm gonna 947 00:46:54,418 --> 00:46:57,969 reposition for it to clean up a row to the west, because I just cleaned up a row sort 948 00:46:57,969 --> 00:46:59,420 of moving to the east - am 949 00:46:59,420 --> 00:47:00,999 I facing your east? Yeah. 950 00:47:00,998 --> 00:47:04,088 I just cleaned up a row to the east, and I wanna say hey, no ceiling here, that 951 00:47:04,088 --> 00:47:08,148 means there's another row to clean up here. So what I wanna do is move up one 952 00:47:08,148 --> 00:47:11,509 row to be able to now head west and clean up the next row, because I'm gonna 953 00:47:11,509 --> 00:47:15,148 go back and forth, that's my algorithm. So I'm just gonna write some 954 00:47:15,148 --> 00:47:18,438 function that says reposition for a row to the west, and go ahead and clean that 955 00:47:18,438 --> 00:47:22,489 row. Now what do I need to do 956 00:47:22,489 --> 00:47:23,079 again? 957 00:47:23,079 --> 00:47:24,890 I need to check my roof. 958 00:47:24,889 --> 00:47:28,929 But now my roof, because I'm not facing that way, isnft on my left 959 00:47:28,929 --> 00:47:32,659 anymore. I need to check my right to actually check my roof, so I say if my right is 960 00:47:32,659 --> 00:47:36,578 clear, then reposition for a row to the east. You're gonna go back the other way 961 00:47:36,579 --> 00:47:38,818 and clean that row, okay? 962 00:47:38,818 --> 00:47:43,759 Now there's this else here, and we'll get to the else in just a second, but just to 963 00:47:43,759 --> 00:47:47,248 figure out what reposition to west and east are, they're actually very simple. I'll 964 00:47:47,248 --> 00:47:49,828 just show you those down here. 965 00:47:49,829 --> 00:47:53,410 Reposition for row to west means I just finished a row to the east, how do I get 966 00:47:53,409 --> 00:47:56,149 up to harvest the next row or to clean up the next row? 967 00:47:56,150 --> 00:47:59,108 I do one turn left that gets me to face north, 968 00:47:59,108 --> 00:48:02,699 I move up one step so now imagine I've floated up one row, 969 00:48:02,699 --> 00:48:05,739 and then I do another turn left and I'm facing like this, and now I'm ready 970 00:48:05,739 --> 00:48:07,598 to plow over the other direction. 971 00:48:07,599 --> 00:48:11,109 And I do sort of the opposite for repositioning for a row to the 972 00:48:11,108 --> 00:48:12,119 east. So I'm standing here, 973 00:48:12,119 --> 00:48:16,108 I do a turn right, I look up, I move up one row, I do another turn right, and now 974 00:48:16,108 --> 00:48:17,719 I'm ready to go back that way. 975 00:48:17,719 --> 00:48:21,099 So these are very simple, right? They're in terms of primitives. 976 00:48:21,099 --> 00:48:24,139 So this is the whole program. Youfve just seen the whole program now. 977 00:48:24,139 --> 00:48:26,689 The only question that comes up 978 00:48:26,688 --> 00:48:28,899 is what happens 979 00:48:28,900 --> 00:48:32,528 with this little turn around here. What's going on there? So you said hey, [inaudible] you 980 00:48:32,528 --> 00:48:33,599 just finished 981 00:48:33,599 --> 00:48:36,869 cleaning a row and you just checked to see with your 982 00:48:36,869 --> 00:48:40,189 right hand, actually, if my right is clear, if there was a ceiling above you. 983 00:48:40,188 --> 00:48:42,998 And if there was a ceiling above you, right? 984 00:48:42,998 --> 00:48:45,958 Or if there wasn't a ceiling above you, you went ahead and got ready to clean 985 00:48:45,958 --> 00:48:49,969 another row. But if there was a ceiling above you, what do you wanna do? And you say hey, 986 00:48:49,969 --> 00:48:53,168 if there was a ceiling above me, I wanna stop - just stop. Is there some way to get 987 00:48:53,168 --> 00:48:56,719 Karel to stop? For the love of god, Karel, stop. No, Karel's 988 00:48:56,719 --> 00:49:01,900 just like no, man, I'm going. I'm going. You're like, there's no way to just say stop, Karel? 989 00:49:01,900 --> 00:49:05,889 How do you get Karel to stop? You need him to finish executing what he's 990 00:49:05,889 --> 00:49:06,528 executing. 991 00:49:06,528 --> 00:49:10,318 What is he executing, right? If we just got here and we cleaned up a row and I checked my 992 00:49:10,318 --> 00:49:11,909 right and there's a ceiling there, 993 00:49:11,909 --> 00:49:16,298 if I don't do anything, what's Karel gonna do? He's gonna go back around and 994 00:49:16,298 --> 00:49:20,018 say hey, is your left clear? Yeah, because my left is below me, it's clear. And it's 995 00:49:20,018 --> 00:49:23,738 gonna go and try to clean more, even though I just finished the last row. 996 00:49:23,739 --> 00:49:27,298 So what I actually do is when my right is blocked over here I say 997 00:49:27,298 --> 00:49:31,429 hey, you know what, Karel? To get you to think that you're done, I'm gonna have you 998 00:49:31,429 --> 00:49:35,179 turn around. So now when you check your left, the ceiling is gonna be there and 999 00:49:35,179 --> 00:49:36,969 you're gonna break out of that loop. 1000 00:49:36,969 --> 00:49:39,729 So you wanna think about the conditions on your loop to actually get 1001 00:49:39,728 --> 00:49:43,528 this to happen. So in our final 10 seconds together, I'll just run this to show you that it 1002 00:49:43,528 --> 00:49:47,289 actually works. Doo, doo, doo. 1003 00:49:47,289 --> 00:49:50,819 And you're like actually, [inaudible] you're in, like, our, you know, final minus four minutes 1004 00:49:50,818 --> 00:49:54,579 together. I know, I'm sorry. Education's one of those odd things that the more - you 1005 00:49:54,579 --> 00:49:55,709 get more of it 1006 00:49:55,708 --> 00:49:59,659 for paying the same amount, and somehow you feel like you were cheated. 1007 00:49:59,659 --> 00:50:00,478 1008 00:50:00,478 --> 00:50:03,608 So let's start Karel. Look, if I 1009 00:50:03,608 --> 00:50:07,348 let you out 10 minutes early, everyone would be like oh, yeah, rock on, but if I gave 1010 00:50:07,349 --> 00:50:10,769 you a car that only had three doors on it, you'd be like what's up with that? 1011 00:50:10,768 --> 00:50:11,848 Strange, how that works. 1012 00:50:11,849 --> 00:50:14,939 And there's Karel, just cleaned up his world. All righty, so I will see you on 1013 00:50:14,938 --> 00:50:15,998 Monday. Have a good weekend.