1 00:00:10,558 --> 00:00:12,979 2 00:00:12,980 --> 00:00:16,240 This presentation is delivered by the Stanford Center for Professional 3 00:00:16,239 --> 00:00:23,239 Development. 4 00:00:24,439 --> 00:00:27,300 All right. So one real quick point 5 00:00:27,300 --> 00:00:31,320 before we dive into the main meat of the lecture, it's just a clarification on 6 00:00:31,320 --> 00:00:33,520 something we did last time called The Cast. 7 00:00:33,520 --> 00:00:36,609 So remember last time where we had a cast, which was this 8 00:00:36,609 --> 00:00:39,179 thing that allowed us to say treat this one 9 00:00:39,179 --> 00:00:42,490 data point, or this one data item, this one variable 10 00:00:42,490 --> 00:00:45,710 as a different type for one particular operation. 11 00:00:45,710 --> 00:00:49,320 And so last time I told you you could actually do something like int X 12 00:00:49,320 --> 00:00:51,659 equals - let's say we had some double up here, 13 00:00:51,659 --> 00:00:53,359 like YY, 14 00:00:53,359 --> 00:00:57,060 and maybe Y got some value like 3.5, 15 00:00:57,060 --> 00:00:59,670 and I said you could do a cast like this, 16 00:00:59,670 --> 00:01:03,969 and we put Y here, right? And so that'll take Y. It'll truncate it, right? It drops 17 00:01:03,969 --> 00:01:07,349 everything out of the decimal point and assigns it to an int. And 18 00:01:07,349 --> 00:01:08,938 I, sort of, said, 19 00:01:08,938 --> 00:01:13,009 misspeaking, actually, that you could drop this part. It turns out 20 00:01:13,010 --> 00:01:14,389 that in Java, 21 00:01:14,388 --> 00:01:18,170 any time you do a cast of something, that loses information. Like, when you go from 22 00:01:18,170 --> 00:01:21,509 a double to an integer because it truncates, it drops everything after the 23 00:01:21,509 --> 00:01:22,239 decimal point, 24 00:01:22,239 --> 00:01:25,589 you have to put in the cast, so you have to make this explicit. 25 00:01:25,590 --> 00:01:28,450 If you go the other way around where you're not losing information, like, if 26 00:01:28,450 --> 00:01:32,740 you were to have Y equals X at some point where you know that X is an int and Y 27 00:01:32,739 --> 00:01:36,808 is a double, you're not gonna lose information by assigning an int to a double. 28 00:01:36,808 --> 00:01:38,179 So there you're okay. 29 00:01:38,180 --> 00:01:42,720 You can do the explicit cast if you want, and actually I would encourage you, actually, to 30 00:01:42,719 --> 00:01:46,079 use the explicit cast, but in this case, you don't need it. 31 00:01:46,079 --> 00:01:49,599 In this case over here you actually do. So when you lose information, you need to 32 00:01:49,599 --> 00:01:52,459 make an explicit cast. For most of you, it probably won't affect your day-to-day 33 00:01:52,459 --> 00:01:53,529 life, 34 00:01:53,530 --> 00:01:55,500 but that's just something you should know. 35 00:01:55,500 --> 00:01:56,430 Alrighty. 36 00:01:56,430 --> 00:01:58,789 So any questions about anything we've done 37 00:01:58,789 --> 00:02:00,829 up to now? 38 00:02:00,829 --> 00:02:03,649 We're just gonna, kind of, press ahead with something that we've talked about last 39 00:02:03,650 --> 00:02:06,530 time. Remember last time we're talking about our friend the while loop? 40 00:02:06,530 --> 00:02:10,319 And the while loop was something that you used when you don't know how many 41 00:02:10,319 --> 00:02:13,998 times you're gonna do something. This is what we refer to as an 42 00:02:13,998 --> 00:02:17,418 indefinite loop - not an infinite loop, right? It's important that you have "de" in 43 00:02:17,419 --> 00:02:22,029 there. Infinite loop keeps going forever, but an indefinite loop is one that when it 44 00:02:22,028 --> 00:02:24,789 starts out, you don't know how many times you're gonna do something. You're just 45 00:02:24,789 --> 00:02:26,620 gonna do it some number of times. 46 00:02:26,620 --> 00:02:28,800 Well, it turns out there's times in life where 47 00:02:28,800 --> 00:02:32,040 you're gonna do something some number of times. You don't know how many, 48 00:02:32,039 --> 00:02:34,819 but you figure you probably want to do it at least once, 49 00:02:34,819 --> 00:02:38,229 okay? This was, kind of, the case of my brother going to college. Like, he figured, "I'm gonna 50 00:02:38,229 --> 00:02:40,959 go to college." But when he got there, he didn't know how many degrees he was 51 00:02:40,960 --> 00:02:44,590 gonna get, and he just keep going and going, and five degrees later, he was, 52 00:02:44,590 --> 00:02:48,090 kind of, like, "Okay. I think I'm done now." So that was his while loop, right? 53 00:02:48,090 --> 00:02:51,408 He didn't know how many times, but he knew he wanted to do it at least once. 54 00:02:51,408 --> 00:02:54,949 Now, the interesting thing is that in programming, this actually happens fairly 55 00:02:54,949 --> 00:02:57,429 commonly. So I'll show you a quick example of this. 56 00:02:57,429 --> 00:02:59,218 If we go over to the computer, 57 00:02:59,218 --> 00:03:01,509 there's something we call the loop and a half 58 00:03:01,509 --> 00:03:03,969 because in some sense, you want to half of the loop 59 00:03:03,969 --> 00:03:05,799 at least once, okay? 60 00:03:05,799 --> 00:03:09,430 Think about it this way. Let's say we want to take some program that reads in a 61 00:03:09,430 --> 00:03:13,169 bunch of numbers from the user until the user enters zero to stop, and it, kind 62 00:03:13,169 --> 00:03:16,159 of, computes the sum of all those numbers, so it adds them all together. 63 00:03:16,158 --> 00:03:20,098 So we might have, basically, some sentinel value, right? We're gonna use our 64 00:03:20,098 --> 00:03:23,310 constant, our little friend last time that we talked about, the constant, to 65 00:03:23,310 --> 00:03:25,860 define what is that value that the user enters 66 00:03:25,860 --> 00:03:29,230 in which point they want to stop entering values. This, is in programming 67 00:03:29,229 --> 00:03:31,419 speak, is often referred to as a sentinel. 68 00:03:31,419 --> 00:03:34,669 It's, kind of, the last thing I'm gonna give you that tells you I'm done; I'm not 69 00:03:34,669 --> 00:03:37,859 gonna enter any more values. But until I give you that sentinel, I'm gonna 70 00:03:37,860 --> 00:03:41,240 give you a bunch of values that I want you to add together, for example. So that 71 00:03:41,240 --> 00:03:44,560 sentinel we might define as a constant, and so we could have some loop that says 72 00:03:44,560 --> 00:03:48,080 well, if I'm gonna keep track of some total or some sum of all the values you enter, 73 00:03:48,080 --> 00:03:49,829 that home's gonna start at zero. 74 00:03:49,829 --> 00:03:53,439 I'm gonna ask you for some value, so I'm gonna read in some integer and 75 00:03:53,438 --> 00:03:57,378 store it in the little box named Val, right? So I just declare it a variable, 76 00:03:57,378 --> 00:03:58,808 and then I'm gonna have a loop, 77 00:03:58,808 --> 00:04:01,459 and what my loop says is 78 00:04:01,459 --> 00:04:04,609 if the value you gave me is not the sentinel, that means you don't want to 79 00:04:04,609 --> 00:04:07,719 stop yet, right? So I have a logical condition in here that's checking against 80 00:04:07,718 --> 00:04:10,079 the constant, and that's a perfectly fine thing to do. So 81 00:04:10,080 --> 00:04:13,049 we're taking a lot of those concepts you saw last time and just, kind of, sticking them all 82 00:04:13,049 --> 00:04:14,829 together into one bigger program. 83 00:04:14,829 --> 00:04:18,858 If I haven't seen that sentinel yet, then take the value you just gave me, and 84 00:04:18,858 --> 00:04:22,508 add it to my total, and store it back in the total using the little plus equals 85 00:04:22,509 --> 00:04:24,658 shorthand you saw last time, okay? 86 00:04:24,658 --> 00:04:27,959 Then what I need to do is after I've done this, I need to ask you for a value 87 00:04:27,959 --> 00:04:28,439 again. 88 00:04:28,439 --> 00:04:31,978 So I say value equals read it. Notice I already declared 89 00:04:31,978 --> 00:04:35,478 the value up here, right? I already declared this variable up here, 90 00:04:35,478 --> 00:04:39,610 and this guy's lifetime is until it gets to the closing brace at the level at 91 00:04:39,610 --> 00:04:40,870 which it's declared. 92 00:04:40,870 --> 00:04:44,069 It's not declared inside the while loop, so this closing brace doesn't make it go 93 00:04:44,069 --> 00:04:45,169 away. 94 00:04:45,168 --> 00:04:48,218 This closing brace makes it go away, which means it's actually alive the whole 95 00:04:48,218 --> 00:04:51,550 time in the while loop, and it's perfectly fine. So 96 00:04:51,550 --> 00:04:54,949 val, I read in some other value from the user, and, again, I go back through this loop. Did they give 97 00:04:54,949 --> 00:04:57,889 me zero? No, add it in. Did they give me, zero? No, add it in. 98 00:04:57,889 --> 00:05:00,908 And when they give me zero, then I say, oh, the value is equal to the sentinel, 99 00:05:00,908 --> 00:05:04,199 so this test becomes false, and I come down here, and I write out the total as 100 00:05:04,199 --> 00:05:04,780 total, 101 00:05:04,779 --> 00:05:08,149 and you might say, oh, okay, Mehran, that's fine. What's wrong with that? 102 00:05:08,149 --> 00:05:12,459 Well, in computer science or actually in computer programming, we really hate duplicated 103 00:05:12,459 --> 00:05:13,180 code. 104 00:05:13,180 --> 00:05:17,790 If we can avoid duplicating code, even if it's one line, we generally try to do it, 105 00:05:17,790 --> 00:05:20,800 and you can see there's a duplication here, right? What's happening is 106 00:05:20,800 --> 00:05:24,709 there's something we want it to do at least once, which was to ask the user for some 107 00:05:24,709 --> 00:05:25,269 value, 108 00:05:25,269 --> 00:05:28,930 but we need to keep doing that over and over in the loop, even after that first 109 00:05:28,930 --> 00:05:31,810 time. So we, kind of, run into this conundrum where we say are we gonna have 110 00:05:31,810 --> 00:05:33,278 to repeat code? 111 00:05:33,278 --> 00:05:36,879 And there's a way around this. So the way around this is we pop out that piece of code, 112 00:05:36,879 --> 00:05:37,620 and 113 00:05:37,620 --> 00:05:41,139 we're gonna pop in our friend the loop and a half, okay? 114 00:05:41,139 --> 00:05:44,199 And so what the loop and a half says, the first thing that looks funky about it is we 115 00:05:44,199 --> 00:05:46,579 say while true, and you see that and you go, 116 00:05:46,579 --> 00:05:50,139 "Oh, my God." Right? Any time you see ewhile true' you're thinking 117 00:05:50,139 --> 00:05:53,500 bad times, right? "I'm never gonna break out of the loop. Every time I come here and evaluate the 118 00:05:53,500 --> 00:05:56,028 condition, it's always true. Isn't this an infinite loop, Mehran?" 119 00:05:56,028 --> 00:05:59,348 And it would be except for one caveat, and here's the caveat. 120 00:05:59,348 --> 00:06:01,628 We're gonna ask the user for a value, 121 00:06:01,629 --> 00:06:04,860 right? Notice we declared the int val inside here. We could've declared it outside if 122 00:06:04,860 --> 00:06:08,908 we wanted to, as long as we didn't do the read into outside. We could've just said int val and 123 00:06:08,908 --> 00:06:09,360 declared it out here, 124 00:06:09,360 --> 00:06:11,939 but we're only gonna use it inside the loop, so we're just gonna declare it 125 00:06:11,939 --> 00:06:12,990 here. 126 00:06:12,990 --> 00:06:16,809 We read an integer from the user. We ask if the value is the sentinel. 127 00:06:16,809 --> 00:06:18,759 If it is, we break. 128 00:06:18,759 --> 00:06:23,830 What a break statement does is it will break you out of your closest 129 00:06:23,829 --> 00:06:27,318 encompassing loop. What does that mean? It means it finds whatever loop is 130 00:06:27,319 --> 00:06:28,910 the loop you're currently in 131 00:06:28,910 --> 00:06:31,400 and jumps you out of just that loop. 132 00:06:31,399 --> 00:06:36,429 So if you hit a break statement, it will jump out to, essentially, the place right 133 00:06:36,430 --> 00:06:39,088 after the closing brace for that loop and keep executing. 134 00:06:39,088 --> 00:06:42,399 So what it allows you to do, essentially, is to check the condition to break out 135 00:06:42,399 --> 00:06:45,869 of the loop in the middle of a loop, which is, kind of, a funky thing, rather than at 136 00:06:45,869 --> 00:06:48,830 the very beginning or every time you iterate through. So 137 00:06:48,829 --> 00:06:52,348 we get the value from the user. If the value is the sentinel, we break, which means we 138 00:06:52,348 --> 00:06:55,988 never execute this line or the rest of the loop. We jump down here. 139 00:06:55,988 --> 00:06:59,359 If it is not the sentinel, then we add it to the total. 140 00:06:59,360 --> 00:07:02,119 While is true, so we execute the loop again and go and read another value from the 141 00:07:02,119 --> 00:07:02,879 user, right? 142 00:07:02,879 --> 00:07:06,759 So notice that this read int line is no longer repeated. We only do it once, 143 00:07:06,759 --> 00:07:09,719 but because of the structure of this, we're always guaranteed that this portion 144 00:07:09,720 --> 00:07:13,160 of the loop up here is always executed at least once because we had a while 145 00:07:13,160 --> 00:07:17,020 true, and we're checking the condition to break out of the loop in the middle, okay? 146 00:07:17,019 --> 00:07:18,748 Now, one caveat with that is 147 00:07:18,749 --> 00:07:22,800 in terms of programming style, it's generally a bad time to have multiple of 148 00:07:22,800 --> 00:07:24,949 these kind of breaks inside a loop because it 149 00:07:24,949 --> 00:07:28,740 makes it very difficult for a programmer to figure out what condition has to be 150 00:07:28,740 --> 00:07:32,129 true to break out of the loop. If there's only place you see a break, 151 00:07:32,129 --> 00:07:35,169 then there's only one condition you need to check to break out of the loop. 152 00:07:35,168 --> 00:07:39,508 If you have multiple statements, the multiple if, you know, blah-blah-blah break 153 00:07:39,509 --> 00:07:40,550 in your loop, 154 00:07:40,550 --> 00:07:43,598 that means the programmer has to keep track of multiple possible places you 155 00:07:43,598 --> 00:07:46,938 could've broken out of the loop in their head, and that gets pretty dicey. So you 156 00:07:46,939 --> 00:07:49,579 generally want to avoid that when you're doing it, okay? So 157 00:07:49,579 --> 00:07:54,528 any questions about the loop and a half? Uh huh. Is it 158 00:07:54,528 --> 00:07:55,139 okay 159 00:07:55,139 --> 00:07:57,210 to redeclare a variable in a loop 160 00:07:57,209 --> 00:07:58,769 like that over and over 161 00:07:58,769 --> 00:08:01,649 again? Yeah, it's fine to actually declare that variable inside the loop. You don't need 162 00:08:01,649 --> 00:08:05,348 to worry about, like, efficiency issues or anything like that. 163 00:08:05,348 --> 00:08:09,288 So let me show this to you in action in a larger program, right? So 164 00:08:09,288 --> 00:08:12,568 here's that loop and a half in the context of an actual run method. It prints 165 00:08:12,569 --> 00:08:13,800 something on the screen. 166 00:08:13,800 --> 00:08:18,319 It says total to be equal to zero, and then it comes here, while true will always 167 00:08:18,319 --> 00:08:22,360 execute, right, because the condition's true. So it asks the user for some value. We enter 168 00:08:22,360 --> 00:08:25,569 one; one is not equal to the sentinel. So we add that to the total, and you can 169 00:08:25,569 --> 00:08:29,699 see here it's keep track of value and total. Here's just my two declared variables and the 170 00:08:29,699 --> 00:08:32,539 little boxes for them, and they get updated as I go along. 171 00:08:32,539 --> 00:08:36,019 So while it's still true, I read another value. Let's say the user gives me two. 172 00:08:36,019 --> 00:08:38,909 I add it to the total, which is now three, 173 00:08:38,909 --> 00:08:42,728 and I come back up here. I'll go through this a couple more times, okay? 174 00:08:42,729 --> 00:08:44,050 User gives me zero. 175 00:08:44,049 --> 00:08:46,559 Now, notice when the user gave me zero here, 176 00:08:46,559 --> 00:08:51,000 I hit the break because my value is zero, and that's equal to the sentinel. So it says, hey, 177 00:08:51,000 --> 00:08:52,399 the if part is true, 178 00:08:52,399 --> 00:08:56,490 so do the break, and it immediately jumps out to the end of a loop. It did not do - 179 00:08:56,490 --> 00:08:59,549 even though it [inaudible]. So 180 00:08:59,549 --> 00:09:01,339 181 00:09:01,340 --> 00:09:02,490 I feel 182 00:09:02,490 --> 00:09:06,720 a little like Roger Daltrey when I'm doing this. Anyone know Roger Daltrey, 183 00:09:06,720 --> 00:09:08,690 lead singer of The Who? 184 00:09:08,690 --> 00:09:12,300 Go look it up on Wikipedia, and watch the video. 185 00:09:12,299 --> 00:09:15,879 Man, I'm feeling old. All right. And then it says the total is, so this total plus 186 00:09:15,879 --> 00:09:18,149 equals value. The value just 187 00:09:18,149 --> 00:09:20,759 doesn't get added that last time, even though it would've been a zero and wouldn't 188 00:09:20,759 --> 00:09:21,369 affect the things. 189 00:09:21,369 --> 00:09:24,160 It would be different, right, if we said the sentinel equaled a negative one. Then 190 00:09:24,159 --> 00:09:27,868 you would actually see that the total didn't get one subtracted from it, and we could've set 191 00:09:27,869 --> 00:09:32,690 the sentinel to whatever we wanted, but in this case, we used zero, okay? So 192 00:09:32,690 --> 00:09:35,700 that's our little funkiness, and it writes out the total of zero for actually doing the 193 00:09:35,700 --> 00:09:37,530 loop and a half. Now, 194 00:09:37,529 --> 00:09:40,309 in terms of doing all this stuff, okay, 195 00:09:40,309 --> 00:09:43,509 you might think, "Well, hey, Mehran, you showed me about the for loop last time. You 196 00:09:43,509 --> 00:09:46,600 showed me about the while loop. It turns out I can write the same loop in 197 00:09:46,600 --> 00:09:48,308 equivalent ways using both of them." So you 198 00:09:48,308 --> 00:09:51,848 remember the for has the initialization, the test, and the step initialization happens 199 00:09:51,849 --> 00:09:52,339 once. 200 00:09:52,339 --> 00:09:55,629 Then the test happens every time through the loop, and then the step happens, kind 201 00:09:55,629 --> 00:09:57,560 of, every time at the end of the loop? 202 00:09:57,559 --> 00:10:00,268 That would be exactly equivalent if I wrote it like this. 203 00:10:00,269 --> 00:10:02,049 The int happens once before the loop. 204 00:10:02,049 --> 00:10:05,279 I have some while loop that checks the same test. 205 00:10:05,279 --> 00:10:08,839 I have the same set of statements that would be in the for loop body, and I do 206 00:10:08,840 --> 00:10:12,180 the equivalent of the step in the for loop at the very end of the loop. 207 00:10:12,179 --> 00:10:15,659 So these two forms are exactly equivalent to each other, and you might say, "Hey, 208 00:10:15,659 --> 00:10:19,009 Mehran, if they're exactly equivalent to each other, why do I have these two loops, and 209 00:10:19,009 --> 00:10:21,389 when do I use one versus the other?" Well, 210 00:10:21,389 --> 00:10:25,480 for loops, you want to think about what we refer to as Definite Iteration. 211 00:10:25,480 --> 00:10:26,580 That means we generally know 212 00:10:26,580 --> 00:10:29,800 how many times we want to do something, and that number of times we want to do 213 00:10:29,799 --> 00:10:30,370 something 214 00:10:30,370 --> 00:10:32,899 is how we, sort of, count in our test. 215 00:10:32,899 --> 00:10:36,350 When we don't know how many times we actually want to do something, that's an 216 00:10:36,350 --> 00:10:39,060 indefinite loop, or indefinite iteration as I just talked about, 217 00:10:39,059 --> 00:10:41,519 that's when we use a while loop, when we generally don't know how many times 218 00:10:41,519 --> 00:10:42,879 we're gonna do something, okay? 219 00:10:42,879 --> 00:10:46,289 So that, kind of, why we give you both forms, and most programming languages 220 00:10:46,289 --> 00:10:47,360 actually have both forms, 221 00:10:47,360 --> 00:10:50,360 but to a programmer, it's more clear between seeing the difference between 222 00:10:50,360 --> 00:10:51,239 for and a while, 223 00:10:51,239 --> 00:10:54,319 it's, kind of, the meaning of it, right? Are you counting up to something some number of 224 00:10:54,318 --> 00:10:54,988 times, 225 00:10:54,989 --> 00:10:57,920 or are you just gonna do something until some condition is true? That's, kind of, 226 00:10:57,919 --> 00:10:58,279 227 00:10:58,279 --> 00:11:02,559 the differentiator there. So any questions about loops? 228 00:11:02,559 --> 00:11:06,059 So let's put this all together in the context of a bigger program, okay? 229 00:11:06,059 --> 00:11:09,289 So I will show you a bigger program here. So we'll come over here. 230 00:11:09,289 --> 00:11:12,370 Here's a little program; let me just run it for you real quickly. 231 00:11:12,370 --> 00:11:16,940 Come on. 232 00:11:16,940 --> 00:11:20,190 So we run along, and what this program's gonna do is draw a checkerboard, 233 00:11:20,190 --> 00:11:22,720 right? Just like you're used to in the game of checkers or chess, a little 234 00:11:22,720 --> 00:11:25,329 checkerboard on the screen. So this is gonna be a graphics program. 235 00:11:25,328 --> 00:11:28,788 It's gonna draw a bunch of G rects, which are just rectangles - in this 236 00:11:28,788 --> 00:11:29,999 case, they're gonna be squares, 237 00:11:29,999 --> 00:11:33,470 to draw a little checkerboard, and you should think, "Huh, this might have some 238 00:11:33,470 --> 00:11:37,250 similarities to, maybe, some of the stuff you were doing in your current assignment. Yeah, 239 00:11:37,250 --> 00:11:40,259 so let's go through this code and see what's actually going on, and we can put 240 00:11:40,259 --> 00:11:44,240 a bunch of things we learned together like constants, and loops, and blah-blah-blah. 241 00:11:44,240 --> 00:11:46,680 So what's going on over here 242 00:11:46,679 --> 00:11:49,809 is, first of all, we start off with a couple constants, right? And the 243 00:11:49,809 --> 00:11:50,478 constants we're gonna 244 00:11:50,479 --> 00:11:51,330 have tell us 245 00:11:51,330 --> 00:11:55,270 how many rows and columns are gonna be in our checkerboard. We want to draw a 246 00:11:55,269 --> 00:11:58,389 standard checkerboard, which is an 8x8 checkerboard. So we have 247 00:11:58,389 --> 00:11:59,938 private static final int, 248 00:11:59,938 --> 00:12:04,610 end rows is eight, and private static final int, end columns is also eight. 249 00:12:04,610 --> 00:12:07,720 Notice that these two constants are not defined inside a method. They're defined 250 00:12:07,720 --> 00:12:08,879 inside the class 251 00:12:08,879 --> 00:12:11,720 but not inside a particular method, which is generally where your constants are 252 00:12:11,720 --> 00:12:15,230 gonna be defined, right, in a class but not inside a particular method, 253 00:12:15,230 --> 00:12:18,190 and then for the run part of the program, first thing we need to know is we need 254 00:12:18,190 --> 00:12:21,650 to figure out - because we want this thing to be general, right? No matter how big our window is, 255 00:12:21,649 --> 00:12:23,658 we want to draw a nice little checkerboard. 256 00:12:23,658 --> 00:12:26,918 So we need to figure out how big do those squares on the checkerboard need 257 00:12:26,918 --> 00:12:28,039 to actually be 258 00:12:28,039 --> 00:12:30,259 so they appropriately fill up the screen. 259 00:12:30,259 --> 00:12:33,759 How do we do that? We get the height of the graphics window - remember our 260 00:12:33,759 --> 00:12:36,939 little friend, the method Get Height, which tells us how many pixels high the 261 00:12:36,940 --> 00:12:37,899 graphics window is, 262 00:12:37,899 --> 00:12:40,820 and we divide that by the number of rows. 263 00:12:40,820 --> 00:12:43,290 Since we divide that by the number of rows, this is gonna give us integer 264 00:12:43,289 --> 00:12:46,169 division, right? This is gonna give us back an integer, and we're gonna assign 265 00:12:46,169 --> 00:12:49,399 that to square size, which is an integer. So everything's perfectly fine because 266 00:12:49,399 --> 00:12:50,860 this is some number of pixels. 267 00:12:50,860 --> 00:12:51,938 This is an integer, 268 00:12:51,938 --> 00:12:55,129 and so this is an integer value divided by an integer value gives you an integer division. 269 00:12:55,129 --> 00:12:58,819 So that tells you basically how many squares can you fit in, sort of, the 270 00:12:58,818 --> 00:12:59,998 height of the screen, 271 00:12:59,999 --> 00:13:02,668 and that's gonna be the size of one of the sides of our squares, and, as 272 00:13:02,668 --> 00:13:06,419 you know, squares have the same size on both sides, 273 00:13:06,419 --> 00:13:08,870 same length on both sides, so we're perfectly fine. 274 00:13:08,870 --> 00:13:11,379 Now, what we're gonna do here is, right, we want to get the structure that's like a 275 00:13:11,379 --> 00:13:12,330 grid. 276 00:13:12,330 --> 00:13:15,790 In order to get a grid, we're gonna have what we refer to as a pair of 277 00:13:15,789 --> 00:13:16,969 Nested Loops. 278 00:13:16,970 --> 00:13:20,060 All that means is one loop is inside the other loop, 279 00:13:20,059 --> 00:13:23,209 okay? So we have two for loops here, and you can see with the indenting, 280 00:13:23,210 --> 00:13:26,570 one is, kind of, nested inside the other one. So one is gonna count through the number 281 00:13:26,570 --> 00:13:28,689 of rows. So we're gonna have one loop 282 00:13:28,688 --> 00:13:31,219 that's gonna go through the number of rows we want to display 283 00:13:31,220 --> 00:13:33,160 and another loop 284 00:13:33,159 --> 00:13:36,870 that, within each row, is going to, essentially, count the number of columns 285 00:13:36,870 --> 00:13:40,399 in that row because we're gonna put one box for every little square grid on 286 00:13:40,399 --> 00:13:43,299 there, which means for every row, we need to do something for every column in that 287 00:13:43,299 --> 00:13:43,919 row. 288 00:13:43,919 --> 00:13:45,078 So we're gonna have 289 00:13:45,078 --> 00:13:49,528 one for loop that's going to have I as what we refer to as its index 290 00:13:49,528 --> 00:13:52,220 variable. So remember when we talked about for loops, and we talked about counting, 291 00:13:52,220 --> 00:13:52,990 we said, 292 00:13:52,990 --> 00:13:57,440 "Oh, yeah, you say something like int I equals zero I less the num rows." 293 00:13:57,440 --> 00:14:00,250 And that'll count it from zero up to num rows minus one, which 294 00:14:00,250 --> 00:14:03,110 that iterates num rows times or N rows times. 295 00:14:03,110 --> 00:14:04,000 That's great. 296 00:14:04,000 --> 00:14:07,429 The only problem is this variable I, remember, is alive 297 00:14:07,429 --> 00:14:10,979 until it gets to the closing brace that matches this brace, which means that I 298 00:14:10,980 --> 00:14:13,409 is alive all the way to down here. 299 00:14:13,409 --> 00:14:16,379 Well, if that I is alive all the way to down here, if we have some other for loop 300 00:14:16,379 --> 00:14:18,730 in the middle, if it's also using I, 301 00:14:18,730 --> 00:14:21,779 those I's are gonna clash with each other, and that's bad times. 302 00:14:21,779 --> 00:14:22,639 So what do we do? 303 00:14:22,639 --> 00:14:24,990 We just use a different variable name. 304 00:14:24,990 --> 00:14:27,500 What we're gonna do is what's the next 305 00:14:27,500 --> 00:14:28,688 letter after I? 306 00:14:28,688 --> 00:14:32,858 J, right, so I, J, K you'll see are very popular names for loops, 307 00:14:32,859 --> 00:14:35,000 and those are the one place where descriptive names - 308 00:14:35,000 --> 00:14:38,730 you don't need to have some big name like, "Oh, this is the Counter Through 309 00:14:38,730 --> 00:14:40,269 Rows variable," or something like that. 310 00:14:40,269 --> 00:14:44,399 I and J are perfectly fine because most programmers are used to I, J, K 311 00:14:44,399 --> 00:14:46,230 as the names for 312 00:14:46,230 --> 00:14:49,589 control variables, or index variables, and loops is how we refer to it because this 313 00:14:49,589 --> 00:14:53,480 is what the loop is using to count through, so we think of I as the loop index. 314 00:14:53,480 --> 00:14:57,360 So we have one here for J, and notice all the places I would've had I are now just 315 00:14:57,360 --> 00:15:00,938 replaced by J. So J gets initialized to zero. We count up to N columns, 316 00:15:00,938 --> 00:15:03,099 and J plus plus is how we increment J. 317 00:15:03,100 --> 00:15:05,250 So a common error with nested loops is 318 00:15:05,250 --> 00:15:08,509 you have the same variable up here as you had down here, and that's generally bad 319 00:15:08,509 --> 00:15:10,330 times, okay? Now, 320 00:15:10,330 --> 00:15:13,259 once we're counting through all the columns, the thing we need to figure out is where 321 00:15:13,259 --> 00:15:17,750 is this square that we're gonna draw gonna go? What's its XY location? 322 00:15:17,750 --> 00:15:21,409 Well, it's XY location says, well, first of all, if you want to figure out its X, 323 00:15:21,409 --> 00:15:25,289 that's, sort of, going horizontally across the screen, right? Going horizontally 324 00:15:25,289 --> 00:15:29,159 across the screen depends on which column you're currently in. So I take 325 00:15:29,159 --> 00:15:33,299 J, which is the index variable for this loop over the number of columns here, 326 00:15:33,299 --> 00:15:36,818 and I multiply it by my square size. What I'm essentially saying is 327 00:15:36,818 --> 00:15:41,658 move over in the X direction J many squares. That will tell you where the X 328 00:15:41,658 --> 00:15:44,700 location is for the next square of the tracks you're gonna write out. 329 00:15:44,700 --> 00:15:47,330 Similarly, Y, which is the vertical direction, I need to figure out which 330 00:15:47,330 --> 00:15:51,480 row I'm in. Well, I is what's indexing my rows, right? So I'm just gonna take 331 00:15:51,480 --> 00:15:53,190 I and multiply it by the square size. 332 00:15:53,190 --> 00:15:56,700 That tells me how far down the screen I need to go to get the Y location for 333 00:15:56,700 --> 00:15:57,960 this square. 334 00:15:57,960 --> 00:16:01,269 So now I have the X and Y location. Any questions about how I computed the X and Y 335 00:16:01,269 --> 00:16:03,449 location? Don't be shy. All 336 00:16:03,450 --> 00:16:07,350 right. Are you feeling good about X and Y? If you are, nod your head. All right. 337 00:16:07,350 --> 00:16:10,370 I'm seeing some nods in there, some blank stares. 338 00:16:10,370 --> 00:16:14,330 If you have no idea what I'm talking about, nod your head. 339 00:16:14,330 --> 00:16:18,129 All right. That's better. There's always this indefinite ground where it's, sort of, like, 340 00:16:18,129 --> 00:16:21,649 no one nods, and they don't give you any feedback. So I'm begging you for feedback, 341 00:16:21,649 --> 00:16:28,649 all right? [Off mic]. 342 00:16:28,899 --> 00:16:31,509 We could've put this outside of 343 00:16:31,509 --> 00:16:35,249 the J loop and just computed - or we could've put the Y location outside of here 344 00:16:35,249 --> 00:16:36,379 and just computed it once 345 00:16:36,379 --> 00:16:39,250 because that Y location will remain the same for the whole row. So that would've 346 00:16:39,250 --> 00:16:42,179 been an interesting little optimization we could've made, but we didn't make it here because I 347 00:16:42,179 --> 00:16:45,758 just wanted to compute X and Y together so you would see them computed together, and because 348 00:16:45,759 --> 00:16:48,970 our forms are pretty similar to each other, okay? 349 00:16:48,970 --> 00:16:52,610 So once I know the X and Y location for that square, I need to say, hey, get 350 00:16:52,610 --> 00:16:57,019 me a new square at that location. So I say new G rec. That gets me one of these 351 00:16:57,019 --> 00:16:58,369 new square objects. 352 00:16:58,369 --> 00:17:02,428 Where is the upper left-hand corner for that square? It's at the XY location I just 353 00:17:02,428 --> 00:17:02,949 computed. 354 00:17:02,950 --> 00:17:04,420 How big is the square? 355 00:17:04,420 --> 00:17:07,490 Square size width and square size height, right, because it's a square; it's got the 356 00:17:07,490 --> 00:17:08,519 same width and height. So 357 00:17:08,519 --> 00:17:10,220 that gives me a new square. 358 00:17:10,220 --> 00:17:14,069 Now, I gotta square at some location that I want to display that square out, 359 00:17:14,069 --> 00:17:17,009 right, because I computed the right X and Y. The only problem is at the checkerboard. 360 00:17:17,009 --> 00:17:18,730 Some of the squares are filled; 361 00:17:18,730 --> 00:17:19,970 some are not filled. 362 00:17:19,970 --> 00:17:23,240 How do I figure out which squares should be filled? 363 00:17:23,240 --> 00:17:26,730 This is where I do a little bit of math and a little bit of Boolean logic, and it 364 00:17:26,730 --> 00:17:28,749 just makes your life, kind of, beautiful. 365 00:17:28,749 --> 00:17:31,808 So rather than having a whole bunch of ifs in there and trying to figure out, 366 00:17:31,808 --> 00:17:34,888 oh, am I on an odd square, an even square, which row am I in, 367 00:17:34,888 --> 00:17:35,599 blah-blah-blah? 368 00:17:35,599 --> 00:17:37,349 You do a little bit of math, 369 00:17:37,349 --> 00:17:39,099 and you figure out your math, and you say, 370 00:17:39,099 --> 00:17:42,839 hey, you know what, that very first square that I draw in the upper-right-hand 371 00:17:42,839 --> 00:17:44,000 corner, 372 00:17:44,000 --> 00:17:46,200 okay, let me see if I can bring that little 373 00:17:46,200 --> 00:17:48,110 square back up again. 374 00:17:48,109 --> 00:17:51,099 So I'll run this one more time. 375 00:17:51,099 --> 00:17:54,798 This guy up here, I can think of that square all the way up there as the 376 00:17:54,798 --> 00:17:57,309 00 square. That guy's not filled, 377 00:17:57,309 --> 00:18:01,509 but if I were to move one over that is filled, or if I were to move one down 378 00:18:01,509 --> 00:18:05,069 that is filled - so if I think about the index location that I'm currently at, 379 00:18:05,069 --> 00:18:08,919 and I take my X and Y coordinates, or I should say my I and J variables 380 00:18:08,920 --> 00:18:11,600 because that's what indexing my rows, and my columns 381 00:18:11,599 --> 00:18:13,289 and add them together, 382 00:18:13,289 --> 00:18:15,379 if that puppy's even, 383 00:18:15,380 --> 00:18:17,280 then it's not a filled square. 384 00:18:17,279 --> 00:18:21,480 If it's odd, then it is a filled square because the very first one is 00. That 385 00:18:21,480 --> 00:18:23,860 one's not filled, so even's not filled, 386 00:18:23,859 --> 00:18:26,099 but if I move one in either direction, 387 00:18:26,099 --> 00:18:29,959 I get something that should be filled. If I move two in either direction, I get 388 00:18:29,960 --> 00:18:32,090 something that should not be filled, et cetera. 389 00:18:32,089 --> 00:18:35,470 So the way that translates into logic for the code 390 00:18:35,470 --> 00:18:38,480 is I come over here, and it looks a little hairier than it actually is, but I say 391 00:18:38,480 --> 00:18:40,519 take the I and J, and 392 00:18:40,519 --> 00:18:41,759 add them together, 393 00:18:41,759 --> 00:18:44,880 and mod it by - or I should say - remainder it by two, 394 00:18:44,880 --> 00:18:48,100 right? If I remainder by two, if that remainder is zero, that means it's 395 00:18:48,099 --> 00:18:48,548 even. 396 00:18:48,548 --> 00:18:52,089 If the remainder is one, that means it has to be odd. The remainder can't be anything 397 00:18:52,089 --> 00:18:55,409 other than zero or one when I'm taking the remainder and dividing by two. 398 00:18:55,410 --> 00:18:59,300 So if that remainder is not equal to zero, that means it's an odd square, right, 399 00:18:59,299 --> 00:19:02,210 because my remainder divided by two is not equal to zero; it 400 00:19:02,210 --> 00:19:03,640 means it was equal to one. 401 00:19:03,640 --> 00:19:05,049 What I'm gonna do in that case 402 00:19:05,048 --> 00:19:06,980 is I'm gonna set filled. 403 00:19:06,980 --> 00:19:10,500 So this looks very funky. You might be inclined to think, hey, shouldn't you have an if 404 00:19:10,500 --> 00:19:12,619 statement here, and say if this thing is true, 405 00:19:12,619 --> 00:19:14,209 then set filled? 406 00:19:14,210 --> 00:19:15,370 Well, think about, right, 407 00:19:15,369 --> 00:19:16,839 if this thing is true, 408 00:19:16,839 --> 00:19:19,648 set filled will become true, and I'll get a filled square. 409 00:19:19,648 --> 00:19:23,439 If this thing is false, set filled will become false, and I'll get an unfilled 410 00:19:23,440 --> 00:19:27,090 square. So it works for me the way I want it to work in both cases, 411 00:19:27,089 --> 00:19:30,069 so rather than worrying about all this controlled logic for an if statement or all this 412 00:19:30,069 --> 00:19:30,789 other stuff, 413 00:19:30,789 --> 00:19:35,069 I just take my condition here, which is this big honking Boolean expression that 414 00:19:35,069 --> 00:19:36,249 evaluates the true or false 415 00:19:36,249 --> 00:19:37,528 and just stick it in 416 00:19:37,528 --> 00:19:40,929 for the value for - that set filled is actually expected, okay? 417 00:19:40,929 --> 00:19:43,298 And this will give me the alternating effect for squares, 418 00:19:43,298 --> 00:19:46,429 and when I finally do this, I need to say, hey, you know, I got that square. It's 419 00:19:46,429 --> 00:19:48,890 filled the way I want it to be filled. I know its location. 420 00:19:48,890 --> 00:19:51,630 Throw it up on the canvas for me, so I add it. 421 00:19:51,630 --> 00:19:54,760 And then I just keep doing this over and over, and I get all of my 422 00:19:54,759 --> 00:19:58,339 - for every row, I'll get a line of squares across and all the columns. Then I'll come 423 00:19:58,339 --> 00:20:05,189 down to the next row, and I'll just keep doing this until I get eight rows of squares. Uh huh? 424 00:20:05,190 --> 00:20:10,710 [Off mic]. Use a mic, please. That'd be great. Thanks. 425 00:20:10,710 --> 00:20:14,930 How do you designate is as a Boolean expression? Does it just automatically 426 00:20:14,930 --> 00:20:18,830 assume that is going to be true or false, or - Instructor: Right. So that's a good question. 427 00:20:18,829 --> 00:20:22,789 The key that's going on here is this little operator right here, the not equal 428 00:20:22,789 --> 00:20:23,609 operator, right? 429 00:20:23,609 --> 00:20:27,389 The not equal operator is a logical operator, which means it's gonna 430 00:20:27,390 --> 00:20:30,240 have two arguments that it works on, 431 00:20:30,240 --> 00:20:33,269 and what it's gonna give you back is true or false. So the way it knows that it's 432 00:20:33,269 --> 00:20:33,849 Boolean 433 00:20:33,849 --> 00:20:35,109 is that this 434 00:20:35,109 --> 00:20:38,719 particular operator, the not equal operator, always gives you back a Boolean value, 435 00:20:38,720 --> 00:20:41,779 right? Just like greater than or less than, all those operators are Boolean 436 00:20:41,779 --> 00:20:43,769 operators; they give you back a Boolean value. 437 00:20:43,769 --> 00:20:47,069 Whereas, the stuff I'm doing over here, right, is just math. This is giving me 438 00:20:47,069 --> 00:20:48,118 back an integer, 439 00:20:48,118 --> 00:20:51,209 and I'm comparing that integer with another integer using a Boolean 440 00:20:51,210 --> 00:20:57,200 operation. That's why I get a Boolean. 441 00:20:57,200 --> 00:21:00,440 Uh huh? [Off mic]. Does this only - well, this will work in any case as long as the window is wider than 442 00:21:00,440 --> 00:21:03,690 it is tall because the way we compute the squares is based on the height as 443 00:21:03,690 --> 00:21:10,690 opposed to the width. Uh huh? [Off mic]. 444 00:21:14,000 --> 00:21:17,049 No, that's different, and we'll get into that now. Well, the private part's the 445 00:21:17,049 --> 00:21:20,769 same, but I'll show you what all of those things mean in just a second. So we're gonna do 446 00:21:20,769 --> 00:21:26,349 methods in just a second, and then it'll become clear. Uh huh? [Off mic]. 447 00:21:26,349 --> 00:21:30,359 Because everything's in terms of pixels, and the pixels are always integers. So we just want to compute in 448 00:21:30,359 --> 00:21:33,529 terms of integers because all of our values mean our integer values. 449 00:21:33,529 --> 00:21:35,799 So let me push on just a little bit. Uh huh, 450 00:21:35,799 --> 00:21:36,408 question right there? 451 00:21:36,409 --> 00:21:42,370 Why are we doing it this way if we're supposed to be doing top-down programming; is 452 00:21:42,369 --> 00:21:46,019 it the opposite? Well, in this case, top-down programming is how you break things 453 00:21:46,019 --> 00:21:49,430 up into smaller functions, right? Here we only have one function. 454 00:21:49,430 --> 00:21:51,340 So we could actually think about, "Oh, 455 00:21:51,339 --> 00:21:54,579 should I do one row at a time?" And that would actually be an interesting 456 00:21:54,579 --> 00:21:55,849 decomposition is to have a function 457 00:21:55,849 --> 00:21:57,149 or some method that 458 00:21:57,150 --> 00:21:59,960 does one row for you, and I iterate that some number of times, 459 00:21:59,960 --> 00:22:02,769 and the reason why we didn't do it that way is we haven't done methods in Java 460 00:22:02,769 --> 00:22:06,730 yet, which is the next topic we're gonna get into. So thank you for the segue. So the next topic we're 461 00:22:06,730 --> 00:22:10,769 actually gonna deal with is our friend the method, but in the Java world. So 462 00:22:10,769 --> 00:22:13,349 hopefully you've already seen in Carroll, right? You saw how to break things 463 00:22:13,349 --> 00:22:15,788 down in Carroll, and we did decomposition in Carroll, 464 00:22:15,788 --> 00:22:19,460 and now it's time to bring that into the Java world, okay? 465 00:22:19,460 --> 00:22:22,539 So when we think about methods in Java - you already saw a couple of these things, right? 466 00:22:22,539 --> 00:22:23,869 Like, read ints, 467 00:22:23,869 --> 00:22:26,109 for example, 468 00:22:26,109 --> 00:22:30,119 is some method that you've seen, and so we might say, you know, int 469 00:22:30,119 --> 00:22:33,699 X equals read int, or print lin is some other method you've seen, right, that just 470 00:22:33,700 --> 00:22:34,830 prints out a line. 471 00:22:34,829 --> 00:22:37,220 Now, one way we can take these notions 472 00:22:37,220 --> 00:22:40,430 and make them a little bit more familiar is, first of all, we can say the idea of a method 473 00:22:40,430 --> 00:22:42,190 is just like in Carroll. 474 00:22:42,190 --> 00:22:46,009 You want to use methods to be able to break down programs into smaller pieces 475 00:22:46,009 --> 00:22:48,309 that you can reuse; that's critical. 476 00:22:48,309 --> 00:22:51,809 But, in Java, they get a little bit more complicated, and here's where the complexity 477 00:22:51,809 --> 00:22:52,658 comes in, 478 00:22:52,659 --> 00:22:54,780 and it's easiest to draw the 479 00:22:54,779 --> 00:22:55,398 notion of 480 00:22:55,398 --> 00:23:00,409 how they differ in the Java world by thinking about mathematical functions, right? So 481 00:23:00,410 --> 00:23:03,730 somewhere, someday, which you're never gonna have to worry about again as long 482 00:23:03,730 --> 00:23:06,920 this class is concerned, is you learned about this thing called the sign 483 00:23:06,920 --> 00:23:10,360 function. You're like, "Oh, Mehran, you told me there was gonna be, like, no, like, calculus in 484 00:23:10,359 --> 00:23:10,738 here." 485 00:23:10,739 --> 00:23:11,679 Yeah, don't worry. 486 00:23:11,679 --> 00:23:14,470 This is just for this example, and then the sign function goes away. You never have 487 00:23:14,470 --> 00:23:16,690 to worry about it again, at least for this class, right? 488 00:23:16,690 --> 00:23:20,440 But the way the sign function worked is it took some value that you computed 489 00:23:20,440 --> 00:23:21,889 the sign of, right? 490 00:23:21,888 --> 00:23:23,949 So there was something here like X 491 00:23:23,950 --> 00:23:26,450 that you took the sign of, and what you got back 492 00:23:26,450 --> 00:23:28,819 from the sign function was some value 493 00:23:28,819 --> 00:23:31,579 that was essentially the sign of X. 494 00:23:31,579 --> 00:23:35,599 So you not only had something that we called a parameter 495 00:23:35,599 --> 00:23:39,039 that was what that function was expecting in terms of information going 496 00:23:39,039 --> 00:23:39,569 in, 497 00:23:39,569 --> 00:23:44,928 you also got some information coming out, which is what the value of that function 498 00:23:44,929 --> 00:23:47,620 returned to you or gave back to you, okay? 499 00:23:47,619 --> 00:23:49,798 That's the one bit of additional complexity 500 00:23:49,798 --> 00:23:51,819 that comes up with 501 00:23:51,819 --> 00:23:52,899 methods in 502 00:23:52,900 --> 00:23:55,759 the Java world as opposed to the Carroll world, and the Carroll world, 503 00:23:55,759 --> 00:23:59,190 you didn't have any parameters that went in, and no values came out. 504 00:23:59,190 --> 00:24:02,110 In Java's world, you do, okay? So, 505 00:24:02,109 --> 00:24:05,219 first of all, just to wrap up on the little math 506 00:24:05,220 --> 00:24:05,980 example, 507 00:24:05,980 --> 00:24:09,400 a couple people asked last time about some mathy functions, and it turns out that 508 00:24:09,400 --> 00:24:12,110 there's this thing you import called 509 00:24:12,109 --> 00:24:13,869 Java.lang.math. 510 00:24:13,869 --> 00:24:14,989 511 00:24:14,990 --> 00:24:17,190 512 00:24:17,190 --> 00:24:20,640 They're a bunch of mathematical functions you get in the Java world for free, 513 00:24:20,640 --> 00:24:23,669 and some of those are, for example, oh, a function you might be using on this next 514 00:24:23,669 --> 00:24:25,690 assignment like square root. So 515 00:24:25,690 --> 00:24:27,990 if we have double, 516 00:24:27,990 --> 00:24:29,309 let's say Y - 517 00:24:29,309 --> 00:24:31,200 or I'll say XX, 518 00:24:31,200 --> 00:24:35,990 XX equals 9.5, and then I might have some other YY 519 00:24:35,990 --> 00:24:41,099 here, and I want Y to be the square root of 9.5. I would say math.sqrt, 520 00:24:41,099 --> 00:24:44,939 which is the name of the method for square root, and then I 521 00:24:44,940 --> 00:24:46,429 give it the value, 522 00:24:46,429 --> 00:24:50,009 or I give it a variable whose value I want to compute the square root of. So 523 00:24:50,009 --> 00:24:51,769 this guy takes in 524 00:24:51,769 --> 00:24:53,900 something of type double, 525 00:24:53,900 --> 00:24:56,850 computes the square root, and gives you back that value that you can now just 526 00:24:56,849 --> 00:25:00,049 assign to some other double, for example, okay? 527 00:25:00,049 --> 00:25:01,329 And there are some other ones in there. There's a 528 00:25:01,329 --> 00:25:04,048 power function. Someone asked about power last time. That's where you also get 529 00:25:04,048 --> 00:25:05,950 the function; it's also in there, 530 00:25:05,950 --> 00:25:09,990 but power gives you a nice example of you can have multiple 531 00:25:09,990 --> 00:25:14,779 parameters. So power you actually give it an X and a Y, and what it 532 00:25:14,779 --> 00:25:18,609 does is computes X to the Y power. So it's essentially computing something 533 00:25:18,609 --> 00:25:21,679 that would look like that if we were to write it out in math-ese, 534 00:25:21,680 --> 00:25:23,660 and it gives you back that 535 00:25:23,660 --> 00:25:27,980 as a double. So we could assign that somewhere else. But notice we have two 536 00:25:27,980 --> 00:25:30,899 parameters here, and they're separated by a comma, okay? 537 00:25:30,898 --> 00:25:33,658 And that's, generally, what we're gonna use to separate parameters 538 00:25:33,659 --> 00:25:37,880 is commas, just like you saw when we created, for example, objects over here, 539 00:25:37,880 --> 00:25:40,890 right? We created a new G rect, and we had to give it four parameters. They were just 540 00:25:40,890 --> 00:25:43,940 separated by commas, same kind of idea going on over here. 541 00:25:43,940 --> 00:25:47,940 Now, the question comes up, what's the whole point of having these 542 00:25:47,940 --> 00:25:49,890 kind of functions 543 00:25:49,890 --> 00:25:51,919 exist in the Java world or methods in general? 544 00:25:51,919 --> 00:25:55,850 And the critical part about methods in Java, there's the whole notion of 545 00:25:55,849 --> 00:25:57,449 decomposition and top-down design. 546 00:25:57,450 --> 00:25:58,549 That's part of it. 547 00:25:58,549 --> 00:26:02,000 That's not the most critical part. The most critical part has 548 00:26:02,000 --> 00:26:05,500 to do with a notion we think of as information hiding, okay? 549 00:26:05,500 --> 00:26:07,499 So what is information hiding all about? 550 00:26:07,499 --> 00:26:11,338 The real idea - the way you can think about this is that everything in the 551 00:26:11,338 --> 00:26:11,849 world 552 00:26:11,849 --> 00:26:13,219 is a function 553 00:26:13,220 --> 00:26:19,160 or some method. So, for example, anyone know what this is? CD player. 554 00:26:19,160 --> 00:26:22,310 CD player, you're like, "Mehran, a CD player, come on. You've gotta be kidding. Like, what, 555 00:26:22,309 --> 00:26:25,659 were you, like, on some 1990s archeological dig or something?" It's like, oh, 556 00:26:25,660 --> 00:26:26,038 okay. 557 00:26:26,038 --> 00:26:28,099 I think I've found the CD player. 558 00:26:28,099 --> 00:26:31,439 This is a CD player, right? It's also a method. 559 00:26:31,440 --> 00:26:34,059 What does that mean that it's a method? Well, guess what? 560 00:26:34,059 --> 00:26:38,419 It takes something in, and the thing it takes in happens to be CDs, and we have a little CD, 561 00:26:38,420 --> 00:26:40,970 Alice in Chains, always a good call, 562 00:26:40,970 --> 00:26:43,180 Apocalyptica. Any Apocalyptica fans in here? 563 00:26:43,180 --> 00:26:46,538 Yeah, a few, it's Metallica done on the cello, 564 00:26:46,538 --> 00:26:47,629 and 565 00:26:47,630 --> 00:26:49,260 Tracy Chapman just to date myself. 566 00:26:49,259 --> 00:26:53,450 But what do you do? You take a CD. You put inside here. You hit close, and you hit play, 567 00:26:53,450 --> 00:26:54,819 and out comes music, 568 00:26:54,819 --> 00:26:58,980 and the music that comes out of this thing depends on which CD you put in, 569 00:26:58,980 --> 00:27:02,120 but the interesting thing about it, if you think about it, is all of the 570 00:27:02,119 --> 00:27:06,159 electronics in here are completely reusable, right? I can use this CD player 571 00:27:06,160 --> 00:27:08,660 on, virtually, any CD, right? 572 00:27:08,660 --> 00:27:11,890 Someday we might get to the day where you go to the store, and you buy a CD, and all 573 00:27:11,890 --> 00:27:15,250 the electronics to actually play the CD are inside the box, and you just plug your 574 00:27:15,250 --> 00:27:18,240 headphones into this, and you just listen to your CDs, and then we toss away those electronics when 575 00:27:18,240 --> 00:27:21,250 we get rid of that CD, and we get a copy of all the 576 00:27:21,250 --> 00:27:22,179 electronics again. That would be 577 00:27:22,179 --> 00:27:25,880 a huge waste, right, if you kind of think about landfill space and all that stuff. 578 00:27:25,880 --> 00:27:27,260 So why don't they do that? 579 00:27:27,259 --> 00:27:30,789 Why don't all the electronics to listen to a CD come with the CD? 580 00:27:30,789 --> 00:27:34,389 Because I can create them once and generalize them in a way 581 00:27:34,390 --> 00:27:37,400 so that if I pass in the right parameter, which is the CD, 582 00:27:37,400 --> 00:27:41,780 I can use the same thing over and over for all different kinds of values that go 583 00:27:41,779 --> 00:27:42,138 in, 584 00:27:42,138 --> 00:27:45,629 and, as a result, produce all kinds of values coming out, okay? 585 00:27:45,630 --> 00:27:48,100 So that's the thing you want to think about in terms of methods is you want to 586 00:27:48,099 --> 00:27:51,129 think about methods and defining what's gonna go into them and what's gonna come out 587 00:27:51,130 --> 00:27:51,730 of them 588 00:27:51,730 --> 00:27:55,230 in a way that's general enough that they can be used over and over again, and 589 00:27:55,230 --> 00:27:58,118 that's what the whole beauty of software engineering about is thinking about 590 00:27:58,118 --> 00:28:00,429 that generality, okay? 591 00:28:00,430 --> 00:28:03,750 So with that said, how do we actually define a method, okay? 592 00:28:03,750 --> 00:28:08,269 So we need a little bit of terminology to think about how we define methods. 593 00:28:08,269 --> 00:28:09,819 Yeah, it's time for 594 00:28:09,819 --> 00:28:13,950 that dreaded terminology again in the object-oriented world, okay? So 595 00:28:13,950 --> 00:28:15,470 when we actually 596 00:28:15,470 --> 00:28:19,120 call a method, right, you've seen this a little bit, just like we did over there. 597 00:28:19,119 --> 00:28:22,039 We have what we refer to as the receiver, 598 00:28:22,039 --> 00:28:26,069 the name of the method, and then some arguments, 599 00:28:26,069 --> 00:28:29,799 which are the parameters, right? So here the receiver happens to be math. 600 00:28:29,799 --> 00:28:33,599 The method name is power, and then there's some arguments X and Y that 601 00:28:33,599 --> 00:28:35,480 get passed in, okay? 602 00:28:35,480 --> 00:28:39,569 The way we think about this thing is we say that we are calling or invoking a 603 00:28:39,569 --> 00:28:40,439 particular method; 604 00:28:40,440 --> 00:28:44,039 here is the name of that method. So a lot of times you'll hear me say, "Call the method." 605 00:28:44,039 --> 00:28:46,670 That's what we're referring to is invoking that method. 606 00:28:46,670 --> 00:28:49,860 What we passed in terms of these arguments is 607 00:28:49,859 --> 00:28:54,299 the parameters, what that function is actually or that method is actually 608 00:28:54,299 --> 00:28:57,849 expecting, and you'll hear me sometimes use the terms function and method 609 00:28:57,849 --> 00:28:59,799 interchangeably, and they basically mean the same thing. 610 00:28:59,799 --> 00:29:03,569 And this guy can, potentially, give me back some value, just like square root and 611 00:29:03,569 --> 00:29:04,889 power did over there. 612 00:29:04,890 --> 00:29:08,509 Now, sometimes we also refer to this, and you may have heard me say this in the 613 00:29:08,509 --> 00:29:09,369 Carroll world, right, 614 00:29:09,369 --> 00:29:11,199 as sending a message 615 00:29:11,200 --> 00:29:13,730 to an object, right? So 616 00:29:13,730 --> 00:29:16,269 if we have our friend the G label, 617 00:29:16,269 --> 00:29:20,240 and I declare, let's say, I'll just call this lab to make it short, which is short 618 00:29:20,240 --> 00:29:21,250 for label, 619 00:29:21,250 --> 00:29:24,859 and I ask for a new G label, 620 00:29:24,859 --> 00:29:27,379 and that G label, I'm just gonna give it 621 00:29:27,380 --> 00:29:30,990 the words high at location 10, 10 just to keep it short and 622 00:29:30,990 --> 00:29:32,370 fit it on the board, 623 00:29:32,369 --> 00:29:34,699 and then I set its color by saying label - 624 00:29:34,700 --> 00:29:37,100 or in this case just lab.set 625 00:29:37,099 --> 00:29:40,109 color 626 00:29:40,109 --> 00:29:44,418 is color.red, 627 00:29:44,419 --> 00:29:45,000 okay? 628 00:29:45,000 --> 00:29:46,628 So when I've done that, 629 00:29:46,628 --> 00:29:50,668 lab is the receiver. The method name is called Set Color, and, alternatively, I 630 00:29:50,669 --> 00:29:55,680 would say that I'm sending the set color message to lab, okay? 631 00:29:55,680 --> 00:29:58,830 That's just the terminology that we use. You should just be aware of it. 632 00:29:58,829 --> 00:30:03,109 So set color is the message that we're sending, okay? 633 00:30:03,109 --> 00:30:06,928 That's the receiver of the message, same kind of thing as the name of the method 634 00:30:06,929 --> 00:30:08,950 and the receiver of that method, or 635 00:30:08,950 --> 00:30:12,480 making a method called or a method invocation; those names just get used 636 00:30:12,480 --> 00:30:13,549 interchangeably. 637 00:30:13,549 --> 00:30:17,480 So how do I actually create a method in Java, okay? 638 00:30:17,480 --> 00:30:20,319 Let me show you the syntax for creating a method. It's 639 00:30:20,319 --> 00:30:22,109 a little bit 640 00:30:22,109 --> 00:30:25,638 different, just slightly different than Carroll, but actually very similar, and so the 641 00:30:25,638 --> 00:30:26,928 way this looks is 642 00:30:26,929 --> 00:30:29,240 I first specify the visibility. You're 643 00:30:29,240 --> 00:30:30,970 like, "Huh? What is that?" 644 00:30:30,970 --> 00:30:33,750 You'll see in just a second. So we have the visibility, 645 00:30:33,750 --> 00:30:35,000 the type 646 00:30:35,000 --> 00:30:38,680 that the method may potentially return, what value it may return, 647 00:30:38,680 --> 00:30:41,799 the name of the method, all these have squiggly lines under them because these 648 00:30:41,799 --> 00:30:44,569 are things that you will fill in momentarily, 649 00:30:44,569 --> 00:30:46,480 and some parameters 650 00:30:46,480 --> 00:30:48,509 that get sent to that method, 651 00:30:48,509 --> 00:30:52,369 and inside here is the body, which is just a set of statements that would get 652 00:30:52,369 --> 00:30:55,399 executed as part of that method. Now, what do all these things mean? 653 00:30:55,400 --> 00:30:58,259 First of all, let's start with the visibility, which I'll just abbreviate 654 00:30:58,259 --> 00:30:59,079 as vis. 655 00:30:59,079 --> 00:31:03,980 The visibility, at least as far as you need to know right now, is either private 656 00:31:03,980 --> 00:31:06,509 or public, and you're like, "Hey, 657 00:31:06,509 --> 00:31:07,690 yeah, I remember those from 658 00:31:07,690 --> 00:31:11,369 the Carroll world." Yeah, and they're exactly the same. The way you want to think about it is 659 00:31:11,368 --> 00:31:14,259 right now all you need to worry about is your run method 660 00:31:14,259 --> 00:31:15,450 is public, 661 00:31:15,450 --> 00:31:19,309 and, pretty much, all the other methods you write, as long as that method is only 662 00:31:19,309 --> 00:31:22,649 used in a single class, which is generally the class that you are writing that 663 00:31:22,650 --> 00:31:25,919 method in, they will be private. So, for 664 00:31:25,919 --> 00:31:28,960 all intensive purposes right now, all the methods that you're gonna write, 665 00:31:28,960 --> 00:31:31,058 except for run, are all gonna be private, 666 00:31:31,058 --> 00:31:35,220 and run has to be public. Uh huh? [Off 667 00:31:35,220 --> 00:31:38,509 mic]. The way things are set up, you just need to make run public because it needs to 668 00:31:38,509 --> 00:31:41,569 have a larger visibility the way we've, kind of, defined 669 00:31:41,569 --> 00:31:44,759 the CS106 stuff right now. At the very end of class, like, when we get to the end of 670 00:31:44,759 --> 00:31:49,049 the quarter, I'll, sort of, lift the covers on everything, and you'll see why, okay? 671 00:31:49,049 --> 00:31:52,919 So that's the visibility, private or public. Run is public, most other things are 672 00:31:52,920 --> 00:31:55,038 private. All private means in terms of visibility 673 00:31:55,038 --> 00:31:59,329 is the only people who get to see this method are other methods inside the class 674 00:31:59,329 --> 00:32:00,819 that you're defining, okay? 675 00:32:00,819 --> 00:32:04,460 So that's why it's visibility. It's who else can see it, 676 00:32:04,460 --> 00:32:06,740 okay? The other thing you want to think about is the type. 677 00:32:06,740 --> 00:32:08,450 What's the type? The 678 00:32:08,450 --> 00:32:12,440 type here is specifying what gets returned by this method. So remember we talked 679 00:32:12,440 --> 00:32:16,370 about some methods, for example, that give you back something, okay? It 680 00:32:16,369 --> 00:32:19,028 is the type of the return 681 00:32:19,028 --> 00:32:20,029 value. 682 00:32:20,029 --> 00:32:23,369 What does that mean? If I have some function that's going to compute the 683 00:32:23,369 --> 00:32:26,168 sign of something, and you give me a double, and it's gonna give you back a 684 00:32:26,169 --> 00:32:28,659 double, it's return type is double, 685 00:32:28,659 --> 00:32:29,470 and you might say, 686 00:32:29,470 --> 00:32:32,019 "Hey, but, Mehran, when I was doing methods with Carroll, 687 00:32:32,019 --> 00:32:36,529 none of my Carroll methods actually returned any values." Right? Yeah, that was perfectly 688 00:32:36,529 --> 00:32:40,599 fine, and guess what word you used when they didn't return a value? Void. 689 00:32:40,599 --> 00:32:42,138 Void, yeah, that's a social. 690 00:32:42,138 --> 00:32:45,259 Good times all around, right? So 691 00:32:45,259 --> 00:32:46,558 void 692 00:32:46,558 --> 00:32:50,819 is a special type, which basically just means 693 00:32:50,819 --> 00:32:54,220 I'm not gonna return anything, right? So its return type is void. It's like you're 694 00:32:54,220 --> 00:32:57,829 sinking into the void. You're getting nothing from me. You know, it's, kind of, 695 00:32:57,829 --> 00:32:58,519 standoffish, 696 00:32:58,519 --> 00:33:02,099 but that's what it means is I'm not giving you back a value. You need to know 697 00:33:02,099 --> 00:33:06,029 that I'm not gonna give you back a value, so I still specify a return type as void. 698 00:33:06,029 --> 00:33:10,079 Then the name, you know about the name, right? That can be any valid name for a 699 00:33:10,079 --> 00:33:12,460 method just like we talked about in Carroll, 700 00:33:12,460 --> 00:33:14,090 and there's some parameters. 701 00:33:14,089 --> 00:33:17,970 The parameters are information we pass into this method that it may potentially do 702 00:33:17,970 --> 00:33:19,220 some computation on. 703 00:33:19,220 --> 00:33:22,430 So I'll show you an example of that in just a second, and what parameters actually 704 00:33:22,430 --> 00:33:24,610 are, and how we set them up, okay? 705 00:33:24,609 --> 00:33:28,279 Some functions may actually have no parameters, like the functions you wrote in Carroll, 706 00:33:28,279 --> 00:33:30,668 right? There was no information that got passed into those functions, 707 00:33:30,669 --> 00:33:34,880 in which case, the parameter list was empty, and all you had was open 708 00:33:34,880 --> 00:33:36,470 parens, close parens, which 709 00:33:36,470 --> 00:33:38,960 means there's no parameters for this function. When you call it, 710 00:33:38,960 --> 00:33:42,909 you just say the name of the function and open paren, close paren, okay? 711 00:33:42,909 --> 00:33:44,990 So, with that said, 712 00:33:44,990 --> 00:33:49,839 let me show you one more thing and then some examples. So 713 00:33:49,839 --> 00:33:54,389 you've seen a whole bunch of syntax so far of while loops, for loops, variables, 714 00:33:54,390 --> 00:33:55,919 declarations, all this happy stuff, 715 00:33:55,919 --> 00:33:59,190 and now I tell you, "Hey, there are these methods, and this is, kind of, how you write a method." And 716 00:33:59,190 --> 00:34:01,548 you're like, "Yeah, that has a lot of similarities to Carroll." 717 00:34:01,548 --> 00:34:05,460 But there's this value that you're returning. How do I return the value? 718 00:34:05,460 --> 00:34:08,999 Well, surprisingly enough, you use something called the return statement, 719 00:34:08,998 --> 00:34:14,058 which just says return, and then after return what's over here is some expression 720 00:34:14,059 --> 00:34:18,039 which just could be some mathematical logical expression, 721 00:34:18,039 --> 00:34:22,639 and when you get to a return statement somewhere inside a method, that method 722 00:34:22,639 --> 00:34:25,899 stops immediately. It does not complete the end of the method, 723 00:34:25,898 --> 00:34:29,848 and immediately returns whatever the value of expression is. A lot of times, 724 00:34:29,849 --> 00:34:33,700 the return will come at the end of a method, but it need not come at the end. 725 00:34:33,699 --> 00:34:37,109 Your method, at the point that it's currently executing, will stop and return 726 00:34:37,110 --> 00:34:38,929 with that value that is expression 727 00:34:38,929 --> 00:34:40,729 as soon as you hit it, okay? 728 00:34:40,728 --> 00:34:43,368 So that's, kind of, a whole mouthful of stuff. What does this actually mean? Let me 729 00:34:43,369 --> 00:34:45,669 just show you a bunch of examples, and hopefully this will make it a little 730 00:34:45,668 --> 00:34:47,429 more clear. 731 00:34:47,429 --> 00:34:52,599 732 00:34:52,599 --> 00:34:53,969 We want some methods; 733 00:34:53,969 --> 00:34:57,509 give me methods. So here's an example of a simple method. 734 00:34:57,509 --> 00:35:00,349 This is a method that converts feet to inches, 735 00:35:00,349 --> 00:35:03,250 right? It's, kind of, like, "Oh, Mehran, yeah, this is, you know, 736 00:35:03,250 --> 00:35:07,039 yet again, an entirely useless thing for you $2,000.00 computer to do." 737 00:35:07,039 --> 00:35:10,569 But it shows you a simple example of all the pieces you need to know to write a 738 00:35:10,568 --> 00:35:13,759 method. What's the visibility of this method? It's private. It's only gonna be used 739 00:35:13,759 --> 00:35:15,059 inside this class. 740 00:35:15,059 --> 00:35:18,139 What type is this feet to inches thing gonna give back to me? It's gonna give me 741 00:35:18,139 --> 00:35:21,649 back something that is of type double. The name of the function is Feet to 742 00:35:21,648 --> 00:35:22,679 Inches. 743 00:35:22,679 --> 00:35:24,969 What parameters does this function take? 744 00:35:24,969 --> 00:35:29,659 It takes in one parameter. The type of that parameter is a double, so what 745 00:35:29,659 --> 00:35:32,929 I'm expecting from you is something to double. It could actually be an 746 00:35:32,929 --> 00:35:34,189 explicit value that's a double. 747 00:35:34,188 --> 00:35:36,949 It could be a variable that holds a double U inside it, 748 00:35:36,949 --> 00:35:38,778 but that's what I'm expecting is a double, 749 00:35:38,778 --> 00:35:42,139 and the way I'm going to refer to the double that you give me in this 750 00:35:42,139 --> 00:35:44,509 function is with the name Feet. So 751 00:35:44,509 --> 00:35:48,059 all parameters have a type and a name, and the name is the name you're 752 00:35:48,059 --> 00:35:50,429 gonna use inside this method 753 00:35:50,429 --> 00:35:52,068 to refer to that parameter. 754 00:35:52,068 --> 00:35:53,170 So what do I do? 755 00:35:53,170 --> 00:35:55,130 You give me some number of feet as a double; 756 00:35:55,130 --> 00:35:58,509 I multiply it by 12, and that's what I'm gonna return to you. 757 00:35:58,509 --> 00:36:02,059 So when I get to this return statement, the method is immediately done, and it 758 00:36:02,059 --> 00:36:05,159 returns the value over here in the expression. Often times, you're not 759 00:36:05,159 --> 00:36:09,000 required to, but I like to parenthesize my expressions to make sure it's clear 760 00:36:09,000 --> 00:36:10,480 what's actually being returned. 761 00:36:10,480 --> 00:36:13,778 So let me show you another example. We'll just go through a bunch of them. Uh huh? Is it 762 00:36:13,778 --> 00:36:15,219 common to not 763 00:36:15,219 --> 00:36:18,309 put a comma as a separator for the 764 00:36:18,309 --> 00:36:19,250 type and then the 765 00:36:19,250 --> 00:36:22,969 name of the [inaudible] - Well, the type and the name come together, so there is no comma there. Let 766 00:36:22,969 --> 00:36:26,698 me show you another example that actually has more than one parameter, okay? 767 00:36:26,699 --> 00:36:28,670 We'll get to that right now. 768 00:36:28,670 --> 00:36:31,289 So here is a function that's the maximum function, 769 00:36:31,289 --> 00:36:33,839 right? Again, it's private. It's gonna return an integer because you're gonna 770 00:36:33,838 --> 00:36:35,650 give it two integers, 771 00:36:35,650 --> 00:36:38,979 and it's gonna return to you whichever one has the larger value. 772 00:36:38,978 --> 00:36:41,848 So it's name is Max. It's going to return an integer, 773 00:36:41,849 --> 00:36:44,798 and what it's gonna take is two arguments here, 774 00:36:44,798 --> 00:36:46,170 two parameters. 775 00:36:46,170 --> 00:36:49,900 Well, the first parameter, val one is an integer, and val two is also an integer. 776 00:36:49,900 --> 00:36:52,849 So the way I specify them is always they come in pairs, 777 00:36:52,849 --> 00:36:55,110 type and the name that I used to specify - 778 00:36:55,110 --> 00:36:59,170 type and the name I used to specify it, right? So val one and val two are both integers. 779 00:36:59,170 --> 00:37:01,970 What do I do inside here? I just have an if-val statement. 780 00:37:01,969 --> 00:37:05,118 If val one is greater than val two, then val one is the max. 781 00:37:05,119 --> 00:37:07,430 So right here I'm gonna return val one. 782 00:37:07,429 --> 00:37:11,389 As soon as I hit that return, I'm done with the method. I don't go do anything 783 00:37:11,389 --> 00:37:13,799 else, even though there was only an else that I wouldn't have done anyway, 784 00:37:13,800 --> 00:37:15,269 I return 785 00:37:15,269 --> 00:37:17,298 val one. If 786 00:37:17,298 --> 00:37:20,329 val one is not greater than val two, then I return val two because val two needs 787 00:37:20,329 --> 00:37:24,599 to be at least greater than or equal to val one, so it's the maximal value, okay? 788 00:37:24,599 --> 00:37:27,950 So, again, if you have multiple - you can have as many parameters as you want, sort 789 00:37:27,949 --> 00:37:30,509 of, within reason, right? And within reason I mean, like, sort of, a few 790 00:37:30,509 --> 00:37:31,519 thousand. 791 00:37:31,518 --> 00:37:35,318 You can have as many parameters as you want, and that's a lot of typing. 792 00:37:35,318 --> 00:37:39,028 They get separated by commas and they come in pairs, value and the name that you 793 00:37:39,028 --> 00:37:43,710 use to refer to it. Uh huh? [Off mic]. 794 00:37:43,710 --> 00:37:47,639 No, it will not print anything on the screen. All this will do is return that 795 00:37:47,639 --> 00:37:50,480 value to whichever place invoked this method. 796 00:37:50,480 --> 00:37:53,199 So if you think about something like read ints, right? Read ints gives you 797 00:37:53,199 --> 00:37:54,779 back a value, but 798 00:37:54,780 --> 00:37:58,640 it doesn't actually, I mean, that value happens to show up because these are typed it on the screen, 799 00:37:58,639 --> 00:38:01,118 but it wouldn't have printed it out otherwise, right? 800 00:38:01,119 --> 00:38:04,200 This doesn't print anything out; it just returns it back to where you, sort of, 801 00:38:04,199 --> 00:38:11,199 invoked this method from. Uh huh? [Off mic]. 802 00:38:12,719 --> 00:38:18,048 There is, and we'll get to it in about three weeks. [Off mic]. Yeah. 803 00:38:18,048 --> 00:38:20,798 So here's another one. This is something we refer to as a predicate method, and 804 00:38:20,798 --> 00:38:24,230 a predicate method is just our fancy, computer sciency term for 805 00:38:24,230 --> 00:38:27,300 a method that returns a Boolean, right? A method can return any type you 806 00:38:27,300 --> 00:38:29,609 want. This one happens to return a Boolean. 807 00:38:29,608 --> 00:38:32,019 Private Boolean is odd. What does this do? 808 00:38:32,019 --> 00:38:36,170 You give it an integer; it tells you back true or false. Is it odd, right? The 809 00:38:36,170 --> 00:38:39,420 name is pretty self-explanatory. So it takes that variable, 810 00:38:39,420 --> 00:38:43,000 computes the remainder after we divide by two, and if that remainder is equal 811 00:38:43,000 --> 00:38:46,518 equal to one, this whole thing is gonna be true, and it'll return true. 812 00:38:46,518 --> 00:38:48,098 If it's not equal equal to one, 813 00:38:48,099 --> 00:38:49,890 that means it's equal equal to zero. 814 00:38:49,889 --> 00:38:53,019 In that case, this is false, and it'll return false. So this whole expression 815 00:38:53,019 --> 00:38:54,969 here again because we have an equal equal, 816 00:38:54,969 --> 00:38:57,769 which is a logical operation, right, 817 00:38:57,768 --> 00:39:00,738 that's gonna give us something back that's true or false, and that's what we're 818 00:39:00,739 --> 00:39:02,739 gonna directly return to 819 00:39:02,739 --> 00:39:05,378 the person who called this function, okay? So 820 00:39:05,378 --> 00:39:09,368 any questions about that? Alrighty. 821 00:39:09,369 --> 00:39:12,838 So let's look at something in a slightly larger context, right? Let's put this all 822 00:39:12,838 --> 00:39:15,799 together in a full program so you can get a notion of 823 00:39:15,800 --> 00:39:18,620 defining the class, and defining the methods in it, and you can, sort of, see the whole thing 824 00:39:18,619 --> 00:39:22,569 as we build it up. So here is something called the Factorial Example Program, and 825 00:39:22,570 --> 00:39:26,640 it just extends the console program, and all this puppy's gonna do 826 00:39:26,639 --> 00:39:29,219 is basically compute some factorials 827 00:39:29,219 --> 00:39:32,969 from zero up to max num. So we have some constant here, which is the maximum 828 00:39:32,969 --> 00:39:37,219 number we want to compute factorials up to, and if you remember - let's take a slight, little 829 00:39:37,219 --> 00:39:40,480 digression to talk about factorials. So if you remember in your math class in the days 830 00:39:40,480 --> 00:39:41,769 of yore, 831 00:39:41,768 --> 00:39:43,938 there was this thing 832 00:39:43,938 --> 00:39:47,958 that looked like this, all right? 833 00:39:47,958 --> 00:39:51,739 And you saw it and you went, "Oh, It's n!, right?" I 834 00:39:51,739 --> 00:39:53,599 hopefully caught your attention. 835 00:39:53,599 --> 00:39:55,480 This was actually n factorial, 836 00:39:55,480 --> 00:39:58,579 right? Yeah, the explanation point - totally different now because a couple people were, 837 00:39:58,579 --> 00:40:00,049 like, sleeping, and 838 00:40:00,050 --> 00:40:02,239 "What? What was he talking about?" 839 00:40:02,239 --> 00:40:06,519 N factorial, and all n factorial is is multiplying all the numbers from one up 840 00:40:06,518 --> 00:40:08,438 to n!, okay? 841 00:40:08,438 --> 00:40:12,598 So five factorials, strangely enough, is just 1 x 2 x 3 x 842 00:40:12,599 --> 00:40:14,088 4 x 5, 843 00:40:14,088 --> 00:40:17,108 and one factorial is just one, 844 00:40:17,108 --> 00:40:20,328 and here is the bonus question; what's zero factorial? One. Oh, 845 00:40:20,329 --> 00:40:22,939 I love it. It just warms the [inaudible] of my heart. 846 00:40:22,938 --> 00:40:27,378 Yeah, it's one, and there are some people who ask, "But zero, you know, like, I, uh - " 847 00:40:27,378 --> 00:40:30,199 and they get all wound up tight, and they're like, "No, man, 848 00:40:30,199 --> 00:40:31,588 mathematicians just said it was." 849 00:40:31,588 --> 00:40:32,909 Because 850 00:40:32,909 --> 00:40:36,399 if you think about it, it's multiplying - it's starting with one and multiplying zero 851 00:40:36,400 --> 00:40:40,689 additional terms, right? So I'm left with one. It's a good time. So 852 00:40:40,688 --> 00:40:44,158 how do I actually compute that? Well, in my program I'm gonna have some run 853 00:40:44,159 --> 00:40:48,439 method that's going to count from zero up to max num and write out all the factorials. 854 00:40:48,438 --> 00:40:48,929 855 00:40:48,929 --> 00:40:52,919 It'll say zero factorials this, one factorials this, two factorials this, 856 00:40:52,920 --> 00:40:55,820 by calling a method called Factorial 857 00:40:55,820 --> 00:40:59,588 passing in the value of the thing I want to compute the factorial of. 858 00:40:59,588 --> 00:41:03,708 How do I compute the factorial? Well, I just add some method. 859 00:41:03,708 --> 00:41:07,018 This probably is gonna return an int. It's name is factorial. It's gonna take 860 00:41:07,018 --> 00:41:09,649 an n! of the thing I want to compute the factorial of, 861 00:41:09,650 --> 00:41:13,090 and how do I do that? Well, I start off with some result that's value is one, 862 00:41:13,090 --> 00:41:15,320 and I'm gonna have a for loop that's gonna count - here's one of the 863 00:41:15,320 --> 00:41:18,530 few times as computer scientists we don't count starting from zero; we actually 864 00:41:18,530 --> 00:41:20,519 count starting from one because it makes a difference. 865 00:41:20,519 --> 00:41:21,920 We're gonna count from one 866 00:41:21,920 --> 00:41:26,110 up to and including n!. That's why this is less than or equal rather than just less than. 867 00:41:26,110 --> 00:41:29,900 So from one up to and including n!, so there's still n! terms I'm gonna 868 00:41:29,900 --> 00:41:30,869 count through. 869 00:41:30,869 --> 00:41:34,680 What am I gonna do? I'm gonna take my results as I'm going along and times 870 00:41:34,679 --> 00:41:38,069 equals it by I, which means the first time I multiply by one, 871 00:41:38,070 --> 00:41:41,910 store it back to result, then by two, then three, then four, all the way up whatever this 872 00:41:41,909 --> 00:41:43,098 n! value was, 873 00:41:43,099 --> 00:41:44,670 and when I'm done doing this loop, 874 00:41:44,670 --> 00:41:48,180 I return to my result, which is my factorial, okay? 875 00:41:48,179 --> 00:41:51,928 Any questions about the factorial method by itself? 876 00:41:51,929 --> 00:41:55,048 Now, one thing that comes up, freaks people out a little bit, don't get 877 00:41:55,048 --> 00:41:56,259 freaked out; it's okay. 878 00:41:56,260 --> 00:41:57,910 You see an I here, 879 00:41:57,909 --> 00:41:59,509 and you see an I there, 880 00:41:59,509 --> 00:42:02,659 and you're like, "Oh, we just did that thing with nested loops, Mehran, where you 881 00:42:02,659 --> 00:42:05,489 told me that if one thing is inside another loop, 882 00:42:05,489 --> 00:42:09,659 like, I shouldn't use the same name. So why are you using the same name here?" And the 883 00:42:09,659 --> 00:42:12,978 reason why I'm using the same name is to make a little bit of an example, which is 884 00:42:12,978 --> 00:42:13,938 that 885 00:42:13,938 --> 00:42:18,848 the I here and the I here are different I's, okay? 886 00:42:18,849 --> 00:42:22,939 When you think about a particular method, a method can declare its own variable. So 887 00:42:22,938 --> 00:42:25,440 this result, its lifetime or its scope, 888 00:42:25,440 --> 00:42:28,619 is until we reach the end of the method down here. 889 00:42:28,619 --> 00:42:30,619 This guy lives inside this method. 890 00:42:30,619 --> 00:42:35,490 This I is an I that only lives inside the factorial method. It doesn't interact at 891 00:42:35,489 --> 00:42:39,939 all with the I outside here. So every method, and the interesting thing here is 892 00:42:39,940 --> 00:42:43,170 run is just another method. It just happens to be the special method that we 893 00:42:43,170 --> 00:42:44,759 always start executing from, 894 00:42:44,759 --> 00:42:46,619 but every method 895 00:42:46,619 --> 00:42:50,829 can have it's own declared variables, and this I here is just 896 00:42:50,829 --> 00:42:52,769 its own declarative version of I. 897 00:42:52,768 --> 00:42:59,768 It has nothing to do with the I up there, okay? Question? 898 00:43:02,208 --> 00:43:04,618 Okay, so is it possible to, 899 00:43:04,619 --> 00:43:08,239 like, give a variable from one method to the other method? We'll talk 900 00:43:08,239 --> 00:43:11,958 about that in just a bit. 901 00:43:11,958 --> 00:43:15,648 So someone was wondering is there a way I can, sort of, give some variable from over here 902 00:43:15,648 --> 00:43:17,308 down to over here? And 903 00:43:17,309 --> 00:43:20,979 I'll show you how you have to, kind of, think about that in just a little bit, okay? 904 00:43:20,978 --> 00:43:24,908 But any questions about this notion of the same I, that this I is actually a 905 00:43:24,909 --> 00:43:26,009 different I than that I. 906 00:43:26,009 --> 00:43:28,838 When you declare a variable in the method, you are getting your own version 907 00:43:28,838 --> 00:43:32,349 of that variable in the method. It has nothing to do with the same named 908 00:43:32,349 --> 00:43:32,890 variable 909 00:43:32,889 --> 00:43:35,929 in any other method, okay? 910 00:43:35,929 --> 00:43:41,878 Kind of, a funky thing. Uh huh? [Off mic]. 911 00:43:41,878 --> 00:43:44,130 That's just 912 00:43:44,130 --> 00:43:47,380 starting off the factorial, right? So if someone gives this five, 913 00:43:47,380 --> 00:43:49,278 we need to start off by saying 914 00:43:49,278 --> 00:43:52,548 we have some initial result, and then we're gonna multiply that by one, 915 00:43:52,548 --> 00:43:55,699 and then we're gonna multiply that by two. If we didn't have some initial thing 916 00:43:55,699 --> 00:43:57,099 we started multiplying, 917 00:43:57,099 --> 00:43:59,478 there would've been some unknown value there, 918 00:43:59,478 --> 00:44:02,868 and actually Java would've given us an error because it doesn't allow you to use 919 00:44:02,869 --> 00:44:04,539 uninitialized variables, okay? 920 00:44:04,539 --> 00:44:06,999 So this is, kind of, a funky thing, 921 00:44:06,998 --> 00:44:09,638 but important to, sort of, think about. 922 00:44:09,639 --> 00:44:13,059 There is a method in the context of a larger program. Variables within 923 00:44:13,059 --> 00:44:14,689 methods are not the same 924 00:44:14,688 --> 00:44:18,548 as the same named variable in another method. That's the key take home thing, like, 925 00:44:18,548 --> 00:44:21,449 know it, learn it, love it, tattoo it backward on your forehead so every day 926 00:44:21,449 --> 00:44:23,778 when you look in the mirror when you take a shower, 927 00:44:23,778 --> 00:44:28,449 you see it, and you just know because that's one thing that trips people up. Uh huh? Was 928 00:44:28,449 --> 00:44:32,679 there a question up here? 929 00:44:32,679 --> 00:44:37,369 So when you return result, does it get mapped into I? 930 00:44:37,369 --> 00:44:41,079 Is that why [inaudible] - Ah, good question, so when I return here, what's actually 931 00:44:41,079 --> 00:44:45,709 going on? When I actually could say I'm gonna compute the factorial of five, okay? 932 00:44:45,708 --> 00:44:48,648 This guy comes here - let's make it easy. Let's have him computing a 933 00:44:48,648 --> 00:44:49,828 factorial of two. So 934 00:44:49,829 --> 00:44:53,649 it multiplies one by one, then it multiplies it by two. I get two. It returns this 935 00:44:53,648 --> 00:44:55,429 two. What happens to the two? 936 00:44:55,429 --> 00:45:00,418 That two goes back to the place at which this function was called. Where was that? 937 00:45:00,418 --> 00:45:04,308 That was up here. So this place up here where I said, hey, 938 00:45:04,309 --> 00:45:06,189 two 939 00:45:06,188 --> 00:45:09,398 plus your explanation point equals is what I would print on the screen. If I 940 00:45:09,398 --> 00:45:12,099 was equal to two here, right, two exclamation equals, 941 00:45:12,099 --> 00:45:16,329 and then what's the value that I get back when I call factorial of two? 942 00:45:16,329 --> 00:45:19,568 Well, it computes factorial of two. It happens to be the value of two. 943 00:45:19,568 --> 00:45:22,429 It just sticks in a two for whatever 944 00:45:22,429 --> 00:45:25,078 you had as the calling method, okay? 945 00:45:25,079 --> 00:45:26,690 So you can think of 946 00:45:26,690 --> 00:45:28,579 when you call a particular method, 947 00:45:28,579 --> 00:45:30,279 the name of that method, 948 00:45:30,280 --> 00:45:32,280 think of it as getting replaced 949 00:45:32,280 --> 00:45:35,269 by the result that gets returned from that method, okay? 950 00:45:35,268 --> 00:45:38,858 So if I run this program, just to make it clear, 951 00:45:38,858 --> 00:45:39,768 952 00:45:39,768 --> 00:45:41,298 let me show it to you in its 953 00:45:41,298 --> 00:45:45,148 full glory over here. Here's a factorial example. I'll go ahead and run it, and you can see 954 00:45:45,148 --> 00:45:49,679 what its 955 00:45:49,679 --> 00:45:50,750 output is. 956 00:45:50,750 --> 00:45:53,119 Factorial example, we'll 957 00:45:53,119 --> 00:45:55,798 run it, and there it is, right? So it says - 958 00:45:55,798 --> 00:45:59,588 if you can see up there, a zero factorial equals one because it writes out 959 00:45:59,588 --> 00:46:02,228 zero factorial, 960 00:46:02,228 --> 00:46:04,888 right, in the code over here. It would write out I has the value zero 961 00:46:04,889 --> 00:46:07,209 the first time through. So it writes out zero 962 00:46:07,208 --> 00:46:10,259 factorial equals, and then it calls factorial on zero, 963 00:46:10,260 --> 00:46:12,369 and what it gets back from factorial of zero 964 00:46:12,369 --> 00:46:16,190 is one. So that one is just what gets displayed in the place of factorial zero because that value I get back from factorial zero is what's actually used 965 00:46:16,190 --> 00:46:19,838 here, 966 00:46:19,838 --> 00:46:22,619 and it keeps doing that all the way through the loop, right? So down here it calls 967 00:46:22,619 --> 00:46:24,369 a factorial of nine, 968 00:46:24,369 --> 00:46:26,349 gets the value for factorial of nine, 969 00:46:26,349 --> 00:46:27,979 and returns that 970 00:46:27,978 --> 00:46:31,338 to the place where it's actually gonna print it out, okay? 971 00:46:31,338 --> 00:46:38,338 So let me show you one more quick thing. Okay. 972 00:46:43,789 --> 00:46:45,980 Which is returning an object from a function, 973 00:46:45,980 --> 00:46:48,579 okay? So it turns out, interestingly enough, 974 00:46:48,579 --> 00:46:51,989 you can not only return, like, ints, and doubles, and Booleans from functions, you 975 00:46:51,989 --> 00:46:54,048 can also return whole objects. 976 00:46:54,048 --> 00:46:55,699 So here is a 977 00:46:55,699 --> 00:46:56,328 method 978 00:46:56,329 --> 00:46:57,610 called Filled Circle, 979 00:46:57,610 --> 00:47:01,410 and what I'm gonna give to this method is some X and Y location for the 980 00:47:01,409 --> 00:47:04,679 center of the circle. Notice that's different than what G Oval 981 00:47:04,679 --> 00:47:06,748 Expecting to the upper left-hand corner. I'm gonna specify 982 00:47:06,748 --> 00:47:09,658 an X, Y location which is the center of the circle, 983 00:47:09,659 --> 00:47:13,838 a radius for that circle, and a color, and what I want this method to give back to 984 00:47:13,838 --> 00:47:18,478 me is an actual object that is a circle whose center is that XY. 985 00:47:18,478 --> 00:47:22,649 It has the given radius R, and it's filled in of that particular color. Well, 986 00:47:22,650 --> 00:47:24,159 how do I do that? 987 00:47:24,159 --> 00:47:27,230 Inside here, I say, hey, I'm gonna create a new object 988 00:47:27,230 --> 00:47:29,978 that's of type G Oval, and I'm gonna call it Circle, 989 00:47:29,978 --> 00:47:34,308 and I'm going to get a new oval, right, and remember oval expecting the upper 990 00:47:34,309 --> 00:47:36,119 left-hand coordinate for the oval. 991 00:47:36,119 --> 00:47:39,030 I want the circle centered at XY, so what do I do? 992 00:47:39,030 --> 00:47:43,260 I take that XY, and I subtract the radius from the X direction and the Y direction, 993 00:47:43,260 --> 00:47:43,859 994 00:47:43,858 --> 00:47:46,239 which gives me the upper left-hand corner 995 00:47:46,239 --> 00:47:48,829 for the bounding box for that circle, 996 00:47:48,829 --> 00:47:51,169 and then I say give me something that has 997 00:47:51,168 --> 00:47:55,449 a width of two times the radius and a height of two times the radius because 998 00:47:55,449 --> 00:47:57,280 that's what defines that circle, right? 999 00:47:57,280 --> 00:48:01,019 The diameter is two times the radius in both the height and the width direction. 1000 00:48:01,018 --> 00:48:02,548 So I get that oval. 1001 00:48:02,548 --> 00:48:06,038 I've now called this Circle because it's my new object. I tell that circle to be 1002 00:48:06,039 --> 00:48:08,490 filled because that's what I want to return is a filled circle. 1003 00:48:08,489 --> 00:48:10,729 I tell that circle to set its color 1004 00:48:10,730 --> 00:48:15,298 to be whatever color was passed in here. Notice the K sensitivity. 1005 00:48:15,298 --> 00:48:17,469 Color with a capital "C" is a type. 1006 00:48:17,469 --> 00:48:20,979 Color with a lowercase "c" is actually the name of the parameter that I'm gonna 1007 00:48:20,978 --> 00:48:21,828 refer to, 1008 00:48:21,829 --> 00:48:24,639 and that's what I use here is lower case "c." 1009 00:48:24,639 --> 00:48:27,829 So I set the color of the circle to be whatever color was passed in, and then I 1010 00:48:27,829 --> 00:48:31,849 return this object. So that whole big box that contains this object that is some 1011 00:48:31,849 --> 00:48:33,700 filled-in circle at that location, 1012 00:48:33,699 --> 00:48:36,858 I say, hey, you, this is what you wanted because I'm giving you back a G 1013 00:48:36,858 --> 00:48:37,400 oval. 1014 00:48:37,400 --> 00:48:39,440 Here you go, and I give you back the whole thing, 1015 00:48:39,440 --> 00:48:41,639 which means when you invoke this method, 1016 00:48:41,639 --> 00:48:45,509 you better take the result and assign it to someplace that's a G oval, otherwise - 1017 00:48:45,510 --> 00:48:48,619 or just add it to your canvas. You need to do something with it that you would 1018 00:48:48,619 --> 00:48:50,469 normally do with a G oval, and 1019 00:48:50,469 --> 00:48:56,778 you can't assign it to an int because an int and a G oval aren't compatible. Uh huh, question? [Off mic]. 1020 00:48:56,778 --> 00:49:00,619 Okay, yeah, you can't assign it to an int; you have to assign it to an object of the same type. 1021 00:49:00,619 --> 00:49:02,929 Uh huh, question back 1022 00:49:02,929 --> 00:49:05,519 there? Why are X and Y set to 1023 00:49:05,519 --> 00:49:09,068 doubles if the pixels are all whole numbers? 1024 00:49:09,068 --> 00:49:10,088 Ah, good question, 1025 00:49:10,088 --> 00:49:13,048 so I could've actually made this with int and I probably should have made it 1026 00:49:13,048 --> 00:49:13,728 with int, so 1027 00:49:13,728 --> 00:49:20,728 it was my bad. Uh huh? In the 1028 00:49:24,528 --> 00:49:26,148 red method, 1029 00:49:26,148 --> 00:49:28,028 how would you add that 1030 00:49:28,028 --> 00:49:31,998 oval? Like, what's this, like, syntax? So the way you could think of it - let me just write it for you up here. So if 1031 00:49:31,998 --> 00:49:33,038 1032 00:49:33,039 --> 00:49:34,449 1033 00:49:34,449 --> 00:49:38,599 I actually had the run method, 1034 00:49:38,599 --> 00:49:40,359 so I'll 1035 00:49:40,358 --> 00:49:41,409 blank, 1036 00:49:41,409 --> 00:49:42,929 void, 1037 00:49:42,929 --> 00:49:44,479 run, 1038 00:49:44,478 --> 00:49:45,379 and then 1039 00:49:45,380 --> 00:49:50,599 let's say I had this function filled circle, right? So I called the method 1040 00:49:50,599 --> 00:49:52,390 Filled Circle, and 1041 00:49:52,389 --> 00:49:56,728 let's just say I want to put this at location 10, 10, and its radius is two 1042 00:49:56,728 --> 00:50:00,919 and the color that I want to give it is red, okay? 1043 00:50:00,920 --> 00:50:05,088 Now, the thing that this gives me back is a G oval object. There's a couple things I could 1044 00:50:05,088 --> 00:50:08,179 do with it. I could declare a G oval out here, 1045 00:50:08,179 --> 00:50:10,179 call this O, and 1046 00:50:10,179 --> 00:50:13,608 assigned filled circle to it, and now I have the object O out here, which is 1047 00:50:13,608 --> 00:50:17,058 actually that circle that this method gave back to me, and I 1048 00:50:17,059 --> 00:50:18,480 can do whatever I want with it. 1049 00:50:18,480 --> 00:50:20,608 Like, I could say add 1050 00:50:20,608 --> 00:50:25,068 O, and it would add it to my canvas. Alternatively, if I really wanted to, 1051 00:50:25,068 --> 00:50:27,538 I wouldn't even need to set filled circle 1052 00:50:27,539 --> 00:50:30,949 to some object that I want to keep track of out here. I'm just gonna say, 1053 00:50:30,949 --> 00:50:32,630 hey, you're gonna give me a circle, 1054 00:50:32,630 --> 00:50:35,849 I'm gonna add it directly to my canvas. 1055 00:50:35,849 --> 00:50:40,430 So anything that I would be able to do in my run method with a regular G oval 1056 00:50:40,429 --> 00:50:43,669 is the same thing I can do with this G oval that's coming back from a method because 1057 00:50:43,670 --> 00:50:47,700 it's just giving me back a G oval, and I can use it in the same way. Alrighty? 1058 00:50:47,699 --> 00:50:51,348 I will see you on Wednesday then. We'll talk a little bit more about functions 1059 00:50:51,349 --> 00:50:51,970 and methods then.