1 00:00:09,830 --> 00:00:12,599 2 00:00:12,599 --> 00:00:15,868 This presentation is delivered by the Stanford Center for Professional 3 00:00:15,868 --> 00:00:22,868 Development. 4 00:00:24,009 --> 00:00:27,118 We're gonna do a little bit of review of what we talked about last time. Remember last time we talked about 5 00:00:27,118 --> 00:00:28,669 our friend, the method, 6 00:00:28,670 --> 00:00:32,160 and one of the things we said about method was, like, a CD player was like a method, 7 00:00:32,159 --> 00:00:35,500 and part of the idea was it had this generalization capability, that it had some parameter 8 00:00:35,500 --> 00:00:39,200 of the CD you put in, and out came music, and the music that came out depended on 9 00:00:39,200 --> 00:00:42,070 the CD that you actually put in. 10 00:00:42,070 --> 00:00:46,140 Now part of the reason, besides just getting generality, is this notion of 11 00:00:46,140 --> 00:00:47,670 information hiding. 12 00:00:47,670 --> 00:00:51,679 So when we think about having - this is, kind of, the software engineering principle 13 00:00:51,679 --> 00:00:52,469 for the day - 14 00:00:52,469 --> 00:00:55,408 when we think about having methods, part of the reason to have a method, and to 15 00:00:55,408 --> 00:00:57,869 give it a good name, and to generalize it with parameters 16 00:00:57,869 --> 00:01:00,988 is because you don't want the person who's using that method to have to worry 17 00:01:00,988 --> 00:01:03,320 about all of the internals of that method. 18 00:01:03,320 --> 00:01:04,679 Here is a simple example. 19 00:01:04,679 --> 00:01:07,460 Most of you have seen the readInt 20 00:01:07,459 --> 00:01:10,489 function or the readInt method, and it reads information from the user, and we did 21 00:01:10,489 --> 00:01:13,519 a little example in class where the user typed in something that wasn't an integer, and it 22 00:01:13,519 --> 00:01:16,269 said, "Illegal format," and it asked for it again. 23 00:01:16,269 --> 00:01:20,509 It does all of that for you. Do you need to know any of the internals of readInts? No. 24 00:01:20,510 --> 00:01:21,780 You can just use it 25 00:01:21,780 --> 00:01:24,899 because it's hiding information from you. It's hiding all of the complexity 26 00:01:24,899 --> 00:01:28,250 of being able to get some data from the user, do error checking on that data, 27 00:01:28,250 --> 00:01:29,659 give the data back to you. 28 00:01:29,659 --> 00:01:32,580 And so, as a result, you can write your programs where you just have this one line 29 00:01:32,579 --> 00:01:33,649 in there. 30 00:01:33,650 --> 00:01:37,520 You get data from the user, and you don't care what all is going on in order to get 31 00:01:37,519 --> 00:01:38,868 that data from the user. 32 00:01:38,868 --> 00:01:42,028 That's the same kind of mindset you should be thinking about when you write your 33 00:01:42,028 --> 00:01:46,319 methods. Your methods should basically think about solving one problem each, 34 00:01:46,319 --> 00:01:49,500 and you want to think about that problem, sort of, being something general that 35 00:01:49,500 --> 00:01:50,280 makes sense 36 00:01:50,280 --> 00:01:52,700 to hide that information from the user. So 37 00:01:52,700 --> 00:01:55,700 when the user called your method, for example, there's a nice comment that 38 00:01:55,700 --> 00:01:57,969 explains what it does, that explains what the parameters are, 39 00:01:57,969 --> 00:02:01,209 so they don't need to go into the code of you method to actually figure out 40 00:02:01,209 --> 00:02:02,739 what it does, okay? 41 00:02:02,739 --> 00:02:05,938 Let me give you a simple, real life example, okay? 42 00:02:05,938 --> 00:02:08,750 There is this thing that most of you are probably familiar with that looks, oh, 43 00:02:08,750 --> 00:02:12,650 like this, and has two slots in it up here, and a little thing that looks like a 44 00:02:12,650 --> 00:02:14,060 plunger over here, 45 00:02:14,060 --> 00:02:16,939 and it's something we happen to affectionately - anyone that can identify what this 46 00:02:16,939 --> 00:02:18,829 object is? A toaster. 47 00:02:18,829 --> 00:02:20,699 Toaster, rock on. That's a social. 48 00:02:20,699 --> 00:02:25,449 A toaster is essentially doing information hiding, right? And you're like, "No, 49 00:02:25,449 --> 00:02:28,579 really? Like, my - " yeah, your toaster's alive. It's sitting on the counter, and it 50 00:02:28,579 --> 00:02:30,498 is hiding information from you, 51 00:02:30,498 --> 00:02:34,149 and it's information you don't care about, right? It's got a parameter. The parameter 52 00:02:34,149 --> 00:02:36,359 you stick in is bread. 53 00:02:36,359 --> 00:02:40,180 You can stick in lots of different bread, but the type that you stick in is 54 00:02:40,180 --> 00:02:42,919 bread, right? There can be some specialization of that type like rye bread, 55 00:02:42,919 --> 00:02:45,109 or wheat bread, or whole bread, or whatever it is, 56 00:02:45,109 --> 00:02:49,808 but you gotta stick in bread. You call the method by sticking the plunger, and 57 00:02:49,808 --> 00:02:54,568 some amount of processing later, what you get out of this thing is toast. 58 00:02:54,568 --> 00:02:57,998 You don't care how the toast is produced. One way the toast is produced is 59 00:02:57,998 --> 00:03:01,628 there's coils in here that head up, that, sort of, bake the bread, and bread comes 60 00:03:01,628 --> 00:03:02,288 out, 61 00:03:02,288 --> 00:03:06,038 that's great. Another way the toast is produced is there's Keebler Elves inside here, 62 00:03:06,038 --> 00:03:06,799 and they're like, 63 00:03:06,800 --> 00:03:09,150 "Hey, we got bread." And they eat the bread, 64 00:03:09,150 --> 00:03:11,789 and they excrement toast, right? 65 00:03:11,789 --> 00:03:13,888 And that's just what comes out. 66 00:03:13,889 --> 00:03:19,480 Elf excrement, but you don't care because it's toast, right? You're like, "That's great. 67 00:03:19,479 --> 00:03:22,349 Something's being hidden from me." And in the case of elves, you're glad it's being hidden 68 00:03:22,349 --> 00:03:23,090 from you, 69 00:03:23,090 --> 00:03:25,240 but you get back what you cared about, 70 00:03:25,240 --> 00:03:28,219 right? And the beauty of this is that this can be implemented in any way 71 00:03:28,219 --> 00:03:32,129 possible. Like, someday down the line we'll figure out fusion, right? And your 72 00:03:32,129 --> 00:03:35,079 toaster will toast bread in a fraction of a second because there'll be a little 73 00:03:35,080 --> 00:03:36,000 fusion reaction in there. 74 00:03:36,000 --> 00:03:39,110 You'll put in the bread; you'll press a button, and immediately toast will come out. It'll be 75 00:03:39,110 --> 00:03:41,149 charred at around two million degrees, 76 00:03:41,149 --> 00:03:43,618 but you'll get out toast, and the 77 00:03:43,618 --> 00:03:46,758 stuff in here, you still don't care about. So information hiding, the other thing it 78 00:03:46,758 --> 00:03:50,289 gives you is the ability to change the implementation here, 79 00:03:50,289 --> 00:03:53,769 and the person who is using that thing as a black box 80 00:03:53,770 --> 00:03:56,859 never has to care about if you found some more efficient way to do it 81 00:03:56,859 --> 00:03:59,950 or however you implemented it, okay? And that's, kind of, the key principle that you want to 82 00:03:59,949 --> 00:04:02,938 think about. This is actually so powerfully that 83 00:04:02,938 --> 00:04:05,079 about 15 years ago I was teaching this class, 84 00:04:05,080 --> 00:04:06,900 and I had a student in there - 85 00:04:06,900 --> 00:04:07,719 totally true story - and 86 00:04:07,719 --> 00:04:09,628 I told him, "Hey - " 87 00:04:09,628 --> 00:04:11,828 his name was Afra, by the way. Afra, if you're watching this, 88 00:04:11,829 --> 00:04:15,748 this is all about you. I told him methods - at that time I was calling them functions - 89 00:04:15,748 --> 00:04:17,639 but methods are toasters, 90 00:04:17,639 --> 00:04:20,609 and I ran into him, like, three years ago, so it's been 12 years, and the first 91 00:04:20,608 --> 00:04:23,388 thing he said, "Hi, Mehran." So he did say hi, and then the next thing he said to me, 92 00:04:23,389 --> 00:04:24,908 no joke, was 93 00:04:24,908 --> 00:04:30,528 "A function is a toaster." And I was like, "Oh, I love you." And we hugged, and it was, oh, 94 00:04:30,528 --> 00:04:32,189 plus plus, 95 00:04:32,189 --> 00:04:35,289 and now he's a professor himself, and I hope he's telling his 96 00:04:35,288 --> 00:04:36,870 students about the dreaded toaster, 97 00:04:36,870 --> 00:04:39,879 but that's what it is. That's the thing you should just think about, information hiding in 98 00:04:39,879 --> 00:04:42,769 the little toaster you have, okay? Now, 99 00:04:42,769 --> 00:04:45,748 along with that, one of the things we also talked about last time is functions 100 00:04:45,749 --> 00:04:48,749 returning values, and I told you sometimes a function doesn't actually - or 101 00:04:48,749 --> 00:04:51,449 method returning values, and sometimes a method doesn't actually have to return a 102 00:04:51,449 --> 00:04:52,658 value, 103 00:04:52,658 --> 00:04:56,839 and that's when we have a private void as the return type, right, or 104 00:04:56,839 --> 00:04:59,599 void is the return type; private's just the qualifier, 105 00:04:59,600 --> 00:05:03,460 and we might call this, for example, something like intro. If we want to just 106 00:05:03,459 --> 00:05:06,159 have it write out some, you know, 107 00:05:06,160 --> 00:05:10,740 hello to the user, or it explains how the program works, or whatever, 108 00:05:10,740 --> 00:05:12,069 and then we have the end 109 00:05:12,069 --> 00:05:13,110 of the method. 110 00:05:13,110 --> 00:05:16,649 There is not necessarily a return anywhere inside this method, just like 111 00:05:16,649 --> 00:05:19,918 you did with Carol. When you were writing methods in Carol, you were writing 112 00:05:19,918 --> 00:05:22,978 methods that didn't return anything, and you never had to worry about return, and 113 00:05:22,978 --> 00:05:26,558 that's perfectly fine. In methods that don't return anything you never have to say 114 00:05:26,559 --> 00:05:27,739 return, and that's great. 115 00:05:27,738 --> 00:05:31,528 If you want to say return somewhere, you can say return 116 00:05:31,528 --> 00:05:34,439 and then put a semicolon because there's nothing you're actually returning, so 117 00:05:34,439 --> 00:05:36,509 there's no expression over here for what you're returning, 118 00:05:36,509 --> 00:05:39,660 but, generally, we don't like to return in the middle of a method. 119 00:05:39,660 --> 00:05:42,970 So these are usually out. Usually, we just don't have a return in there, and we 120 00:05:42,970 --> 00:05:48,560 expect it to execute all the way through to get to the bottom, that's how it's done, all right? So 121 00:05:48,560 --> 00:05:52,300 when we talk about calling these methods and what's going on parameter passing, a 122 00:05:52,300 --> 00:05:55,220 little bit of complexity comes in, and today we're gonna go through that 123 00:05:55,220 --> 00:05:58,600 complexity in excruciating detail. Hopefully, it won't be too excruciating, 124 00:05:58,600 --> 00:06:00,159 but enough detail that, hopefully, 125 00:06:00,158 --> 00:06:02,118 you will know how it works. 126 00:06:02,119 --> 00:06:04,490 As we talked about last time, right, 127 00:06:04,490 --> 00:06:07,329 you can have some method over here Run. 128 00:06:07,329 --> 00:06:10,059 So we'll say Private, 129 00:06:10,059 --> 00:06:11,069 Void, 130 00:06:11,069 --> 00:06:15,999 Run, and inside here we might have some Integer I that we do something 131 00:06:15,999 --> 00:06:17,619 with, 132 00:06:17,619 --> 00:06:20,530 and over here we might have some other method, 133 00:06:20,529 --> 00:06:22,298 Private - I'll 134 00:06:22,298 --> 00:06:26,509 make this one Void too, and we'll call this, just for lack of a better name, Word 135 00:06:26,509 --> 00:06:27,688 Cal, 136 00:06:27,689 --> 00:06:31,960 and over here Cal might also have in it some other Integer I, 137 00:06:31,959 --> 00:06:36,269 and it might do something with it, and these two Integer I's are not the same 138 00:06:36,269 --> 00:06:40,129 because the lifetime of a variable 139 00:06:40,129 --> 00:06:43,960 exists only in the method in which it is defined for these kind of variables, 140 00:06:43,959 --> 00:06:47,688 and I'll show you some different kinds of variables in just a second, but a 141 00:06:47,689 --> 00:06:51,159 variable like this that is declared inside of a method is what we refer 142 00:06:51,158 --> 00:06:52,310 to as 143 00:06:52,310 --> 00:06:55,720 a local variable. It's, kind of, a variable that, kind of, hangs out in its 144 00:06:55,720 --> 00:06:58,810 little hood, and its hood is the method that it's declared in, so it's 145 00:06:58,810 --> 00:07:00,009 local to its hood. It's like, 146 00:07:00,009 --> 00:07:03,069 yeah, I'm just hanging out with the boys and girls in run, 147 00:07:03,069 --> 00:07:04,689 and that's where I live, 148 00:07:04,689 --> 00:07:09,159 and so if Run happens to call Cal somewhere inside here, well, I don't live 149 00:07:09,158 --> 00:07:11,248 in Cal, so 150 00:07:11,249 --> 00:07:15,499 I'm not going over to Cal, right? And Cal comes, and it gets called, and 151 00:07:15,499 --> 00:07:18,739 there's some other I here, and it doesn't know about this I. It's, like, hanging out 152 00:07:18,738 --> 00:07:21,769 in a different hood. Like, this is Manhattan, and there's an I over here, 153 00:07:21,769 --> 00:07:24,088 and this is Brooklyn, and there is another I over there, 154 00:07:24,088 --> 00:07:25,928 and they just don't need to know about each other, 155 00:07:25,928 --> 00:07:28,988 and, as a matter of fact, they don't know about each other because if they did, 156 00:07:28,988 --> 00:07:33,818 life would be bad. Like, they'd get in fights, knives would come out, the whole deal. So 157 00:07:33,819 --> 00:07:35,580 these two things are actually separate. 158 00:07:35,579 --> 00:07:39,300 Now, what does that mean in terms of the mechanics of when we call functions 159 00:07:39,300 --> 00:07:41,210 parameters? For example, what would happen 160 00:07:41,209 --> 00:07:44,609 if Cal actually expected some parameter over here, 161 00:07:44,610 --> 00:07:46,098 let's call it N, 162 00:07:46,098 --> 00:07:49,139 and I passed into it I; 163 00:07:49,139 --> 00:07:52,120 what does that mean? Is that gonna conflict with this I over here; what's 164 00:07:52,120 --> 00:07:53,408 actually going on? 165 00:07:53,408 --> 00:07:56,819 So to understand what's going on, let's actually go through an example in 166 00:07:56,819 --> 00:08:00,098 excruciating detail of what's going on inside the computer, and you can see 167 00:08:00,098 --> 00:08:03,000 exactly what's going on. So let's come to the 168 00:08:03,000 --> 00:08:04,139 slides, 169 00:08:04,139 --> 00:08:06,579 and here is a program that you saw last time. 170 00:08:06,579 --> 00:08:09,759 This is just our little factorial program that we computed last time that you 171 00:08:09,759 --> 00:08:12,759 saw an example of different methods, and we have the run method 172 00:08:12,759 --> 00:08:14,730 that calls this factorial method 173 00:08:14,730 --> 00:08:19,420 a bunch of times, up to four times, right? Because maximum happens to be four, 174 00:08:19,420 --> 00:08:22,960 and inside here we have an I, and over here we have an 175 00:08:22,959 --> 00:08:26,359 I, and here we have this things called Result, and we're like, "Hey, what's going on with 176 00:08:26,360 --> 00:08:27,419 all this?" 177 00:08:27,418 --> 00:08:30,379 Well, here let's trace it through. So let's actually start 178 00:08:30,379 --> 00:08:33,279 with Run because that's where the computer starts executing things, right? 179 00:08:33,279 --> 00:08:35,009 So it comes to Run, and it says, 180 00:08:35,009 --> 00:08:38,679 okay, inside Run I have this four loop, and it's got an I in there. 181 00:08:38,679 --> 00:08:41,849 So in the hood or in the method 182 00:08:41,850 --> 00:08:46,180 for Run, we have our own little I, and when we start off, we set I equal to 183 00:08:46,179 --> 00:08:49,319 zero. We check to make sure that's less than Max Num, which is four, which it is. 184 00:08:49,320 --> 00:08:51,020 So we say, oh, we enter the loop, 185 00:08:51,019 --> 00:08:53,019 and now we come to execute this line. 186 00:08:53,019 --> 00:08:56,539 Well, to execute this line, we need to make a method call over here to 187 00:08:56,539 --> 00:08:58,019 factorial because 188 00:08:58,019 --> 00:09:00,769 we need to figure out what's the factorial of I 189 00:09:00,769 --> 00:09:03,529 in order to be able to print it out on the line, right? 190 00:09:03,529 --> 00:09:07,578 So we call factorial, and we say, hey, factorial, what I'm going to give you 191 00:09:07,578 --> 00:09:08,669 is I. 192 00:09:08,669 --> 00:09:12,110 Now, I'm not actually going to give you I because I is my little friend. What I'm 193 00:09:12,110 --> 00:09:15,269 gonna give you is a copy of I, okay? 194 00:09:15,269 --> 00:09:17,198 So when factorial gets called, 195 00:09:17,198 --> 00:09:20,049 we get what's called a new Stack Frame, 196 00:09:20,049 --> 00:09:23,529 and the reason why it's called a Stack Frame is that these things get stacked 197 00:09:23,529 --> 00:09:25,909 on top of each other. Notice the box for 198 00:09:25,909 --> 00:09:27,559 back here or the box for Run, 199 00:09:27,559 --> 00:09:30,959 sort of, gets occluded by a new box or a new frame 200 00:09:30,960 --> 00:09:32,210 for factorial. 201 00:09:32,210 --> 00:09:35,929 That's why this is called a Stacked Frame because we frame it inside a box, 202 00:09:35,928 --> 00:09:39,068 and we're gonna stack these things together as functions or methods get 203 00:09:39,068 --> 00:09:40,329 called, okay? 204 00:09:40,330 --> 00:09:44,410 So when factorial gets called, it's expecting a parameter called N. 205 00:09:44,409 --> 00:09:48,459 What it got passed back here was I. What we did at this point was we said what's 206 00:09:48,460 --> 00:09:49,850 the value of I? 207 00:09:49,850 --> 00:09:55,159 It's zero. We make a copy of that value and pass it over to factorial as its 208 00:09:55,159 --> 00:09:56,230 parameter. 209 00:09:56,230 --> 00:09:59,100 Factorial says, hey, my parameter's called N. 210 00:09:59,100 --> 00:10:02,019 What value did you give me? You gave me a zero. I'm 211 00:10:02,019 --> 00:10:04,110 gonna stick that in N, okay? 212 00:10:04,110 --> 00:10:07,489 So it's got this thing in here, N, which is totally distinct from I. 213 00:10:07,489 --> 00:10:11,129 It's gonna have its own I in here, and this I is in a different hood than 214 00:10:11,129 --> 00:10:12,719 the I in Run. 215 00:10:12,719 --> 00:10:16,870 So they're completely separate variables. They just happen to have the same name, 216 00:10:16,870 --> 00:10:20,110 but they have the same name in different context, so they're actually different. It's just 217 00:10:20,110 --> 00:10:20,470 like 218 00:10:20,470 --> 00:10:24,009 if we had someone named Mary in this room, and there is someone named Mary taking Psych 219 00:10:24,009 --> 00:10:26,169 1, you're different Marys. 220 00:10:26,169 --> 00:10:28,929 That's just the way life is, right? You're in different context even though you 221 00:10:28,929 --> 00:10:30,139 have the same name, 222 00:10:30,139 --> 00:10:33,549 so same thing going on here. So factorial fires up and says you gave me that zero, 223 00:10:33,549 --> 00:10:34,500 that's N. 224 00:10:34,500 --> 00:10:38,649 I start off by having result, which is a local variable. It is a variable that is 225 00:10:38,649 --> 00:10:41,309 only defined in my context, 226 00:10:41,309 --> 00:10:45,419 and I set that equal to one, and then I have my own variable I, which is only defined 227 00:10:45,419 --> 00:10:48,939 in my context, and I start off setting that to one, and it comes in here, and this is a 228 00:10:48,940 --> 00:10:53,430 real short loop because it does the test, and it says is one less than or equal to zero? 229 00:10:53,429 --> 00:10:54,088 No. 230 00:10:54,089 --> 00:10:58,960 So the body of the loop never gets executed, and it immediately returns a result, okay? 231 00:10:58,960 --> 00:11:02,900 So what value's gonna come back? What's gonna come back is 232 00:11:02,899 --> 00:11:06,329 basically a copy of this one. So 233 00:11:06,330 --> 00:11:10,420 the value one gets passed back, and we come back, this whole Stack Frame 234 00:11:10,419 --> 00:11:11,860 that we had over here 235 00:11:11,860 --> 00:11:15,710 goes away because factorial's now done. Factorial says, hey, I computed my results; 236 00:11:15,710 --> 00:11:20,910 here you go. I'm going away. So what we say is it gets popped off the stack. 237 00:11:20,909 --> 00:11:24,579 It and everyone in its hood, we, sort of, say thanks factorial. You did your work. Thanks for 238 00:11:24,580 --> 00:11:25,158 playing. 239 00:11:25,158 --> 00:11:27,799 You're done. Blammo, you're gone. 240 00:11:27,799 --> 00:11:32,629 The only thing that remains of you is the value you returned, okay? 241 00:11:32,629 --> 00:11:35,580 So that one gets returned, and we say, hey, now I can actually write something out to 242 00:11:35,580 --> 00:11:38,920 the screen because I got a value of one back from factorial of zero, 243 00:11:38,919 --> 00:11:42,679 and that's what I write out - zero factorial equals one. All right? 244 00:11:42,679 --> 00:11:44,759 One more time, just for good measure, 245 00:11:44,759 --> 00:11:46,120 so - whoa. 246 00:11:46,120 --> 00:11:48,379 That was bad 247 00:11:48,379 --> 00:11:49,830 measure. 248 00:11:49,830 --> 00:11:52,470 Sometimes these things just never work right. You have to hate technology. All right. 249 00:11:52,470 --> 00:11:57,149 So we add one to I. I now becomes one, right? I was zero over here. We add one. 250 00:11:57,149 --> 00:11:58,379 I becomes one. 251 00:11:58,379 --> 00:12:00,220 I is still less than four, 252 00:12:00,220 --> 00:12:04,329 so we execute this line once again. Now we're calling factorial passing at the value 253 00:12:04,328 --> 00:12:05,029 one. 254 00:12:05,029 --> 00:12:07,228 It gets a copy of that one. 255 00:12:07,229 --> 00:12:08,590 Where does that one go? 256 00:12:08,590 --> 00:12:12,660 That one goes into the parameter that it's expecting, the N. So it gets a one 257 00:12:12,659 --> 00:12:15,249 because that's the value we passed in. We passed a copy. 258 00:12:15,249 --> 00:12:18,790 We didn't pass I itself. I is still safe and sound over there, 259 00:12:18,789 --> 00:12:20,629 the actual variable I. 260 00:12:20,629 --> 00:12:23,889 We just got a copy of its value. That's what got passed. 261 00:12:23,889 --> 00:12:27,159 So here we set results equal to one. We set I equal to one. 262 00:12:27,159 --> 00:12:31,189 We checked the test. The test is true because one is less than or equal to one. We 263 00:12:31,190 --> 00:12:35,420 multiply result by one, which is one, and we store it back in results. So that looks like it 264 00:12:35,419 --> 00:12:38,669 does nothing, but it really multiplied one by one, and stored it back into one. 265 00:12:38,669 --> 00:12:41,929 We add another one to I; I is now two. When we do the test, two is not less 266 00:12:41,929 --> 00:12:44,208 than or equal to N, which is one, 267 00:12:44,208 --> 00:12:46,359 so it says I'm done, and it returns a result. 268 00:12:46,360 --> 00:12:49,070 So this guy goes away again, 269 00:12:49,070 --> 00:12:50,629 and factorial of one 270 00:12:50,629 --> 00:12:53,350 is one. So that's what we write out on the screen, okay? 271 00:12:53,350 --> 00:12:56,369 And I won't go through the process in excruciating detail again 272 00:12:56,369 --> 00:13:00,350 other than just to say after we add one to I, we're gonna have to compute factorial of two 273 00:13:00,350 --> 00:13:02,560 and the answer that's gonna come back is two, 274 00:13:02,559 --> 00:13:05,039 and we're gonna compute factorial of three, and the answer that's gonna come 275 00:13:05,039 --> 00:13:07,730 back is six, and after we write that out, 276 00:13:07,730 --> 00:13:11,600 I is now four which is not less than Max Num because Max 277 00:13:11,600 --> 00:13:15,060 Num also has the value of four, so we're done, okay? So 278 00:13:15,059 --> 00:13:17,439 any questions about the mechanics of 279 00:13:17,440 --> 00:13:20,169 how parameters work when you pass them? When you pass them, 280 00:13:20,168 --> 00:13:22,610 you're passing a copy of the value. 281 00:13:22,610 --> 00:13:26,300 You're not passing the actual variable, okay? Now, 282 00:13:26,299 --> 00:13:27,769 what does that actually mean? 283 00:13:27,769 --> 00:13:29,519 It means a couple things. 284 00:13:29,519 --> 00:13:33,470 First of all, what it means is that when you are in some method somewhere, you 285 00:13:33,470 --> 00:13:36,480 can't refer to someone else's variable, okay? 286 00:13:36,480 --> 00:13:40,250 So what do I mean by that? I mean when I'm, sort of, back here, if I back up to 287 00:13:40,250 --> 00:13:41,620 the last place where 288 00:13:41,620 --> 00:13:43,139 you can see a factorial, 289 00:13:43,139 --> 00:13:45,919 when I'm in factorial over here, and I'm referring to I, 290 00:13:45,919 --> 00:13:49,689 I better be referring to my own I; I better have my own local I. I cannot 291 00:13:49,690 --> 00:13:54,389 refer to any other method's I. I don't have visibility to any other local variable 292 00:13:54,389 --> 00:13:55,669 in any other method. 293 00:13:55,669 --> 00:14:00,240 I can refer to variables that are local to me, like I, a result that I 294 00:14:00,240 --> 00:14:03,038 declare. I can also refer to parameters 295 00:14:03,038 --> 00:14:06,509 because N when it gets passed in gets a little box. So all parameters get a box 296 00:14:06,509 --> 00:14:08,789 that gets a copy of the value passed in, 297 00:14:08,789 --> 00:14:12,548 and all my local variables get a box which has the value of whatever I set up 298 00:14:12,548 --> 00:14:15,659 my local variables to have, okay? But 299 00:14:15,659 --> 00:14:18,879 I can't refer to variables in any other method. 300 00:14:18,879 --> 00:14:20,580 That's an important consideration. 301 00:14:20,580 --> 00:14:24,950 So we'll, sort of, come back to where we were. So 302 00:14:24,950 --> 00:14:28,970 this also leads to some weird behavior that may be unexpected, and this weird 303 00:14:28,970 --> 00:14:31,490 behavior is something we like to refer to as 304 00:14:31,490 --> 00:14:34,610 bad times with methods, okay? So 305 00:14:34,610 --> 00:14:35,950 this program's buggy. 306 00:14:35,950 --> 00:14:37,440 Let me show you why. 307 00:14:37,440 --> 00:14:41,330 Here we have Run, and we have inside Run we have some integer X which has the 308 00:14:41,330 --> 00:14:44,740 value three, and we say, hey, you know what, I want to add five to X. 309 00:14:44,740 --> 00:14:47,940 So why don't I pass X over to add five. X 310 00:14:47,940 --> 00:14:52,380 will add five to X - or Add Five will add five to X, and then I should be able to 311 00:14:52,379 --> 00:14:55,809 write out X equals eight, right? 312 00:14:55,809 --> 00:14:58,689 No, right? Because if that was right, this wouldn't be buggy. 313 00:14:58,690 --> 00:15:01,820 What's going on where this happened? So let's actually trace this one on the 314 00:15:01,820 --> 00:15:05,230 board, and I'll show you what's going on. We can draw out all the Stack Frames. 315 00:15:05,230 --> 00:15:08,509 So when Run actually gets going, right, 316 00:15:08,509 --> 00:15:12,149 we get a Stack Frame for Run, so I'm just gonna draw it manually up here so 317 00:15:12,149 --> 00:15:14,199 you can see it as these things are happening. 318 00:15:14,200 --> 00:15:18,350 When run actually runs, it's got its own local variable X, 319 00:15:18,350 --> 00:15:21,740 and X is getting set to the value three, okay? 320 00:15:21,740 --> 00:15:25,058 Then it calls Add Five. What happens when it calls 321 00:15:25,058 --> 00:15:28,788 Add Five? We get a new Stack Frame. I'm not gonna erase Run; I'm just gonna draw 322 00:15:28,788 --> 00:15:29,649 down here. 323 00:15:29,649 --> 00:15:31,899 We get Add Five. 324 00:15:31,899 --> 00:15:36,269 What is Add Five expecting? It's expecting a parameter called X. 325 00:15:36,269 --> 00:15:40,990 So it says, oh, I have my own box for X down here. What are you giving me? I'm 326 00:15:40,990 --> 00:15:45,490 giving you a copy of the value passed in. What's the value passed in? Three. 327 00:15:45,490 --> 00:15:47,139 You get a copy of three. 328 00:15:47,139 --> 00:15:47,889 That's great. 329 00:15:47,889 --> 00:15:51,909 Now, Add Five is over here. It has its X in its own hood that is not the 330 00:15:51,909 --> 00:15:53,219 same as this X, and you 331 00:15:53,220 --> 00:15:55,649 say, "But you passed X as a parameter." 332 00:15:55,649 --> 00:15:58,289 Yeah, the parameter happened to have the same name 333 00:15:58,289 --> 00:16:01,769 in terms of the argument that I passed in and the parameter I was expecting, 334 00:16:01,769 --> 00:16:03,560 but they live in different hoods. 335 00:16:03,559 --> 00:16:07,709 They're actually separate, and what you get is a copy even if the name's the same. 336 00:16:07,710 --> 00:16:11,540 So this guy happily comes along and says, ooh, ooh, I'm gonna Add Five - all kinds of excitement 337 00:16:11,539 --> 00:16:12,778 going on. It 338 00:16:12,778 --> 00:16:13,669 adds five. 339 00:16:13,669 --> 00:16:15,578 This three turns into an eight, 340 00:16:15,578 --> 00:16:19,108 and then it says, hey, I'm done. Good times. I don't even need to return anything. 341 00:16:19,109 --> 00:16:23,278 Thanks for playing, and I'm like hearty handshake, Add Five, you added five to your X, 342 00:16:23,278 --> 00:16:24,649 and now you're done. 343 00:16:24,649 --> 00:16:30,079 Thank you for playing. It goes away. We come over here to Run, and now Run is 344 00:16:30,080 --> 00:16:32,690 going to do a print on X, okay? 345 00:16:32,690 --> 00:16:36,580 So what it's going to print out is three, all right? 346 00:16:36,580 --> 00:16:39,660 That's the place where it seems counterintuitive. What you are passing 347 00:16:39,659 --> 00:16:42,269 when you are passing parameters are copies. You 348 00:16:42,269 --> 00:16:45,590 are not passing the actual variable, all right? 349 00:16:45,590 --> 00:16:46,519 So one way 350 00:16:46,519 --> 00:16:50,649 you can potentially fix this 351 00:16:50,649 --> 00:16:52,909 - I pressed the wrong button. 352 00:16:52,909 --> 00:16:55,059 All right. So this was bad times. 353 00:16:55,059 --> 00:16:56,859 Here is good times with methods. 354 00:16:56,860 --> 00:16:58,919 Anyone remember the show, Good Times, 355 00:16:58,919 --> 00:17:01,849 J.J. Walk - Good Times. All right. I'm old. 356 00:17:01,850 --> 00:17:03,480 How 357 00:17:03,480 --> 00:17:06,789 can we change it? What we need to do is think about information flow 358 00:17:06,789 --> 00:17:10,299 through our program. We need to think about that value that we computed in Add Five, somehow 359 00:17:10,299 --> 00:17:15,269 we want to pass it back out so Run can get it. So let's trace through this code, right? 360 00:17:15,269 --> 00:17:19,078 Run starts, it has a Stack Frame. It has X equals three in it, and what it says is I'm 361 00:17:19,078 --> 00:17:21,159 gonna call Add Five, 362 00:17:21,160 --> 00:17:25,100 and I'm gonna pass it X. So Add Five once again happily comes along and says, hey, I'm Add 363 00:17:25,099 --> 00:17:26,058 Five. 364 00:17:26,058 --> 00:17:30,808 I have a value X which you gave me was three. It adds five to that three, and it gets 365 00:17:30,808 --> 00:17:31,980 the value eight, 366 00:17:31,980 --> 00:17:33,259 but now what did it do? 367 00:17:33,259 --> 00:17:34,809 It returns that eight, 368 00:17:34,809 --> 00:17:37,149 which means when it's going away, 369 00:17:37,150 --> 00:17:40,179 this puppy basically goes away, but what it says is, 370 00:17:40,179 --> 00:17:45,259 hey, what I'm giving you back, in my dying gasp, is this return value eight. 371 00:17:45,259 --> 00:17:48,509 What are you gonna do with it, Run, huh, huh? And it gets a little ornery, 372 00:17:48,509 --> 00:17:51,480 but Run says, hey, I'm just gonna assign it to X. 373 00:17:51,480 --> 00:17:54,690 Which X am I dealing with? I'm dealing with the X in my hood. 374 00:17:54,690 --> 00:17:57,630 So that eight that you just gave me - thanks for giving me the eight; 375 00:17:57,630 --> 00:17:59,789 I'll assign it to my X. 376 00:17:59,788 --> 00:18:01,009 And now, 377 00:18:01,009 --> 00:18:01,919 when I print it out, 378 00:18:01,919 --> 00:18:05,460 I actually have what I care about because the computation that happened 379 00:18:05,460 --> 00:18:09,659 inside the method that got called, the value that I cared about got returned and 380 00:18:09,659 --> 00:18:11,140 assigned to the place I cared about. 381 00:18:11,140 --> 00:18:15,160 That's what we refer to as information flow, okay? 382 00:18:15,160 --> 00:18:17,769 Question? Could you do the same thing - 383 00:18:17,769 --> 00:18:23,109 instead of having a return, just have in the print line method 384 00:18:23,109 --> 00:18:28,549 it concatenated with Add Five of X instead of with X? 385 00:18:28,549 --> 00:18:31,359 I'm not sure what you're saying, but - oh, you're saying - 386 00:18:31,359 --> 00:18:35,058 oh, and just put it on that line, just whatever Add Five returned. Yeah. Yeah, 387 00:18:35,058 --> 00:18:37,750 but then we wouldn't have actually changed the value of X, 388 00:18:37,750 --> 00:18:40,720 okay? We would have just printed it out. 389 00:18:40,720 --> 00:18:43,740 All right. So one way you can actually think about this, this is the way I like to 390 00:18:43,740 --> 00:18:44,410 remember it 391 00:18:44,410 --> 00:18:47,680 is when I was a wee tyke in the days of yore, 392 00:18:47,680 --> 00:18:49,150 my momma 393 00:18:49,150 --> 00:18:50,519 and daddy 394 00:18:50,519 --> 00:18:52,549 took me to a place called the Louvre. 395 00:18:52,549 --> 00:18:54,690 Anyone actually been to the Louvre? 396 00:18:54,690 --> 00:18:57,690 A fair number of people, it's a museum in Paris, and this thing there called 397 00:18:57,690 --> 00:18:58,808 the Mona Lisa, 398 00:18:58,808 --> 00:19:01,170 which is probably actually smaller than this square, but 399 00:19:01,170 --> 00:19:02,730 it happens to be a woman, 400 00:19:02,730 --> 00:19:05,059 very famous, who's smiling. 401 00:19:05,059 --> 00:19:07,889 That's my rendition of the Mona Lisa, okay? 402 00:19:07,890 --> 00:19:11,220 And so when we went there, here was 403 00:19:11,220 --> 00:19:12,720 mommy Sahami, 404 00:19:12,720 --> 00:19:15,259 and here was little Mehran, 405 00:19:15,259 --> 00:19:18,710 and little Mehran, when he was small, there was two things. One is he had a 406 00:19:18,710 --> 00:19:19,100 ponytail, 407 00:19:19,099 --> 00:19:20,569 which I would not recommend. It 408 00:19:20,569 --> 00:19:22,799 was the e70s; what can I say? 409 00:19:22,799 --> 00:19:27,740 The other thing that little Mehran had was he had a chainsaw, 410 00:19:27,740 --> 00:19:30,980 and so when we went to the Louvre, what happened was 411 00:19:30,980 --> 00:19:36,660 I said, "Hey, mommy, what I want to do is I want to do some computation on the Mona 412 00:19:36,660 --> 00:19:37,558 Lisa 413 00:19:37,558 --> 00:19:39,490 with my chainsaw." 414 00:19:39,490 --> 00:19:40,950 And what mommy said was, 415 00:19:40,950 --> 00:19:43,669 "Oh, that's great, but what we're gonna do is we're gonna call the method for 416 00:19:43,669 --> 00:19:46,240 you. That method's called gift shop." 417 00:19:46,240 --> 00:19:50,339 So we went down to the gift shop, and in the gift shop, somewhere along the way, 418 00:19:50,339 --> 00:19:54,289 they had said, "Hey, here's the copy of the Mona Lisa; make some prints of it." 419 00:19:54,289 --> 00:19:57,779 And so what the gift shop has down here, every time you call the gift shop 420 00:19:57,779 --> 00:19:58,339 method 421 00:19:58,339 --> 00:20:01,569 is you get a copy of the Mona Lisa. 422 00:20:01,569 --> 00:20:04,720 And so when happy Mehran comes along and says, "Oh, that's great. You know what? Mona 423 00:20:04,720 --> 00:20:08,329 Lisa looks a lot better split in half." 424 00:20:08,329 --> 00:20:11,609 And now what I've done is changed my copy of the Mona Lisa, 425 00:20:11,609 --> 00:20:16,029 and, luckily, when you go to the Louvre, you still see the original, okay? 426 00:20:16,029 --> 00:20:19,289 So that's the way to think about it. When you are in a method, 427 00:20:19,289 --> 00:20:23,700 you are getting a copy. If you somehow do something to that copy, you are not changing 428 00:20:23,700 --> 00:20:24,690 the original, 429 00:20:24,690 --> 00:20:30,480 and it's a good time for everyone involved, all 430 00:20:30,480 --> 00:20:32,720 right? 431 00:20:32,720 --> 00:20:37,079 Is this true for non-primitive data types, like - We'll get into that when we get into objects, but yeah, that's a good question. 432 00:20:37,079 --> 00:20:39,730 Right now we're just dealing with things like Ints and Doubles, and we'll talk about 433 00:20:39,730 --> 00:20:43,019 other stuff when we get there. All right. So, 434 00:20:43,019 --> 00:20:44,710 with that said, 435 00:20:44,710 --> 00:20:46,690 it's time to think of 436 00:20:46,690 --> 00:20:50,350 eventually writing our own whole classes, right? So, so far, we've been writing, like, 437 00:20:50,349 --> 00:20:53,389 classes that have been, like, one program, and now we want to think about writing 438 00:20:53,390 --> 00:20:54,419 multiple classes, 439 00:20:54,419 --> 00:20:57,090 but before we actually write multiple classes, 440 00:20:57,089 --> 00:20:59,490 we need to have a little bit more 441 00:20:59,490 --> 00:21:02,329 idea about using classes, okay? 442 00:21:02,329 --> 00:21:07,058 And so when you have classes, there's a notion of being a client of a class 443 00:21:07,058 --> 00:21:10,519 and being the implementer of a class. 444 00:21:10,519 --> 00:21:15,230 The client of the class is basically the user of the class. So when you use the 445 00:21:15,230 --> 00:21:19,519 ACM libraries, and you use readInts, you are a client of readInts. 446 00:21:19,519 --> 00:21:22,779 When you write your own class, and you write all the code inside that class, and 447 00:21:22,779 --> 00:21:25,149 say what that class is gonna do, 448 00:21:25,150 --> 00:21:26,590 you are the implementer. 449 00:21:26,589 --> 00:21:28,000 That's the difference, okay? 450 00:21:28,000 --> 00:21:29,049 And so 451 00:21:29,049 --> 00:21:31,138 it's, sort of, an important distinction to know because 452 00:21:31,138 --> 00:21:33,459 so far you've been doing a lot of this, 453 00:21:33,460 --> 00:21:37,990 and you're going to be moving to doing this pretty soon, all right? So 454 00:21:37,990 --> 00:21:40,558 in order to get a little bit more comfortable with this, we're gonna 455 00:21:40,558 --> 00:21:43,918 actually be clients of yet another library, 456 00:21:43,919 --> 00:21:48,280 and that's the library to generate random numbers, okay? 457 00:21:48,279 --> 00:21:51,740 And so the basic idea here is what we want to do is it would be fun if there 458 00:21:51,740 --> 00:21:54,890 was some way the computer could give us random numbers, right, because, like, for 459 00:21:54,890 --> 00:21:56,740 games and stuff. Like, games get real boring 460 00:21:56,740 --> 00:21:59,039 if the same thing happens every time, right? 461 00:21:59,039 --> 00:22:01,849 Unless you're in Vegas, in which case you know you're in good times, 462 00:22:01,849 --> 00:22:03,480 but we won't talk about that. 463 00:22:03,480 --> 00:22:06,900 So the way random numbers work on a computer, okay, is 464 00:22:06,900 --> 00:22:10,410 we actually call them pseudo random numbers because there is no real 465 00:22:10,410 --> 00:22:13,308 randomness on one of these puppies, in as much as you'd like to believe, "Hey, Mehran, like, 466 00:22:13,308 --> 00:22:15,480 3:00 last night when my computer crashed, 467 00:22:15,480 --> 00:22:16,589 there was randomness." It wasn't random. 468 00:22:16,589 --> 00:22:19,459 It was some computer programmer somewhere that didn't do a 469 00:22:19,460 --> 00:22:22,579 good job of implementing the program. So 470 00:22:22,579 --> 00:22:24,158 when we think about randomness, 471 00:22:24,159 --> 00:22:27,639 what we actually think of is what's referred to as pseudo random numbers because they look, for all intensive purposes to human beings, as 472 00:22:27,638 --> 00:22:31,079 though 473 00:22:31,079 --> 00:22:32,129 they're random, 474 00:22:32,130 --> 00:22:35,940 but really there is some process that is generating them in some deterministic 475 00:22:35,940 --> 00:22:37,110 way, okay? 476 00:22:37,109 --> 00:22:40,699 But we can think of some black box, ala, a toaster, 477 00:22:40,700 --> 00:22:43,490 except this one happens to be a Random Generator. 478 00:22:43,490 --> 00:22:46,970 And what you do is you go to this Random Generator, and you say, hey, Random 479 00:22:46,970 --> 00:22:49,120 Generator - you send it a message, right, 480 00:22:49,119 --> 00:22:52,579 which is give me, in some sense, the next random number, 481 00:22:52,579 --> 00:22:54,609 and what it gives you out is some 482 00:22:54,609 --> 00:22:57,209 number that looks random to you, and then you say give me the next one; it gives you 483 00:22:57,210 --> 00:22:59,538 another one. You say give me the next one; it gives you another one, and that's just the way it 484 00:22:59,538 --> 00:23:00,210 works. 485 00:23:00,210 --> 00:23:03,720 It's a black box, and you don't care what's implemented inside of here, 486 00:23:03,720 --> 00:23:05,180 right? It's a toaster to you. 487 00:23:05,180 --> 00:23:09,590 You just ask it for random numbers, and it gives you random numbers instead of toast, okay? 488 00:23:09,589 --> 00:23:13,009 So the way you actually can use one of these Random Generators as a client - this is 489 00:23:13,009 --> 00:23:16,319 actually a standard thing that exists in the ACM libraries. 490 00:23:16,319 --> 00:23:19,799 There is a library that you will import called acm.util, 491 00:23:19,799 --> 00:23:24,750 for utilities, .star, and when you import that, you get this thing 492 00:23:24,750 --> 00:23:29,779 called the Random Generator, and I'll show you how to use that in just a second, okay? 493 00:23:29,779 --> 00:23:32,569 The funky thing about a Random Generator is 494 00:23:32,569 --> 00:23:34,609 a Random Generator 495 00:23:34,609 --> 00:23:36,039 is a class, 496 00:23:36,039 --> 00:23:39,178 but rather than like most classes before when we wanted 497 00:23:39,179 --> 00:23:44,450 an object of that class we said, "New," and you got a new object of that class. 498 00:23:44,450 --> 00:23:49,360 Random Generator's a little bit different. What you actually say is 499 00:23:49,359 --> 00:23:51,269 RandomGenerator., 500 00:23:51,269 --> 00:23:53,329 501 00:23:53,329 --> 00:23:58,549 and the message you send is Get Instants, which means give me an instant - 502 00:23:58,549 --> 00:23:59,379 ah, I 503 00:23:59,380 --> 00:24:01,520 can't fit it all on this board. Let 504 00:24:01,519 --> 00:24:04,740 me write it on two lines. 505 00:24:04,740 --> 00:24:07,450 Oh, I'm just in this hailstorm of chalk. 506 00:24:07,450 --> 00:24:12,930 RandomGenerator.GetInstants, 507 00:24:12,930 --> 00:24:14,009 508 00:24:14,009 --> 00:24:17,890 there is no parameters here, instants, and what that means is give me an 509 00:24:17,890 --> 00:24:21,570 object of the Random Generator type, and what you're gonna assign that to 510 00:24:21,569 --> 00:24:24,659 is somewhere you're gonna have some private variable 511 00:24:24,660 --> 00:24:26,029 of type 512 00:24:26,029 --> 00:24:28,109 Random Generator, 513 00:24:28,109 --> 00:24:29,469 514 00:24:29,470 --> 00:24:30,400 515 00:24:30,400 --> 00:24:35,090 and you need to give it a name; we'll call it Rgen for Random Generator, equals 516 00:24:35,089 --> 00:24:38,629 RandomGenerator.GetInstants. So what RandomGenerator.GetInstants 517 00:24:38,630 --> 00:24:39,489 gives you 518 00:24:39,489 --> 00:24:43,149 is an object that is a Random Generator, so it's type is Random Generator. You're 519 00:24:43,148 --> 00:24:46,988 only going to use it inside your own class, which is why it's private, 520 00:24:46,989 --> 00:24:47,690 and because 521 00:24:47,690 --> 00:24:51,669 you're actually declaring a variable, you need to give it a name. So the name of the 522 00:24:51,669 --> 00:24:53,020 object is Rgen, okay? 523 00:24:53,019 --> 00:24:55,099 That's the thing you would have control over. 524 00:24:55,099 --> 00:24:59,319 Now, the funky thing that comes up when we think about these things, all right, 525 00:24:59,319 --> 00:25:02,309 is that sometimes what we actually want to have is 526 00:25:02,309 --> 00:25:06,869 we want to use this same object inside all of our methods, 527 00:25:06,869 --> 00:25:09,869 right? And so you just say, "Hey, Mehran, you just told me that when I declare a 528 00:25:09,869 --> 00:25:10,889 variable, 529 00:25:10,890 --> 00:25:14,650 that variable only lives inside the method in which it is declared. 530 00:25:14,650 --> 00:25:18,080 So what's going on there? And if I want to have one of these objects, 531 00:25:18,079 --> 00:25:21,960 do I need to actually get one of these inside every one of my methods?" 532 00:25:21,960 --> 00:25:24,829 And the answer is no. There is a way that you can actually say 533 00:25:24,829 --> 00:25:29,239 I want to have some object or some variable that is shared by all of the 534 00:25:29,239 --> 00:25:31,250 methods in my class, okay? 535 00:25:31,250 --> 00:25:32,750 And that's what we refer to 536 00:25:32,750 --> 00:25:34,430 as an Instance Variable. 537 00:25:34,430 --> 00:25:37,779 So the variables we had before were called Local Variables when they live in a 538 00:25:37,779 --> 00:25:42,220 particular hood. So like X over there inside Run is a Local Variable because it's 539 00:25:42,220 --> 00:25:43,370 local to that 540 00:25:43,369 --> 00:25:46,329 particular class, 541 00:25:46,329 --> 00:25:48,149 but - let me erase this. 542 00:25:48,150 --> 00:25:52,090 We can have a notion of an Instance Variable, 543 00:25:52,089 --> 00:25:54,970 and an Instance Variable 544 00:25:54,970 --> 00:26:00,029 refer to as affectionately, an Ivar. So if you're a big fan of seafood, that's probably familiar 545 00:26:00,029 --> 00:26:03,690 with you. Ivar for Instance Variable, it's just short for it, all right, 546 00:26:03,690 --> 00:26:07,289 is just basically a way of saying what I want to have is a variable that 547 00:26:07,289 --> 00:26:08,210 lives 548 00:26:08,210 --> 00:26:13,319 for everything in the object, so all methods can refer to the same variable. Now, 549 00:26:13,319 --> 00:26:16,659 let's contrast the notion of an Instance Variable with a Local Variable in terms 550 00:26:16,660 --> 00:26:18,159 of how they're declared, 551 00:26:18,159 --> 00:26:19,509 how we use them, 552 00:26:19,509 --> 00:26:23,048 what's going on, why we use them. So here is Instance Variable over here; here is 553 00:26:23,048 --> 00:26:24,160 Local Variable 554 00:26:24,160 --> 00:26:27,519 over here, which I'll just call the Local Var. We don't call local variable Lvars, 555 00:26:27,519 --> 00:26:29,410 we just call them locals, 556 00:26:29,410 --> 00:26:31,330 and Instance Variables are Ivars. So a Local Variable, as 557 00:26:31,329 --> 00:26:34,538 we talked about, this is declared - 558 00:26:34,538 --> 00:26:36,799 it's declared in a method, 559 00:26:36,799 --> 00:26:40,269 okay? 560 00:26:40,269 --> 00:26:43,289 Instance Variables are declared 561 00:26:43,289 --> 00:26:45,230 in a class 562 00:26:45,230 --> 00:26:47,569 but not a method. 563 00:26:47,569 --> 00:26:54,569 So what else have you seen that's declared inside a class but not inside a method? Uh huh, Constants. 564 00:26:54,890 --> 00:26:59,170 It turns out those Constants are actually Instance Variables, sorry. They're 565 00:26:59,170 --> 00:27:03,320 Instance Variables whose value is final. So we say they do not change, but those 566 00:27:03,319 --> 00:27:04,210 Instance Variables 567 00:27:04,210 --> 00:27:08,259 are visible to everyone inside that object; all the methods can see it. So 568 00:27:08,259 --> 00:27:10,950 Constants were an example of Instance Variables. We just didn't tell you at the 569 00:27:10,950 --> 00:27:12,490 time they were Instance Variables. 570 00:27:12,490 --> 00:27:16,120 So this guy is only visible in terms of its visibility, which we'll just call 571 00:27:16,119 --> 00:27:17,099 Vis 572 00:27:17,099 --> 00:27:23,048 in the method, and, as a matter of fact, it's lifetime - it only lives in the 573 00:27:23,048 --> 00:27:26,788 method, and when the method is done, the scope of this variable is basically gone, 574 00:27:26,788 --> 00:27:28,730 and it goes away. 575 00:27:28,730 --> 00:27:30,269 These puppies 576 00:27:30,269 --> 00:27:32,740 are visible 577 00:27:32,740 --> 00:27:34,799 in entire 578 00:27:34,799 --> 00:27:35,778 object. So 579 00:27:35,778 --> 00:27:40,769 in the class in which they are declared, when you create an object of that class, you get 580 00:27:40,769 --> 00:27:44,589 an Instance Variable; for every Instance Variable declared, you get one, 581 00:27:44,589 --> 00:27:46,959 and it's visible in the entire object. 582 00:27:46,960 --> 00:27:50,279 That means its lifetime is it lives 583 00:27:50,279 --> 00:27:51,759 as long 584 00:27:51,759 --> 00:27:54,490 as the object lives, 585 00:27:54,490 --> 00:27:58,170 which means once you call some particular method, if that method has local 586 00:27:58,170 --> 00:28:01,460 variables, they come into being, and when the method is done, they go away. 587 00:28:01,460 --> 00:28:05,110 When you create an object, its Instance Variables come into being, and they stay 588 00:28:05,109 --> 00:28:08,429 around until that object goes away, you're done using the object. That's how 589 00:28:08,430 --> 00:28:09,500 long they live. 590 00:28:09,500 --> 00:28:12,440 So you might say, "When do I use one versus the other? What's the point of 591 00:28:12,440 --> 00:28:14,250 having these two kinds of variables?" 592 00:28:14,250 --> 00:28:17,289 What you want to think about is you use Local Variables 593 00:28:17,289 --> 00:28:22,230 when the computation that you're gonna do only lives for the lifetime of the 594 00:28:22,230 --> 00:28:27,339 method that you actually care about, so you're doing some local computation, okay? So 595 00:28:27,339 --> 00:28:31,289 think of this local variable local computation. An 596 00:28:31,289 --> 00:28:35,149 Instance Variable is something where you need to store some value in between method 597 00:28:35,150 --> 00:28:36,790 calls. You can think of this 598 00:28:36,789 --> 00:28:39,149 as the state of the object; 599 00:28:39,150 --> 00:28:41,110 what state is actually in? 600 00:28:41,109 --> 00:28:44,740 So let me give you an example to, sort of, make this concrete because I think sometimes a 601 00:28:44,740 --> 00:28:46,779 specific example makes it a little more clear. 602 00:28:46,779 --> 00:28:48,690 Here's two water bottles. 603 00:28:48,690 --> 00:28:52,850 These are objects which were lovingly provided to me by the Stanford Computer Forum, 604 00:28:52,849 --> 00:28:56,549 okay? I have some class which is water bottle, and I 605 00:28:56,549 --> 00:28:57,200 say, 606 00:28:57,200 --> 00:29:00,569 "Oh, get me a new water bottle." So this is Water Bottle 1, 607 00:29:00,569 --> 00:29:02,798 and I say, "Get me another new water bottle." 608 00:29:02,798 --> 00:29:06,099 This is Water Bottle 2. Now, 609 00:29:06,099 --> 00:29:09,809 sometimes there are some methods I might want to call on this water bottle like 610 00:29:09,809 --> 00:29:13,259 unscrew the cap, and when I unscrew the cap, it turns out if I do four rotations 611 00:29:13,259 --> 00:29:17,160 of the cap, so I have a four loop, and I do four rotations of the cap - one, two, 612 00:29:17,160 --> 00:29:21,630 three, four, five - four, 613 00:29:21,630 --> 00:29:22,760 sub two, 614 00:29:22,759 --> 00:29:24,278 the cap comes off. 615 00:29:24,278 --> 00:29:28,130 I needed an Index Variable to keep track of how many times I turned the cap on 616 00:29:28,130 --> 00:29:29,680 the bottle, but 617 00:29:29,680 --> 00:29:33,580 I don't care about the value of that Index Variable after the cap is off, or if 618 00:29:33,579 --> 00:29:36,500 I say, "Put cap back on," and I screw it on four times, 619 00:29:36,500 --> 00:29:40,390 I did some local computation to figure out for the four loop how many times I went 620 00:29:40,390 --> 00:29:41,020 through it, 621 00:29:41,019 --> 00:29:44,869 but I don't care about that value if I go do something else with the water bottle now. 622 00:29:44,869 --> 00:29:47,619 But there are things I do care about the water bottle 623 00:29:47,619 --> 00:29:49,329 in between method calls, 624 00:29:49,329 --> 00:29:52,000 which is let's say I had some unnamed soda, 625 00:29:52,000 --> 00:29:55,420 and so I have Water Bottle 1, which I might have called unscrew on, and Water 626 00:29:55,420 --> 00:29:57,460 Bottle 2 that I called unscrew on, 627 00:29:57,460 --> 00:29:58,840 and so I said let me - 628 00:29:58,839 --> 00:29:59,709 I don't know if this 629 00:29:59,710 --> 00:30:01,940 is actually covered by copyright or not. Let 630 00:30:01,940 --> 00:30:03,719 me fill up the water bottles, 631 00:30:03,719 --> 00:30:06,330 oh, yay, much over here 632 00:30:06,329 --> 00:30:09,159 and a whole bunch over here, okay? 633 00:30:09,160 --> 00:30:10,629 So I call fill, 634 00:30:10,628 --> 00:30:14,418 the fill method, on each of the respective water bottles. Know that they have 635 00:30:14,419 --> 00:30:18,840 different amounts in them because the Instance Variables that I have, each object gets 636 00:30:18,839 --> 00:30:20,878 its own version of the Instance Variable. 637 00:30:20,878 --> 00:30:24,618 So I have some Instance Variable that basically tells me, let's say, a double what 638 00:30:24,618 --> 00:30:26,490 percentage of the bottle is full. 639 00:30:26,490 --> 00:30:28,960 When I say put the cap on, 640 00:30:28,960 --> 00:30:32,600 that percentage should still stick around, and when I take the cap off, 641 00:30:32,599 --> 00:30:35,029 that percentage better still stick around. 642 00:30:35,029 --> 00:30:39,049 That's part of the state of this object. In between method calls, when I send 643 00:30:39,049 --> 00:30:42,460 other messages to this object like put the cap on or take the cap off, 644 00:30:42,460 --> 00:30:44,549 I still need to be able to store 645 00:30:44,549 --> 00:30:46,789 how much is actually in the bottle 646 00:30:46,789 --> 00:30:48,329 between method calls. 647 00:30:48,329 --> 00:30:52,359 Whereas, how many times I unscrew it is a local computation. I don't care about 648 00:30:52,359 --> 00:30:55,759 storing that value after I've finished unscrewing or putting it back on. 649 00:30:55,759 --> 00:30:58,089 That would be something I would use the Local Variable. 650 00:30:58,089 --> 00:31:01,959 So think of Instance Variable as the thing that state what you need to 651 00:31:01,960 --> 00:31:03,058 store 652 00:31:03,058 --> 00:31:06,349 in between calls to methods that is actually part of the state of the object 653 00:31:06,349 --> 00:31:08,119 that you care about, okay? 654 00:31:08,119 --> 00:31:15,119 Any questions about Instance Variables versus Local Variables? Uh huh? Are 655 00:31:15,529 --> 00:31:18,308 Instance Variables the same thing as Global Variables? 656 00:31:18,308 --> 00:31:21,019 We're not gonna talk about Global Variables. So if you've used another 657 00:31:21,019 --> 00:31:23,788 language that has Global Variables, they're not the same thing, and 658 00:31:23,788 --> 00:31:25,329 Global Variables are just real bad style. 659 00:31:25,329 --> 00:31:32,329 So one way to have you not use them is to not tell you about them. All right. So 660 00:31:32,710 --> 00:31:36,100 given our little example with the water bottle, 661 00:31:36,099 --> 00:31:39,379 one thing we can actually think of is about our little friend, random number 662 00:31:39,380 --> 00:31:43,900 generator. How do we use - this is an Instance Variable inside a program, okay? 663 00:31:43,900 --> 00:31:48,100 So if we have some program - I'll call this Simple Random which just extends the 664 00:31:48,099 --> 00:31:51,529 console program. This is just a program that's gonna generate random numbers. 665 00:31:51,529 --> 00:31:54,950 Here is my Run method over here. I'm gonna fill this in in just a second. 666 00:31:54,950 --> 00:32:00,048 Where I actually declare my Instance Variable is outside of the scope of any 667 00:32:00,048 --> 00:32:05,089 particular method, but it's inside my class just like when you declared Constants, okay? 668 00:32:05,089 --> 00:32:09,658 So here I say Private Random Gen Rgen equals RandomGenerator.GetInstants. 669 00:32:09,659 --> 00:32:14,200 So what this does is whenever I create a new object of this class, 670 00:32:14,200 --> 00:32:15,440 it will 671 00:32:15,440 --> 00:32:19,389 initialize this variable Rgen to be getting an instance of Random Generator, 672 00:32:19,388 --> 00:32:21,219 and that variable, Rgen, 673 00:32:21,220 --> 00:32:24,480 is visible to all of the methods inside my class, 674 00:32:24,480 --> 00:32:28,410 and its lifetime will exist until that object goes away - until I'm actually 675 00:32:28,410 --> 00:32:32,710 done with the object, okay? So 676 00:32:32,710 --> 00:32:35,690 there's a bunch of things I can do with a Random Generator, right? So 677 00:32:35,690 --> 00:32:38,690 besides the fact here that I want to store it as an Instance Variable, you're like, "Okay, 678 00:32:38,690 --> 00:32:41,240 that's great. Now you want to throw all this rigmarole, and you told me about Local Variables 679 00:32:41,240 --> 00:32:44,089 and Instance Variables just so I could store this in Instance Variable. 680 00:32:44,089 --> 00:32:46,699 What do I do with the Random Generator, Mehran?" 681 00:32:46,700 --> 00:32:50,140 All right. So here is the things you can do with a Random Generator. So once you have gotten an 682 00:32:50,140 --> 00:32:52,660 instance of the Random Generator - 683 00:32:52,660 --> 00:32:56,769 man, I hate technology. 684 00:32:56,769 --> 00:32:58,900 We're going old school 685 00:32:58,900 --> 00:33:01,100 because sometimes in life 686 00:33:01,099 --> 00:33:03,480 you get a little upset, 687 00:33:03,480 --> 00:33:06,610 and if the things you get upset about really are just slides that you have to go 688 00:33:06,609 --> 00:33:07,908 through twice in a lecture, 689 00:33:07,909 --> 00:33:10,489 it's really not that bad. Like, in the overall scheme of things, it's, kind of, like, 690 00:33:10,489 --> 00:33:14,120 yeah, you know, there's other things to get upset about like global warming, 691 00:33:14,119 --> 00:33:19,079 but it's like, yeah, I gotta go through these slides twice. Oh, my god, I'm so upset. All right. 692 00:33:19,079 --> 00:33:20,710 Random Generator, 693 00:33:20,710 --> 00:33:22,700 here are some of the things you can do with a Random Generator, 694 00:33:22,700 --> 00:33:25,929 okay? So once you have this thing called Rgen, there are some methods that you 695 00:33:25,929 --> 00:33:29,090 can call on, and one of them says Next Integer, right? We talked about this 696 00:33:29,089 --> 00:33:31,388 thing being a black box, say give me the next random number, 697 00:33:31,388 --> 00:33:32,689 and it gives it to you. 698 00:33:32,690 --> 00:33:37,019 So if you say Rgen.NextInt, you can give it a range. Say, that range being, if 699 00:33:37,019 --> 00:33:39,609 you want to simulate, like, rolling a dice, 700 00:33:39,609 --> 00:33:42,869 one in six, and what you'll get is a random number between one and six that's returned 701 00:33:42,869 --> 00:33:44,059 to you as an integer, 702 00:33:44,059 --> 00:33:47,480 or it can take a single parameter, you can call Next Int just giving it a single 703 00:33:47,480 --> 00:33:48,489 value like ten, 704 00:33:48,489 --> 00:33:52,278 and what you'll get back is a number between zero and N minus one, or in that case, you would get a 705 00:33:52,278 --> 00:33:54,119 value back between zero and nine 706 00:33:54,119 --> 00:33:57,159 because, as computer scientists, we always start counting from zero. 707 00:33:57,160 --> 00:33:59,130 Besides integers, sometimes you want 708 00:33:59,130 --> 00:34:02,670 doubles or real values, so a similar sort of thing. There is next double where you give it 709 00:34:02,670 --> 00:34:03,548 a range, 710 00:34:03,548 --> 00:34:05,740 and you get a value, strangely enough, 711 00:34:05,740 --> 00:34:06,719 between 712 00:34:06,719 --> 00:34:10,500 less than or equal to low but strictly less than high. It's like an open interval kind 713 00:34:10,500 --> 00:34:13,349 of thing, but really, for all intensive purposes, you're getting real values, 714 00:34:13,349 --> 00:34:16,879 you don't really care because the chance of hitting the interval is not a big deal. 715 00:34:16,878 --> 00:34:18,099 So it's 716 00:34:18,099 --> 00:34:20,278 inclusive of the low interval but 717 00:34:20,278 --> 00:34:23,148 exclusive of the high range of the interval, but what it gives you is a double basically in the 718 00:34:23,148 --> 00:34:24,659 range from low to high, 719 00:34:24,659 --> 00:34:27,569 and you can also call next double without giving it a range, and you'll get a 720 00:34:27,570 --> 00:34:29,859 value based between zero and one, okay? 721 00:34:29,858 --> 00:34:33,898 Just if you want because a lot of times people ask for values between zero and one. 722 00:34:33,898 --> 00:34:36,128 There is also next Boolean, 723 00:34:36,128 --> 00:34:38,898 which just gives you - if you don't call with any parameters it's basically like 724 00:34:38,898 --> 00:34:39,808 flipping a coin. 725 00:34:39,809 --> 00:34:42,729 It'll give you back true or false 50/50, 726 00:34:42,728 --> 00:34:45,448 but if you care about having your coin be biased, right? There's actually some 727 00:34:45,449 --> 00:34:48,280 people in the world, including some professors in our statistics department 728 00:34:48,280 --> 00:34:49,200 who can actually 729 00:34:49,199 --> 00:34:50,250 flip a coin 730 00:34:50,250 --> 00:34:53,159 where they can flip it in such a way that they can affect the probability of 731 00:34:53,159 --> 00:34:57,329 how often it comes up heads and tails - pretty frickin' cool, 732 00:34:57,329 --> 00:35:00,068 but we won't talk about that. Next Boolean you give it a double, 733 00:35:00,068 --> 00:35:03,889 and so what it says is it will return true with probability P. So if you set 734 00:35:03,889 --> 00:35:06,420 P to be .5, it's the same thing as this case up here, 735 00:35:06,420 --> 00:35:11,318 but you can set it to be different if you want. P has to be between zero and one because it's probability. 736 00:35:11,318 --> 00:35:14,599 Last, but not least, just to be funky there's something called Next Color. So 737 00:35:14,599 --> 00:35:15,860 you can, sort of, say, oh, 738 00:35:15,860 --> 00:35:19,309 I'm feeling adventurous, and I want to just paint the world random colors. 739 00:35:19,309 --> 00:35:23,259 Get me a random color. So just next color and it has no parameters, and it just gives you a 740 00:35:23,260 --> 00:35:26,640 random color. It's like there you go, good times. 741 00:35:26,639 --> 00:35:29,778 It's fun every once - if you have no fashion sense, like me, that's, like, what I do 742 00:35:29,778 --> 00:35:32,239 every morning when I wake up, and I put on clothes. I'm just like, 743 00:35:32,239 --> 00:35:34,260 "Next color," and I walk out, and my wife's like, 744 00:35:34,260 --> 00:35:40,639 "No, no, no, no, no, no." And she actually has a deterministic algorithm for dressing me. All right. So 745 00:35:40,639 --> 00:35:43,959 here is a little example of how that actually works. So all I did was I took 746 00:35:43,958 --> 00:35:48,028 our program that we had before, our simple random, and filled in the Run method. 747 00:35:48,028 --> 00:35:49,230 What's going on when 748 00:35:49,230 --> 00:35:53,969 this, an object of simple random gets created or when this program is run, 749 00:35:53,969 --> 00:35:57,929 Rgen gets initialized to be an instance of the Random Generator, and now I can 750 00:35:57,929 --> 00:36:01,259 refer to it, because it's an Instance Variable, in any method, 751 00:36:01,260 --> 00:36:04,740 right? So Run is just some other method. I can say Int die roll 752 00:36:04,739 --> 00:36:08,379 is a random generated number. Give me the next Int between one and six, 753 00:36:08,380 --> 00:36:11,798 and then it will just print out, "You rolled," and whatever the random value was. So it's 754 00:36:11,798 --> 00:36:13,409 just simulating one die roll. 755 00:36:13,409 --> 00:36:15,019 Not a lot of excitement going on there, 756 00:36:15,019 --> 00:36:17,849 but this is just to show you how the syntax actually looks for 757 00:36:17,849 --> 00:36:20,728 generating a random number. Okay? 758 00:36:20,728 --> 00:36:23,139 So any question about that? 759 00:36:23,139 --> 00:36:26,469 Let me show you a slightly more involved program. 760 00:36:26,469 --> 00:36:29,940 So we can get rid of this craziness, 761 00:36:29,940 --> 00:36:32,588 and here's a little program that rolls some dice. 762 00:36:32,588 --> 00:36:35,940 So what this does - notice now I have the imports and everything in here. I need to 763 00:36:35,940 --> 00:36:39,309 import acm.util.star if I actually want to use the random number 764 00:36:39,309 --> 00:36:40,280 generator, 765 00:36:40,280 --> 00:36:44,269 and now just to be a little bit more complicated, I'm gonna show you an example of having 766 00:36:44,269 --> 00:36:46,500 Constants and Instance Variables together because just where we put 767 00:36:46,500 --> 00:36:48,210 them 768 00:36:48,210 --> 00:36:51,519 and what the convention is in the book is just a little bit funky. So 769 00:36:51,519 --> 00:36:55,909 here's the whole class. Let me extend the window a little bit so you can see the whole class. 770 00:36:55,909 --> 00:37:01,248 Well, you can almost see the whole class. Let's just assume you saw the top line up there, okay? 771 00:37:01,248 --> 00:37:04,788 What we have up at the top is a Constant, which is the number of sides on 772 00:37:04,789 --> 00:37:07,959 some dice we're gonna roll. So I should just ask anyone here ever, like, you 773 00:37:07,958 --> 00:37:10,949 know, played a game that involved 20-sided dice? 774 00:37:10,949 --> 00:37:13,668 Anyone? You can admit it. It's okay. I did too. 775 00:37:13,668 --> 00:37:16,228 12-step program, you'll be fine. 776 00:37:16,228 --> 00:37:20,288 Num Sides, in this case, happens to be six. So we say, "Private Static Final Ints," all 777 00:37:20,289 --> 00:37:23,950 the garbage that we have to save for having a Constant. It's not garbage, it's just 778 00:37:23,949 --> 00:37:25,710 lovely syntax 779 00:37:25,710 --> 00:37:28,548 that's of type integer. It's Num Sides is six. 780 00:37:28,548 --> 00:37:31,918 So inside this program, what it's gonna do is simulate rolling some 781 00:37:31,918 --> 00:37:35,138 number of dice until you get the maximal value on all the dice. So if 782 00:37:35,139 --> 00:37:36,559 you're rolling one die, 783 00:37:36,559 --> 00:37:39,548 it keeps rolling until it gets a six, and it tells me how many times it has to roll 784 00:37:39,548 --> 00:37:40,429 to get a six. 785 00:37:40,429 --> 00:37:44,200 If I'm rolling three dice, the maximum value's 18 because I need three six's. 786 00:37:44,199 --> 00:37:47,739 So it keeps rolling until it gets an 18 and tells me how many rolls are involved. 787 00:37:47,739 --> 00:37:50,028 So what's going on here is I first 788 00:37:50,028 --> 00:37:53,759 get the number of dice, the local variable, from the user by asking the 789 00:37:53,759 --> 00:37:55,528 user for the number of dice. 790 00:37:55,528 --> 00:37:58,748 They give me the number of dice, and I compute what's the maximum roll? Well, it's just 791 00:37:58,748 --> 00:38:02,298 the number of dice times the number of sides on the dice because that's the maximum 792 00:38:02,298 --> 00:38:05,889 value for each die, which is the singular form of dice if you're wondering. But 793 00:38:05,889 --> 00:38:07,429 that's the maximum value. 794 00:38:07,429 --> 00:38:11,960 I have another local variable, Num Rolls, that I initialized to be zero, 795 00:38:11,960 --> 00:38:16,119 and I'm gonna have a loop and a half, basically, where I say, "While true." 796 00:38:16,119 --> 00:38:19,619 It's not an infinite loop because we're gonna have a break inside. 797 00:38:19,619 --> 00:38:24,129 Int roll equals roll dice, the number of dice I want you to roll. So 798 00:38:24,128 --> 00:38:26,569 over here, I have some other method 799 00:38:26,570 --> 00:38:30,499 down here, which I'll just scroll down to down here so you can see the whole method, 800 00:38:30,498 --> 00:38:32,628 called Roll Dice. 801 00:38:32,628 --> 00:38:37,268 Roll Dice takes in a parameter called Num Dice. It's getting a copy of that 802 00:38:37,268 --> 00:38:41,769 value. It says I'm gonna have sum totaled at zero. What I'm going to do is 803 00:38:41,769 --> 00:38:46,009 have a four loop that counts up from I up to the number of dice you told me to 804 00:38:46,010 --> 00:38:46,778 roll, 805 00:38:46,778 --> 00:38:50,679 and every time I'm just gonna generate a random number between one and the number 806 00:38:50,679 --> 00:38:55,398 of sides on the die, and add it to my total. So that simulates rolling the dice, 807 00:38:55,398 --> 00:38:56,969 Num Dice number of times, 808 00:38:56,969 --> 00:39:00,088 and when I'm done, I've gotten the total, and I'm gonna return that to you. 809 00:39:00,088 --> 00:39:01,179 So up here, 810 00:39:01,179 --> 00:39:03,168 when I roll dice, what I get back 811 00:39:03,168 --> 00:39:06,529 from my roll is a number that is just the sum of 812 00:39:06,530 --> 00:39:08,090 that number of dice rolled 813 00:39:08,090 --> 00:39:09,749 in terms of random numbers. 814 00:39:09,748 --> 00:39:14,358 I say, okay, now that I've rolled the dice, increment my number of rolls by one. 815 00:39:14,358 --> 00:39:18,318 If the roll that I got was the maximum roll, then I'm done. Hey, I just rolled an 816 00:39:18,318 --> 00:39:21,369 18 if I have three dice or whatever, and I break out of this loop. 817 00:39:21,369 --> 00:39:24,749 If I'm not yet done, I say, hey, you rolled whatever your roll was, and I go 818 00:39:24,748 --> 00:39:28,768 through this loop again, and roll the dice again, and write out the value again 819 00:39:28,768 --> 00:39:29,179 until 820 00:39:29,179 --> 00:39:32,558 I finally get Max Roll, and when I get Max Roll I say, hey, you 821 00:39:32,559 --> 00:39:36,749 rolled whatever that Max Roll was after some number of rolls. So it tells the user how 822 00:39:36,748 --> 00:39:40,208 many times I had to roll before I got that Max Roll, okay? 823 00:39:40,208 --> 00:39:41,779 Now, one thing you might say is, 824 00:39:41,780 --> 00:39:45,900 "Hey, Mehran, you had this thing called Num Dice up there, right, and you have this 825 00:39:45,900 --> 00:39:47,650 thing called Num Dice over here, 826 00:39:47,650 --> 00:39:50,930 and you happen to be passing a parameter called Num Dice." 827 00:39:50,929 --> 00:39:55,210 Again, we're making copies, right? I can't directly - if I didn't have this value 828 00:39:55,210 --> 00:39:58,409 here for a parameter, I can't directly refer to Num Dice 829 00:39:58,409 --> 00:40:01,618 because Num Dice is declared in another method; it lives in another 830 00:40:01,619 --> 00:40:02,150 hood. 831 00:40:02,150 --> 00:40:05,769 The only way I can refer to Num Dice if it's passed into me as a parameter, and 832 00:40:05,769 --> 00:40:07,318 I give it the same name. 833 00:40:07,318 --> 00:40:10,679 So even if Num Dice was passed in as a parameter, if this was Int X here, 834 00:40:10,679 --> 00:40:13,529 I still couldn't say Num Dice. I'd have to say X, 835 00:40:13,530 --> 00:40:16,410 because the only things you can refer to in a method - and this is an important 836 00:40:16,409 --> 00:40:19,318 rule. This is one of those ones that, sort of, like, you know, 837 00:40:19,318 --> 00:40:22,949 just shave it on your arm if you happen to have arm hair, and if you don't, 838 00:40:22,949 --> 00:40:25,308 just write it on a piece of paper. 839 00:40:25,309 --> 00:40:29,769 What you want to do inside a method - what can you refer to? You can refer to the local 840 00:40:29,768 --> 00:40:31,318 variables inside that method. 841 00:40:31,318 --> 00:40:35,849 You can refer to parameters of that method, and you can refer to Instance 842 00:40:35,849 --> 00:40:38,548 Variables of the object, okay? 843 00:40:38,548 --> 00:40:41,548 So here is the Instance Variable down here 844 00:40:41,548 --> 00:40:44,699 is our little random number generator, and I put it all on one line. It 845 00:40:44,699 --> 00:40:47,150 slightly goes - the semicolon, sort of, goes off the screen, 846 00:40:47,150 --> 00:40:50,880 but here is my Instance Variable. So you can refer to Local Variables, Instance 847 00:40:50,880 --> 00:40:53,930 Variables, and Parameters. And you might say, "But, 848 00:40:53,929 --> 00:40:59,029 Mehran, [inaudible] Num Sides; isn't that a Constant?" Yeah, Constants are just a special form, 849 00:40:59,030 --> 00:41:01,729 basically, of Instance Variables, 850 00:41:01,728 --> 00:41:03,529 okay? So the other thing you might say is, 851 00:41:03,530 --> 00:41:06,860 "So why did you split them up? Why are the Instance Variables down here and the 852 00:41:06,860 --> 00:41:08,140 Constants up there?" That's 853 00:41:08,139 --> 00:41:09,719 just convention. 854 00:41:09,719 --> 00:41:13,688 Convention in the Java world is all the Constants get defined up at the top, 855 00:41:13,688 --> 00:41:15,690 even though they might be Instance Variables, 856 00:41:15,690 --> 00:41:18,869 and all of the Instance Variables that are actual variables, that are not 857 00:41:18,869 --> 00:41:25,869 final, they can actually change, are declared down at the bottom. Uh huh? [Off mic]. 858 00:41:31,398 --> 00:41:34,699 If it's a Private Instance Variable, and you extend it, no, but we'll get into that in a 859 00:41:34,699 --> 00:41:37,398 couple weeks, and we'll talk more about public and private when we get 860 00:41:37,398 --> 00:41:44,398 more into classes. Uh huh? [Off mic]. 861 00:41:45,469 --> 00:41:48,278 It does print out the number of rolls it took. Okay. 862 00:41:48,278 --> 00:41:51,798 That's up here, right? It says rolled whatever Max Roll was after Num Rolls. 863 00:41:51,798 --> 00:41:55,139 Num Rolls is just the local variable up there that I keep track of. 864 00:41:55,139 --> 00:42:02,099 Question in the back? [Off mic]. 865 00:42:02,099 --> 00:42:05,028 Yeah, these names, as long as they're consistent, could be anything else. This 866 00:42:05,028 --> 00:42:06,639 could be X here and X there 867 00:42:06,639 --> 00:42:09,889 because all this guy cares about is I'm getting some value passed into me. What 868 00:42:09,889 --> 00:42:11,179 am I getting passed in? 869 00:42:11,179 --> 00:42:14,579 I happen to be getting passed a copy of Num Dice, but I don't have to name it the 870 00:42:14,579 --> 00:42:16,889 same thing; I could name it whatever I want. 871 00:42:16,889 --> 00:42:21,219 The reason why I named it the same thing is I wanted you to know that 872 00:42:21,219 --> 00:42:22,999 even if you name it the same thing, 873 00:42:22,998 --> 00:42:27,528 you're not getting a copy of the variable you're - yeah, 874 00:42:27,528 --> 00:42:30,268 you're getting beamed with a piece of candy. 875 00:42:30,268 --> 00:42:33,518 You're just getting a copy; you're not getting the actual variable. 876 00:42:33,518 --> 00:42:36,758 So one other thing I want to mention to you real quickly is 877 00:42:36,759 --> 00:42:40,989 when random values come up, it becomes real hard, sometimes, to write 878 00:42:40,989 --> 00:42:41,539 programs 879 00:42:41,539 --> 00:42:43,550 because every time you run the program, 880 00:42:43,550 --> 00:42:47,289 you get a different set of random numbers, right? That's just the whole point of 881 00:42:47,289 --> 00:42:49,920 random numbers. If they were the same every time you ran the program, 882 00:42:49,920 --> 00:42:52,659 that would be, sort of, bad times, right? You'd be playing the same game over and 883 00:42:52,659 --> 00:42:53,528 over. 884 00:42:53,528 --> 00:42:56,748 So how do we actually generate random numbers? What's the random number all 885 00:42:56,748 --> 00:42:57,578 about? 886 00:42:57,579 --> 00:43:01,719 Well, remember the old black box. The way this black box works - this is the 887 00:43:01,719 --> 00:43:04,269 Random Generator, which I'll just say RG. 888 00:43:04,268 --> 00:43:05,649 It's, sort of, weird. 889 00:43:05,650 --> 00:43:09,139 When you ask it for a number, it gives you back a number like five, 890 00:43:09,139 --> 00:43:10,528 and then secretly, 891 00:43:10,528 --> 00:43:13,958 sort of, secretly we've replaced our Random Generator's regular coffee with 892 00:43:13,958 --> 00:43:15,848 our new flaked coffee. 893 00:43:15,849 --> 00:43:19,249 Secretly, what it does - anyone remember that commercial? God, I 894 00:43:19,248 --> 00:43:21,868 am so old. All right. 895 00:43:21,869 --> 00:43:26,068 It secretly saves that number and uses that number through some complicated 896 00:43:26,068 --> 00:43:29,079 equation that you don't need to worry about because this is the black box, 897 00:43:29,079 --> 00:43:31,969 to generate the next number when you need the next number, 898 00:43:31,969 --> 00:43:34,699 and when you say next number again, it does the same thing. So when you ask 899 00:43:34,699 --> 00:43:38,398 for next number, it might've given you six. It stores that six, and the next time you ask for a 900 00:43:38,398 --> 00:43:40,679 number, it does something with the six, and scrambles 901 00:43:40,679 --> 00:43:44,769 it, and chops it up, and reformulates it, and it says, oh, 17. okay? 902 00:43:44,768 --> 00:43:47,578 So 903 00:43:47,579 --> 00:43:50,839 the thing is, you don't know what number this thing actually started with to 904 00:43:50,838 --> 00:43:53,568 begin with. As a matter of fact, the number that it starts with has to do with the 905 00:43:53,568 --> 00:43:55,099 time on your computer 906 00:43:55,099 --> 00:43:57,088 in sixtieth of a second, 907 00:43:57,088 --> 00:43:58,469 or actually, in thousandths of a second. 908 00:43:58,469 --> 00:44:01,528 So most people don't know what that was, and to the extent that you don't know what 909 00:44:01,528 --> 00:44:03,778 that was, you can't figure out what the sequence is. 910 00:44:03,778 --> 00:44:06,458 The only problem is if you don't know what that was, and you run your 911 00:44:06,458 --> 00:44:10,989 program, and it's got a bug. First time you run your program it crashes because you got 17, 912 00:44:10,989 --> 00:44:14,318 and you're like, "Oh, I need to go find that bug." So you run your program again, but 913 00:44:14,318 --> 00:44:18,170 this time instead of getting 17, you get a six, and your program works just fine. So you're 914 00:44:18,170 --> 00:44:20,989 like, "Oh, bummer." And you run it again, and you get eight, and your program works just 915 00:44:20,989 --> 00:44:24,869 fine. You say, "Oh, it must just work. Let me get ready to submit it." And the last time you're ready to 916 00:44:24,869 --> 00:44:27,929 submit it, you get a nine, and it crashes. 917 00:44:27,929 --> 00:44:29,528 That's real frustrating. 918 00:44:29,528 --> 00:44:32,938 That makes you want to kill. 919 00:44:32,938 --> 00:44:37,038 I don't want anyone to kill, right, because if there's 300 of you and one of me, 920 00:44:37,039 --> 00:44:41,380 one of the people who might get killed in this process is me. So 921 00:44:41,380 --> 00:44:45,009 what you can do is you can say I want you to generate the same sequence of 922 00:44:45,009 --> 00:44:46,798 random numbers ever time. 923 00:44:46,798 --> 00:44:50,139 How do I do that? I need to tell you that the number that you're gonna start 924 00:44:50,139 --> 00:44:51,598 with the very first time 925 00:44:51,599 --> 00:44:55,809 is some specific value so you will always generate the same sequence given that 926 00:44:55,809 --> 00:44:56,660 first number, 927 00:44:56,659 --> 00:44:59,179 and that first number is something we refer to as 928 00:44:59,179 --> 00:45:00,989 the Seed. It's like the 929 00:45:00,989 --> 00:45:03,349 number from which all other numbers sprout, 930 00:45:03,349 --> 00:45:06,278 and how do I set this seed? 931 00:45:06,278 --> 00:45:08,518 It's extremely complicated 932 00:45:08,518 --> 00:45:10,308 - actually, not at all. 933 00:45:10,309 --> 00:45:14,349 If I had Rgen, which is my 934 00:45:14,349 --> 00:45:16,550 random number generator, this is my 935 00:45:16,550 --> 00:45:17,650 Instance Variable, 936 00:45:17,650 --> 00:45:19,139 which is my random number generator, 937 00:45:19,139 --> 00:45:22,038 I say .SetSeed, 938 00:45:22,039 --> 00:45:25,839 and I give it the value of the seed. A real fun seed to use, by the way, in case 939 00:45:25,838 --> 00:45:29,869 you're wondering, and you're having difficulty coming up with a seed is one, okay? 940 00:45:29,869 --> 00:45:31,919 So set seed equal to one. 941 00:45:31,918 --> 00:45:35,098 You do this somewhere at the beginning of your program, and all the random numbers 942 00:45:35,099 --> 00:45:37,740 you generate in each run of your program 943 00:45:37,739 --> 00:45:41,618 will be the same set. You won't always get the same random number 944 00:45:41,619 --> 00:45:44,979 each time you call for next random; what you'll get is the same random sequence. So if 945 00:45:44,978 --> 00:45:49,448 you happen to get the sequence seven, five, then nine, two, three - I don't know, you 946 00:45:49,449 --> 00:45:50,280 know, whatever, 947 00:45:50,280 --> 00:45:53,230 the next time you run your program, you'll get the same sequence, and that 948 00:45:53,230 --> 00:45:55,130 makes it much easier for debugging. 949 00:45:55,130 --> 00:45:59,309 So let me show you a quick example of a program that actually uses that 950 00:45:59,309 --> 00:46:01,459 which will take 951 00:46:01,458 --> 00:46:03,409 our simple random program, 952 00:46:03,409 --> 00:46:05,548 which you saw in a previous slide, 953 00:46:05,548 --> 00:46:07,869 and add the little seed, okay? 954 00:46:07,869 --> 00:46:11,219 So now if we happen to run this program 955 00:46:11,219 --> 00:46:13,798 - we're 956 00:46:13,798 --> 00:46:16,900 running; we're feeling good. We want to run simple random. 957 00:46:16,900 --> 00:46:19,249 It's a good time. You 958 00:46:19,248 --> 00:46:23,218 rolled a five, and we're like, oh, that's great. That's wonderful. Let me run this 959 00:46:23,219 --> 00:46:23,898 again. 960 00:46:23,898 --> 00:46:27,418 So we run it again. We, sort of, do the quick - well, you rolled a five, 961 00:46:27,418 --> 00:46:31,158 right? Notice that the seed was actually set to one, so I'm not getting a one; I'm 962 00:46:31,159 --> 00:46:34,459 getting whatever would've happened after I put one through this complicated function. 963 00:46:34,458 --> 00:46:37,828 I could go in here and change the seed to something else if I wanted to. I could change 964 00:46:37,829 --> 00:46:39,190 it to three, 965 00:46:39,190 --> 00:46:41,019 and the next time I run this program, 966 00:46:41,019 --> 00:46:44,068 I'll just happen to get a different 967 00:46:44,068 --> 00:46:47,599 initial value for that random number, but the sequence of random numbers I get 968 00:46:47,599 --> 00:46:51,309 will be the same based on the number three. So if I do 969 00:46:51,309 --> 00:46:53,640 simple random based on three, 970 00:46:53,639 --> 00:46:54,828 you rolled five. It 971 00:46:54,829 --> 00:46:57,450 just turns out one and three give you the same thing. 972 00:46:57,449 --> 00:47:00,078 That's life in the city. 973 00:47:00,079 --> 00:47:04,150 Maybe Mehran should've done a little more testing before class. All right. Uh 974 00:47:04,150 --> 00:47:10,898 huh, question? 975 00:47:10,898 --> 00:47:14,298 [Off mic]. The sequence is always based on whatever that first value was. So if it 976 00:47:14,298 --> 00:47:17,378 was one and then you changed it to three and got a different sequence, and you changed it back 977 00:47:17,378 --> 00:47:27,728 to one, you're gonna get that same sequence based on one. Uh huh? [Off mic] 978 00:47:27,728 --> 00:47:30,538 No, the seed always just sets one number. It'll automatically 979 00:47:30,539 --> 00:47:34,110 do the appropriate conversions to whatever you want, but you should always just set the seed, 980 00:47:34,110 --> 00:47:41,088 like, one or whatever you want it to be. 981 00:47:41,088 --> 00:47:43,659 Uh huh? [Off mic]. Oh, no. The seed doesn't actually have to be in your inter - 982 00:47:43,659 --> 00:47:46,109 if you have an inter. It's a good question, but it doesn't have to be. 983 00:47:46,110 --> 00:47:49,360 The value of the seed, in most cases, is actually entirely irrelevant because 984 00:47:49,360 --> 00:47:52,630 basically just add this when you do debugging, and when you think your program 985 00:47:52,630 --> 00:47:53,798 actually runs, 986 00:47:53,798 --> 00:47:57,929 then guess what? You take this puppy out, and you see if it still runs, 987 00:47:57,929 --> 00:48:01,259 and then if it doesn't run after you take that out, you try a different seed because 988 00:48:01,259 --> 00:48:04,269 it's gonna give you a different sequence in numbers, all right? 989 00:48:04,268 --> 00:48:09,848 Any other questions? Uh huh, one more question there? [Off mic]. 990 00:48:09,849 --> 00:48:15,309 Oh, can you use the microphone, please? If I use the 991 00:48:15,309 --> 00:48:19,559 same seed in different computers, does it give the same sequence? Oh, if 992 00:48:19,559 --> 00:48:25,009 you use the same seed on different computers, it should give you the same sequence, 993 00:48:25,009 --> 00:48:26,880 yeah. Alrighty. Any other questions? I 994 00:48:26,880 --> 00:48:27,910 will see you on Friday then.