1 00:00:03,520 --> 00:00:10,519 2 00:00:11,398 --> 00:00:14,668 This presentation is delivered by the Stanford Center for Professional 3 00:00:14,669 --> 00:00:21,669 Development. 4 00:00:24,798 --> 00:00:28,108 With that said it's time to actually get into our next great topic in computer 5 00:00:28,109 --> 00:00:31,559 science. So I'll just leave this up here if you want to write down these numbers for 6 00:00:31,559 --> 00:00:33,060 whatever reason. But 7 00:00:33,060 --> 00:00:37,429 to wrap up a little bit about one of our last great topics, which was arrays, and 8 00:00:37,429 --> 00:00:42,369 specifically multi-dimensional arrays. And I'll just call them multi-dim arrays. 9 00:00:42,369 --> 00:00:45,448 And you've seen a couple examples of these right now, but I want to - 10 00:00:45,448 --> 00:00:48,558 already, right, in the last couple classes you saw some examples of multidimensional arrays. 11 00:00:48,558 --> 00:00:51,198 But I want to show you yet another example to show 12 00:00:51,198 --> 00:00:55,218 in a very nuts, and bolts kind of way how funky things get when you have arrays of arrays, 13 00:00:55,219 --> 00:00:58,658 and you pass them around as parameters in a program - just so you see yet 14 00:00:58,658 --> 00:01:01,119 another example to drive the point home. 15 00:01:01,119 --> 00:01:04,939 So let's say I was gonna write a program to, oh, keep track of midterm, and final 16 00:01:04,939 --> 00:01:06,129 scores, for example. 17 00:01:06,129 --> 00:01:09,209 And let's say the class was smaller than this. So I had at most 100 students 18 00:01:09,209 --> 00:01:12,179 because I was teaching some class like basket weaving or something, and 19 00:01:12,180 --> 00:01:13,780 it's just small. It's cuddly. 20 00:01:13,780 --> 00:01:17,840 We all hold hands. So I have some two-dimensional array that I want to have because 21 00:01:17,840 --> 00:01:21,430 I want to have midterm scores, and final scores, but I want to keep them 22 00:01:21,430 --> 00:01:21,909 separate. 23 00:01:21,909 --> 00:01:25,640 So I'm gonna have one array of midterm scores, and another array of 24 00:01:25,640 --> 00:01:29,170 final exam scores. But in some sense what I really have is a two-dimensional array 25 00:01:29,170 --> 00:01:30,439 that's two by 26 00:01:30,439 --> 00:01:33,230 the number of scores that I have as a maximum. So let's say 27 00:01:33,230 --> 00:01:36,170 100 is the maximum number of scores I'm gonna have. So I'm gonna a 28 00:01:36,170 --> 00:01:38,219 two by 100 array, 29 00:01:38,219 --> 00:01:40,039 and I'll call this scores, 30 00:01:40,040 --> 00:01:43,320 and I'm just gonna create this by basically saying new 31 00:01:43,319 --> 00:01:47,009 int. And you've seen the syntax before. Two - 32 00:01:47,010 --> 00:01:48,880 and this is 100. 33 00:01:48,879 --> 00:01:52,019 And these would probably be constants that I would have in a program, but just to save 34 00:01:52,019 --> 00:01:58,219 space, and save time, I'm not - you actually declare public static final int., and 35 00:01:58,219 --> 00:02:00,920 saw max size 100. So I'm just gonna put 100 in there. 36 00:02:00,920 --> 00:02:02,969 And as we talked about a little bit before, 37 00:02:02,968 --> 00:02:05,928 some particular entry of this grid, right - because this is just a 38 00:02:05,929 --> 00:02:09,009 two-dimensional grid - like score zero comma zero 39 00:02:09,008 --> 00:02:12,268 is just - this is just a single int. basically. 40 00:02:12,269 --> 00:02:15,169 But then we talked about this funky thing where we said, "Hey, you know what? 41 00:02:15,169 --> 00:02:18,789 A multi-dimensional array is actually an array of arrays." So a two-dimensional 42 00:02:18,788 --> 00:02:20,379 array's an array of arrays. A 43 00:02:20,379 --> 00:02:24,399 three-dimensional array is an array of arrays of arrays, right? 44 00:02:24,399 --> 00:02:28,158 So we could actually think about, "Well, what happens if I only specify one of the 45 00:02:28,158 --> 00:02:29,179 dimensions of the array?" 46 00:02:29,180 --> 00:02:34,319 So what do I get if I say, for example, scores sub zero, right? That's not one 47 00:02:34,318 --> 00:02:36,278 entry, so it's not an integer. 48 00:02:36,278 --> 00:02:38,239 That's in fact a whole array, 49 00:02:38,239 --> 00:02:40,400 right? The way to thing about this is I have 50 00:02:40,400 --> 00:02:42,968 two arrays 51 00:02:42,968 --> 00:02:46,608 that have size 100. All right. So we have a little break in there. 52 00:02:46,609 --> 00:02:49,719 And so score sub zero is this 53 00:02:49,718 --> 00:02:54,478 array, and score sub one is this array, and scores is kind of this array of arrays, 54 00:02:54,479 --> 00:02:55,109 right? 55 00:02:55,109 --> 00:02:58,248 It's really an array that has two elements. So this is element No. 56 00:02:58,248 --> 00:03:00,719 1 over here in the heavy chalk line of the array 57 00:03:00,719 --> 00:03:04,899 or element No. 0 - it's the first element in here is element number one. Okay. 58 00:03:04,899 --> 00:03:09,908 So this thing - the type of this thing is really an integer array. 59 00:03:09,908 --> 00:03:12,459 And that's the funky thing to kind of think about. Okay. 60 00:03:12,459 --> 00:03:15,639 So score sub zero behaves in your program 61 00:03:15,639 --> 00:03:18,329 just like if you had declared a one-dimensional array, which means you 62 00:03:18,329 --> 00:03:22,760 can pass score sub zero to a function that expects a one-dimensional array, 63 00:03:22,759 --> 00:03:26,989 and that's perfectly fine. What it's doing is passing this element of 64 00:03:26,989 --> 00:03:30,389 the scores array of arrays, which happens to be a whole array of integers 65 00:03:30,389 --> 00:03:31,618 in its self. Okay. 66 00:03:31,618 --> 00:03:33,919 So any questions conceptually 67 00:03:33,919 --> 00:03:35,269 about that? All 68 00:03:35,269 --> 00:03:39,959 right. So why don't we look at a little code that actually drives that point home. All right. 69 00:03:39,959 --> 00:03:41,718 So we'll put this away. 70 00:03:41,718 --> 00:03:44,878 Bye-bye midterm scores. Don't save. 71 00:03:44,878 --> 00:03:47,248 And we'll come to our friend the Eclipse, 72 00:03:47,248 --> 00:03:48,859 and we'll say, "Hey, Eclipse -" 73 00:03:48,860 --> 00:03:51,370 oh, we'll get to Roulette in just a moment. Don't worry. It's 74 00:03:51,370 --> 00:03:55,179 all about gambling in this class. All right. So 75 00:03:55,179 --> 00:03:58,179 it's not really gambling. All right. 76 00:03:58,179 --> 00:04:01,269 So I'll tell you we have a program that wants to keep track of test scores. So here what I'm gonna do is 77 00:04:01,269 --> 00:04:02,049 I'm gonna 78 00:04:02,049 --> 00:04:05,289 have a program test scores, again, we're just gonna set the font to be large. It's 79 00:04:05,288 --> 00:04:08,778 not a big deal. And I'm gonna ask the user for the number of scores. So I'm gonna ask the user 80 00:04:08,778 --> 00:04:12,429 for number of scores, and whatever the user gives me is num. scores. I'm 81 00:04:12,429 --> 00:04:14,378 going to create my scores array 82 00:04:14,378 --> 00:04:16,139 to be a new array of 83 00:04:16,139 --> 00:04:19,430 integers that's two by however many scores you have. And you should see this 84 00:04:19,430 --> 00:04:20,619 right now, and you should go, 85 00:04:20,619 --> 00:04:21,759 "But, Marron, 86 00:04:21,759 --> 00:04:25,000 you're missing the declaration of scores, right? This creates a new array of 87 00:04:25,000 --> 00:04:28,949 arrays, but where's the declaration of scores?" 88 00:04:28,949 --> 00:04:29,930 Ah, 89 00:04:29,930 --> 00:04:33,269 instance variable. So I want to show you an example of where we have an instance 90 00:04:33,269 --> 00:04:36,478 variable that's an array of arrays. So here I declare the instance variable. 91 00:04:36,478 --> 00:04:40,038 This just tells me that score's is going to be a two-dimensional array. It is not 92 00:04:40,038 --> 00:04:42,689 yet created the two-dimensional array. 93 00:04:42,689 --> 00:04:45,529 It is just set - I'm gonna have this thing called scores that gonna be a 94 00:04:45,529 --> 00:04:46,429 two-dimensional array, 95 00:04:46,430 --> 00:04:48,348 I still need to create it. 96 00:04:48,348 --> 00:04:51,009 So when my program runs up here, 97 00:04:51,009 --> 00:04:54,980 I ask the user for the number of scores, and I actually create the two-dimensional 98 00:04:54,980 --> 00:04:57,840 array. I don't need to declare scores because scores was declared 99 00:04:57,839 --> 00:05:00,769 as an instance variable or an ivar, so it's already declared. 100 00:05:00,769 --> 00:05:04,250 I need to create all the space for the all the elements of that array. 101 00:05:04,250 --> 00:05:05,500 Any questions about that? 102 00:05:05,500 --> 00:05:06,848 Hopefully that's clear. 103 00:05:06,848 --> 00:05:11,418 All right. So first thing I'm gonna do is I'm gonna initialize my scores, and 104 00:05:11,418 --> 00:05:14,568 here - and its scores. All this is gonna do is have two for loops that 105 00:05:14,569 --> 00:05:17,718 are gonna go through, and set all the scores in the grid to be zero to 106 00:05:17,718 --> 00:05:20,788 begin with. Now, I told you before, when you declare an array - when you create an 107 00:05:20,788 --> 00:05:21,248 array, 108 00:05:21,249 --> 00:05:24,819 the values of the array get initialized to be the default value for that type. And 109 00:05:24,819 --> 00:05:27,439 the default value for integers is zero. So you might 110 00:05:27,439 --> 00:05:29,939 say, "But, Marron, they're already zeros anyway, so why are you doing this?" I'm 111 00:05:29,939 --> 00:05:33,990 just doing this to kind of exemplify the point. I could put in a one here, and just say, "Hey, 112 00:05:33,990 --> 00:05:37,590 everyone starts with a one on the exam." Right? But we'll just say it's zero. 113 00:05:37,589 --> 00:05:40,329 How do I actually think about this two-dimensional array? 114 00:05:40,329 --> 00:05:43,800 Well, first of all, I want to think about the rows of the array, and then for every 115 00:05:43,800 --> 00:05:46,970 row it's got a certain number of entries or essentially the number of columns in 116 00:05:46,970 --> 00:05:47,910 the grid. 117 00:05:47,910 --> 00:05:50,300 So how many rows are there in the array? 118 00:05:50,300 --> 00:05:55,220 If I look at the two-dimensional grid scores, and I say scores dot length, 119 00:05:55,220 --> 00:05:57,890 and scores is an array of arrays, 120 00:05:57,889 --> 00:06:00,279 it tells me what is the size of the 121 00:06:00,279 --> 00:06:04,089 first dimension, right? So if scores is a two by 100 array, 122 00:06:04,089 --> 00:06:06,008 what's the size of the first dimension? 123 00:06:06,009 --> 00:06:07,300 It's two. 124 00:06:07,300 --> 00:06:11,000 So scores dot length - the way you want to think about it is this is scores. 125 00:06:11,000 --> 00:06:14,449 How big is scores? Scores actually is of size two. 126 00:06:14,449 --> 00:06:19,210 Each of the elements of scores of size 100, but if each of the 127 00:06:19,209 --> 00:06:23,488 individual entries of scores - score sub zero, and score sub one - is an array. 128 00:06:23,488 --> 00:06:26,288 So there's only two arrays inside scores. 129 00:06:26,288 --> 00:06:30,219 So scores dot length, back over here in my program, will in this case have the 130 00:06:30,220 --> 00:06:32,060 value too. Okay. 131 00:06:32,060 --> 00:06:35,310 So that's gonna be my outer loop. My outer loop's gonna say, "I want you to 132 00:06:35,310 --> 00:06:39,038 loop over however many rows you have in your grid, " is the way to think about it. 133 00:06:39,038 --> 00:06:42,060 For every row, how many entries do you have in that row? 134 00:06:42,060 --> 00:06:44,108 What I'm gonna do is I'm gonna say, "Hey, 135 00:06:44,108 --> 00:06:47,649 all the rows have the same size, so I'm just gonna arbitrarily pick the first 136 00:06:47,649 --> 00:06:50,369 row because I know there's always gonna be one row. If I knew there was always 137 00:06:50,369 --> 00:06:52,900 going to be ten rows, I could say, "Score sub nine." 138 00:06:52,899 --> 00:06:55,769 But for programmer, they're just like, "Why are you doing score sub nine?" So 139 00:06:55,769 --> 00:06:59,188 the general idiom - the way we using do it, is just score sub zero because you 140 00:06:59,189 --> 00:07:02,359 always know that there is a first - always a first entry in an array - 141 00:07:02,358 --> 00:07:03,579 or in an array of arrays. 142 00:07:03,579 --> 00:07:05,228 So score sub zero, 143 00:07:05,228 --> 00:07:09,680 what is that? Score sub zero I just told you is a whole array of integers, right? 144 00:07:09,680 --> 00:07:13,030 It's this thing right here. So its length is 100 because there's 145 00:07:13,029 --> 00:07:16,489 100 integers in it. So in this for loop we loop through 100 146 00:07:16,490 --> 00:07:19,918 elements, and so all we're gonna do inside here is we're gonna set scores 147 00:07:19,918 --> 00:07:22,508 I sub J. I's gonna very from 148 00:07:22,509 --> 00:07:26,210 zero to one because it's actually gonna count from one to two, but not include two. 149 00:07:26,209 --> 00:07:28,739 And J's gonna vary from zero to 99. 150 00:07:28,740 --> 00:07:31,778 And so it's - we're gonna set all the elements of the grid to be zero. 151 00:07:31,778 --> 00:07:35,360 Any questions about that? The syntax just kind of looks funky because here we have 152 00:07:35,360 --> 00:07:37,160 scores without a sub script on it. 153 00:07:37,160 --> 00:07:40,650 Here we have scores with a subscript on it. In both cases we're taking the 154 00:07:40,649 --> 00:07:41,138 length. 155 00:07:41,139 --> 00:07:45,120 And the important thing to think of conceptually is it's an array of arrays. 156 00:07:45,120 --> 00:07:46,399 So when we just take 157 00:07:46,399 --> 00:07:47,939 the first length, we're saying, 158 00:07:47,939 --> 00:07:52,290 "Hey, how many sub arrays do you contain?" It says, "Hey, I contain two arrays. I'm an array of 159 00:07:52,290 --> 00:07:52,718 arrays." 160 00:07:52,718 --> 00:07:56,069 And when we take the length of the first sub array, we get the actual number of 161 00:07:56,069 --> 00:07:57,519 columns for a grid. Okay. 162 00:07:57,519 --> 00:08:00,478 And you can generalize this to three, and four dimensional arrays, but for this class 163 00:08:00,478 --> 00:08:04,909 really all you're gonna need to worry about is two-dimensional arrays at most. Okay. So 164 00:08:04,910 --> 00:08:07,729 that's gonna initialize all the grid to be zero. So that's what an int. scores 165 00:08:07,728 --> 00:08:12,089 is gonna do. And then we're gonna write out score sub zero before we do any 166 00:08:12,089 --> 00:08:15,239 incrementing - action increment all the scores inside because it's just good to 167 00:08:15,240 --> 00:08:17,139 give everyone points. 168 00:08:17,139 --> 00:08:19,649 But before the increment, we're gonna print out 169 00:08:19,649 --> 00:08:21,550 the scores array 170 00:08:21,550 --> 00:08:22,978 at score sub zero. 171 00:08:22,978 --> 00:08:25,108 So if we look at print list - 172 00:08:25,108 --> 00:08:28,428 right - all print list does is - this is a really simple thing. It just gets passed 173 00:08:28,428 --> 00:08:29,878 an array of integers. 174 00:08:29,879 --> 00:08:33,778 It goes through all the elements of that array, right? So we just are calling the 175 00:08:33,778 --> 00:08:36,870 array list in this case, so it's just counting up from zero up to the length of the 176 00:08:36,870 --> 00:08:37,259 list, 177 00:08:37,259 --> 00:08:40,908 and just writing out all the individual elements, one on each line. So it's a trivial 178 00:08:40,908 --> 00:08:44,169 thing to write. You've seen this a whole bunch of times with arrays so far. 179 00:08:44,169 --> 00:08:47,969 But what are we passing to this guy, right? This guy's expecting a one-dimensional 180 00:08:47,970 --> 00:08:48,720 array. 181 00:08:48,720 --> 00:08:53,670 What we're passing is score sub zero here, which means what we're passing to it is 182 00:08:53,669 --> 00:08:55,808 just this single entry 183 00:08:55,808 --> 00:08:57,219 from the scores grid. 184 00:08:57,220 --> 00:09:01,810 We're passing a sub array of the scores grid. We're passing the very first array 185 00:09:01,809 --> 00:09:03,649 that's at score sub zero, 186 00:09:03,649 --> 00:09:06,840 which means score sub zero here is a whole array of integers, and that's the 187 00:09:06,840 --> 00:09:10,060 type this thing's expecting, so everything's perfectly fine. 188 00:09:10,059 --> 00:09:14,099 It may seem weird that we're sort of taking this grid, and dissecting it into its 189 00:09:14,100 --> 00:09:15,610 individual rows, but the 190 00:09:15,610 --> 00:09:17,470 important concept - and I'll say it once again - 191 00:09:17,470 --> 00:09:22,980 each one of those individual rows is just an array in itself. Okay. So 192 00:09:22,980 --> 00:09:26,120 last thing I want to do besides just printing out the scores, I want to go 193 00:09:26,120 --> 00:09:28,090 through, and increment the score list. 194 00:09:28,090 --> 00:09:31,350 Now, here's something that's funky. An increment score list - I'm gonna pass in one 195 00:09:31,350 --> 00:09:34,230 of the sub arrays. I'm gonna pass in score sub zero. 196 00:09:34,230 --> 00:09:36,060 What is increment, and score list do? 197 00:09:36,059 --> 00:09:37,928 It's expecting an array of integers. 198 00:09:37,928 --> 00:09:41,249 It just has a loop through that list, and it's gonna go through, and just add 199 00:09:41,249 --> 00:09:44,428 one to every entry in that list, right? Very standard thing you would think of 200 00:09:44,428 --> 00:09:46,679 doing maybe with some array of numbers. 201 00:09:46,679 --> 00:09:48,609 You just want to go, and add one to it. 202 00:09:48,610 --> 00:09:50,060 And that's all this 203 00:09:50,059 --> 00:09:53,719 method is doing. The only thing that's funky about it is when we call that method, 204 00:09:53,720 --> 00:09:56,529 we're passing it a portion of 205 00:09:56,529 --> 00:09:59,999 the scores grid, which is just the first array in that scores grid. Okay. So 206 00:09:59,999 --> 00:10:02,149 any questions about any of this? 207 00:10:02,149 --> 00:10:04,850 If this is all making sense nod your head. 208 00:10:04,850 --> 00:10:08,580 Excellent. If it's not making sense shake your head. Feel no qualms to shake your head. Good, good 209 00:10:08,580 --> 00:10:08,970 times. 210 00:10:08,970 --> 00:10:10,870 So let's run this. 211 00:10:10,870 --> 00:10:13,139 We'll be happy. We'll just make sure it's working. 212 00:10:13,139 --> 00:10:17,110 I'll give it a small number of scores because it'll be like a tiny wee class. 213 00:10:17,110 --> 00:10:20,399 How many - I should just ask. How many people are in the smallest class that 214 00:10:20,399 --> 00:10:22,799 you happen to be in not counting the instructor? 215 00:10:22,799 --> 00:10:26,469 Anyone lower than five? 216 00:10:26,470 --> 00:10:30,050 One. How many people are in that class? Four. 217 00:10:30,049 --> 00:10:32,219 Four. That's less than five. 218 00:10:32,220 --> 00:10:35,830 And as a matter of fact, that's all you need for candy 219 00:10:35,830 --> 00:10:37,490 because one is - 220 00:10:37,490 --> 00:10:42,870 one here - and that's amazing, right? It's just a class of four people. That's a good time. 221 00:10:42,870 --> 00:10:46,960 Sometimes - all right. So we'll say the number of scores is four just to emulate your class, and 222 00:10:46,960 --> 00:10:49,769 everyone started with a zero, and then they all got a one, 223 00:10:49,769 --> 00:10:54,669 which in a class of four people probably means 100 percent. All right. So 224 00:10:54,669 --> 00:10:55,639 that's all it's doing, 225 00:10:55,639 --> 00:10:59,359 right? And all of this is in terms of score sub zero. Score sub one 226 00:10:59,359 --> 00:11:02,579 has the value zero. It still maintains the value zero. We never touched it when we 227 00:11:02,578 --> 00:11:07,399 were doing the incrementing. So any questions about that? No. No. No. All right. So 228 00:11:07,399 --> 00:11:08,538 besides just doing life 229 00:11:08,538 --> 00:11:12,919 in this array way, you can also do things in the array list way. 230 00:11:12,919 --> 00:11:16,319 Okay. So we talked about arrays, and array lists. So here I'm just gonna have a 231 00:11:16,320 --> 00:11:20,310 single array of - array list of scores. And what I'm gonna do here, just so you can see the syntax, 232 00:11:20,309 --> 00:11:21,689 is again, 233 00:11:21,690 --> 00:11:25,149 I'm gonna have a private instance variable that's my array list scores 234 00:11:25,149 --> 00:11:28,839 that's an array list of integers. I only have one, and I'm not asking the 235 00:11:28,839 --> 00:11:32,029 user how big it needs to be, right, because array lists are dynamically sized. It 236 00:11:32,028 --> 00:11:35,288 doesn't matter. As the user keeps giving me scores, it's just gonna get bigger, 237 00:11:35,288 --> 00:11:36,009 and bigger. 238 00:11:36,009 --> 00:11:37,970 So I don't need to have the size a priori. 239 00:11:37,970 --> 00:11:41,190 So in my instance variable, I actually not only declare scores, but I 240 00:11:41,190 --> 00:11:43,079 actually create the array list. 241 00:11:43,078 --> 00:11:46,698 So I create a new array list. It's empty to begin with. I can just add stuff to it. 242 00:11:46,698 --> 00:11:49,169 Not a whole lot of excitement going on there. 243 00:11:49,169 --> 00:11:50,159 What do I do? 244 00:11:50,159 --> 00:11:52,939 I'm still gonna ask the user for the number of scores, 245 00:11:52,940 --> 00:11:57,270 but here when I'm initializing my scores, I don't need to worry about creating an 246 00:11:57,269 --> 00:12:00,759 array of a particular size. Here I just say, "Hey, you gave me some number of scores. 247 00:12:00,759 --> 00:12:04,208 You want me to initialize this array with how ever many scores you gave me." 248 00:12:04,208 --> 00:12:05,389 So I'm just gonna go, and 249 00:12:05,389 --> 00:12:09,110 do a for loop, and add that many elements to the array list because every time I 250 00:12:09,110 --> 00:12:12,089 add, the array list just automatically grows in size to be able to store all 251 00:12:12,089 --> 00:12:13,150 the scores. 252 00:12:13,149 --> 00:12:14,759 And I'm gonna store zeros. 253 00:12:14,759 --> 00:12:18,090 Well, as you know a priori, right, zero is just an int. 254 00:12:18,090 --> 00:12:19,830 It is not an integer. 255 00:12:19,830 --> 00:12:24,440 So in Java 5.0, and later because life is happy for us now, we get this 256 00:12:24,440 --> 00:12:27,319 automatic boxing, which means the computer sees the zero, and goes, 257 00:12:27,318 --> 00:12:28,938 "Oh, that zero's an int." 258 00:12:28,938 --> 00:12:33,438 Your array list stores integers, right? That's the parameterized type of your 259 00:12:33,438 --> 00:12:38,379 array list. So I'm automatically gonna box up your little zero like you're at the integer 260 00:12:38,379 --> 00:12:40,450 restaurant, right, and it's like, "Oh, I got this O." 261 00:12:40,450 --> 00:12:41,259 And it's like, "Yeah. Yeah. 262 00:12:41,259 --> 00:12:44,809 I can't eat the O, let me box it up for you as this thing called an integer, and 263 00:12:44,809 --> 00:12:47,609 then you can have it." And then it says, "Oh, I can add an integer to scores." And it just 264 00:12:47,610 --> 00:12:49,460 takes care of that for you. Okay. 265 00:12:49,460 --> 00:12:53,220 So when it scores it just goes through, and adds that many zeros to the list, and then we can write out the 266 00:12:53,220 --> 00:12:56,110 scores before we increment, and we'll print list our scores. 267 00:12:56,110 --> 00:12:59,399 And all print list does is - you've actually saw this exact same code 268 00:12:59,399 --> 00:12:59,990 last time. 269 00:12:59,990 --> 00:13:01,289 It has an array list. 270 00:13:01,289 --> 00:13:04,469 We don't need to specify the parameterized type here. This will just work on any 271 00:13:04,470 --> 00:13:05,649 kind of array list. 272 00:13:05,649 --> 00:13:09,719 It goes through up to the size of the array, and it gets each of the individual 273 00:13:09,719 --> 00:13:12,520 elements, and prints them out. Okay. 274 00:13:12,519 --> 00:13:15,480 So all we're gonna do now - the interesting part is to increment the score 275 00:13:15,480 --> 00:13:15,959 list. 276 00:13:15,958 --> 00:13:19,248 Here again, we're gonna go through scores up to the size of scores, 277 00:13:19,249 --> 00:13:23,339 but the syntax looks different. The syntax is actually a little bulkier than our 278 00:13:23,339 --> 00:13:27,080 array syntax, so we could just say, "Scores sub, 279 00:13:27,080 --> 00:13:30,600 you know, I plus/equals one." Right? To add one or plus plus if we just wanted to add 280 00:13:30,600 --> 00:13:31,470 one. 281 00:13:31,470 --> 00:13:35,340 Here the funky thing if we want to set a score is inside the scores list we have 282 00:13:35,340 --> 00:13:36,990 to use the set method. 283 00:13:36,990 --> 00:13:39,480 There's two parameters we sent to the set method. 284 00:13:39,480 --> 00:13:43,509 The first is the index. So we're gonna say, "Set at location I." So whatever the 285 00:13:43,509 --> 00:13:46,590 old value was, we don't care. We're just gonna slam something new over it, 286 00:13:46,590 --> 00:13:48,310 but set something at val - 287 00:13:48,309 --> 00:13:51,989 in place I. What are we gonna place at location I? 288 00:13:51,990 --> 00:13:56,049 We're going to get whatever was previously at location I, which is an 289 00:13:56,049 --> 00:13:56,959 integer, 290 00:13:56,960 --> 00:13:59,879 and we're gonna add one to it, which means if I have this thing that's an 291 00:13:59,879 --> 00:14:01,808 integer, and I want to add one to it, 292 00:14:01,808 --> 00:14:03,778 I take that integer, 293 00:14:03,778 --> 00:14:07,509 I un-box it to become an int. This happens for you automatically. So I get 294 00:14:07,509 --> 00:14:11,669 the int., which is zero, which is what I put in there before. I add one to it. So now I have the int. 295 00:14:11,669 --> 00:14:12,188 one, 296 00:14:12,188 --> 00:14:15,298 and now I want to store it back into something that's stores integers, so it automatically 297 00:14:15,298 --> 00:14:17,948 gets boxed up for you as this integer, 298 00:14:17,948 --> 00:14:21,229 and gets stored back. So the syntax looks a little bulky, and that's really the 299 00:14:21,230 --> 00:14:22,548 point of showing this to you. 300 00:14:22,548 --> 00:14:26,110 It - sometimes using array lists can actually be a bulky thing to do. 301 00:14:26,110 --> 00:14:27,970 But if we go ahead, and run this - do 302 00:14:27,970 --> 00:14:30,090 te do te do - we're 303 00:14:30,090 --> 00:14:33,230 running. We're running. 304 00:14:33,230 --> 00:14:35,250 Life is good. 305 00:14:35,250 --> 00:14:40,090 It chugs along. I got to get a faster Mac. 306 00:14:40,090 --> 00:14:42,570 Alrighty. Test scores with array lists - 307 00:14:42,570 --> 00:14:45,379 and it's gonna ask me for the number of scores. We'll say three this time because 308 00:14:45,379 --> 00:14:47,250 someone in your class dropped. 309 00:14:47,250 --> 00:14:48,100 And we had 310 00:14:48,100 --> 00:14:51,019 three zeros that got entered to begin with, and then we went through, and for each 311 00:14:51,019 --> 00:14:54,759 one of those zeros we sort of took the zero, added one to it, and stuffed it back in. So we 312 00:14:54,759 --> 00:14:58,379 got three ones. Not a whole lot of excitement going on there, but you see the difference. 313 00:14:58,379 --> 00:15:01,259 So when you see these two things one natural question that kind of comes up in 314 00:15:01,259 --> 00:15:02,519 your head is, 315 00:15:02,519 --> 00:15:06,340 "Okay, Marron, I sort of see two ways to do this. Which one should I use? Should I 316 00:15:06,340 --> 00:15:08,499 use an array list or should I use an array? And 317 00:15:08,499 --> 00:15:11,308 as a matter of fact you just told me on this next assignment 318 00:15:11,308 --> 00:15:15,409 I'm gonna be working my array muscles. So what's a good idea to think about?" 319 00:15:15,409 --> 00:15:17,579 Okay. And here are the pros, and cons. 320 00:15:17,580 --> 00:15:21,780 So if we think of array lists - and I'll just give you the pros, and cons relative to 321 00:15:21,779 --> 00:15:23,699 array lists versus arrays. 322 00:15:23,700 --> 00:15:26,850 The pros of an array list, 323 00:15:26,850 --> 00:15:31,320 right, are that at - the biggest pro is that it has this dynamic resizing. 324 00:15:31,320 --> 00:15:35,280 So if I need to add more elements - if I don't know the size a priori, this is a great 325 00:15:35,279 --> 00:15:38,339 way to go because it automatically resizes. Okay. 326 00:15:38,340 --> 00:15:41,780 And the other pro that it has is there is a bunch of high-level operations that 327 00:15:41,779 --> 00:15:44,860 automatically supports things like contains, right? So you can just see if an 328 00:15:44,860 --> 00:15:47,070 array list contains a particular element or not. 329 00:15:47,070 --> 00:15:49,810 So it has other operations - I'll just 330 00:15:49,809 --> 00:15:52,000 say ops. that are provided for you. 331 00:15:52,000 --> 00:15:54,639 Right? Which is a nice thing to have - so you don't need to worry about going, and 332 00:15:54,639 --> 00:15:55,989 finding some element in array. 333 00:15:55,989 --> 00:16:00,499 It does have some cons though, and the cons are that it's less efficient in terms 334 00:16:00,499 --> 00:16:04,589 of how the computer actually deals with an array list versus an array 335 00:16:04,589 --> 00:16:06,740 than an array. So it's less efficient 336 00:16:06,740 --> 00:16:08,250 than an array. 337 00:16:08,250 --> 00:16:12,889 The other con that it has is its syntax can get kind of bulky. 338 00:16:12,889 --> 00:16:16,319 Syntax is bulky because if a lot - one thing that I really want to do often is 339 00:16:16,320 --> 00:16:19,280 if I want to have some array, I want to make some modifications to values in 340 00:16:19,279 --> 00:16:20,169 that array. Well, in 341 00:16:20,169 --> 00:16:21,669 an array I just say, "Hey, 342 00:16:21,669 --> 00:16:25,378 you know, I have score sub three, and I stick in some value, and life is good." Or 343 00:16:25,379 --> 00:16:29,970 I say, "Score sub three plus/equals five, and I add five to it, and life is good." 344 00:16:29,970 --> 00:16:33,700 Here the syntax is bulky because I need to say, "Get the old value, do the 345 00:16:33,700 --> 00:16:36,860 modification to it, and then set it as the new value." And so I'm really doing two 346 00:16:36,860 --> 00:16:41,259 method calls every time I want to get, and set a meth - a value, and that's just bulky. 347 00:16:41,259 --> 00:16:43,820 And bulk means it becomes more error prone. 348 00:16:43,820 --> 00:16:47,960 The other thing is in the pre-Java 5.0 world. So we'll just say 349 00:16:47,960 --> 00:16:51,970 pre-5.0, which would you sad, and weep, and any time any - or if you run into 350 00:16:51,970 --> 00:16:55,590 any friends that are using a version of Java earlier than 5.0, you should just 351 00:16:55,590 --> 00:16:59,570 pat them on the shoulder, and take a moment of silence with them, and then like - 352 00:16:59,570 --> 00:17:02,490 I wouldn't say slap them around, but I would say, 353 00:17:02,490 --> 00:17:05,299 "Encourage them strongly to get a newer version of Java." 354 00:17:05,299 --> 00:17:08,930 Pre-5.0 - this was horrendously bulky to use 355 00:17:08,930 --> 00:17:11,490 because all of that boxing/un-boxing stuff didn't happen automatically, and you had to worry 356 00:17:11,490 --> 00:17:13,828 about, "Oh, I need to box, and I need to un-box." 357 00:17:13,828 --> 00:17:15,450 It was a bad time. 358 00:17:15,450 --> 00:17:17,880 The main difference - and you might say, "Yeah, 359 00:17:17,880 --> 00:17:21,870 well, you know, I don't care about efficiency, right, Marron? I got a whole bunch of memory, and my 360 00:17:21,869 --> 00:17:23,099 computer's fast. And 361 00:17:23,099 --> 00:17:26,578 yeah, bulky syntax, I can deal with that because I cut, and paste anyway. 362 00:17:26,578 --> 00:17:29,278 And I'm using Java 5.0 or later, so 363 00:17:29,278 --> 00:17:32,970 I don't really care about any of this, man. I'm just - I'm going the easy route." 364 00:17:32,970 --> 00:17:36,889 And the real big difference - the thing to keep in mind for one versus the other 365 00:17:36,888 --> 00:17:40,519 is if you know the size of your array beforehand - 366 00:17:40,519 --> 00:17:45,359 if you have a fixed size, 367 00:17:45,359 --> 00:17:48,638 almost always go with a regular array, 368 00:17:48,638 --> 00:17:50,009 not an array list. 369 00:17:50,009 --> 00:17:53,169 Keep that in your mind when you're thinking about the program that you're 370 00:17:53,169 --> 00:17:55,070 going to be writing. 371 00:17:55,069 --> 00:17:56,269 Fixed size - 372 00:17:56,269 --> 00:18:00,259 there's a lot of things for which you know they will have a fixed size or some 373 00:18:00,259 --> 00:18:03,058 maximum size. 374 00:18:03,058 --> 00:18:05,309 Hum. Let's take a moment to think about that. 375 00:18:05,309 --> 00:18:08,089 Fixed size - your next program - 376 00:18:08,089 --> 00:18:12,019 array. Good? All right. 377 00:18:12,019 --> 00:18:14,930 So just in case it wasn't clear. All right. 378 00:18:14,930 --> 00:18:18,170 So now, with that said, it's time to actually move onto the main topic for today. So 379 00:18:18,170 --> 00:18:21,550 any questions about array versus array list? 380 00:18:21,549 --> 00:18:24,089 Hopefully that should be clarified. All right. 381 00:18:24,089 --> 00:18:28,629 And you should just be thinking right now, "Oh, Marron, [Easy 382 00:18:28,630 --> 00:18:29,920 Button] That was easy." Can you 383 00:18:29,920 --> 00:18:31,580 even hear that? [Easy Button] "That was easy." 384 00:18:31,579 --> 00:18:34,099 Yeah. That's 385 00:18:34,099 --> 00:18:35,969 what I'm talking about. 386 00:18:35,970 --> 00:18:39,150 That was easy broadcast in stereo. 387 00:18:39,150 --> 00:18:41,950 All right - which means it's easy from two different sides. 388 00:18:41,950 --> 00:18:43,289 So now, the 389 00:18:43,289 --> 00:18:44,649 thing to think about 390 00:18:44,650 --> 00:18:46,920 is our friend debugging. All 391 00:18:46,920 --> 00:18:50,039 right. Which is something you've done this whole time, and you're like, "But, Marron, 392 00:18:50,039 --> 00:18:53,250 I've been doing debugging this whole time. How can we have a whole lecture on 393 00:18:53,250 --> 00:18:54,240 debugging?" 394 00:18:54,240 --> 00:18:57,349 Well, because it's time to think a little bit about that zen, and the art of 395 00:18:57,349 --> 00:19:00,819 debugging, and actually a bunch of practical tools I'm gonna show you to do debugging. 396 00:19:00,819 --> 00:19:01,899 Okay. 397 00:19:01,900 --> 00:19:04,160 The first thing to think about debugging is 398 00:19:04,160 --> 00:19:08,259 when you are a computer programmer or software engineer or in the larger sense 399 00:19:08,259 --> 00:19:10,910 computer scientist, there are different roles you play 400 00:19:10,910 --> 00:19:12,570 when you're building software. 401 00:19:12,569 --> 00:19:16,460 There's a portion of your time which is spent on designing software, and here you're 402 00:19:16,460 --> 00:19:18,210 really like an architect. 403 00:19:18,210 --> 00:19:21,860 You're kind of looking at big pieces, and how they fit together, and drawing 404 00:19:21,859 --> 00:19:25,000 schematics, and figuring out what your sub pieces are, and doing 405 00:19:25,000 --> 00:19:26,539 decomposition, and the whole deal. 406 00:19:26,539 --> 00:19:28,399 Then you go into coding. 407 00:19:28,400 --> 00:19:31,810 And when you're a coding - when you're coding, you're an 408 00:19:31,809 --> 00:19:33,769 engineer. 409 00:19:33,769 --> 00:19:36,980 Woo-woo. You got you're little hat on. You're in your train. No. 410 00:19:36,980 --> 00:19:41,059 You're writing code, and you're thinking about good software engineering, but 411 00:19:41,059 --> 00:19:44,809 you're building something, and you're driving a process forward. 412 00:19:44,809 --> 00:19:48,349 Then there's this thing called testing, which you do to your code. And a lot of 413 00:19:48,349 --> 00:19:52,009 times that means it's 4:00 a.m., you think your code works, the project is due 414 00:19:52,009 --> 00:19:52,888 tomorrow, 415 00:19:52,888 --> 00:19:55,298 and you decide, "Hey, I'm gonna do some testing." 416 00:19:55,298 --> 00:19:58,609 And this is the role that does not get taken seriously because it's 4:00 a.m., and 417 00:19:58,609 --> 00:20:00,319 your project is due. 418 00:20:00,319 --> 00:20:02,309 Often times testing means, "Hey, 419 00:20:02,309 --> 00:20:04,038 I ran it once - 420 00:20:04,038 --> 00:20:05,119 maybe twice, 421 00:20:05,119 --> 00:20:07,619 and I felt good, and I stopped. 422 00:20:07,619 --> 00:20:11,079 And I tried the simplest possible case I could try." 423 00:20:11,079 --> 00:20:14,949 That's not the role you want to play when you're testing. When you're testing, 424 00:20:14,950 --> 00:20:17,390 you're the vandal. 425 00:20:17,390 --> 00:20:20,100 And that - like you got the spray can in your hand, and you're like, 426 00:20:20,099 --> 00:20:22,459 "Yeah, we're going out." 427 00:20:22,460 --> 00:20:26,509 You are thinking about, "What do I need to do to break this program?" 428 00:20:26,509 --> 00:20:30,369 You're not thinking about, "What's the least I can do to please make the program 429 00:20:30,369 --> 00:20:33,899 run once, and get through, and hopefully there's no bugs, and if it makes it through once, 430 00:20:33,900 --> 00:20:37,900 I'll just believe it's correct, and that's a good time, and I'll turn it in?" 431 00:20:37,900 --> 00:20:41,630 You're just banging on the thing until you break it. As a matter of fact, a lot of 432 00:20:41,630 --> 00:20:43,790 software companies - most good software companies 433 00:20:43,789 --> 00:20:47,289 have people who this is their whole job, right? They don't do this. 434 00:20:47,289 --> 00:20:50,579 They get code from other people, and just try to break it. 435 00:20:50,579 --> 00:20:54,099 And this is a skill. You actually - you see some amazing things that people do to 436 00:20:54,099 --> 00:20:54,769 break code, 437 00:20:54,769 --> 00:20:55,940 but they do it, right? 438 00:20:55,940 --> 00:20:59,450 And you got to appreciate that because when you're writing your paper on your word 439 00:20:59,450 --> 00:21:02,400 processor at 4:00 in the morning, and it crashes, 440 00:21:02,400 --> 00:21:05,470 this person wasn't doing their job because you found the crash, and they 441 00:21:05,470 --> 00:21:06,660 didn't. Okay. 442 00:21:06,660 --> 00:21:09,920 So that's what you want to think about. You really want these people to be 443 00:21:09,920 --> 00:21:11,890 vandalizing the software. Okay. 444 00:21:11,890 --> 00:21:14,440 And then after the vandal comes along, and 445 00:21:14,440 --> 00:21:16,049 smashes everything up, 446 00:21:16,049 --> 00:21:20,690 then you have this job that's left to you, which is debugging. 447 00:21:20,690 --> 00:21:23,920 And when you're debugging, you could think, "Well, I'm the police officer, and I 448 00:21:23,920 --> 00:21:26,570 go, and track down the vandal, and make them stop." 449 00:21:26,569 --> 00:21:27,990 In fact, no, 450 00:21:27,990 --> 00:21:31,990 you want the vandal to keep vandalizing. You're the vandal. You got to keep trying 451 00:21:31,990 --> 00:21:33,558 to break your software, 452 00:21:33,558 --> 00:21:37,529 but at the same time you're a detective to try to find out 453 00:21:37,529 --> 00:21:40,809 where the breaks are happening, and how to fix them. Okay. 454 00:21:40,809 --> 00:21:44,700 Now, here's something that people find a little odd, and I like to call them the four - 455 00:21:44,700 --> 00:21:47,380 well, some people don't find them odd. The people who do software engineering don't 456 00:21:47,380 --> 00:21:49,390 find them odd. 457 00:21:49,390 --> 00:21:53,059 I like to call them the four D's of software. Okay. 458 00:21:53,058 --> 00:21:55,970 And the first D is design. You've already seen that. 459 00:21:55,970 --> 00:21:59,400 And then there's development, which is actually writing your code. 460 00:21:59,400 --> 00:22:02,410 And then there's debugging. 461 00:22:02,410 --> 00:22:06,240 And then - anyone know what comes after debugging? You're like, 462 00:22:06,240 --> 00:22:09,460 "D party." No. 463 00:22:09,460 --> 00:22:12,279 Deployment. Okay. 464 00:22:12,279 --> 00:22:16,460 And in this class so far we've focused on design, and development. 465 00:22:16,460 --> 00:22:19,390 And now, we're gonna spend some time thinking about debugging. And deployment 466 00:22:19,390 --> 00:22:22,278 is kind of like when you give it to your section leader. We don't want to worry about like 467 00:22:22,278 --> 00:22:25,788 you released software version 1.0 of your hangman program, and then 468 00:22:25,788 --> 00:22:26,578 we're like, 469 00:22:26,578 --> 00:22:29,539 "Oh, so when is 2.0 coming out?" And you're like, 470 00:22:29,539 --> 00:22:33,089 "Hum? Let me get back to you on that." And then you issue a press release, and the 471 00:22:33,089 --> 00:22:35,579 dates slip, and shareholders get angry. All right. Yeah, 472 00:22:35,579 --> 00:22:36,678 like - see 473 00:22:36,679 --> 00:22:39,329 we should have CS106a shareholders for like, 474 00:22:39,328 --> 00:22:43,349 "When is hangman coming out? Oh, my God, you slipped the deadline." All right. 475 00:22:43,349 --> 00:22:45,969 Then you'll know how serious those late days are. 476 00:22:45,970 --> 00:22:47,819 Then there's deployment. Okay. 477 00:22:47,819 --> 00:22:51,929 And the one thing that I would pause at, and actually a lot of people agree with, is that 478 00:22:51,930 --> 00:22:56,049 any problem that cascades from one step to another 479 00:22:56,049 --> 00:22:57,319 causes a factor of 480 00:22:57,319 --> 00:23:00,319 ten times in the cost to fix it. Okay. 481 00:23:00,319 --> 00:23:03,299 Which means you come up with a bad design for something, you're 482 00:23:03,299 --> 00:23:07,220 gonna spend ten times as much time writing the code for that bad design. 483 00:23:07,220 --> 00:23:10,370 And the bugs that come up as a result of bad design are gonna be a 100 484 00:23:10,369 --> 00:23:12,439 times as costly to fix. 485 00:23:12,440 --> 00:23:15,950 And once that software gets shipped out into the field, and the only way to fix it 486 00:23:15,950 --> 00:23:17,610 is to issue a new version, 487 00:23:17,609 --> 00:23:19,919 that's a 1,000 times more expensive. 488 00:23:19,920 --> 00:23:23,519 And people look at this, and they're like, "But, Marron, you can't be serious, right? How 489 00:23:23,519 --> 00:23:25,829 could it be a 1,000 times more expensive?" 490 00:23:25,829 --> 00:23:29,029 Well, let me give you a little example. 491 00:23:29,029 --> 00:23:31,379 How about our friend the space shuttle? 492 00:23:31,380 --> 00:23:35,040 All right. We have a space shuttle mission going on. 493 00:23:35,039 --> 00:23:38,829 Space shuttle had an initial set of software that was written for it. Okay. 494 00:23:38,829 --> 00:23:41,220 Right. And we're thinking like, "Oh, you know, like 495 00:23:41,220 --> 00:23:42,000 in tex." 496 00:23:42,000 --> 00:23:45,150 How much do you think one line of code costs when they were all said, and done? If 497 00:23:45,150 --> 00:23:48,269 you think of the total amount of money spent on the software development effort 498 00:23:48,269 --> 00:23:49,319 for the space shuttle 499 00:23:49,319 --> 00:23:51,329 versus the number of lines of code that were written, 500 00:23:51,329 --> 00:23:55,329 how much did it cost per line of code? 501 00:23:55,329 --> 00:23:56,669 Anyone want to venture a guess? [Inaudible]. 502 00:23:56,670 --> 00:23:57,580 Like dollars. 503 00:23:57,579 --> 00:23:58,980 $10.00. 504 00:23:58,980 --> 00:24:05,980 That's kind of fun. That's probably a job I'd take. Anyone want to venture 505 00:24:06,009 --> 00:24:10,069 another guess? [Inaudible] thousand. $1.00. $1.00. Higher - higher. 506 00:24:10,069 --> 00:24:12,619 Does that scare you? No. 507 00:24:12,619 --> 00:24:14,239 Does that scare you? 508 00:24:14,240 --> 00:24:17,230 I'd take that job. I'd be like, "$1,000.00 a line? 509 00:24:17,230 --> 00:24:19,240 Yeah, today in tex. 510 00:24:19,240 --> 00:24:20,919 Tomorrow, [inaudible]." Right? 511 00:24:20,919 --> 00:24:23,028 Like 512 00:24:23,028 --> 00:24:27,308 I'll take that job. 513 00:24:27,308 --> 00:24:29,899 That's the right number. 514 00:24:29,900 --> 00:24:33,920 Okay. There is about 500,000 lines of code - and now there is more - 515 00:24:33,920 --> 00:24:36,039 but in the initial version of the space shuttle, 516 00:24:36,039 --> 00:24:40,000 and it took a total cost of $500 million to develop that 517 00:24:40,000 --> 00:24:41,369 software. Okay. 518 00:24:41,369 --> 00:24:43,750 So this is serious, 519 00:24:43,750 --> 00:24:48,109 right? This isn't something where we're like, "Yeah, it's fine, and we're vandals, and we're debugging." Right? 520 00:24:48,109 --> 00:24:49,379 When you're on the space shuttle, 521 00:24:49,380 --> 00:24:50,700 you better hope 522 00:24:50,700 --> 00:24:53,679 someone was doing their job doing this, right? Because if something's wrong, 523 00:24:53,679 --> 00:24:57,909 you can't fix it up in space, right? In space no one can hear you scream - well, if you're 524 00:24:57,909 --> 00:24:58,480 in 525 00:24:58,480 --> 00:24:59,880 the movie "Alien.h 526 00:24:59,880 --> 00:25:03,870 But if you're on the space shuttle, it's real difficult to actually fix software. 527 00:25:03,869 --> 00:25:07,059 Okay. Mars Rover was actually lost because of a software issue. 528 00:25:07,059 --> 00:25:10,190 Not actually - well, it was a software design problem that came up. All 529 00:25:10,190 --> 00:25:14,170 right. Anyone know what the design problem was? [Inaudible]. 530 00:25:14,170 --> 00:25:17,789 Yeah, it was basically two different - it wasn't - it was all basically the same 531 00:25:17,789 --> 00:25:21,109 difference as feet versus meters, right? One person was coding to a spec that said, 532 00:25:21,109 --> 00:25:23,099 "Oh, the input you're gonna get is in feet." 533 00:25:23,099 --> 00:25:26,308 Another one thought it was in meters. Right? Those are off by a factor of three. 534 00:25:26,308 --> 00:25:27,869 That's enough to kill the project. 535 00:25:27,869 --> 00:25:30,879 And guess how much money was spent on that thing. A lot. 536 00:25:30,880 --> 00:25:32,990 All right. 537 00:25:32,990 --> 00:25:36,769 So it's important to think of this - well, so how do you actually think about doing 538 00:25:36,769 --> 00:25:39,019 good debugging? 539 00:25:39,019 --> 00:25:41,819 So the way we want to think about debugging 540 00:25:41,819 --> 00:25:44,909 is in some sense it's a river. And you're like, 541 00:25:44,910 --> 00:25:46,240 "What? What is 542 00:25:46,240 --> 00:25:48,210 Marron talking about?" It's - 543 00:25:48,210 --> 00:25:51,940 this is a river. This is the river of your program flowing. All 544 00:25:51,940 --> 00:25:54,680 right. And it's just nice. It's wonderful. And you're like sitting there, 545 00:25:54,680 --> 00:25:57,769 and you're having a picnic down here, and the sun is shining, and the birds 546 00:25:57,769 --> 00:25:58,720 are chirping, and you're like, 547 00:25:58,720 --> 00:26:01,640 "If my program gets here, and life is good, 548 00:26:01,640 --> 00:26:04,340 everything's just fine." But what happens when the river is flowing? You run your 549 00:26:04,339 --> 00:26:05,199 program, 550 00:26:05,200 --> 00:26:08,389 and you get all this sewage down here. And you're like, 551 00:26:08,388 --> 00:26:11,089 "Having a picnic next to sewage is no fun." 552 00:26:11,089 --> 00:26:14,490 Well, what happened? So you go all the way upstream, and you look at the beginning of 553 00:26:14,490 --> 00:26:17,690 your program. You say, "Well, at the beginning I knew I had no bugs 554 00:26:17,690 --> 00:26:20,820 because my program didn't contain any lines of code." 555 00:26:20,819 --> 00:26:24,339 And somewhere along the way as my program's running 556 00:26:24,339 --> 00:26:26,939 there was - oh, say this 557 00:26:26,940 --> 00:26:31,259 factory that was belching sewage into your water stream which showed up at the 558 00:26:31,259 --> 00:26:35,379 end when you're running your program, and this thing is a bug. 559 00:26:35,380 --> 00:26:38,250 And often times the problem is there is not just one. If there was one 560 00:26:38,250 --> 00:26:42,220 you'd be like, "Oh, I can it. That's all good." Yeah, there's like factories all down, and up the 561 00:26:42,220 --> 00:26:42,740 river. 562 00:26:42,740 --> 00:26:44,558 There's some on the other side of the river. 563 00:26:44,558 --> 00:26:46,960 There's some in the middle of the river. All right. 564 00:26:46,960 --> 00:26:47,980 It's a bad time. 565 00:26:47,980 --> 00:26:51,230 So how do you find those factories that are belching sewage? So here are some things 566 00:26:51,230 --> 00:26:52,610 to think about. 567 00:26:52,609 --> 00:26:58,019 First of which is the mindset of what causes - what tends to cause bugs. Okay. 568 00:26:58,019 --> 00:27:01,339 There's three things. I wouldn't say they cause all bugs, but they cause the vast 569 00:27:01,339 --> 00:27:02,359 majority of bugs. 570 00:27:02,359 --> 00:27:05,709 One is bad variable values. Okay. 571 00:27:05,710 --> 00:27:08,669 Some where in your program you have a value that is not what you expected, and 572 00:27:08,669 --> 00:27:12,090 as a result your program is doing something that it shouldn't do. 573 00:27:12,089 --> 00:27:15,778 What that tells you if you think about this mind frame is knowing the values of 574 00:27:15,778 --> 00:27:19,259 your variables at different points in your program is critical to understanding 575 00:27:19,259 --> 00:27:20,990 when the bad values 576 00:27:20,990 --> 00:27:23,039 entered your water supply. 577 00:27:23,039 --> 00:27:26,269 Second of which is faulty logic, right? 578 00:27:26,269 --> 00:27:31,480 You meant for the computer to do X. You programmed the computer to do Y. 579 00:27:31,480 --> 00:27:33,750 Right? X, and Y were not the same. 580 00:27:33,750 --> 00:27:37,230 There's no such thing as a computer glitch. The computer does what you tell 581 00:27:37,230 --> 00:27:40,380 it to do. And so you might say, "Hey, the computer's not doing what I told it to 582 00:27:40,380 --> 00:27:41,130 do." 583 00:27:41,130 --> 00:27:44,440 Yeah, it's doing exactly what you told it to do, you just didn't tell it to do the right 584 00:27:44,440 --> 00:27:45,490 thing. 585 00:27:45,490 --> 00:27:49,150 And last, but not least, and this is probably the biggest one is unwarranted 586 00:27:49,150 --> 00:27:52,690 assumptions. 587 00:27:52,690 --> 00:27:53,410 Right? 588 00:27:53,410 --> 00:27:56,110 "Hey, I thought the value were gonna be meters. You 589 00:27:56,109 --> 00:27:57,819 didn't tell me they were feet 590 00:27:57,819 --> 00:28:01,129 until after the mission launched, and we lost the Rover." 591 00:28:01,130 --> 00:28:04,290 Think about what you're assumptions are. And that's why when we talked a long 592 00:28:04,289 --> 00:28:08,339 time ago about pre and post conditions - right - when we talked about it in the little world of 593 00:28:08,339 --> 00:28:08,740 Carol - 594 00:28:08,740 --> 00:28:12,038 "Oh, we just didn't want them to walk into a wall." That was what assumptions were all 595 00:28:12,038 --> 00:28:15,980 about. What can your method assume when it's called, and what can you assume 596 00:28:15,980 --> 00:28:17,569 after your method's actually done, 597 00:28:17,569 --> 00:28:19,149 and do those match up? 598 00:28:19,150 --> 00:28:21,370 So the things to keep in mind - 599 00:28:21,369 --> 00:28:24,189 and these are s - part of the zen of programming 600 00:28:24,190 --> 00:28:28,899 - debugging. And then I'll show the actual doing of debugging. Okay. 601 00:28:28,898 --> 00:28:32,018 People have a tendency to look for complex problems. 602 00:28:32,019 --> 00:28:34,779 Most of the problems are simple. 603 00:28:34,779 --> 00:28:38,819 So don't fool yourself into thinking a problem always has to be complex to be a 604 00:28:38,819 --> 00:28:42,129 bug. Some times it's just a very simple piece of faulty logic 605 00:28:42,130 --> 00:28:46,220 or some value that you mistakenly added one to or added three to 606 00:28:46,220 --> 00:28:49,460 some where that you didn't mean to under some conditions. Okay. 607 00:28:49,460 --> 00:28:51,990 Be systematic. 608 00:28:51,990 --> 00:28:55,460 There's a tendency, and this tendency tends to increase as the amount of time 609 00:28:55,460 --> 00:28:58,259 left until the deadline for the assignment decreases, 610 00:28:58,259 --> 00:29:01,028 which is to just haphazardly jump around code, and be like, 611 00:29:01,028 --> 00:29:04,298 "It's got to be here some where. Is it over here?" No, that's the game I play with my one and 612 00:29:04,298 --> 00:29:06,449 a half year old son. "Is he over here? 613 00:29:06,449 --> 00:29:09,560 No. Is he over -" and he's just standing there with his hands over his eyes 614 00:29:09,559 --> 00:29:10,419 because 615 00:29:10,420 --> 00:29:14,140 he has the simple idea in mind, and I'm looking for something complex, but I'm 616 00:29:14,140 --> 00:29:17,659 not being systematic. I'm just like, "Is it over here? Is it over here?" 617 00:29:17,659 --> 00:29:19,820 And then finally at the end, right, 618 00:29:19,819 --> 00:29:23,038 had I just started at the beginning of my program, and began to trace it through, 619 00:29:23,038 --> 00:29:26,670 I would've found my problem. But I have a tendency not to do that. 620 00:29:26,670 --> 00:29:28,980 And part of the reason I have a tendency not to do that 621 00:29:28,980 --> 00:29:35,610 is that I make assumptions about where the problem is. 622 00:29:35,609 --> 00:29:36,798 Now, 623 00:29:36,798 --> 00:29:40,450 hunches are great. It's great to think about, "Oh, I think the problem should be 624 00:29:40,450 --> 00:29:43,630 here," and have some intuition, and hunch about that. But 625 00:29:43,630 --> 00:29:45,419 when you have an intuition 626 00:29:45,419 --> 00:29:47,950 versus a fact, and the two collide - 627 00:29:47,950 --> 00:29:50,470 this is in your eleven rules of debugging on your next - 628 00:29:50,470 --> 00:29:55,140 on one of your handouts that you get. The fact always wins. Right? Your hunch never beats 629 00:29:55,140 --> 00:29:57,180 out a fact. So 630 00:29:57,180 --> 00:30:00,340 if you make assumptions about the problem, and you don't see the problem in 631 00:30:00,339 --> 00:30:01,898 the place of the code 632 00:30:01,898 --> 00:30:04,079 where you thought the problem should be, 633 00:30:04,079 --> 00:30:07,148 maybe the problem's not there, and you need to reassess your assumptions about those 634 00:30:07,148 --> 00:30:10,559 pieces of code that you thought were simple, and so they were error free, 635 00:30:10,559 --> 00:30:13,269 and in fact the error could be there. Okay. 636 00:30:13,269 --> 00:30:16,250 Other thing is be critical of your code. 637 00:30:16,250 --> 00:30:19,569 Right? People have a tendency to look at some function or some method, and they think, 638 00:30:19,569 --> 00:30:22,799 "Oh, it's super simple. There can't be an error there." Or they just glance over it. 639 00:30:22,799 --> 00:30:24,730 I've actually seen at 640 00:30:24,730 --> 00:30:27,329 major software development companies, 641 00:30:27,329 --> 00:30:32,000 very large pieces of software written by brilliant, top of the line software 642 00:30:32,000 --> 00:30:32,900 engineers 643 00:30:32,900 --> 00:30:35,659 who the problem in their code 644 00:30:35,659 --> 00:30:39,100 was this instead of this. Right. 645 00:30:39,099 --> 00:30:43,349 It was harder for them to find that bug than to find issues with, like, memory, 646 00:30:43,349 --> 00:30:46,829 and garbage collection, and leakage, and all this other stuff because 647 00:30:46,829 --> 00:30:48,779 they were just used to that. Right? 648 00:30:48,779 --> 00:30:51,740 They weren't critical of things like this. And when they finally saw this error you 649 00:30:51,740 --> 00:30:55,460 could just see like the eyes just nearly pop out of their head, and their head was gonna explode. 650 00:30:55,460 --> 00:30:58,620 But that's all it was. It was something simple they just - the code - they glanced through 651 00:30:58,619 --> 00:31:01,669 that code a thousand times and couldn't find it because they just didn't look at it 652 00:31:01,670 --> 00:31:03,298 critically. Okay. 653 00:31:03,298 --> 00:31:05,750 And last but not least - and this is the important thing 654 00:31:05,750 --> 00:31:07,989 at 5:00 in the morning where you're 655 00:31:07,989 --> 00:31:10,620 programs not working. 656 00:31:10,619 --> 00:31:12,429 Remember this. 657 00:31:12,430 --> 00:31:14,120 Trust me. 658 00:31:14,119 --> 00:31:15,539 Don't panic. 659 00:31:15,539 --> 00:31:18,298 Panic is the worst thing you can do with bugs. 660 00:31:18,298 --> 00:31:21,849 And why? Because people sit there, and they think, "Oh, my God, there's this bug in 661 00:31:21,849 --> 00:31:22,250 my program, and 662 00:31:22,250 --> 00:31:26,259 it's over here. No, it's over here. No, it's over there." No. 663 00:31:26,259 --> 00:31:30,199 Your code's not changing unless you change it. If you have a bug in your code, 664 00:31:30,199 --> 00:31:31,350 there's a bug. 665 00:31:31,349 --> 00:31:32,748 It's in one place. 666 00:31:32,749 --> 00:31:34,750 It remains in one place. 667 00:31:34,750 --> 00:31:37,859 Every time you run your program it's in that same place just begging you. It's 668 00:31:37,859 --> 00:31:38,919 like, "Come on. 669 00:31:38,920 --> 00:31:41,730 Come find me. Did he find now? No. 670 00:31:41,730 --> 00:31:45,370 Come on. Come find me." It's just there. It's that same little line of code, right? How many 671 00:31:45,369 --> 00:31:48,538 total lines of code are there? Like a couple hundred at most, 672 00:31:48,538 --> 00:31:52,028 right? It's in there. It's not moving. It's up to you to find. 673 00:31:52,028 --> 00:31:53,289 Don't panic about it. 674 00:31:53,289 --> 00:31:54,788 Be systematic, 675 00:31:54,788 --> 00:31:58,489 question your assumptions, be critical of your code, and eventually you'll get there. 676 00:31:58,489 --> 00:32:01,819 But if you panic you won't because you won't be seeing it. Okay. 677 00:32:01,819 --> 00:32:04,869 So what are some approaches we can take to debugging 678 00:32:04,869 --> 00:32:08,429 now that we have some of the basic sort of philosophy down, and the fact that the 679 00:32:08,430 --> 00:32:11,000 real critical thing is don't panic? 680 00:32:11,000 --> 00:32:14,200 The simplest approach to debugging is something that's called printf debugging, and printf 681 00:32:14,200 --> 00:32:17,090 is actually something that comes from the language C. The way you can think about 682 00:32:17,089 --> 00:32:18,659 it is 683 00:32:18,660 --> 00:32:20,860 println debugging. 684 00:32:20,859 --> 00:32:24,729 And the basic idea is right - if bad values are the problem with your program, 685 00:32:24,730 --> 00:32:26,690 which in most cases they are, 686 00:32:26,690 --> 00:32:30,130 put some extra printlns in your code that tell you what the value of your 687 00:32:30,130 --> 00:32:32,470 variables are at different places in your code. 688 00:32:32,470 --> 00:32:36,039 Use printlns to let you know that you called a particular method. So at the top 689 00:32:36,039 --> 00:32:38,109 of a method just have a println that says, 690 00:32:38,109 --> 00:32:42,308 "Hey, method X just got called." And right before that method's supposed to be done, 691 00:32:42,308 --> 00:32:46,690 you add another println that says, "Hey, method X is just about to finish." And if you 692 00:32:46,690 --> 00:32:49,869 notice some problem that happens between those lines probably something's going 693 00:32:49,868 --> 00:32:51,189 on in method X. 694 00:32:51,190 --> 00:32:55,630 Another thing you can do is just write out the values of your variables. And that's probably 695 00:32:55,630 --> 00:32:57,360 the thing I would recommend the most. 696 00:32:57,359 --> 00:33:00,779 At different places in your program just stick in printlns, and see is the 697 00:33:00,779 --> 00:33:02,740 values that you expect there 698 00:33:02,740 --> 00:33:03,859 matching what you 699 00:33:03,859 --> 00:33:07,339 thought was gonna actually be. Right? And if they're not you can sort of see 700 00:33:07,339 --> 00:33:09,339 at what point did the variables 701 00:33:09,339 --> 00:33:12,500 change the values that you didn't expect, and so something bad must have 702 00:33:12,500 --> 00:33:15,460 happened between the last time you saw the values that were okay, and the time in 703 00:33:15,460 --> 00:33:17,360 which they became bad. Okay. 704 00:33:17,359 --> 00:33:20,479 The other thing - and this is a little bit more involved, but a not a big deal. It's something 705 00:33:20,480 --> 00:33:22,640 called unit testing. Right? 706 00:33:22,640 --> 00:33:25,770 People have a tendency to test their entire program at once. They write the 707 00:33:25,769 --> 00:33:27,889 program, and they run it, and something doesn't work. 708 00:33:27,890 --> 00:33:31,960 Well, why not actually test out individual units or individual methods in your 709 00:33:31,960 --> 00:33:33,100 program, right? 710 00:33:33,099 --> 00:33:36,548 Write a call to a particular method where you pass in the values for all the 711 00:33:36,548 --> 00:33:37,970 parameters with known values, 712 00:33:37,970 --> 00:33:41,500 and see if the thing that it gives you back is what you expected it to do. 713 00:33:41,500 --> 00:33:44,609 That's called unit testing because you're just testing one unit at a time. And when 714 00:33:44,609 --> 00:33:45,639 you're done 715 00:33:45,640 --> 00:33:48,749 doing the testing you can take those calls out, and run your whole program. 716 00:33:48,749 --> 00:33:51,589 But when you write some method, especially if it's a complicated method, 717 00:33:51,589 --> 00:33:54,629 just write a couple lines at the top of run that the first thing they do is 718 00:33:54,630 --> 00:33:57,389 call that method to see if it's actually doing something reasonable 719 00:33:57,388 --> 00:34:01,750 with some artificial values that you give it. Okay. So, 720 00:34:01,750 --> 00:34:05,039 one way that you can combine a lot of this stuff is if you happen to be working 721 00:34:05,039 --> 00:34:08,519 in a development environment that gives you facilities to do debugging more 722 00:34:08,519 --> 00:34:09,168 easily. 723 00:34:09,168 --> 00:34:10,349 And so I'll show you, 724 00:34:10,349 --> 00:34:12,159 if we come to the computer, 725 00:34:12,159 --> 00:34:15,909 Eclipse actually has a very nice debugging environment. Okay. 726 00:34:15,909 --> 00:34:19,789 And so rather these printlns, you can do sort of in any program, right, because even if 727 00:34:19,789 --> 00:34:22,069 you're doing a program that you don't have a nice debugging environment, you 728 00:34:22,068 --> 00:34:24,489 can still do println, and life will be good. 729 00:34:24,489 --> 00:34:26,088 So let's actually 730 00:34:26,088 --> 00:34:29,719 debug a program using the debugger in Eclipse. So here's a little Roulette 731 00:34:29,719 --> 00:34:30,278 program, 732 00:34:30,278 --> 00:34:33,579 and the way Roulette works is we're gonna have some constants to begin 733 00:34:33,579 --> 00:34:37,709 with, how much money you start with, and how much you're basically wagering or 734 00:34:37,708 --> 00:34:40,819 betting on every spin of the Roulette wheel. So the way Roulette works 735 00:34:40,820 --> 00:34:42,989 is we have a big wheel that we spin. 736 00:34:42,989 --> 00:34:46,659 And here's all the rules of Roulette down here. I'll just show you the rules. 737 00:34:46,659 --> 00:34:49,950 Ah, too much text to read. The basic idea is the wheel has a bunch of different 738 00:34:49,949 --> 00:34:53,939 numbers on it from zero to 36 all sort of consecutive numbers. 739 00:34:53,940 --> 00:34:57,150 And the way you can bet on numbers is you can either bet for an odd number to 740 00:34:57,150 --> 00:34:57,930 come up, 741 00:34:57,929 --> 00:35:01,759 an even number to come up, a high number to come up, which means a number in the 742 00:35:01,759 --> 00:35:06,400 top half from 17 to 36, or a low number to come up, which means from one to 743 00:35:06,400 --> 00:35:07,269 16. 744 00:35:07,268 --> 00:35:10,559 And you might - or if - one to 18. You might say, "But, Marron, what about zero?" 745 00:35:10,559 --> 00:35:12,230 Zero is always a loser. 746 00:35:12,230 --> 00:35:15,590 So, zero doesn't count as an even number. It doesn't count as a low. It's just 747 00:35:15,590 --> 00:35:20,849 always the losing value. And the reason why that exists is because that's the house's 748 00:35:20,849 --> 00:35:22,980 advantage. It gives them a slight advantage of 749 00:35:22,980 --> 00:35:26,380 about three percent over the player, and that's how in the long term they make money because zero is always a winner for the 750 00:35:26,380 --> 00:35:28,719 casino, 751 00:35:28,719 --> 00:35:32,088 never a winner for the lo - the player, other than that, everything's even money. If 752 00:35:32,088 --> 00:35:36,239 I bet $10.00 on low, and a five comes up, which is in the low half of the range, 753 00:35:36,239 --> 00:35:40,699 then I win $5.00. Okay. So 754 00:35:40,699 --> 00:35:43,449 that's what the instructions basically say. Just a bunch of printlns they 755 00:35:43,449 --> 00:35:45,369 write out in instructions. Okay. 756 00:35:45,369 --> 00:35:48,579 We're gonna have a random number generator, rgen. That's an instance 757 00:35:48,579 --> 00:35:51,289 variable that's just generates random numbers, and it gets an instance 758 00:35:51,289 --> 00:35:53,749 of the random number generator. Hopefully after 759 00:35:53,748 --> 00:35:57,288 Breakout, and Hangman, this should be fairly comfortable 760 00:35:57,289 --> 00:35:58,160 to you. 761 00:35:58,159 --> 00:36:00,558 And now we kind of come to the rest of the code. 762 00:36:00,559 --> 00:36:02,750 Okay. First thing I'm gonna do is set the font to be big. 763 00:36:02,750 --> 00:36:06,139 I'm gonna write out the instructions, and then I'm gonna play Roulette. 764 00:36:06,139 --> 00:36:09,828 And so I say, "Hey, in playing Roulette I have some starting amount of money that the user 765 00:36:09,829 --> 00:36:10,430 gets. 766 00:36:10,429 --> 00:36:13,629 As long as their money is greater zero, I tell the user you have some amount 767 00:36:13,630 --> 00:36:17,750 of money. I ask them which betting category they want to bet." That means 768 00:36:17,750 --> 00:36:22,338 either even or odd or high or low. So they type in a string. There's four betting categories. 769 00:36:22,338 --> 00:36:26,179 I spin the Roulette wheel to get a value between zero and 36, 770 00:36:26,179 --> 00:36:30,129 and then if the winning - if there's a winning category based on the 771 00:36:30,130 --> 00:36:32,710 outcome that they have, I give them - 772 00:36:32,710 --> 00:36:36,670 I basically say, "You won this amount," and I increase their amount. 773 00:36:36,670 --> 00:36:42,259 If they didn't get a win in that winning category, then I say, "The number is 774 00:36:42,259 --> 00:36:43,628 not -" basically, 775 00:36:43,628 --> 00:36:46,318 "That number is not whatever you bet. So 776 00:36:46,318 --> 00:36:47,289 you're gonna lose." 777 00:36:47,289 --> 00:36:50,159 And I subtract the amount of wager from the total money. And if they happen to 778 00:36:50,159 --> 00:36:51,309 get out of this loop 779 00:36:51,309 --> 00:36:55,998 then their money is down to zero, and I say, "You ran out of money," and we stop. Okay. 780 00:36:55,998 --> 00:36:58,478 So I might just run this program. Do 781 00:36:58,478 --> 00:36:59,509 te do te do - 782 00:36:59,509 --> 00:37:02,469 and see what's gonna happen. Notice I didn't show you all the other code yet? Because I 783 00:37:02,469 --> 00:37:05,608 want to show you the facilities of the debugger rather than just having you stare at code for a 784 00:37:05,608 --> 00:37:06,489 bunch of time, 785 00:37:06,489 --> 00:37:09,978 and try to debug it because that's not the way you want to debug by just staring at code. You 786 00:37:09,978 --> 00:37:11,629 want to look at what the symptoms are. 787 00:37:11,630 --> 00:37:13,999 So here we're gonna run Roulette. 788 00:37:13,998 --> 00:37:17,698 And it writes out all the instructions, and it says, "Enter betting category." 789 00:37:17,699 --> 00:37:20,829 And so we'll say, "Hey, I'm gonna bet even." 790 00:37:20,829 --> 00:37:24,359 "Ball lands on 33. That number's not even so you lose." I'm like, "Okay. I'm gonna go 791 00:37:24,358 --> 00:37:28,848 for even again." Good old even. Even always wins. "Number 35. You lose." 792 00:37:28,849 --> 00:37:30,940 "I'm going for even again." 793 00:37:30,940 --> 00:37:33,378 "27, you lose." "Even come on." 794 00:37:33,378 --> 00:37:34,588 795 00:37:34,588 --> 00:37:36,828 796 00:37:36,829 --> 00:37:39,548 "Nine - 31 - 15 -" 797 00:37:39,548 --> 00:37:42,918 and then you're like, "I had so much faith. I lost all this money, and then ball 798 00:37:42,918 --> 00:37:48,879 landed in two, and I still lost money." 799 00:37:48,880 --> 00:37:52,269 And you just kind of sit there, and you're like, "Yeah, this casino's not really 800 00:37:52,268 --> 00:37:53,889 what I had in mind." And 801 00:37:53,889 --> 00:37:57,219 you try to leave before the bouncers get involved. But you realize 802 00:37:57,219 --> 00:38:00,419 you actually have a bug in your program, right? You bet on even. The ball landed 803 00:38:00,420 --> 00:38:02,509 in two. That should be an even number. 804 00:38:02,509 --> 00:38:03,778 And you still lost money. 805 00:38:03,778 --> 00:38:07,659 So then you think, "Okay. Something's wrong. What's wrong? Well, 806 00:38:07,659 --> 00:38:11,920 let me figure out right - this winning category thing, I should've said, 'If the 807 00:38:11,920 --> 00:38:13,760 outcome moded by two 808 00:38:13,760 --> 00:38:17,900 or remaindered by two was equal to one that should be odd. And if the 809 00:38:17,900 --> 00:38:20,519 remainder after I divided by two is zero 810 00:38:20,518 --> 00:38:22,638 that should be even.' So 811 00:38:22,639 --> 00:38:25,489 I would've thought this would've been the case. What's going on here?" And 812 00:38:25,489 --> 00:38:29,039 so what I can do in Eclipse is I can set up what's called a breakpoint. 813 00:38:29,039 --> 00:38:32,609 The way you set a breakpoint is that a particular line over here on the gray bar you 814 00:38:32,608 --> 00:38:35,929 click twice. And if you click twice, notice I got that little circle there. Can 815 00:38:35,929 --> 00:38:38,509 you see the circle on the very edge of the screen? 816 00:38:38,509 --> 00:38:39,869 That's called a breakpoint. 817 00:38:39,869 --> 00:38:43,949 That means if I run the program now with the debugger my program will stop at that 818 00:38:43,949 --> 00:38:47,429 point, and allow me to look at, like, values of variables, and other kinds of 819 00:38:47,429 --> 00:38:52,059 things. Okay. So I set that breakpoint by double clicking, and now I need to run again. 820 00:38:52,059 --> 00:38:54,890 So the way I run again is see this little bug up here. 821 00:38:54,889 --> 00:38:57,699 That means run with the debugger. See up here? It looks like a little 822 00:38:57,699 --> 00:38:58,889 tiny bug. 823 00:38:58,889 --> 00:39:03,278 So I click that to run with the debugger, and now it starts rerunning my program 824 00:39:03,278 --> 00:39:03,809 again. 825 00:39:03,809 --> 00:39:07,269 And it says, "Enter a bet." And so I say, "Even." 826 00:39:07,268 --> 00:39:08,328 And it says, 827 00:39:08,329 --> 00:39:11,119 "This kind of launch is configurative. Open the Stanford debugger perspective when it 828 00:39:11,119 --> 00:39:14,778 suspends. Should I open this perspective now?" All right. It looks all big, and nasty, and you just say, 829 00:39:14,778 --> 00:39:16,548 "Yes." Okay. 830 00:39:16,548 --> 00:39:19,688 And it says, "Hey, your program got here. 831 00:39:19,688 --> 00:39:22,690 Look at what values you actually have." 832 00:39:22,690 --> 00:39:26,729 And so there's a couple things to look at. Up in this debug window up here, it 833 00:39:26,728 --> 00:39:30,268 shows me - remember when we talked about stack frames. This is what's called the 834 00:39:30,268 --> 00:39:31,228 call stack. 835 00:39:31,228 --> 00:39:34,578 The run method called the play Roulette method which called the is winning 836 00:39:34,579 --> 00:39:38,519 category method which is where I'm currently suspended. So it tells you all 837 00:39:38,518 --> 00:39:41,808 the functions that are currently - or the methods that are currently active to get you at 838 00:39:41,809 --> 00:39:43,409 your current point. 839 00:39:43,409 --> 00:39:47,039 Over here in the variables window, it actually tells you the value of the 840 00:39:47,039 --> 00:39:47,880 variables. 841 00:39:47,880 --> 00:39:51,778 So my bet is even, outcome 13. I say, "Huh? 842 00:39:51,778 --> 00:39:53,728 It says the ball lands in ten. 843 00:39:53,728 --> 00:39:57,188 I thought that was supposed to be even, and I was gonna even. What's going on? 844 00:39:57,188 --> 00:40:01,328 Over here it tells me that my outcome, which is the thing that I'm gonna mod or 845 00:40:01,329 --> 00:40:05,599 divide by two to see if it's odd or even is 13. That's not what I got, 846 00:40:05,599 --> 00:40:07,170 which means at this point 847 00:40:07,170 --> 00:40:08,709 sewage has entered the system. 848 00:40:08,708 --> 00:40:11,688 The value that I thought I should have is not the value that my program 849 00:40:11,688 --> 00:40:13,288 actually has." Okay. 850 00:40:13,289 --> 00:40:15,799 So what am I gonna do? There's two things I'm gonna do. First of all I want 851 00:40:15,798 --> 00:40:19,389 to say, "Oh, I'm in this debugging perspective. How do I get back to the little editor 852 00:40:19,389 --> 00:40:20,378 that's my friend?" 853 00:40:20,378 --> 00:40:23,278 You come to the Stanford 854 00:40:23,278 --> 00:40:25,699 menu, and you pick switch to editor. Okay. 855 00:40:25,699 --> 00:40:28,578 That takes you back to what you're used to. Notice we're still stopped here, but 856 00:40:28,579 --> 00:40:30,599 you're back just in this editor view. 857 00:40:30,599 --> 00:40:32,829 There's a couple things we're gonna do. 858 00:40:32,829 --> 00:40:35,739 First thing we're gonna do is I'm just gonna say, "Whoa. Stop the program. 859 00:40:35,739 --> 00:40:37,519 Something bad is going on." 860 00:40:37,518 --> 00:40:41,348 Next thing I'm gonna do is say, "Hey, my program involves random numbers. So I'm 861 00:40:41,349 --> 00:40:45,380 gonna set the seed for my random number generator, so I can count on 862 00:40:45,380 --> 00:40:46,660 always getting the values 863 00:40:46,659 --> 00:40:50,829 the same every time." So if you're debugging with something that involves random numbers, set 864 00:40:50,829 --> 00:40:53,719 the seed before you go into debugging, and that'll guarantee you always get the 865 00:40:53,719 --> 00:40:56,168 same numbers. So I set the seed. 866 00:40:56,168 --> 00:40:59,088 The other thing I'm gonna do is say, "Hey, the number that got printed on the screen was 867 00:40:59,088 --> 00:41:01,528 different than the number that I actually got. 868 00:41:01,528 --> 00:41:04,748 So let me look at the code that prints a number on the screen." So here's spin 869 00:41:04,748 --> 00:41:08,068 Roulette wheel. The ball lands then it generates a random number, and then it 870 00:41:08,068 --> 00:41:09,829 returns a random number. 871 00:41:09,829 --> 00:41:15,669 What's the problem here? 872 00:41:15,668 --> 00:41:18,150 Anyone want to venture a guess? [Inaudible]. 873 00:41:18,150 --> 00:41:21,559 They're not the same random number, right? It generates one random number to say, 874 00:41:21,559 --> 00:41:25,150 "Hey, the ball lands in some number." That was kind of a social. 875 00:41:25,150 --> 00:41:29,099 And then it says, "Hey, you know what? Yeah, our casino - we just remove that ball, 876 00:41:29,099 --> 00:41:33,739 and we just pick another number randomly to say that's what it really was." Okay. That's - 877 00:41:33,739 --> 00:41:37,298 maybe a lot of fun for the casino, not so much fun for you. So what we really 878 00:41:37,298 --> 00:41:40,219 need to do is inside here we'll have something like spin, 879 00:41:40,219 --> 00:41:45,028 and spin is gonna be whatever we get from the random number generator. Okay. 880 00:41:45,028 --> 00:41:47,309 So we'll just say, "Hey, spin is this random number." 881 00:41:47,309 --> 00:41:49,050 And then what we're gonna write 882 00:41:49,050 --> 00:41:52,200 is spin over here. And what we're gonna return 883 00:41:52,199 --> 00:41:56,108 is also spin. So we only generate one random number. 884 00:41:56,108 --> 00:41:59,608 Let's save that, and we're like, "Oh, we only generated one random number. That must be the bug. 885 00:41:59,608 --> 00:42:05,088 I must be done. Oh, yeah. Yeah, good times. Good times." So I run my program again. Do 886 00:42:05,088 --> 00:42:09,128 te do - and I get this whole thing, and it says, "Enter betting category." "Even." "27, 887 00:42:09,128 --> 00:42:12,378 you lose." I'm like, "Oh, it must be working." "Even." "15." I'm like, "Oh, yeah, 888 00:42:12,378 --> 00:42:16,798 my program is so good. I'm such good -" yeah. 889 00:42:16,798 --> 00:42:18,829 And you think you've found your bug, 890 00:42:18,829 --> 00:42:22,259 and then you realize when you're the vandal, and you're trying to see if that 891 00:42:22,260 --> 00:42:23,839 error condition still exists, 892 00:42:23,838 --> 00:42:26,199 "Hey, we found one bug. That was great. 893 00:42:26,199 --> 00:42:28,129 That didn't solve this problem." 894 00:42:28,130 --> 00:42:31,519 So you say, "Okay. Back to the drawing board. Let me quit out of here. I'm gonna 895 00:42:31,518 --> 00:42:34,508 go, and set another breakpoint over here. Okay. 896 00:42:34,509 --> 00:42:37,228 So let me clear this breakpoint. I'm gonna set another breakpoint. I'm gonna run 897 00:42:37,228 --> 00:42:38,418 it with the debugger. Okay 898 00:42:38,418 --> 00:42:40,179 - which is what I didn't do before." 899 00:42:40,179 --> 00:42:41,978 So now I run with the debugger, 900 00:42:41,978 --> 00:42:44,768 and it says, "Enter a bet." And I say, "Even." 901 00:42:44,768 --> 00:42:47,878 And it says, "You've stopped here." And so, "Yes. I want to do this debugger 902 00:42:47,878 --> 00:42:48,509 perspective." 903 00:42:48,509 --> 00:42:51,228 And I say, "Hey, the outcome is 27, 904 00:42:51,228 --> 00:42:53,118 and so my - 905 00:42:53,119 --> 00:42:57,690 if I look at - my bet is not even, right? It should be odd." So I say, "Oh, that's great. 906 00:42:57,690 --> 00:43:00,599 Well, it shouldn't be odd, so that shouldn't match." There are some tools up here I can use to 907 00:43:00,599 --> 00:43:02,519 say, "Step through my program." 908 00:43:02,518 --> 00:43:05,128 There's one in the middle up here if you can look at this line that has sort of 909 00:43:05,128 --> 00:43:07,558 this curve to it. That means step over. 910 00:43:07,559 --> 00:43:10,929 And all of these - all these different icons are actually explained in your debugging 911 00:43:10,929 --> 00:43:13,300 handout, but step over is the critical one. It says, 912 00:43:13,300 --> 00:43:16,619 "Just execute this line of code, and go down to the next line of code." You have two 913 00:43:16,619 --> 00:43:20,700 other options. You have step into, which actually makes a function call or 914 00:43:20,699 --> 00:43:22,458 method call if you happen to be in a method, 915 00:43:22,458 --> 00:43:26,038 and step return, which says, "Step out of the current method I'm in." They get a 916 00:43:26,039 --> 00:43:28,220 little funky. So step over is what we're gonna do. 917 00:43:28,219 --> 00:43:29,778 So we step over, 918 00:43:29,778 --> 00:43:33,010 and odd doesn't execute, and you're like, "Yeah, odd shouldn't have executed. Even - yeah, 919 00:43:33,010 --> 00:43:37,380 my bet is even, right? I know over here my bet has the value even, so I execute 920 00:43:37,380 --> 00:43:39,229 another line. 921 00:43:39,228 --> 00:43:40,268 Whoa. 922 00:43:40,268 --> 00:43:43,258 What's going on there? I should've gotten bet was equal to even, and 923 00:43:43,259 --> 00:43:48,199 return the fact that my outcome was actually true that I actually won." 924 00:43:48,199 --> 00:43:55,199 What's the problem here? [Inaudible]. [Inaudible]. Pardon? [Inaudible]. 925 00:43:55,798 --> 00:43:58,398 Ah, strings. These 926 00:43:58,398 --> 00:44:01,989 are strings. I can't use equal equal on strings, right? 927 00:44:01,989 --> 00:44:05,618 It just doesn't work. 928 00:44:05,619 --> 00:44:08,579 It doesn't work when you're not looking either. So 929 00:44:08,579 --> 00:44:12,249 I say, "Hey, come back to the editor, and when you want to make some changes - yeah, 930 00:44:12,248 --> 00:44:15,449 instead of checking for equal equals, I actually need to say dot equals 931 00:44:15,449 --> 00:44:16,048 here." 932 00:44:16,048 --> 00:44:19,268 As a matter of fact I'm gonna use equals ignore case because that'll make 933 00:44:19,268 --> 00:44:23,608 the life of the user even better. They can either put in odd or even in 934 00:44:23,608 --> 00:44:26,748 upper case or lower case. So I'm just gonna do a little copy, and paste, which is 935 00:44:26,748 --> 00:44:33,528 always a dangerous thing to do. Do te do - 936 00:44:33,528 --> 00:44:37,478 so close. Do 937 00:44:37,478 --> 00:44:41,038 do - and then for high - 938 00:44:41,039 --> 00:44:46,149 and at this point I could go ahead, and save. 939 00:44:46,148 --> 00:44:48,889 Oh, it's still in this perspective. I want to 940 00:44:48,889 --> 00:44:51,038 quit out of my program. 941 00:44:51,039 --> 00:44:53,769 And now there's one final thing I'm gonna do here. Let me switch to the 942 00:44:53,768 --> 00:44:57,088 editor, and I'm gonna run one - let me save, and run one last time. 943 00:44:57,088 --> 00:45:01,328 And this becomes very subtle. So if I say, "Hey, I'm gonna bet on odd. Yeah, look I'm 944 00:45:01,329 --> 00:45:04,278 winning now because I know that odd numbers come up. I should've remembered this from 945 00:45:04,278 --> 00:45:06,039 last time because I set the seed, right?" 946 00:45:06,039 --> 00:45:08,498 And I keep betting on odd. 947 00:45:08,498 --> 00:45:12,208 And then one interesting thing that happens is zero came up, right. This is 948 00:45:12,208 --> 00:45:15,348 something that may take a while to come up. So I start betting on even 949 00:45:15,349 --> 00:45:18,599 because I lost with zero before. It's gonna take me a while before zero might 950 00:45:18,599 --> 00:45:21,890 ever come up again, but I need to be - I need to persevere. 951 00:45:21,889 --> 00:45:24,328 And it's probably not gonna come up in this run. 952 00:45:24,329 --> 00:45:28,880 So let me quit this, and run it again. Do te do - 953 00:45:28,880 --> 00:45:33,660 even. And I should get the same sequence of numbers. 954 00:45:33,659 --> 00:45:35,078 Even, 955 00:45:35,079 --> 00:45:36,278 even, 956 00:45:36,278 --> 00:45:37,139 even - oh, 957 00:45:37,139 --> 00:45:41,729 I lost on zero. Right? There's one subtle bug that's still remains in my code 958 00:45:41,728 --> 00:45:42,399 which is 959 00:45:42,400 --> 00:45:46,450 for the case for even I not only need to check for even, I also need to make sure 960 00:45:46,449 --> 00:45:50,469 that the value is not equal to zero. So the other final thing I need to put in 961 00:45:50,469 --> 00:45:53,898 here is and and 962 00:45:53,898 --> 00:45:57,199 outcome 963 00:45:57,199 --> 00:45:58,369 outcome 964 00:45:58,369 --> 00:46:01,358 not equal to zero. Okay. 965 00:46:01,358 --> 00:46:04,768 And then my code is actually fixed. So sometimes it may take me a while to find 966 00:46:04,768 --> 00:46:08,188 the actual error in my code through running, but now you can use the 967 00:46:08,188 --> 00:46:11,489 debugger to find it. All right. And if there's any questions I'll take them after class, 968 00:46:11,489 --> 00:46:14,869 and you can pick up your finals - Thank you. - over from - or the - Thank you. - midterm exams 969 00:46:14,869 --> 00:46:15,519 over from Ben.