1 00:00:12,708 --> 00:00:15,978 This presentation is delivered by the Stanford Center for Professional 2 00:00:15,978 --> 00:00:22,978 Development. 3 00:00:25,439 --> 00:00:27,230 Alrighty, welcome back. Wow, that's 4 00:00:27,230 --> 00:00:28,579 pretty loud. 5 00:00:28,579 --> 00:00:33,609 Welcome back to CS106a. I hope I didn't just shatter your eardrums. 6 00:00:33,609 --> 00:00:36,549 And thanks for making it out in the rain. I think the rain might have stopped a few 7 00:00:36,549 --> 00:00:39,829 people from making it today. But, actually, today is one of the most important lectures 8 00:00:39,829 --> 00:00:41,259 of the whole quarter, 9 00:00:41,259 --> 00:00:44,509 so it's too bad that it would happen to happen that way. 10 00:00:44,509 --> 00:00:48,839 So a couple of quick announcements: One is that there's two handouts, one on coding 11 00:00:48,840 --> 00:00:52,110 style, and one on the use of variables. I'd encourage you to read both of them because 12 00:00:52,109 --> 00:00:55,819 they are both extremely critical concepts in this class. There are some things that - it's like, 13 00:00:55,819 --> 00:00:59,709 "Yeah, it's not so important." These two are really important so please make sure to read 14 00:00:59,709 --> 00:01:01,430 the handout. 15 00:01:01,429 --> 00:01:05,030 There is a couple of new concepts I want to show you briefly in the beginning, and 16 00:01:05,030 --> 00:01:08,239 then we're gonna begin to put a whole bunch of things together. 17 00:01:08,239 --> 00:01:11,459 So so far in class, what we've done is, when you saw Caroline we did a bunch of 18 00:01:11,459 --> 00:01:15,449 stuff with Java so far, we've shown you a bunch of bits and pieces of 19 00:01:15,450 --> 00:01:16,170 things and 20 00:01:16,170 --> 00:01:19,290 put some larger pieces together. And today's the day where it really all comes 21 00:01:19,290 --> 00:01:20,200 together, 22 00:01:20,200 --> 00:01:23,899 and a whole bunch of stuff that we've talked about before, where I said, "Oh, later 23 00:01:23,899 --> 00:01:25,439 on this'll make more sense," 24 00:01:25,438 --> 00:01:27,989 today's the day when hopefully that will all make sense. 25 00:01:27,989 --> 00:01:30,989 So first thing to cover, just very briefly, 26 00:01:30,989 --> 00:01:32,920 is something called, "Strings." 27 00:01:32,920 --> 00:01:35,680 And if you've been reading along in the book you've seen some references to 28 00:01:35,680 --> 00:01:38,729 Strings. We haven't talked about them in class, and now it's time that we spent a little bit of 29 00:01:38,728 --> 00:01:42,069 time talking about Strings. In about another week and a half we'll spend a whole bunch of 30 00:01:42,069 --> 00:01:43,239 time talking about Strings, 31 00:01:43,239 --> 00:01:45,619 but you should at least get a little introduction to them. 32 00:01:45,620 --> 00:01:47,040 So what is a String? 33 00:01:47,040 --> 00:01:50,880 A String is just a type, like we had ints and we had doubles, and 34 00:01:50,879 --> 00:01:54,539 those boolean, stuff like that. String, except String starts with a 35 00:01:54,540 --> 00:01:58,220 capital "S," it's not lower case, is actually a type, and so we can declare 36 00:01:58,219 --> 00:02:01,549 variables of this type; like, we can have some variable called "str," which is a 37 00:02:01,549 --> 00:02:02,539 String. 38 00:02:02,540 --> 00:02:05,630 And what does a String hold? What a String really sort of means is a string 39 00:02:05,629 --> 00:02:09,348 of characters, it's a piece of text is all a String is. 40 00:02:09,348 --> 00:02:12,938 And the way we usually denote a String is its inside double-quotes, or 41 00:02:12,938 --> 00:02:13,909 the 42 00:02:13,909 --> 00:02:17,789 " " that we're used to. So we might have some String, and initialize it 43 00:02:17,789 --> 00:02:22,579 by setting it equal to, "Hello, space, there." So it's perfectly fine to have 44 00:02:22,580 --> 00:02:25,999 spaces and other kinds of characters inside of a String. 45 00:02:25,998 --> 00:02:29,259 And, basically, it's just a piece of text that says, "Hello there." It's just one 46 00:02:29,259 --> 00:02:31,899 variable that happens to have this value, "Hello there." 47 00:02:31,899 --> 00:02:35,479 And so the important thing is it's text enclosed in double quotes. 48 00:02:35,479 --> 00:02:38,908 And you can think of the text that you actually assign to the String. You can use 49 00:02:38,908 --> 00:02:41,938 things on it like concatenation, just like you did in a print lin, when you 50 00:02:41,938 --> 00:02:45,968 actually want to print some stuff on the screen, we use the plus operation to 51 00:02:45,968 --> 00:02:47,408 concatenate pieces of text together. 52 00:02:47,408 --> 00:02:50,519 Well, we can do that with Strings. So we could have some String, 53 00:02:50,520 --> 00:02:56,260 we'll call this String, "name." And we might set name = "Bob." 54 00:02:56,259 --> 00:02:57,079 55 00:02:57,080 --> 00:02:59,700 And we might have some integer age, 56 00:02:59,699 --> 00:03:03,848 which is just set to "20." And then we might have some other String, 57 00:03:03,848 --> 00:03:10,328 so some other String s, which we want to set to be something like "name:+," 58 00:03:10,329 --> 00:03:12,310 59 00:03:12,310 --> 00:03:16,019 and then whatever the name actually is in this other String, so name. 60 00:03:16,019 --> 00:03:19,129 What that'll do is concatenate the word Bob 61 00:03:19,129 --> 00:03:24,310 onto name:. And then maybe we want to concatenate onto that something like 62 00:03:24,310 --> 00:03:25,259 age, 63 00:03:25,259 --> 00:03:29,998 and then concatenate onto that the value of the variable age. And so what we'll get here is we'll 64 00:03:29,998 --> 00:03:31,978 get something that in the end 65 00:03:31,979 --> 00:03:36,159 s is just some variable, so it's just some box that contains something. And what it 66 00:03:36,158 --> 00:03:38,378 will contain is 67 00:03:38,378 --> 00:03:40,120 "name:" 68 00:03:40,120 --> 00:03:45,169 then concatenated on with whatever name evaluated to. Well, name:, when we look it up 69 00:03:45,169 --> 00:03:48,219 in the box is just Bob, so we'll say, "name: Bob." 70 00:03:48,218 --> 00:03:49,229 And then 71 00:03:49,229 --> 00:03:53,528 concatenate onto space "age:," 72 00:03:53,528 --> 00:03:55,938 73 00:03:55,938 --> 00:03:59,328 and then the value of whatever age was, which is "20." Just like 74 00:03:59,329 --> 00:04:02,379 this would work in a print lin, if you were to say print line, 75 00:04:02,378 --> 00:04:05,599 and have parens around this whole thing, you would expect it to print this out. 76 00:04:05,599 --> 00:04:09,008 You could actually just assign that to a String. 77 00:04:09,008 --> 00:04:12,378 So same sort of concatenation works. And then you might wonder, how 78 00:04:12,378 --> 00:04:15,209 do you assign these Strings or get these Strings? 79 00:04:15,209 --> 00:04:18,509 You can assign what we refer to as "literal," which is an actual value to 80 00:04:18,509 --> 00:04:19,220 the String. 81 00:04:19,220 --> 00:04:21,750 You can build up a String by concatenating a bunch of other stuff 82 00:04:21,750 --> 00:04:22,689 together. 83 00:04:22,689 --> 00:04:25,389 Sometimes you want to read in some text from the user, and there's a little 84 00:04:25,389 --> 00:04:29,970 method called "readLine," just like read int and read double, 85 00:04:29,970 --> 00:04:33,110 where you give it some piece of text but it's gonna display 86 00:04:33,110 --> 00:04:36,610 a question mark because it's gonna ask for something. And what it gives you 87 00:04:36,610 --> 00:04:40,840 back is basically the whole line that the user typed in. So it's not just 88 00:04:40,839 --> 00:04:42,109 looking for an int or a double, 89 00:04:42,110 --> 00:04:44,770 but if they type in a whole line of text, including characters, 90 00:04:44,769 --> 00:04:48,649 it gives you that back as a String, that's what it returns, and you can 91 00:04:48,649 --> 00:04:53,579 assign it somewhere. Like some variable we might call "line," that's a type String. 92 00:04:53,579 --> 00:04:57,868 So you should just, at this level, see what a String is, understand it, be able to get 93 00:04:57,869 --> 00:05:01,169 them from users, in about a week and a half we'll do a whole bunch of funky things 94 00:05:01,168 --> 00:05:02,088 with Strings and 95 00:05:02,088 --> 00:05:04,819 get into the nitty-gritty. But at this point, this is just to give you a little bit 96 00:05:04,819 --> 00:05:07,468 of an idea as to what they are because we're gonna actually use them later on 97 00:05:07,468 --> 00:05:08,240 today. 98 00:05:08,240 --> 00:05:10,629 And there are some references in the book that hopefully will make a little bit 99 00:05:10,629 --> 00:05:13,540 more sense, but up until now, we didn't actually need them. So we just kind of 100 00:05:13,540 --> 00:05:15,050 deferred the discussion. Is 101 00:05:15,050 --> 00:05:19,579 there any questions about String? All right. So 102 00:05:19,579 --> 00:05:22,608 the next big thing, here is the real big topic that we're gonna cover 103 00:05:22,608 --> 00:05:23,120 today, 104 00:05:23,120 --> 00:05:25,119 is writing our own "classes." 105 00:05:25,119 --> 00:05:28,259 So this whole time, when we've been writing classes, so we would write something 106 00:05:28,259 --> 00:05:32,218 like, "My Program," and it would extend Console Program, and we'd have all of our 107 00:05:32,218 --> 00:05:35,618 methods and stuff inside of that class for My Program. 108 00:05:35,619 --> 00:05:38,529 Wouldn't it be kind of interesting to actually have multiple classes. Because in 109 00:05:38,528 --> 00:05:41,548 the days of yore, we talked about a Java program as really just comprised of 110 00:05:41,548 --> 00:05:42,818 multiple classes. 111 00:05:42,819 --> 00:05:46,139 So today's the day where you learn how to write other classes other than just 112 00:05:46,139 --> 00:05:48,408 the main program that you're actually writing. 113 00:05:48,408 --> 00:05:51,528 So what's the general form for actually writing a class. 114 00:05:51,528 --> 00:05:56,028 The idea is we say "public." For right now all of our classes are gonna be public. 115 00:05:56,028 --> 00:05:59,608 The word class, and then we give the name of the class. So this should be 116 00:05:59,608 --> 00:06:03,668 familiar to you, just like when we wrote programs that extended Console Program, 117 00:06:03,668 --> 00:06:07,649 we had some name here, I'll put an underline to indicate that that's not actually a literal 118 00:06:07,649 --> 00:06:09,488 name you just give it a name, 119 00:06:09,488 --> 00:06:13,039 and then potentially you could also add onto this, 120 00:06:13,040 --> 00:06:14,239 "extends," 121 00:06:14,238 --> 00:06:16,498 and then some super class. 122 00:06:16,499 --> 00:06:20,239 And so what you're saying is that you're creating a new class with this name 123 00:06:20,238 --> 00:06:24,968 that potentially extends some other class. And then inside you have some 124 00:06:24,968 --> 00:06:26,569 open brace, 125 00:06:26,569 --> 00:06:29,770 you have the body inside here, so this could be all your methods. It could also 126 00:06:29,769 --> 00:06:33,878 be variables that are defined, as we talked about before, various instance variables or 127 00:06:33,879 --> 00:06:34,860 ivar's in the class, 128 00:06:34,860 --> 00:06:37,679 and then the close of the class. Now, 129 00:06:37,678 --> 00:06:41,459 this part over here, I'll put sort of in dashed parens because it's optional. You 130 00:06:41,459 --> 00:06:44,239 actually don't have to extend something as part of a class. 131 00:06:44,240 --> 00:06:46,840 If you don't extend something as part of a class, 132 00:06:46,839 --> 00:06:51,528 what Java sort of says is the default is everything in my universe, 133 00:06:51,528 --> 00:06:52,829 at least for the time being, 134 00:06:52,829 --> 00:06:54,178 are classes. 135 00:06:54,178 --> 00:06:57,259 So if you don't tell me that you're extending some existing class, 136 00:06:57,259 --> 00:07:00,039 there are some default class called "object," 137 00:07:00,040 --> 00:07:03,830 which by default you will extend if you don't say extend some other existing 138 00:07:03,829 --> 00:07:06,889 class. So everything at the end of the day in Java's world 139 00:07:06,889 --> 00:07:10,069 sort of moves up the chain, or the hierarchy that we talked about a couple 140 00:07:10,069 --> 00:07:13,370 weeks ago, and ends up an object. So everything at the end of the day really 141 00:07:13,370 --> 00:07:16,860 is an object, and if you're not extending something in particular then you're just 142 00:07:16,860 --> 00:07:18,939 directly an object. 143 00:07:18,939 --> 00:07:22,978 So if you want to write this class, what you end up doing is you want to create a new 144 00:07:22,978 --> 00:07:25,699 file. And I'll show you how to do that in Eclipse in just a second. 145 00:07:25,699 --> 00:07:27,308 But, basically, 146 00:07:27,309 --> 00:07:30,789 that's why I put it in the box here, the name of the file should be 147 00:07:30,788 --> 00:07:35,038 - we kind of define it to be whatever the class name was 148 00:07:35,038 --> 00:07:39,399 and then a .java at the end. And the .java let's us know that this is a Java program. 149 00:07:39,399 --> 00:07:41,540 So if this thing over here, 150 00:07:41,540 --> 00:07:42,710 this class, 151 00:07:42,709 --> 00:07:44,928 happens to be class 152 00:07:44,928 --> 00:07:45,728 Bob, 153 00:07:45,728 --> 00:07:49,818 we would have some file Bob.java 154 00:07:49,819 --> 00:07:53,829 that would be where all the code that we write for the class Bob actually lives. 155 00:07:53,829 --> 00:07:57,459 And the stuff that's gonna be inside here is all the stuff like 156 00:07:57,459 --> 00:07:59,438 the methods, 157 00:07:59,439 --> 00:08:03,860 and variables, and constants, which are just a particular form of variable, they're just 158 00:08:03,860 --> 00:08:07,249 a final variable so they get no other value. 159 00:08:07,249 --> 00:08:10,319 And the stuff that you have inside here, the methods and the variables, 160 00:08:10,319 --> 00:08:13,400 have certain visibility associated with them, as we talked about before. 161 00:08:13,399 --> 00:08:18,888 These things can either be public or they can be private. 162 00:08:18,889 --> 00:08:21,750 And the difference between those two, hopefully it'll make a little bit more sense, 163 00:08:21,750 --> 00:08:22,649 is 164 00:08:22,649 --> 00:08:24,649 methods or variables that are public 165 00:08:24,649 --> 00:08:28,299 are what we refer to as being "exported out of the class." 166 00:08:28,300 --> 00:08:31,538 So if you write a whole bunch of classes, let's say we write three 167 00:08:31,538 --> 00:08:33,338 classes. And so I have my class Bob, 168 00:08:33,339 --> 00:08:35,770 and Bob might have in it some public method. 169 00:08:35,769 --> 00:08:38,408 Well, if I have some other class Mary, 170 00:08:38,408 --> 00:08:40,069 Mary can actually call 171 00:08:40,070 --> 00:08:42,980 the public methods of Bob. 172 00:08:42,980 --> 00:08:45,389 So if it's public anyone can access them. 173 00:08:45,389 --> 00:08:49,698 If they're private, they can only be accessed by other methods 174 00:08:49,698 --> 00:08:50,809 inside of the class. 175 00:08:50,809 --> 00:08:55,679 So if I have some method inside Bob, or some variable inside Bob that's private, 176 00:08:55,679 --> 00:08:59,459 the only things that can refer to it are other methods inside Bob. 177 00:08:59,460 --> 00:09:03,090 If I have some other class Mary, the methods in Mary cannot refer to the 178 00:09:03,090 --> 00:09:06,030 private members, or the private elements of Bob. 179 00:09:06,029 --> 00:09:07,860 So that's the important thing to keep in mind. 180 00:09:07,860 --> 00:09:11,039 There's a couple other notions. There's a notion known as "protected." And we'll talk 181 00:09:11,039 --> 00:09:13,829 about that potentially toward the end of class. For right now, you don't need to 182 00:09:13,830 --> 00:09:14,930 worry about it. 183 00:09:14,929 --> 00:09:16,599 The thing you do want to keep in mind 184 00:09:16,600 --> 00:09:20,829 is that most things in classes will actually be private 185 00:09:20,828 --> 00:09:23,299 unless there's a good reason to make them public. 186 00:09:23,299 --> 00:09:26,509 In some sense think about yourself, you want to have some notion of 187 00:09:26,509 --> 00:09:27,049 privacy. 188 00:09:27,049 --> 00:09:30,219 You don't want to just broadcast to the world or let the world be able to come 189 00:09:30,220 --> 00:09:34,450 in, and say, "Hey, you have some variable here which is, like, 190 00:09:34,450 --> 00:09:38,160 your age," and someone else is gonna go and modify your age. That would kind 191 00:09:38,159 --> 00:09:42,449 of bother you, right? And 192 00:09:42,450 --> 00:09:43,660 193 00:09:43,659 --> 00:09:44,438 194 00:09:44,438 --> 00:09:47,968 so most things you keep private unless there is a good reason 195 00:09:47,969 --> 00:09:51,199 to actually make them public. And so the run method that we 196 00:09:51,198 --> 00:09:54,539 talked about so far, I actually sort of told you, it needs to be public. 197 00:09:54,539 --> 00:09:57,169 Most things will be private. And I'll show you some examples of classes where we 198 00:09:57,169 --> 00:10:00,259 look at things that are private versus public. Is 199 00:10:00,259 --> 00:10:03,549 there any question about that, what public or private actually means or sort of 200 00:10:03,549 --> 00:10:07,859 the general notion of how we would define the class? Hopefully, that's familiar to you 201 00:10:07,860 --> 00:10:14,300 because you've seen it before. Um hm. [Inaudible.] 202 00:10:14,299 --> 00:10:16,309 Right. There is someone else, 203 00:10:16,309 --> 00:10:18,108 us, the 106a people, 204 00:10:18,109 --> 00:10:21,910 who provided, actually the ACM people who provided the libraries for you to use, 205 00:10:21,909 --> 00:10:25,709 that call your run method automatically when a program starts. 206 00:10:25,710 --> 00:10:29,070 So that's what's actually going on. And when we get toward the end of the class I'll 207 00:10:29,070 --> 00:10:36,070 kind of lift the covers on what's going on there. Um hm. [Inaudible] any class, like, that somebody's using [inaudible]? 208 00:10:40,980 --> 00:10:44,940 Yeah, exactly. And unless you want people in Timbuktu touching stuff 209 00:10:44,940 --> 00:10:47,800 they shouldn't be touching it should be private. 210 00:10:47,799 --> 00:10:50,579 The way I like to think about it is think about your private parts, 211 00:10:50,580 --> 00:10:54,639 you like to keep them private, you donft want them to be public. Most 212 00:10:54,639 --> 00:10:58,590 things that you have on your body are private. You are an object, so most things in 213 00:10:58,590 --> 00:11:00,759 an object are private. All right. 214 00:11:00,759 --> 00:11:02,100 Just keep that in mind. 215 00:11:02,100 --> 00:11:05,820 So, with that said, let's actually go and create a "new class." It's kind of fun to 216 00:11:05,820 --> 00:11:08,890 create classes. So I want to create a new class. So what I'm gonna do is I'm 217 00:11:08,889 --> 00:11:09,470 gonna 218 00:11:09,470 --> 00:11:11,810 get my Eclipse out and I'm gonna fire it up. 219 00:11:11,809 --> 00:11:15,250 And the way you can actually create a new class in Eclipse is over here in this 220 00:11:15,250 --> 00:11:18,649 little window, that we haven't done all that much stuff with so far, 221 00:11:18,649 --> 00:11:22,169 usually we give you some folder that's called Assignment 1 or Assignment 222 00:11:22,169 --> 00:11:25,208 2, and you work on it. If you right click on the name of that folder, 223 00:11:25,208 --> 00:11:29,289 and if you happen to be a Mac person that's the same thing as "control clicking" on that, 224 00:11:29,289 --> 00:11:32,629 you'll get this big scary menu that comes up, and you just want to pick New. 225 00:11:32,629 --> 00:11:35,990 And when you pick New you pick Class. So that's all that's going on. A right click 226 00:11:35,990 --> 00:11:37,539 on the name of the folder, 227 00:11:37,539 --> 00:11:39,198 pick New and pick Class. 228 00:11:39,198 --> 00:11:42,318 And what happens now is this thing comes up, and it says, "Create a new job of class," 229 00:11:42,318 --> 00:11:45,458 and there's all these fields, and things like dogs and cats sleeping 230 00:11:45,458 --> 00:11:47,539 together, it's just totally out of control, 231 00:11:47,539 --> 00:11:50,599 and the only thing you need to worry about here is what are you gonna name 232 00:11:50,600 --> 00:11:52,420 your class. 233 00:11:52,419 --> 00:11:54,799 And so maybe we want to write some class, 234 00:11:54,799 --> 00:11:58,209 I'll just call this "my counter.h Because I'm gonna write a little class that creates counter. And 235 00:11:58,210 --> 00:12:00,590 notice when I started typing, as soon as I started typing, let me get rid of this, I'll start 236 00:12:00,590 --> 00:12:04,629 typing again "my counter." Anyone see what happened? 237 00:12:04,629 --> 00:12:08,250 Eclipse had a cow, and the cow that it had was up here up at the top. It says, "The use 238 00:12:08,250 --> 00:12:11,600 of the default package is discouraged." And you sit there and you think, 239 00:12:11,600 --> 00:12:16,190 1.) What is the default package? And 2.) Why is its use discouraged? 240 00:12:16,190 --> 00:12:19,020 And the important thing to keep in mind is you don't care. This is one of those moments in 241 00:12:19,019 --> 00:12:21,819 your life where you're gonna be rebellious, and you're like, "Bring it on, I'm 242 00:12:21,820 --> 00:12:24,800 in the default package," that's life in the city. 243 00:12:24,799 --> 00:12:28,639 And what the package really is, is the package is just a collection of classes; 244 00:12:28,639 --> 00:12:32,549 a set of classes that make sense together. Java provides a facility to say 245 00:12:32,549 --> 00:12:34,659 we're gonna put them all in the same package. 246 00:12:34,659 --> 00:12:37,048 Right now we're not gonna define a whole bunch of classes that all work 247 00:12:37,048 --> 00:12:39,169 together, so we don't need to worry about packages. 248 00:12:39,169 --> 00:12:42,519 So all of our classes that we write are gonna be in the default package, which 249 00:12:42,519 --> 00:12:45,299 means, kind of, the un-named package, it's just sort of the package 250 00:12:45,299 --> 00:12:48,750 that everyone's a part of unless they're a part of some other package. 251 00:12:48,750 --> 00:12:52,080 So it says, "Oh, it's discouraged," and you're like, "I don't care I'm in the default package, 252 00:12:52,080 --> 00:12:54,350 I'm living for big gustos." 253 00:12:54,350 --> 00:12:59,470 And then you do something extremely complicated, which is you click "Finish." 254 00:12:59,470 --> 00:13:00,329 And guess what? You 255 00:13:00,328 --> 00:13:02,569 just created a new class. 256 00:13:02,570 --> 00:13:05,290 Now, a couple of things to keep in mind: Over here, 257 00:13:05,289 --> 00:13:08,839 in your little project that you have, there is now an entry called, "my 258 00:13:08,840 --> 00:13:11,739 counter.java." Remember, we just named the class "counter." 259 00:13:11,739 --> 00:13:13,240 Guess what Eclipse did for you, 260 00:13:13,240 --> 00:13:14,568 it did this, 261 00:13:14,568 --> 00:13:17,219 it created for you a file, 262 00:13:17,220 --> 00:13:21,189 automatically ending with .java, that's name matched the name of the class. 263 00:13:21,188 --> 00:13:22,349 264 00:13:22,350 --> 00:13:25,659 What else did it do for you? It created what we refer to as a "stub." 265 00:13:25,659 --> 00:13:28,689 It said, "I'm gonna create an empty class for you." You're gonna create a class 266 00:13:28,690 --> 00:13:31,080 called, "my counter." So what I'm gonna do is create, for you, 267 00:13:31,080 --> 00:13:34,959 the empty shell. Right. It gives you basically two lines of code and one of them is a brace. 268 00:13:34,958 --> 00:13:39,500 So it's not providing a whole lot to you, but it's, like, ooh, ooh, you've gotta define a class, like, 269 00:13:39,500 --> 00:13:41,860 aren't we excited to write a class. And you're like, all right, 270 00:13:41,860 --> 00:13:43,519 let's actually write the class. 271 00:13:43,519 --> 00:13:47,088 So what I want to do is let's go ahead and actually write a class. 272 00:13:47,089 --> 00:13:49,919 And the class that we're gonna write is something that's basically a 273 00:13:49,918 --> 00:13:52,199 counter, or I'll refer to also as an "incrementor." 274 00:13:52,200 --> 00:13:56,040 It's a very simple idea here. What I'm going to create is a class 275 00:13:56,039 --> 00:13:58,389 that is going to allow me to get, 276 00:13:58,389 --> 00:13:58,990 essentially, 277 00:13:58,990 --> 00:14:02,419 numeric values that are sequentially coming from each other. You might want to say, "Why 278 00:14:02,419 --> 00:14:05,049 would I want to do this?" This is the kind of thing, say, that Stanford would 279 00:14:05,049 --> 00:14:07,599 actually want to do when they're assigning you ID numbers. Right? 280 00:14:07,600 --> 00:14:10,930 They want to have some object somewhere, which is the ID number provider. And they're, 281 00:14:10,929 --> 00:14:14,229 like, "Hey, you just came to Stanford, get me a new ID number." 282 00:14:14,230 --> 00:14:17,048 And you just came to Stanford, "Get me the next ID number." 283 00:14:17,048 --> 00:14:20,528 So what I want to have is some way of saying, "Hey, create this object for me that 284 00:14:20,528 --> 00:14:23,889 just gives me numbers, and I'll just ask you for the next number," and you just 285 00:14:23,889 --> 00:14:25,089 sequentially give me new numbers. 286 00:14:25,089 --> 00:14:28,050 Or how many of you have actually been at a bakery or something where they have one 287 00:14:28,049 --> 00:14:31,469 of those big red wheels that has the little tags that you pull out of it? 288 00:14:31,470 --> 00:14:32,570 That's a 289 00:14:32,570 --> 00:14:35,660 counter. So that's what we're gonna write as a class, and then we can create 290 00:14:35,659 --> 00:14:37,669 objects of this counter type. Okay. 291 00:14:37,669 --> 00:14:39,779 So how do we actually create the class? 292 00:14:39,779 --> 00:14:43,199 This is a little bit different than writing a class that extends a program. 293 00:14:43,200 --> 00:14:46,790 Because we're not writing a full program we're writing a class. So we don't have a run 294 00:14:46,789 --> 00:14:47,919 method here. 295 00:14:47,919 --> 00:14:50,528 What we do have is something that's called a "constructor." 296 00:14:50,528 --> 00:14:53,860 And what a constructor is, is it's sort of the 297 00:14:53,860 --> 00:14:57,370 method that initializes everything that should be going on with 298 00:14:57,370 --> 00:14:58,528 this class. Now, here 299 00:14:58,528 --> 00:15:01,188 is the properties of the constructor. First of all, we're gonna 300 00:15:01,188 --> 00:15:04,328 make it public. At least for right now, all the constructors you're gonna see are gonna be 301 00:15:04,328 --> 00:15:04,870 public. 302 00:15:04,870 --> 00:15:07,909 A constructor does not return a value. 303 00:15:07,909 --> 00:15:10,608 So we just skip the whole notion of a return type, 304 00:15:10,609 --> 00:15:14,709 and we give the name of the constructor the same name as the name of the class. 305 00:15:14,708 --> 00:15:15,929 So we say "public 306 00:15:15,929 --> 00:15:18,639 my counter." 307 00:15:18,639 --> 00:15:23,019 And now, at this point, we have no return type. Then we provide whatever parameters 308 00:15:23,019 --> 00:15:24,519 we're gonna take in here. 309 00:15:24,519 --> 00:15:27,429 Now, what I'm gonna do for my little counter is I'm gonna be kind of funky, 310 00:15:27,429 --> 00:15:29,979 I'm not necessarily gonna start at one, I'm gonna allow someone to say, 311 00:15:29,980 --> 00:15:34,028 "Hey, you know, like, Stanford ID's don't start at one, like, they actually start at, like, 30-some 312 00:15:34,028 --> 00:15:36,909 odd million." Right. And most of you actually have 313 00:15:36,909 --> 00:15:39,308 one that's in, like, the 40-odd millions 314 00:15:39,308 --> 00:15:41,838 or probably 50-odd millions. Is your first ID No. a five now? 315 00:15:41,839 --> 00:15:44,590 How many people have a five? Anyone have a six? Anyone 316 00:15:44,590 --> 00:15:46,700 have a four? 317 00:15:46,700 --> 00:15:50,030 Anyone have a three? Yeah, 318 00:15:50,029 --> 00:15:53,120 good times. It's come a long way, evidently. We've admitted 20 million 319 00:15:53,120 --> 00:15:55,100 students in the last 15 years. 320 00:15:55,100 --> 00:15:59,000 So what we're gonna allow my counter to do is have some starting values. So we're 321 00:15:59,000 --> 00:16:01,599 gonna specify parameter start value 322 00:16:01,599 --> 00:16:05,160 that's going to be, essentially, where we want this counter to start. 323 00:16:05,159 --> 00:16:08,519 And now you think for a moment, you think, "Hey, we've gotta do something with that 324 00:16:08,519 --> 00:16:09,688 start value," right? 325 00:16:09,688 --> 00:16:13,498 This is state of the object that we need to keep around. Somehow we need to start 326 00:16:13,499 --> 00:16:16,139 off by saying this counter starts at start value, and 327 00:16:16,139 --> 00:16:19,068 every time you ask me for a new value of the counter I'm gonna give you a new 328 00:16:19,068 --> 00:16:19,990 number. 329 00:16:19,990 --> 00:16:24,289 That means I need to keep track between method calls of what the old number was. 330 00:16:24,289 --> 00:16:24,879 331 00:16:24,879 --> 00:16:27,710 So if I need to keep track of something between method calls what am I gonna 332 00:16:27,710 --> 00:16:30,370 need? 333 00:16:30,370 --> 00:16:34,120 Instance variable. There's a few people over here, 334 00:16:34,120 --> 00:16:36,509 one person lying on the floor down there. 335 00:16:36,509 --> 00:16:39,939 So what I'm gonna have is an instance variable. And to find the instance variable 336 00:16:39,940 --> 00:16:43,020 down at the bottom I'm going to make the instance variable private because I 337 00:16:43,019 --> 00:16:46,139 don't want someone being able to muck with my instance variable. So I'm going 338 00:16:46,139 --> 00:16:48,309 to have private int, 339 00:16:48,309 --> 00:16:52,789 and I'll just call this "counter." So this is my instance variable over here. And 340 00:16:52,789 --> 00:16:55,379 so what I'm going to do is, in my constructor 341 00:16:55,379 --> 00:16:57,239 I'm going to say, "Hey, 342 00:16:57,239 --> 00:17:01,550 initialize that counter to be equal to whatever start value the user gave me." 343 00:17:01,549 --> 00:17:05,328 So I have my own little instance variable here. Every counter object is 344 00:17:05,328 --> 00:17:07,349 gonna have its own little counter variable, 345 00:17:07,349 --> 00:17:10,538 and I'm just gonna start it off by giving it whatever value the user told 346 00:17:10,538 --> 00:17:13,939 me to set it to when they created an object of this type. And I'll show you in just a 347 00:17:13,939 --> 00:17:16,199 second how we create an object of this type. 348 00:17:16,199 --> 00:17:19,240 Now, the other thing we could do, that's kind of funky, is you can actually have more than 349 00:17:19,240 --> 00:17:20,750 one constructor. 350 00:17:20,750 --> 00:17:24,000 You can have multiple constructors that have the same 351 00:17:24,000 --> 00:17:28,068 form, they're all public, at least so far. They return no type. 352 00:17:28,068 --> 00:17:31,418 But the thing that differentiates them, and the way the computer knows which 353 00:17:31,419 --> 00:17:32,528 constructor to actually use 354 00:17:32,528 --> 00:17:35,940 is what the parameters are. So we just created a constructor that has one parameter. 355 00:17:35,940 --> 00:17:38,940 I'm also going to create another constructor that has no parameters. 356 00:17:38,940 --> 00:17:42,049 So someone can actually create a counter without telling me what the starting value 357 00:17:42,049 --> 00:17:45,299 is. Well, what is the starting value if they don't give me one? I still need to 358 00:17:45,299 --> 00:17:45,819 assign one. 359 00:17:45,819 --> 00:17:48,539 So I'm gonna say, "Hey, if you didn't give me one, I'm gonna set 360 00:17:48,539 --> 00:17:49,490 the 361 00:17:49,490 --> 00:17:52,460 starting value to be one." Because most of the time when people want a 362 00:17:52,460 --> 00:17:53,808 counter they just want to count from one. 363 00:17:53,808 --> 00:17:57,028 But if you want to do something funky, like have ID numbers for a university, 364 00:17:57,028 --> 00:17:58,819 you can tell me the starting value. 365 00:17:58,819 --> 00:18:02,109 And so I'll show you in just a second, how we actually create objects of this type, 366 00:18:02,109 --> 00:18:05,038 and when we create objects how it differentiates between whether to call 367 00:18:05,038 --> 00:18:10,440 this method up here with a parameter or this method over here without a parameter. A 368 00:18:10,440 --> 00:18:14,179 couple of other things we need to do. We need to find and specify some way of saying, 369 00:18:14,179 --> 00:18:18,150 "Give me the next value." Right. How do I get new numbers, how do I request new 370 00:18:18,150 --> 00:18:20,870 numbers from this counter? Well, I need to have some method. 371 00:18:20,869 --> 00:18:24,359 This method's going to return for me an integer, because my counter is just 372 00:18:24,359 --> 00:18:24,949 integers, 373 00:18:24,950 --> 00:18:27,080 and I'll call this "next value." 374 00:18:27,079 --> 00:18:30,589 It takes in no parameters because I don't need to tell you anything. I don't need to tell 375 00:18:30,589 --> 00:18:33,538 the object anything to get the next value. I just say, "Hey, buddy, 376 00:18:33,538 --> 00:18:37,538 next value." And it gives me the next value. So the way it's gonna give me the next 377 00:18:37,538 --> 00:18:41,890 value is it's going to give me whatever value the counter has right now. 378 00:18:41,890 --> 00:18:44,590 So I might be inclined to say, "return counter." 379 00:18:44,589 --> 00:18:49,419 What's the problem with doing that? 380 00:18:49,420 --> 00:18:52,700 What happens the next time I call next value? 381 00:18:52,700 --> 00:18:55,769 I return the same. But that's not very exciting, right. You go into the bakery, and you're like, 382 00:18:55,769 --> 00:18:59,440 "Hey, I got No. 1." And you look around and there's 300 other people that have No. 1. The 383 00:18:59,440 --> 00:19:01,058 fights break out, it's bad times. 384 00:19:01,058 --> 00:19:03,960 So what we need to do is we need to say, "Hey, 385 00:19:03,960 --> 00:19:07,480 I need to keep track of what value the counter is currently right now because that's what I'm gonna return to you." So I'm gonna have some 386 00:19:07,480 --> 00:19:10,470 "temporary." 387 00:19:10,470 --> 00:19:15,000 And this temporary is a local variable, it only needs to live inside this method 388 00:19:15,000 --> 00:19:16,779 for the lifetime of this method. 389 00:19:16,779 --> 00:19:21,279 And I will initialize that temporary to be whatever the current counter value is. 390 00:19:21,279 --> 00:19:24,849 Then I'll go ahead and add one to the counter value. So I'll increment the counter 391 00:19:24,849 --> 00:19:28,039 so the number next time this gets called is gonna be one higher. 392 00:19:28,039 --> 00:19:31,170 And what am I gonna do now when I return? 393 00:19:31,170 --> 00:19:33,170 Any ideas? 394 00:19:33,170 --> 00:19:36,340 Return the temporary. The temporary 395 00:19:36,339 --> 00:19:38,169 is 396 00:19:38,170 --> 00:19:41,440 what the value of the counter started at when this method was first called. So I said, 397 00:19:41,440 --> 00:19:43,409 "Oh, store off a copy of it, 398 00:19:43,409 --> 00:19:47,200 add one to the counter," because next time it'll be one greater and return whatever the old 399 00:19:47,200 --> 00:19:48,278 value was in temporary. 400 00:19:48,278 --> 00:19:51,669 And after this function's gone, temporary gets blown away. And it's like, "Hey, I was a 401 00:19:51,669 --> 00:19:54,830 local variable, I gave my life for the method." And we're like, "Thank you very much, 402 00:19:54,829 --> 00:19:55,909 temporary, that was 403 00:19:55,910 --> 00:19:56,929 very 404 00:19:56,929 --> 00:20:01,038 courageous of you." But you're gone now but you managed to return this 405 00:20:01,038 --> 00:20:05,009 value that you stored. So good times, you actually did what we needed you to do. 406 00:20:05,009 --> 00:20:08,680 And this is my whole class. Is there 407 00:20:08,680 --> 00:20:12,929 any questions about this class? Now, 408 00:20:12,929 --> 00:20:15,800 let me show you one other thing before we actually 409 00:20:15,799 --> 00:20:17,950 make use of this class. 410 00:20:17,950 --> 00:20:19,048 And it's 411 00:20:19,048 --> 00:20:21,970 a way that we could have actually done things slightly differently. 412 00:20:21,970 --> 00:20:24,990 So when we actually created the class here's our constructor. 413 00:20:24,990 --> 00:20:28,839 In this case, what I've done is I've just renamed the class from my counter to be 414 00:20:28,839 --> 00:20:31,859 incrementor, because incrementor is kind of the more funky computer science term 415 00:20:31,859 --> 00:20:33,669 for it, and incrementor is something that 416 00:20:33,670 --> 00:20:35,419 adds one to something and counts. 417 00:20:35,419 --> 00:20:39,020 So other than my counter, which is just all fuzzy and good, it's like, "Oh, it's My Counter," 418 00:20:39,019 --> 00:20:41,889 but people might think, "Oh, your counter, is that, like, the 419 00:20:41,890 --> 00:20:44,230 restaurant in Palo Alto that gives you burgers?" 420 00:20:44,230 --> 00:20:45,960 Anyone ever eaten there? It's 421 00:20:45,960 --> 00:20:48,049 a good time. 422 00:20:48,049 --> 00:20:51,149 And then they didn't even pay for the product placement, I've gotta say. 423 00:20:51,150 --> 00:20:54,310 We'll call it incrementor. But same kind of thing here, we're gonna have 424 00:20:54,309 --> 00:20:57,960 some constructor for incrementor. It's gonna take some start value. I'm just showing 425 00:20:57,960 --> 00:20:59,680 one of the constructors here. 426 00:20:59,680 --> 00:21:02,210 And one thing you might say is, 427 00:21:02,210 --> 00:21:05,190 what are the properties, just to reiterate, of constructors? The name 428 00:21:05,190 --> 00:21:08,179 of the class is the same as the constructor name. The constructor does 429 00:21:08,179 --> 00:21:12,528 not specify return type, it's responsible for initializing whatever values we care 430 00:21:12,528 --> 00:21:17,089 about in the object. And this puppy gets called when an object is created. And 431 00:21:17,089 --> 00:21:20,730 I'll show you an example. Just like you created graphical objects in the past, 432 00:21:20,730 --> 00:21:23,929 we're gonna create an object an object of type incrementor in just a second, and I'll show 433 00:21:23,929 --> 00:21:24,989 the code for that. 434 00:21:24,989 --> 00:21:29,660 But that's when this implementor method actually gets invoked. 435 00:21:29,660 --> 00:21:32,009 So one thing that people think about when they see this is, 436 00:21:32,009 --> 00:21:35,240 "Hey, you have this start value thing, and then over here you call it counter, why 437 00:21:35,240 --> 00:21:37,940 don't you just call it counter both places? 438 00:21:37,940 --> 00:21:41,519 Why don't you rewrite the code like that?" 439 00:21:41,519 --> 00:21:47,470 What happens when we do that? Is there any issue with doing that? You're, like, ah. 440 00:21:47,470 --> 00:21:50,970 There's the one part of you that's, like, "Oh, that looks so clean," like, counter equals counter. And then there's a part of 441 00:21:50,970 --> 00:21:53,299 you that get really uneasy. 442 00:21:53,299 --> 00:21:55,009 Because you see this line here? 443 00:21:55,009 --> 00:21:56,569 That line does nothing. 444 00:21:56,569 --> 00:22:00,460 It assigns the same value back to itself. And you're, like, "But I wanted this 445 00:22:00,460 --> 00:22:03,680 counter to be like my instance variable counter, and I wanted this counter 446 00:22:03,680 --> 00:22:05,620 to be my parameter counter, can't the 447 00:22:05,619 --> 00:22:07,919 computer just figure that out?" 448 00:22:07,920 --> 00:22:09,130 449 00:22:09,130 --> 00:22:11,700 No. 450 00:22:11,700 --> 00:22:14,590 You can't do that here 451 00:22:14,589 --> 00:22:18,399 because sometimes it 452 00:22:18,400 --> 00:22:20,380 will prevent you. So the problem here 453 00:22:20,380 --> 00:22:21,580 is that the 454 00:22:21,579 --> 00:22:24,210 parameter counter is actually getting assigned to itself. 455 00:22:24,210 --> 00:22:28,000 You're, like, "Hey, the counter here before was the instance variable, what's going on?" 456 00:22:28,000 --> 00:22:31,460 If you have a parameter that has the same name as one of your instance 457 00:22:31,460 --> 00:22:34,090 variables, you get something called, "shadowing." 458 00:22:34,089 --> 00:22:37,980 What shadowing is, is when I refer to some variable, the first place I look is 459 00:22:37,980 --> 00:22:38,489 do I 460 00:22:38,489 --> 00:22:42,140 have any parameters or local variables that have that name. 461 00:22:42,140 --> 00:22:47,220 If I do then that's what I'm referring to. If I don't have any parameters or local 462 00:22:47,220 --> 00:22:50,640 variables with that name then I check to see if I have an instance variable with that 463 00:22:50,640 --> 00:22:54,320 name. So there's actually an order in which I check for these names. And because in 464 00:22:54,319 --> 00:22:58,359 this case I have a parameter named counter, this counter's referring to that parameter, 465 00:22:58,359 --> 00:23:00,639 and this counter's referring to that parameter. 466 00:23:00,640 --> 00:23:02,778 In the previous case, 467 00:23:02,778 --> 00:23:06,190 I didn't have a parameter named counter. So it said, "Do I have a parameter named counter?" 468 00:23:06,190 --> 00:23:09,870 "No." "Do I have an instance variable named counter?" "Yes." So it uses the instance 469 00:23:09,869 --> 00:23:10,779 variable. 470 00:23:10,779 --> 00:23:14,740 So you could say, "Hey, is there a way I can force it to know the difference between the 471 00:23:14,740 --> 00:23:17,259 two counters?" And the answer is actually yes. 472 00:23:17,259 --> 00:23:19,150 So even though there's a problem here 473 00:23:19,150 --> 00:23:20,970 we can fix this. 474 00:23:20,970 --> 00:23:24,470 And the way we fix this is with a notion called, "this." 475 00:23:24,470 --> 00:23:26,569 The way you can think of this, 476 00:23:26,569 --> 00:23:31,689 is this is always referring to the receiving object. What does that mean? 477 00:23:31,690 --> 00:23:32,950 That's a mouthful. 478 00:23:32,950 --> 00:23:36,480 It means that when some method gets called, remember when method gets called 479 00:23:36,480 --> 00:23:39,569 there's always some object that that method is getting called on, 480 00:23:39,569 --> 00:23:42,079 well, when we call that method, 481 00:23:42,079 --> 00:23:45,819 inside the method we can refer to this.counter, which says 482 00:23:45,819 --> 00:23:50,679 refer to myself, my own object counter, don't refer to my parameter 483 00:23:50,680 --> 00:23:54,619 counter. So this.counter will look for an instance variable directly. 484 00:23:54,618 --> 00:23:58,579 It will not look for a local variable; it will not look for a parameter. 485 00:23:58,579 --> 00:24:02,250 So here when I say, this.counter, it says you have an instance variable named 486 00:24:02,250 --> 00:24:05,089 counter. That's what this.counter is referring to. This 487 00:24:05,089 --> 00:24:07,059 counter over here, it says, 488 00:24:07,059 --> 00:24:09,379 "Hey, I don't have any kind of qualifier." 489 00:24:09,380 --> 00:24:12,530 Let me check the parameters and local variables first. I have a parameter named 490 00:24:12,529 --> 00:24:15,779 counter, so that's what the counter over here is actually referring to. 491 00:24:15,779 --> 00:24:19,538 Now, this is, kind of, ugly syntax. There's some reasons to use it here and there, but 492 00:24:19,538 --> 00:24:22,490 it's ugly syntax. The way to think about this is 493 00:24:22,490 --> 00:24:25,660 just give them different names, that's the easiest way to do it. 494 00:24:25,660 --> 00:24:28,670 Keep the names of your parameters distinct from the name of your instance 495 00:24:28,670 --> 00:24:31,720 variables and there'll just be no confusion with this or that or whatever, you just 496 00:24:31,720 --> 00:24:35,809 keep the names distinct. And it's the preferred root. You want to think about writing 497 00:24:35,809 --> 00:24:37,288 programs that require the human 498 00:24:37,288 --> 00:24:40,538 to think less when they're actually looking at the program. You're, like, "That's 499 00:24:40,538 --> 00:24:42,878 really weird, I thought computer science was all about thinking." 500 00:24:42,878 --> 00:24:45,949 Yeah, it's about thinking higher up the food chain, you don't want to worry about 501 00:24:45,950 --> 00:24:49,889 little details like this and get caught in these weird shadowing effects or aliasing 502 00:24:49,888 --> 00:24:53,969 effects, you just want to say, "Yeah, they're distinct," and then it's clear. Is there are 503 00:24:53,970 --> 00:24:56,049 any questions about that? It's kind of 504 00:24:56,049 --> 00:24:58,930 a funky concept but it's just important for you to see it. 505 00:24:58,930 --> 00:25:01,870 So with that said, let's actually look at 506 00:25:01,869 --> 00:25:05,519 not my counter anymore. We sort of wrote the bare bones code for my counter, 507 00:25:05,519 --> 00:25:08,700 but here are the full-fledged incrementor version. And so when we're now 508 00:25:08,700 --> 00:25:11,000 computer scientists, and we think about good programming style, 509 00:25:11,000 --> 00:25:14,190 we have some comment for the class. So just like the programs you wrote, you want to 510 00:25:14,190 --> 00:25:17,769 have some comment for the whole class, you want to have some comment per method 511 00:25:17,769 --> 00:25:22,339 you write. So this is all the same code we just wrote, now it's just commented. 512 00:25:22,339 --> 00:25:29,339 So good programming style. Um hm. [Inaudible.] 513 00:25:29,819 --> 00:25:31,730 Because if I declare it every time 514 00:25:31,730 --> 00:25:35,089 there is no way of me being able to keep track of what it's previous value was. 515 00:25:35,089 --> 00:25:38,189 So that's the critical concept of an instance variable, I need to 516 00:25:38,190 --> 00:25:42,679 keep track of some information between method calls. If I didn't, like temp, I 517 00:25:42,679 --> 00:25:46,570 didn't care what temp was the last time I called next value, it's just the local 518 00:25:46,569 --> 00:25:49,299 variable and it goes away. But counter I need to keep track of. 519 00:25:49,299 --> 00:25:51,190 So how do I actually use counter? 520 00:25:51,190 --> 00:25:54,038 I wrote a program called "use 521 00:25:54,038 --> 00:25:56,869 counter." 522 00:25:56,869 --> 00:25:58,869 So 523 00:25:58,869 --> 00:26:02,669 use counter is basically just a Java program that is a program. 524 00:26:02,670 --> 00:26:05,509 So it extends Console Program, just like you're used to. 525 00:26:05,509 --> 00:26:08,410 I'm changing the font. You donft need to worry about this line. All this line does is 526 00:26:08,410 --> 00:26:11,710 it just makes the font bigger so when I run the program you can actually see the text. 527 00:26:11,710 --> 00:26:15,069 Because there were people who were having trouble seeing the output text when I ran 528 00:26:15,069 --> 00:26:16,329 programs. Now, 529 00:26:16,329 --> 00:26:20,308 how do I create objects of this class? This is just like you saw with graphical 530 00:26:20,308 --> 00:26:22,990 objects. 531 00:26:22,990 --> 00:26:26,039 What's going on is if I want to create some new counter, 532 00:26:26,039 --> 00:26:27,720 or some new incrementor, 533 00:26:27,720 --> 00:26:31,329 I specify the type of the variable incrementor. I give it a name, I'll call it 534 00:26:31,329 --> 00:26:32,379 "count one." 535 00:26:32,380 --> 00:26:36,130 And because I want to create an object, I go to the incrementor factory and 536 00:26:36,130 --> 00:26:36,610 say, 537 00:26:36,609 --> 00:26:39,369 "Give me a new incrementor." 538 00:26:39,369 --> 00:26:43,729 When I say new and the name of a class, what I get is a new object of that class. 539 00:26:43,730 --> 00:26:45,929 So at this point when I say new incrementor, 540 00:26:45,929 --> 00:26:48,960 a new incrementor object is created for me. 541 00:26:48,960 --> 00:26:52,789 That is when the constructor for that object gets invoked. So that constructor 542 00:26:52,789 --> 00:26:56,779 thing we just wrote, that initializes the object, the place it gets called is when 543 00:26:56,779 --> 00:27:00,470 you ask for a new one of the class, when you get a new object the constructor's 544 00:27:00,470 --> 00:27:01,230 invoked. 545 00:27:01,230 --> 00:27:04,839 Which version of the constructor does it invoke if you have multiple versions? 546 00:27:04,839 --> 00:27:08,168 It depends on the parameters you give. Right. We created two versions, one 547 00:27:08,169 --> 00:27:09,429 without a parameter, 548 00:27:09,429 --> 00:27:13,070 if you didn't specify the parameter it set the counter starting at one. One 549 00:27:13,069 --> 00:27:16,298 with a parameter, so if you specify parameter, it says, "Hey, there was a version 550 00:27:16,298 --> 00:27:19,808 of this that had a parameter," that's the version that's gonna get invoked, and so 551 00:27:19,808 --> 00:27:22,779 it's gonna set the beginning counter to 552 00:27:22,779 --> 00:27:26,059 1,000. Any questions about that? So what are 553 00:27:26,059 --> 00:27:29,500 we gonna do? Now what we're gonna do is write out some values of the counter and count up. So 554 00:27:29,500 --> 00:27:30,489 what I'm gonna do 555 00:27:30,489 --> 00:27:31,798 is write a method. 556 00:27:31,798 --> 00:27:35,359 This method is going to take an object as a parameter. There's no reason why it 557 00:27:35,359 --> 00:27:38,240 can't, parameters just have some type and some name. 558 00:27:38,240 --> 00:27:41,900 So this count five times method is not gonna return anything, but it's going 559 00:27:41,900 --> 00:27:44,030 to take as a parameter 560 00:27:44,029 --> 00:27:47,500 something of type incrementor, that I'll just call "counter." And what it's gonna 561 00:27:47,500 --> 00:27:52,039 do is very simple, it's going to loop through five times and just print out 562 00:27:52,039 --> 00:27:56,019 the next five values of that counter. So that's all it does, it just pulls the 563 00:27:56,019 --> 00:27:59,289 next five tickets from the counter and writes them out to the screen. 564 00:27:59,289 --> 00:28:03,019 So I'm gonna say, what are the first five values for count one, and call 565 00:28:03,019 --> 00:28:06,480 this function. What are the first five values for count two, and call the 566 00:28:06,480 --> 00:28:10,579 function passing and count two, and what are another five values for 567 00:28:10,579 --> 00:28:13,710 count one. So let me go ahead and run this program and you can see what's going on. 568 00:28:13,710 --> 00:28:15,180 So if I run this, 569 00:28:15,180 --> 00:28:20,640 570 00:28:20,640 --> 00:28:22,620 571 00:28:22,619 --> 00:28:23,839 there's what I get. 572 00:28:23,839 --> 00:28:27,259 So I say the first five values for count one. Remember count one 573 00:28:27,259 --> 00:28:30,410 started with a counter of one. It gives me one, two, three, four, five. 574 00:28:30,410 --> 00:28:33,788 Then I asked for the first five value of count two. Count two started at 1,000, so 575 00:28:33,788 --> 00:28:36,089 I get a 1,000 up to a 1,004. 576 00:28:36,089 --> 00:28:39,069 Then I ask for the next five values of count one, 577 00:28:39,069 --> 00:28:41,509 and I get six, seven, eight, nine, ten. 578 00:28:41,509 --> 00:28:44,769 Now, this all seems fairly straightforward, except for one fact that 579 00:28:44,769 --> 00:28:47,829 should slightly disturb you. Is there anything that slightly disturbs you? 580 00:28:47,829 --> 00:28:48,159 581 00:28:48,160 --> 00:28:51,450 582 00:28:51,450 --> 00:28:52,980 583 00:28:52,980 --> 00:28:56,349 The thing that should slightly disturb you is the conversation we had last time, 584 00:28:56,349 --> 00:28:59,819 where we talked about when you pass a parameter to something you get a copy of 585 00:28:59,819 --> 00:29:01,009 that parameter, 586 00:29:01,009 --> 00:29:05,129 or you get a copy of that thing that you're passing. So 587 00:29:05,130 --> 00:29:07,680 the interesting thing that's going on is you would think, 588 00:29:07,680 --> 00:29:10,480 well, you're passing count one here. 589 00:29:10,480 --> 00:29:13,660 When you pass count one there shouldn't you have gotten a copy of the count one 590 00:29:13,660 --> 00:29:15,610 that started with its counter of one? 591 00:29:15,609 --> 00:29:19,899 So when it counts it up here shouldn't it have only been counting in that copy, 592 00:29:19,900 --> 00:29:23,410 and so when it was done here that copy should have gone away, and when you returned 593 00:29:23,410 --> 00:29:26,750 over here your counter should have still been one? 594 00:29:26,750 --> 00:29:29,839 If we were doing integers that's sort of what would have happened. 595 00:29:29,839 --> 00:29:33,669 But the things that's different between integers and doubles and objects, and this is the 596 00:29:33,670 --> 00:29:35,730 important thing to think about objects, 597 00:29:35,730 --> 00:29:38,139 when you pass an object as a parameter 598 00:29:38,138 --> 00:29:42,339 you are not passing a copy of the object, you're passing what we refer to as a reference to 599 00:29:42,339 --> 00:29:43,199 the object, 600 00:29:43,200 --> 00:29:44,640 which you're actually passing 601 00:29:44,640 --> 00:29:47,080 the object itself. 602 00:29:47,079 --> 00:29:50,839 So what we think of as a reference is really there is a way to point to the object. 603 00:29:50,839 --> 00:29:52,918 So let's say you're an object. 604 00:29:52,919 --> 00:29:56,179 When I'm over here and I say give me a new object, you're sort of created, you come into 605 00:29:56,179 --> 00:29:57,070 being. 606 00:29:57,069 --> 00:30:00,720 And what count one really is, is just a way of pointing to you. 607 00:30:00,720 --> 00:30:04,600 And so when I say, "Hey, count five times, count one," what I'm actually passing 608 00:30:04,599 --> 00:30:07,158 is a copy of my finger. 609 00:30:07,159 --> 00:30:08,460 And you're, like, that's a little weird, 610 00:30:08,460 --> 00:30:09,750 but it points to you. 611 00:30:09,750 --> 00:30:14,019 So when we count through five times you're the one counting five times. 612 00:30:14,019 --> 00:30:18,069 And then when we come back over here, and I call count one again, I'm passing 613 00:30:18,069 --> 00:30:21,759 another copy of my finger that's pointing to the same place. So 614 00:30:21,759 --> 00:30:26,250 you're the same object that was invoked before, and you give me the next five values. 615 00:30:26,250 --> 00:30:29,440 So critical thing to remember, when you are passing an 616 00:30:29,440 --> 00:30:31,528 object as a parameter, 617 00:30:31,528 --> 00:30:34,690 you are actually in some sense passing the object itself. The way you can think 618 00:30:34,690 --> 00:30:37,440 about this is it's like the mafia. 619 00:30:37,440 --> 00:30:41,179 We know where objects live, and if I know where you live when I'm doing something, 620 00:30:41,179 --> 00:30:44,100 when I, like, come to your house and put a horse head in your bed or I'm, like, 621 00:30:44,099 --> 00:30:46,449 getting the next value or whatever, 622 00:30:46,450 --> 00:30:47,350 it's you, 623 00:30:47,349 --> 00:30:49,879 and it's you every time because I know where you live. 624 00:30:49,880 --> 00:30:53,230 With integers, we don't know where they live, we just pass copies. Here's a copy of 625 00:30:53,230 --> 00:30:55,989 a double, here's a copy of an integer, we don't know where they live. 626 00:30:55,989 --> 00:30:57,259 That's perfectly fine. 627 00:30:57,259 --> 00:31:00,519 With objects, we know where they live. And so what you're actually passing is 628 00:31:00,519 --> 00:31:02,710 you're passing where that object lives, 629 00:31:02,710 --> 00:31:05,929 and so that way you're always referring to the same object because you're referring to the 630 00:31:05,929 --> 00:31:09,950 same address where that object lives, you're always looking up the object in the same place. 631 00:31:09,950 --> 00:31:11,640 Any questions about that? 632 00:31:11,640 --> 00:31:15,250 That's kind of a critical concept. When we write a program 633 00:31:15,250 --> 00:31:19,200 that makes use of a class that we've written 634 00:31:19,200 --> 00:31:23,720 how does it know to access that class? 635 00:31:23,720 --> 00:31:24,809 Because 636 00:31:24,809 --> 00:31:28,168 when you're creating your classes over here you're creating all your classes 637 00:31:28,169 --> 00:31:31,110 in this default package and they're all in the same project. 638 00:31:31,109 --> 00:31:34,428 So when you say give me a new incrementor, it goes and looks through these, and says, 639 00:31:34,429 --> 00:31:41,429 "Hey, do you have an incrementor class over there," and that's how it actually knows. For the third part of this program, 640 00:31:48,700 --> 00:31:51,840 [inaudible]? Because we already counted one, two, three, four, five. So the counter of the value 641 00:31:51,839 --> 00:31:58,369 that's actually being stored for the counter 642 00:31:58,369 --> 00:32:00,609 643 00:32:00,609 --> 00:32:03,849 is 644 00:32:03,849 --> 00:32:08,039 645 00:32:08,039 --> 00:32:10,009 646 00:32:10,009 --> 00:32:13,759 647 00:32:13,759 --> 00:32:17,430 648 00:32:17,430 --> 00:32:20,580 649 00:32:20,579 --> 00:32:23,980 650 00:32:23,980 --> 00:32:25,940 651 00:32:25,940 --> 00:32:29,090 652 00:32:29,089 --> 00:32:32,629 currently 653 00:32:32,630 --> 00:32:34,130 six. 654 00:32:34,130 --> 00:32:38,330 655 00:32:38,329 --> 00:32:41,250 Any other questions? 656 00:32:41,250 --> 00:32:44,750 657 00:32:44,750 --> 00:32:48,638 So 658 00:32:48,638 --> 00:32:50,538 659 00:32:50,538 --> 00:32:52,990 one thing we can also do that's kind of funky 660 00:32:52,990 --> 00:32:56,819 is, remember when we have our counter, or our incrementor, we have this instance 661 00:32:56,819 --> 00:32:58,049 variable. 662 00:32:58,049 --> 00:33:01,639 There's another kind of variable, which I never told you about before, well, actually, 663 00:33:01,640 --> 00:33:04,820 I did tell you about it, I just didn't give you its actual name before, but now 664 00:33:04,819 --> 00:33:06,460 you're not old enough 665 00:33:06,460 --> 00:33:12,029 so I'll tell you what the real deal is. 666 00:33:12,029 --> 00:33:13,129 667 00:33:13,130 --> 00:33:15,270 668 00:33:15,269 --> 00:33:19,240 669 00:33:19,240 --> 00:33:23,399 The basic idea is so far you've seen things like local variables, 670 00:33:23,398 --> 00:33:26,898 and you've seen instance variables. There is a new kind of variable, 671 00:33:26,898 --> 00:33:28,129 and it is called a 672 00:33:28,130 --> 00:33:30,990 "class variable" or a class var. 673 00:33:30,990 --> 00:33:35,769 What a class variable is, it is a variable that is shared by all objects 674 00:33:35,769 --> 00:33:40,809 of that class. There is one variable that all objects of that class share, that's 675 00:33:40,809 --> 00:33:44,069 the funky concept. In an instance variable 676 00:33:44,069 --> 00:33:48,789 each object of that class has a different version of that variable. 677 00:33:48,789 --> 00:33:51,398 So if we have some counter over here, and if we have 678 00:33:51,398 --> 00:33:55,000 some count one, and some count two, and they each have 679 00:33:55,000 --> 00:33:59,000 an instance variable that we just defined, each of them has its own box. 680 00:33:59,000 --> 00:34:05,730 If I were to create a class variable, there is one variable counter that all 681 00:34:05,730 --> 00:34:09,510 objects, count one, count two, all objects of 682 00:34:09,510 --> 00:34:10,729 the type, 683 00:34:10,728 --> 00:34:12,648 or of the class, that this 684 00:34:12,648 --> 00:34:14,588 class variable is in, 685 00:34:14,588 --> 00:34:19,588 are all referring to the same one single box. Now, 686 00:34:19,588 --> 00:34:22,449 there's sometimes you want to do that. I'll just show you a very simple example of 687 00:34:22,449 --> 00:34:25,930 how that works, and how you actually specify it. So the way you specify it, 688 00:34:25,929 --> 00:34:29,409 is in instance variable you just specified if it was public or private, and you specified 689 00:34:29,409 --> 00:34:31,128 its type and you gave it a name. 690 00:34:31,128 --> 00:34:36,278 In class variable you add this word called "static." And sometimes class variables as 691 00:34:36,278 --> 00:34:38,840 a result are referred to as static variables. And 692 00:34:38,840 --> 00:34:42,000 you've seen static before. When did you actually see static used in conjunction 693 00:34:42,000 --> 00:34:44,108 with a variable? 694 00:34:44,108 --> 00:34:45,389 Constants, right. 695 00:34:45,389 --> 00:34:48,989 Why did we make them class variables by adding static? Because it doesn't make 696 00:34:48,989 --> 00:34:53,119 sense for two different objects that are circles to have their own copy of the 697 00:34:53,119 --> 00:34:54,838 constant value pi. 698 00:34:54,838 --> 00:34:57,659 Pi is in some sense some universal for all circles. 699 00:34:57,659 --> 00:35:02,998 So it would be shared as a class variable among all circles. 700 00:35:02,998 --> 00:35:04,649 So even though it was a constant, 701 00:35:04,650 --> 00:35:08,338 if we didn't say static, we would have gotten one of those constant values for 702 00:35:08,338 --> 00:35:10,429 every circle we created, for example. 703 00:35:10,429 --> 00:35:13,339 We want to just have one that's shared, so we call it static. 704 00:35:13,340 --> 00:35:16,420 But you can have static variables that are not necessarily constants, they don't 705 00:35:16,420 --> 00:35:20,019 have to be final. So let me show you an example on the computer. 706 00:35:20,018 --> 00:35:22,679 We come over here. Here's our incrementor that we just wrote. 707 00:35:22,679 --> 00:35:24,639 And we say private 708 00:35:24,639 --> 00:35:25,348 static 709 00:35:25,349 --> 00:35:26,300 int counter. 710 00:35:26,300 --> 00:35:29,440 So what I've now done is, instead of creating an instance variable I have 711 00:35:29,440 --> 00:35:31,499 created a class variable. 712 00:35:31,498 --> 00:35:35,209 How does this change the program? Let me save this and run this. 713 00:35:35,210 --> 00:35:39,380 What this will now do is, all of the things that I create that are incrementors 714 00:35:39,380 --> 00:35:39,789 715 00:35:39,789 --> 00:35:42,739 are all referring to the same counter. 716 00:35:42,739 --> 00:35:47,728 You're, like, whoa, that's kind of funky. Yeah, let me show you what actually happens. 717 00:35:47,728 --> 00:35:51,259 So when I run use counter, 718 00:35:51,259 --> 00:35:53,039 I get 719 00:35:53,039 --> 00:35:56,749 a 1,000 through a 1,004, then I get 1,005 through a 1,009, 720 00:35:56,748 --> 00:35:59,759 then I get 1,010 through a 1,014. 721 00:35:59,759 --> 00:36:01,458 And you're, like, whoa, 722 00:36:01,458 --> 00:36:03,409 what happened there? 723 00:36:03,409 --> 00:36:06,538 Let me explain to you what happened here and hopefully it'll be clear. Like what did it 724 00:36:06,539 --> 00:36:11,099 start at a 1,000, why didn't it start at one, what was actually going on? 725 00:36:11,099 --> 00:36:13,900 What's going on, if I actually look at use counter, 726 00:36:13,900 --> 00:36:18,170 is remember there is only one counter variable for all objects of the class. 727 00:36:18,170 --> 00:36:21,329 This first count one comes along and say, "Hey, give me a new object that's an 728 00:36:21,329 --> 00:36:24,430 incrementor." And we say, "Okay." And we set it's counter 729 00:36:24,429 --> 00:36:26,649 to be one, right, that's what the constructor did. 730 00:36:26,650 --> 00:36:30,459 Then I say, "Hey, create for me another counter." And it says, "Okay, here's another 731 00:36:30,458 --> 00:36:31,679 incrementor over here, 732 00:36:31,679 --> 00:36:35,058 I'm now setting that counter variable to be a 1,000." 733 00:36:35,059 --> 00:36:36,220 And you're, like, 734 00:36:36,219 --> 00:36:36,899 uh-oh, 735 00:36:36,900 --> 00:36:40,519 that's the same counter variable that count one was using, too. Because now they're 736 00:36:40,519 --> 00:36:42,219 both sharing the same variable, 737 00:36:42,219 --> 00:36:44,158 and that variable is now a 1,000. 738 00:36:44,159 --> 00:36:47,759 The old value of one just got clobbered by this guy because it's referring to 739 00:36:47,759 --> 00:36:51,050 that same class variable. There's only one counter variable 740 00:36:51,050 --> 00:36:52,870 among all objects of the class. 741 00:36:52,869 --> 00:36:57,199 So now when I count the first five values of count one, it's got a 1,000, 742 00:36:57,199 --> 00:36:59,689 it counts up to a 1,004. 743 00:36:59,690 --> 00:37:03,579 That 1,004 is shared by all instances of the class. So when I do the 744 00:37:03,579 --> 00:37:05,380 next five for count two, 745 00:37:05,380 --> 00:37:08,800 it's starting at a 1,005, and it counts from a 1,005 through 746 00:37:08,800 --> 00:37:09,580 1,009. 747 00:37:09,579 --> 00:37:13,170 And then when I do count one again, count one's sharing that 1,009, and that's 748 00:37:13,170 --> 00:37:16,690 why we get a 1,010 through a 1,014. 749 00:37:16,690 --> 00:37:21,309 Any questions about that? Um hm. 750 00:37:21,309 --> 00:37:22,869 [Inaudible.] Oh, yeah, a microphone would be good. 751 00:37:22,869 --> 00:37:25,689 And let me guess what your question is. If you switch the order, 752 00:37:25,688 --> 00:37:28,178 yes, you would actually start at zero - or you'd start at one. 753 00:37:28,179 --> 00:37:31,639 The first constructor would initialize the value at 754 00:37:31,639 --> 00:37:33,519 a 1,000; 755 00:37:33,519 --> 00:37:38,079 the second constructor would overwrite the 1,000 with one and then you'd count from one. 756 00:37:38,079 --> 00:37:42,179 Every once in a while I have this fleeting ten-second ability to 757 00:37:42,179 --> 00:37:44,699 read minds, and it goes away very quickly. 758 00:37:44,699 --> 00:37:47,380 759 00:37:47,380 --> 00:37:50,660 So with that said, it's time for something completely different. So any 760 00:37:50,659 --> 00:37:57,659 questions about instance variables versus class variables? Um hm. [Inaudible.] 761 00:38:11,858 --> 00:38:15,558 Maybe we should talk about that offline. Because it'd be good to know what particular 762 00:38:15,559 --> 00:38:16,740 thing you're referring to. 763 00:38:16,739 --> 00:38:20,208 But most of the time, for a lot of intents and purposes in this class, 764 00:38:20,208 --> 00:38:23,788 the main time you'll actually see static variables are when they're actually constants. There would 765 00:38:23,789 --> 00:38:26,989 be very few times when you actually have a static variable that's not a 766 00:38:26,989 --> 00:38:28,500 final variable, it's not a constant. 767 00:38:28,500 --> 00:38:32,539 But if you're interested, we could certainly talk about that offline. 768 00:38:32,539 --> 00:38:35,379 So with that said, there's a funky concept 769 00:38:35,378 --> 00:38:36,949 called "Javadoc." 770 00:38:36,949 --> 00:38:40,449 And now you're old enough to learn about Javadoc. And 771 00:38:40,449 --> 00:38:45,219 all Javadoc really is, is it's basically Java's documentation system. This is just a 772 00:38:45,219 --> 00:38:48,168 little side point. And the reason why I'm giving this aside to you is you're gonna 773 00:38:48,168 --> 00:38:51,958 see Javadoc in just a second. We're gonna write another class using Javadoc. 774 00:38:51,958 --> 00:38:57,029 The difference between regular comments and Javadoc. comments is very simple. 775 00:38:57,030 --> 00:39:01,390 Javadoc comments start with a slash and two stars instead of just one star, 776 00:39:01,389 --> 00:39:04,028 but they still end with a star and a single slash. 777 00:39:04,028 --> 00:39:08,349 So comments that are in that form, that start with a slash and two stars, are called 778 00:39:08,349 --> 00:39:09,969 Javadoc comments. 779 00:39:09,969 --> 00:39:12,670 And you'll see what that means in just a second. 780 00:39:12,670 --> 00:39:16,539 In the comments you can put special tags that are understood by 781 00:39:16,539 --> 00:39:19,980 the Javadoc system. This is just the system that's gonna generate html 782 00:39:19,980 --> 00:39:20,639 pages 783 00:39:20,639 --> 00:39:24,869 that explain what your code does via its comment. So your comments actually serve a 784 00:39:24,869 --> 00:39:25,768 purpose now. 785 00:39:25,768 --> 00:39:29,509 Your comments are going to beautifully get transformed into html 786 00:39:29,510 --> 00:39:32,329 that someone else can look at when they're trying to understand what 787 00:39:32,329 --> 00:39:35,039 your class does. Instead of having to go through code, they can just look at a 788 00:39:35,039 --> 00:39:35,998 webpage. 789 00:39:35,998 --> 00:39:39,678 So there's some special tags, like @ param or @ results, 790 00:39:39,679 --> 00:39:43,389 that specify what is a parameter of the function you're writing or the 791 00:39:43,389 --> 00:39:46,749 results if the function's actually returning something. And I'll show you examples of 792 00:39:46,748 --> 00:39:49,808 that right now. 793 00:39:49,809 --> 00:39:53,150 So here is a method, and this method might be some method we have that just 794 00:39:53,150 --> 00:39:56,939 sets the number of units someone earns. So it's called "set units." It takes in some 795 00:39:56,938 --> 00:39:59,728 number of units as a double, and let's say we have some instance variables, 796 00:39:59,728 --> 00:40:02,338 some are units earned, which is like how many units you've earned at college so 797 00:40:02,338 --> 00:40:02,748 far, 798 00:40:02,748 --> 00:40:04,488 which just gets set units. 799 00:40:04,489 --> 00:40:08,568 This comment here is a Javadoc comments. It starts with slash, two 800 00:40:08,568 --> 00:40:11,509 stars, and with a star slash. 801 00:40:11,509 --> 00:40:14,559 Inside the comment, we still comment what the function does. 802 00:40:14,559 --> 00:40:18,630 Just like a normal comment, we specify whatever we want to explain to the user. 803 00:40:18,630 --> 00:40:22,450 Additionally, we can have some special tags, like @ param. And the way @ param 804 00:40:22,449 --> 00:40:23,129 works is, 805 00:40:23,130 --> 00:40:26,390 for every one of your parameters you specify @ param. 806 00:40:26,389 --> 00:40:30,228 Then you specify the name of the parameter, so that's why the words look kind of 807 00:40:30,228 --> 00:40:33,838 funky, because the name of the parameter is units, that's the next token here, or the 808 00:40:33,838 --> 00:40:34,679 next 809 00:40:34,679 --> 00:40:36,139 word is basically units, 810 00:40:36,139 --> 00:40:40,668 and then what that parameter actually means, the number of units earned. So for human 811 00:40:40,668 --> 00:40:43,228 reading, and it's not necessarily the most legible thing in the world, but after 812 00:40:43,228 --> 00:40:45,418 you've seen it for a while you just kind of internalize it, 813 00:40:45,418 --> 00:40:49,179 but the computer can actually understand that very well and produce some nice 814 00:40:49,179 --> 00:40:49,858 documentation. 815 00:40:49,858 --> 00:40:53,958 So let me show you an example of this. It turn out, if you're actually interested, 816 00:40:53,958 --> 00:40:57,389 all of the ACM libraries, all the libraries that we're using in 817 00:40:57,389 --> 00:41:01,248 this class, if you want to dig into them, if you go to this 818 00:41:01,248 --> 00:41:06,558 URL, jtf.acm.org/javadoc/student, because 819 00:41:06,559 --> 00:41:09,969 it's the student version of the Javadoc, 820 00:41:09,969 --> 00:41:13,519 you can actually see the Javadoc for all of the ACM libraries. 821 00:41:13,518 --> 00:41:16,358 So you, until your heart's content, look them over. 822 00:41:16,358 --> 00:41:20,018 But let's just look at one particular example. Remember random generator that we sort of 823 00:41:20,018 --> 00:41:23,169 looked at together from the ACM libraries, let me just show you the Javadoc 824 00:41:23,170 --> 00:41:28,170 for that so you get a sense for what's actually going on. So 825 00:41:28,170 --> 00:41:32,650 all Javadoc is, is it's html. It brings up a web browser. And what it 826 00:41:32,650 --> 00:41:35,289 explains in here is here is class random generator. 827 00:41:35,289 --> 00:41:38,039 This is stuff that got automatically generated based on the 828 00:41:38,039 --> 00:41:38,919 comments and the code. 829 00:41:38,918 --> 00:41:42,568 It says, ACM util random generator is actually derived from java 830 00:41:42,568 --> 00:41:46,338 util random, so it's actually a subclass of java util random. 831 00:41:46,338 --> 00:41:47,679 And it tells us 832 00:41:47,679 --> 00:41:51,539 that by saying that actually this public class random generator extends random. So it's 833 00:41:51,539 --> 00:41:53,199 in fact a subclass. 834 00:41:53,199 --> 00:41:57,949 And there's a whole bunch of stuff there that explains how this class actually works, and how you get 835 00:41:57,949 --> 00:42:00,858 random generator.get instance to get a version of it. 836 00:42:00,858 --> 00:42:04,778 But also what's generated for you, very nicely in these nice html tables, are 837 00:42:04,778 --> 00:42:05,570 things like, 838 00:42:05,570 --> 00:42:07,680 "Hey, here's the constructor." 839 00:42:07,679 --> 00:42:10,969 Constructor has no arguments, that's how you might potentially create a new random 840 00:42:10,969 --> 00:42:14,358 generator if you wanted to. But, in fact, most of the time what you want to do is get 841 00:42:14,358 --> 00:42:14,949 instance. 842 00:42:14,949 --> 00:42:18,318 So for all of the methods it has the name of the method, 843 00:42:18,318 --> 00:42:19,288 it has, 844 00:42:19,289 --> 00:42:22,859 basically, the form of the method, which is the method and then whatever 845 00:42:22,858 --> 00:42:25,599 parameters it takes, and then whatever comments you have 846 00:42:25,599 --> 00:42:27,398 about that method. 847 00:42:27,398 --> 00:42:29,618 The other interesting thing about this 848 00:42:29,619 --> 00:42:32,298 is, 849 00:42:32,298 --> 00:42:34,699 it tells you which methods are kind of built in 850 00:42:34,699 --> 00:42:37,458 and which methods actually got 851 00:42:37,458 --> 00:42:41,418 inherited from your super class. So we mentioned that a random generator is 852 00:42:41,418 --> 00:42:43,400 an object that extends random. 853 00:42:43,400 --> 00:42:47,059 Well, some of the methods are actually defined in random generator specifically; 854 00:42:47,059 --> 00:42:50,119 some of the methods are inherited from this thing called random. It doesn't 855 00:42:50,119 --> 00:42:52,248 actually make a difference which ones are inherited or not, 856 00:42:52,248 --> 00:42:55,748 but you get kind of all this text. You get even more text down here. So if you want 857 00:42:55,748 --> 00:42:58,730 to look at the excruciating details of next boolean, 858 00:42:58,730 --> 00:43:00,659 that takes in some double p, 859 00:43:00,659 --> 00:43:04,190 it tells us what the usage is. How do you use next boolean and what are its 860 00:43:04,190 --> 00:43:07,250 parameters. This is where those @ param little tag 861 00:43:07,250 --> 00:43:08,230 that was in 862 00:43:08,230 --> 00:43:12,250 there, there's an @ param tag that says p, and then has this comment, and this automatically 863 00:43:12,250 --> 00:43:14,568 gets turned into nice html. 864 00:43:14,568 --> 00:43:18,259 So anytime you have questions about what something in the ACM library does or 865 00:43:18,259 --> 00:43:21,438 what methods are actually available to you for a particular object, you can 866 00:43:21,438 --> 00:43:24,879 actually go look at the Javadoc. You don't need to scrounge through code, it's 867 00:43:24,880 --> 00:43:28,019 actually just html. You can browse in a web browser by going to that URL 868 00:43:28,018 --> 00:43:30,788 I gave you and search around. But 869 00:43:30,789 --> 00:43:34,780 the reason for showing you all of this, one, is to let you know that it actually exists, 870 00:43:34,780 --> 00:43:37,420 the second is we can now put this all together. 871 00:43:37,420 --> 00:43:42,010 We can create a class that has a bunch of stuff in it that we comment using Javadoc, and 872 00:43:42,010 --> 00:43:46,459 that also makes use of Strings, and gives you one nice big picture at the end. 873 00:43:46,458 --> 00:43:50,598 So what I'm gonna do is I'm gonna create a class. 874 00:43:50,599 --> 00:43:53,730 This class basically is involving a student, 875 00:43:53,730 --> 00:44:00,730 something that hopefully you're all extremely familiar with at this point. 876 00:44:06,108 --> 00:44:09,940 So what a student class is going to provide, it's gonna have Javadoc 877 00:44:09,940 --> 00:44:15,259 comment. So up at the top I have the star, the slash star star, that wasn't a typo, 878 00:44:15,259 --> 00:44:19,298 that's actually a Javadoc comments, and explains what this whole class does. So it 879 00:44:19,298 --> 00:44:22,360 says, what I'm gonna do is create a class that keeps track of the student. 880 00:44:22,360 --> 00:44:25,870 What is information I care about for a student? Well, this is a very simplified form. I care 881 00:44:25,869 --> 00:44:27,389 about a name of a student, 882 00:44:27,389 --> 00:44:31,358 their ID No., and how many units they've earned. That's all I care about for right 883 00:44:31,358 --> 00:44:34,558 now. The book has an example that's a little bit more involved, but I'm gonna give 884 00:44:34,559 --> 00:44:36,359 you a real simple example here. 885 00:44:36,358 --> 00:44:39,498 So what are my constructors? How do I create something as a student? Notice 886 00:44:39,498 --> 00:44:41,679 the class student does not extend anything. 887 00:44:41,679 --> 00:44:45,210 So all students are just objects, I'm not extending anything else, 888 00:44:45,210 --> 00:44:46,849 they're just objects. 889 00:44:46,849 --> 00:44:50,818 One of my constructors for a student says specify the name and ID for the 890 00:44:50,818 --> 00:44:53,489 student. As a matter of fact, that's gonna be my only constructor, I'm not gonna allow 891 00:44:53,489 --> 00:44:56,438 someone to do other kinds of things. Give me a name, which is a String, right, it's 892 00:44:56,438 --> 00:44:58,000 just a piece of text, and 893 00:44:58,000 --> 00:45:01,760 an integer, which is an ID No. So we'll assume all ID No.'s are 894 00:45:01,760 --> 00:45:02,719 integers. 895 00:45:02,719 --> 00:45:06,659 And what it's gonna do is keep track of some information about students 896 00:45:06,659 --> 00:45:11,199 that are going to be stored as instance variables. Because every student is going to 897 00:45:11,199 --> 00:45:13,650 have their own information for their name, 898 00:45:13,650 --> 00:45:16,099 which is just student name that's a String, 899 00:45:16,099 --> 00:45:18,960 student ID, which is just an integer, and the number of units they've earned so 900 00:45:18,960 --> 00:45:22,260 far, which I'll keep as a double because there are some schools that actually allow 901 00:45:22,260 --> 00:45:22,940 902 00:45:22,940 --> 00:45:25,670 half units for whatever classes. They don't here, but 903 00:45:25,670 --> 00:45:28,829 other places they actually do. So we're just making it a double for generality as opposed to 904 00:45:28,829 --> 00:45:32,009 having it be an int. So these are all private instance variables. 905 00:45:32,009 --> 00:45:35,579 No one else outside of a student can get in there and muck with these things. 906 00:45:35,579 --> 00:45:38,740 The only way we can potentially change them is through the methods that are 907 00:45:38,739 --> 00:45:40,758 involved in this class. 908 00:45:40,759 --> 00:45:43,869 We're also gonna have the number of units that are required to graduate (at 909 00:45:43,869 --> 00:45:45,119 Stanford that's 180), 910 00:45:45,119 --> 00:45:48,699 and we're going to specify that as a constant that is static and final, 911 00:45:48,699 --> 00:45:52,039 which means all students share the same number of units to graduate. 912 00:45:52,039 --> 00:45:55,409 And, unfortunately for you, you can't say, "Hey, you know what, I'm just 913 00:45:55,409 --> 00:45:57,700 gonna set units to graduate to be like 914 00:45:57,699 --> 00:46:02,048 ten." Wouldn't that be fun? Like, you take 106a, you take IHUM and you're done. 915 00:46:02,048 --> 00:46:05,838 Final, yeah, you're not changing this, it's 180 until the cows come 916 00:46:05,838 --> 00:46:08,139 home. So that's why it's a constant 917 00:46:08,139 --> 00:46:12,518 as opposed to just being a class variable that's actually modifiable. 918 00:46:12,518 --> 00:46:15,908 So what are all the things we allow someone to do in here? 919 00:46:15,909 --> 00:46:19,079 The things we allow someone to do are, for example, to get the name of a 920 00:46:19,079 --> 00:46:23,459 student. Once I've created a student the student name variable is private. I 921 00:46:23,458 --> 00:46:26,728 can't actually refer to that variable outside of this class. 922 00:46:26,728 --> 00:46:29,289 So how do I ask you for your name 923 00:46:29,289 --> 00:46:32,009 after you've been created? So you were just created as a new student at 924 00:46:32,010 --> 00:46:32,460 Stanford. 925 00:46:32,460 --> 00:46:34,130 And, I'm like, "What's your name?" 926 00:46:34,130 --> 00:46:38,030 And I want to kind of come over and get a little close and be like, "Hey, can I touch your 927 00:46:38,030 --> 00:46:39,880 variables?" And you're, like, 928 00:46:39,880 --> 00:46:43,259 "No, man, this is wrong." 929 00:46:43,259 --> 00:46:46,398 So I'm, like, "Oh, what do you have that's 930 00:46:46,398 --> 00:46:48,449 public? Oh, get 931 00:46:48,449 --> 00:46:51,688 your name." What's your name? Aki. 932 00:46:51,688 --> 00:46:55,098 Aki. So I get back a String Aki from your object. 933 00:46:55,099 --> 00:46:58,778 I can't actually touch any of the private parts. 934 00:46:58,778 --> 00:47:00,438 935 00:47:00,438 --> 00:47:04,148 ID number, same kind of deal. I can't go in and see what your ID - like, I'm not gonna 936 00:47:04,148 --> 00:47:07,838 go into your wallet and pull out your ID card and be, like, "Oh, ID number, let me get out my magic 937 00:47:07,838 --> 00:47:09,398 marker and change it." 938 00:47:09,398 --> 00:47:12,528 The only way I can get it is by asking you for it, and if you've made that 939 00:47:12,528 --> 00:47:15,858 public then I can get that information from you by 940 00:47:15,858 --> 00:47:16,998 making the method public. 941 00:47:16,998 --> 00:47:20,018 A few other things. Now, here are some funky things. 942 00:47:20,018 --> 00:47:23,998 Set units I actually made to be public, which means I can allow someone, even 943 00:47:23,998 --> 00:47:25,398 though 944 00:47:25,398 --> 00:47:29,518 the number of units earned is a private variable, so I can't directly 945 00:47:29,518 --> 00:47:31,779 just go in and modify the variable directly, 946 00:47:31,780 --> 00:47:35,180 I can call set units and give it some number and it will just change the 947 00:47:35,179 --> 00:47:38,428 number of units you have. 948 00:47:38,429 --> 00:47:41,559 I can ask you for how many units you have. So this is a dangerous thing, if you 949 00:47:41,559 --> 00:47:43,819 think about it. And that's the kind of thing you want to think about, in terms of 950 00:47:43,818 --> 00:47:47,498 good programming style, what do you make public and what do you make private. And if red 951 00:47:47,498 --> 00:47:49,389 flags go off because you're making things public 952 00:47:49,389 --> 00:47:50,929 that shouldn't really be public, 953 00:47:50,929 --> 00:47:53,119 that's something you want to think about for style. 954 00:47:53,119 --> 00:47:54,079 Get units, 955 00:47:54,079 --> 00:47:57,269 this just returns how many units you've actually earned as a double. And, 956 00:47:57,268 --> 00:47:59,908 again, you can see we have a return 957 00:47:59,909 --> 00:48:03,469 tag here for the Javadoc that says the number of units the student has earned is 958 00:48:03,469 --> 00:48:05,608 the things we're actually returning. 959 00:48:05,608 --> 00:48:08,659 A couple other things real quickly, and then I'll show you that we're gonna make use 960 00:48:08,659 --> 00:48:11,858 of this class. 961 00:48:11,858 --> 00:48:14,818 There's something called "increment units," which basically just says, every time 962 00:48:14,818 --> 00:48:18,058 you take a class I want to increment the total number of units you have by the 963 00:48:18,059 --> 00:48:19,760 number of units in this class. So I 964 00:48:19,760 --> 00:48:22,839 pass in some value for additional units, and I increment your units earned by 965 00:48:22,838 --> 00:48:24,228 additional units. 966 00:48:24,228 --> 00:48:27,908 Having enough units, that's a predicate method, it returns a boolean. And 967 00:48:27,909 --> 00:48:30,959 what it basically returns is the number of units earned greater than or equal 968 00:48:30,958 --> 00:48:33,739 to this constant for how many units you need to graduate. 969 00:48:33,739 --> 00:48:36,358 And last, but not least, and this is a critical one, 970 00:48:36,358 --> 00:48:40,708 all classes you write should have a method in them called "two String." 971 00:48:40,708 --> 00:48:43,049 972 00:48:43,050 --> 00:48:45,310 What two String actually means 973 00:48:45,309 --> 00:48:51,228 is get me some textual version of what's the information in this class. So 974 00:48:51,228 --> 00:48:53,239 two String for a student 975 00:48:53,239 --> 00:48:58,249 returns back a String, which is the students name, plus their ID number in 976 00:48:58,248 --> 00:49:01,689 parenthesis. It does not necessarily specify how many units they've earned, that's fine. It 977 00:49:01,690 --> 00:49:04,940 doesn't need to include all the information that's actually in the object. 978 00:49:04,940 --> 00:49:08,510 What it just should specify in a nice textual way is the information that you 979 00:49:08,510 --> 00:49:12,789 care about displaying about an object if someone wants to display it. 980 00:49:12,789 --> 00:49:16,179 But all classes you write that are not programs. So something that 981 00:49:16,179 --> 00:49:19,499 extends Console program doesn't need this, any other classes should have a two 982 00:49:19,498 --> 00:49:20,948 String. And 983 00:49:20,949 --> 00:49:22,289 that's the whole class. 984 00:49:22,289 --> 00:49:26,679 So any questions about this class or should I just show you how we use it? 985 00:49:26,679 --> 00:49:29,369 Let me show you how we use it. Well, it's Stanford, you're at Stanford so we've 986 00:49:29,369 --> 00:49:32,390 gotta have a program called Stanford that actually creates students. 987 00:49:32,389 --> 00:49:36,168 So Stanford is a program that extends Console program. 988 00:49:36,168 --> 00:49:38,878 And, again, we have something to increase the font size. But we're gonna 989 00:49:38,878 --> 00:49:42,298 create some new student, 990 00:49:42,298 --> 00:49:43,360 Ben Newman. 991 00:49:43,360 --> 00:49:45,440 And that's pronounced 992 00:49:45,440 --> 00:49:48,219 993 00:49:48,219 --> 00:49:50,438 stud. 994 00:49:50,438 --> 00:49:54,228 So we have some student, stud, that's a new student. And remember we need to 995 00:49:54,228 --> 00:49:57,498 specify the name and ID number. to initialize that student. Ben Newman, your 996 00:49:57,498 --> 00:50:00,728 ID number's now 1,001. 997 00:50:00,728 --> 00:50:03,899 I'm gonna set your estimated number of units 998 00:50:03,900 --> 00:50:05,059 at 179. 999 00:50:05,059 --> 00:50:09,069 And then I want to write out how many units you have. I cannot refer to your 1000 00:50:09,068 --> 00:50:12,259 name and number of units directly, I need to ask 1001 00:50:12,259 --> 00:50:15,559 what's the object, that's the receiver of the message. And the message 1002 00:50:15,559 --> 00:50:18,639 I'm gonna send is the name of the method I just wrote, stud.get name, 1003 00:50:18,639 --> 00:50:21,450 that returns to me a String, which is the name that's Ben Newman. 1004 00:50:21,449 --> 00:50:22,848 I append to that has, 1005 00:50:22,849 --> 00:50:25,620 I append to that the number of units Ben has, 1006 00:50:25,619 --> 00:50:29,150 and then add the word units at the end of it and write that out to the screen. 1007 00:50:29,150 --> 00:50:32,579 Then I want to see if Ben can graduate. So, again, I print out Ben's name, 1008 00:50:32,579 --> 00:50:34,630 if he can graduate, his status 1009 00:50:34,630 --> 00:50:38,769 by calling "has enough units." That will return true or false, that will write true or 1010 00:50:38,769 --> 00:50:39,858 false out 1011 00:50:39,858 --> 00:50:42,598 onto the screen as part of the String. 1012 00:50:42,599 --> 00:50:46,039 Then Ben takes CS106a. So 1013 00:50:46,039 --> 00:50:49,409 Ben takes CS106a, is what's gonna get written out, and then I 1014 00:50:49,409 --> 00:50:52,929 increment Ben's units by five because he's now taking CS106a, a five 1015 00:50:52,929 --> 00:50:56,880 unit class. And then I ask again can he graduate. 1016 00:50:56,880 --> 00:51:01,048 And if he has enough units to graduate, I'll say, rock on, 1017 00:51:01,048 --> 00:51:06,489 and write out the String version of the object that I have by calling two String. 1018 00:51:06,489 --> 00:51:08,409 So when I actually run this 1019 00:51:08,409 --> 00:51:10,358 all here's what I get. 1020 00:51:10,358 --> 00:51:16,048 I'm 1021 00:51:16,048 --> 00:51:21,188 running. I'm running. I want to run Stanford. It's just that easy to run 1022 00:51:21,188 --> 00:51:25,278 1023 00:51:25,278 --> 00:51:27,268 Stanford. 1024 00:51:27,268 --> 00:51:30,668 So Ben Newman has 179.0 units, right, because it's a double so 1025 00:51:30,668 --> 00:51:32,608 it puts that .0 at the end of it. 1026 00:51:32,608 --> 00:51:36,119 Ben Newman can graduate. We get false back because he doesn't have enough units to 1027 00:51:36,119 --> 00:51:39,528 graduate. But that 179 is stored inside the object, 1028 00:51:39,528 --> 00:51:43,188 that stud object has the 179. Ben takes CS106a, so 1029 00:51:43,188 --> 00:51:47,168 we increment that object's number of units by five, by calling the appropriate 1030 00:51:47,168 --> 00:51:50,558 method, and then Ben Newman can graduate is now true because he's got 1031 00:51:50,559 --> 00:51:51,589 184 units. 1032 00:51:51,588 --> 00:51:53,860 And, finally, when we say, "Rock On," 1033 00:51:53,860 --> 00:51:58,010 and we call two String, what two String gives us back is the name 1034 00:51:58,010 --> 00:52:02,660 plus the ID number inside paren. So it says, "Rock On Ben Newman paren number 1035 00:52:02,659 --> 00:52:04,629 1001." 1036 00:52:04,630 --> 00:52:07,548 Any questions about that? 1037 00:52:07,548 --> 00:52:09,719 Alrighty. Then have a good weekend and I will see you on Monday.