2 00:00:12,849 --> 00:00:16,109 This presentation is delivered by the Stanford Center for Professional 3 00:00:16,109 --> 00:00:23,109 Development. 4 00:00:24,009 --> 00:00:27,499 Today we're actually gonna go over some of the graphic stuff you saw, talk a little 5 00:00:27,498 --> 00:00:31,628 bit more about objects and classes and get into variables and values and all kinds 6 00:00:31,629 --> 00:00:32,730 of goodies. 7 00:00:32,729 --> 00:00:34,839 Any questions? 8 00:00:34,840 --> 00:00:37,620 All righty, then let's just dive right in. So 9 00:00:37,619 --> 00:00:41,719 one of the main topics for today is this thing called a variable. 10 00:00:41,719 --> 00:00:46,439 And a variable, you know, like variables come up in mathematics and it's like, oh X and Y are 11 00:00:46,439 --> 00:00:49,369 variables, right and there are these things and we do all these manipulations on variables. In the 12 00:00:49,369 --> 00:00:50,349 computer science world, 13 00:00:50,350 --> 00:00:54,359 they're really friendly, right, and you don't have to worry about integration or 14 00:00:54,359 --> 00:00:56,600 differentiation or you know, those kind of variables. 15 00:00:56,600 --> 00:00:58,030 Variables are kind of your friend, 16 00:00:58,030 --> 00:01:01,308 and basically all the variable is in the computer science worlds 17 00:01:01,308 --> 00:01:03,009 is it's a box. 18 00:01:03,009 --> 00:01:04,959 It's a box where we stick stuff 19 00:01:04,959 --> 00:01:08,649 and the stuff we stick into that box can change. That's why we call it a variable 20 00:01:08,650 --> 00:01:11,159 because it's a box that has a variable contents, 21 00:01:11,159 --> 00:01:15,049 and you think back you know, in the days of yore in math, and you know, oh yeah, it's kind of like X 22 00:01:15,049 --> 00:01:18,140 can have different values, yeah, it's basically just like that. 23 00:01:18,140 --> 00:01:21,590 So in computer science, what we think of as a variable, is each variable has three 24 00:01:21,590 --> 00:01:22,980 things associated with it. 25 00:01:22,980 --> 00:01:25,079 It's got some name, 26 00:01:25,079 --> 00:01:27,808 and that's just how we refer to that particular box. 27 00:01:27,808 --> 00:01:29,239 It has a type, 28 00:01:29,239 --> 00:01:31,709 which is something a little bit different in mathematics, but the 29 00:01:31,709 --> 00:01:33,178 type basically says 30 00:01:33,179 --> 00:01:37,359 what kind of thing does this box store? Some boxes store 31 00:01:37,358 --> 00:01:41,318 numbers; some boxes store letters; some boxes will store other things, like little objects 32 00:01:41,319 --> 00:01:42,090 in the world. 33 00:01:42,090 --> 00:01:44,590 But a type is just what's stored in that box. 34 00:01:44,590 --> 00:01:48,240 And then there's a value, and the value, as you can imagine is just what's in the 35 00:01:48,239 --> 00:01:51,219 box. What is the actual thing that's in there, right? If it stores the number, then it might 36 00:01:51,219 --> 00:01:54,859 store, for example, the Value 3 and that's just the value, and it may have 37 00:01:54,859 --> 00:01:56,560 some name associated with it. 38 00:01:56,560 --> 00:02:00,150 And how do we actually name these? There's actually a rule and it's not a very 39 00:02:00,150 --> 00:02:04,030 complicated rule, but a very simple rule you need to remember for what are valid names 40 00:02:04,030 --> 00:02:07,599 for variables in Java. So a valid name, 41 00:02:07,599 --> 00:02:11,389 so this is how you actually name these puppies, has to start with a letter or 42 00:02:11,389 --> 00:02:16,919 an underscore. So it starts with a letter 43 00:02:16,919 --> 00:02:21,649 like one of the alphabetic letters and can be upper or lower case or the underscore 44 00:02:21,650 --> 00:02:22,579 character. Okay, 45 00:02:22,579 --> 00:02:24,599 and that's kind of you know, 46 00:02:24,598 --> 00:02:26,889 underscore. It's down at the bottom of the line, okay? 47 00:02:26,889 --> 00:02:30,459 And then after you have that initial letter or underscore, 48 00:02:30,460 --> 00:02:31,399 then 49 00:02:31,399 --> 00:02:37,878 after that, you can have any number of letters, numbers, that's like the number 50 00:02:37,878 --> 00:02:43,388 digits, like 1, 2, 3, 4, you know, 0, etc. or underscores, 51 00:02:43,389 --> 00:02:43,890 okay? 52 00:02:43,889 --> 00:02:47,129 So you can't start with a number. You have to start with a letter or underscore, but 53 00:02:47,129 --> 00:02:50,750 for most purposes in this class, just thing about them as letters. When not actually using 54 00:02:50,750 --> 00:02:53,330 underscores, you might occasionally use numbers. You can actually have numbers 55 00:02:53,330 --> 00:02:55,109 after the first letter. 56 00:02:55,109 --> 00:02:58,799 There is one slight caveat to this rule which you can't have any variables 57 00:02:58,799 --> 00:03:02,799 that's name is the same as some which known as a reserved word in Java, which 58 00:03:02,799 --> 00:03:06,170 means its name can't be the same as some special word in Java, like the word 59 00:03:06,169 --> 00:03:09,649 class is a special name in Java, and there's actually a page in your book, I think in 60 00:03:09,650 --> 00:03:11,489 Chapter 2, that lists all the special names. 61 00:03:11,489 --> 00:03:13,938 It's like out of the English language it has about 62 00:03:13,938 --> 00:03:15,078 127,000 words. 63 00:03:15,079 --> 00:03:18,989 I think there is like 40 in Java that you can't make a variable name. Okay, you have 64 00:03:18,989 --> 00:03:20,469 lots of other choices. 65 00:03:20,469 --> 00:03:22,990 As a matter of fact, there's lots of things that don't have to be valid words 66 00:03:22,990 --> 00:03:25,379 in English. They can just be any name that follows this rule. 67 00:03:25,378 --> 00:03:28,158 The important thing to think about in terms of a name and this is one of the 68 00:03:28,158 --> 00:03:31,658 good software engineering principles, is make your name descriptive. 69 00:03:31,658 --> 00:03:34,818 If you have a program that's maintaining for example, the balance in the bank 70 00:03:34,818 --> 00:03:38,759 account, a real good name for the place where you store the value of the balance 71 00:03:38,759 --> 00:03:40,759 would be something like "Balance." 72 00:03:40,759 --> 00:03:43,679 A real bad name would be something like "A," 73 00:03:43,680 --> 00:03:48,180 because no one knows what A is. It's like hey it's A - yeah, I know what A is. And someone says, "Yeah, A is balance," and 74 00:03:48,180 --> 00:03:50,390 someone says, "No, no, no, in my program, A 75 00:03:50,389 --> 00:03:55,759 is actually how many miles I bicycle today," and you're like, "No, no, no, man, A was balanced." 76 00:03:55,759 --> 00:03:58,688 Well, if it's called Balance, there's just no 77 00:03:58,688 --> 00:04:01,118 ambiguity, so give them descriptive names. 78 00:04:01,118 --> 00:04:02,899 So that's kind of a name part of this. 79 00:04:02,899 --> 00:04:06,139 The next thing is what is this type all about. What are the different types that you 80 00:04:06,139 --> 00:04:10,620 can actually have. And there are some things that we refer to as primitive types. 81 00:04:10,620 --> 00:04:13,340 These are the types that have the smaller developed brains and use 82 00:04:13,340 --> 00:04:14,938 knuckles drag on the ground. 83 00:04:14,938 --> 00:04:18,538 No, there just the types that are built into Java, okay and some of the basic 84 00:04:18,538 --> 00:04:20,699 types we have is something we call an INT, 85 00:04:20,699 --> 00:04:22,009 which is short for 86 00:04:22,009 --> 00:04:23,340 an integer, 87 00:04:23,339 --> 00:04:27,888 but we actually write INT, so INT, I-N-T, is the name of the type, okay. 88 00:04:27,889 --> 00:04:30,978 And this is just an integer of value. It's just gonna store some whole number 89 00:04:30,978 --> 00:04:32,859 basically. It stores a number between 90 00:04:32,860 --> 00:04:35,500 minus 2 billion and plus 2 billion, 91 00:04:35,500 --> 00:04:38,550 but for all intents and purposes, you can just imagine you can store any 92 00:04:38,550 --> 00:04:40,119 integer in there, okay? 93 00:04:40,119 --> 00:04:45,759 There's also besides integers what other kinds of numeric data do we have? 94 00:04:45,759 --> 00:04:47,009 People already know, it's like 95 00:04:47,009 --> 00:04:49,899 I would think it would be like real values, but a bunch of people are already 96 00:04:49,899 --> 00:04:53,388 saying double because you've read ahead and you've done the assignment the way you should. And that was kind 97 00:04:53,389 --> 00:04:54,829 of a mini social, but both went 98 00:04:54,829 --> 00:04:58,788 to the same person. There's this thing called a double 99 00:04:58,788 --> 00:05:02,738 and a double is actually some real valued 100 00:05:02,738 --> 00:05:03,878 numeric 101 00:05:03,879 --> 00:05:07,489 value, right. It's something like 2.3 is a double or even 2.0 102 00:05:07,488 --> 00:05:09,108 can be a double, okay? 103 00:05:09,108 --> 00:05:16,108 Why is this thing called a double - anyone know, as opposed to like a real? Uh-huh? 104 00:05:16,899 --> 00:05:20,079 [Inaudible]. Yeah, there's this wonderful verb people call the I triple E, which is 105 00:05:20,079 --> 00:05:22,490 like the Institute for 106 00:05:22,490 --> 00:05:24,408 Electrical and Electronic Engineers and 107 00:05:24,408 --> 00:05:27,069 they come up with all these standards for things. Is anyone a member of I Triple 108 00:05:27,069 --> 00:05:29,919 E here? 109 00:05:29,918 --> 00:05:33,008 No one? Oh, man, join and pay your dues. It's a good time. 110 00:05:33,009 --> 00:05:35,389 I'm not actually a member myself. 111 00:05:35,389 --> 00:05:38,228 But what those folks actually do is they come up with standards for things and 112 00:05:38,228 --> 00:05:41,259 one of the standards they came up with is how you represent 113 00:05:41,259 --> 00:05:44,170 numbers that are real valued numbers inside a computer, right, because remember a 114 00:05:44,170 --> 00:05:48,169 computer only understands ones and zeros, so how do you actually represent a real valued number. 115 00:05:48,168 --> 00:05:49,750 And so there's a standard, 116 00:05:49,750 --> 00:05:53,180 and part of that standard has to do with a precision of the number, how many 117 00:05:53,180 --> 00:05:56,478 digits in some sense and a rough approximation you store and double stands 118 00:05:56,478 --> 00:05:59,240 for a double precision real number. 119 00:05:59,240 --> 00:06:02,189 And so for the purpose of this class, all real valued numbers that we're gonna 120 00:06:02,189 --> 00:06:04,439 use are just of type double. Okay, 121 00:06:04,439 --> 00:06:07,409 there's a couple of other types that I'll just mention now and we'll actually go into them in much 122 00:06:07,408 --> 00:06:10,908 more detail in a couple of future classes. One is called Boolean. 123 00:06:10,908 --> 00:06:15,490 And if you've ever heard of Boolean logic, this is a logical value, so this is 124 00:06:15,490 --> 00:06:19,848 just essentially true or false, and we'll talk about that in excruciating 125 00:06:19,848 --> 00:06:23,219 detail next time, but I'll just let you know that there's a type called Boolean. 126 00:06:23,220 --> 00:06:26,690 There's also a type called care or Car 127 00:06:26,689 --> 00:06:30,079 and as you can imagine it's because we like sort of the first syllable of most 128 00:06:30,079 --> 00:06:32,769 things and this is the first syllable of character, 129 00:06:32,769 --> 00:06:36,718 okay, and so we'll also talk about this in a couple of weeks time when we 130 00:06:36,718 --> 00:06:39,519 actually get into a some things with characters, but that's just a character. 131 00:06:39,519 --> 00:06:40,849 It's a variable; it's a box, but 132 00:06:40,850 --> 00:06:44,439 still is a character. It's a box that stores an integer, box that stores a real value, 133 00:06:44,439 --> 00:06:47,779 box that stores true or false and a box that stores a character. Those would be the different 134 00:06:47,779 --> 00:06:48,798 types of them. 135 00:06:48,798 --> 00:06:52,829 So one thing people - so that's kind of types, at least some of the basic types. 136 00:06:52,829 --> 00:06:55,778 And then when you think about value that we actually store in this box, people always 137 00:06:55,778 --> 00:06:59,618 get uncomfortable when they see INTs and they see double, right, and they sort 138 00:06:59,619 --> 00:07:02,870 of say, "But Meron, like 2 is an integer, right?" And I'm like, "Yeah." 139 00:07:02,870 --> 00:07:03,750 And they're like, 140 00:07:03,750 --> 00:07:07,720 "But you just told me 2.0 is a double, right?" And I'm like, "Yeah, 2.0 is a 141 00:07:07,720 --> 00:07:08,409 double." 142 00:07:08,408 --> 00:07:09,930 And so the natural question is 143 00:07:09,930 --> 00:07:13,620 why? Why do we have both these things? How come like all integers aren't subsumed 144 00:07:13,620 --> 00:07:17,149 by the real values, right, you're kind of the mathematician type and you're like, "Yeah, there's like 145 00:07:17,149 --> 00:07:19,168 strictly more of these than there are of these. 146 00:07:19,168 --> 00:07:23,359 So why are all these like subsumed in here. Why do we have this integer type," okay? 147 00:07:23,360 --> 00:07:26,449 And the reason we do boils down to a simple question and the simple 148 00:07:26,449 --> 00:07:28,040 question you want to ask yourself 149 00:07:28,040 --> 00:07:31,360 is how much versus how many, okay? 150 00:07:31,360 --> 00:07:33,240 So if you ask someone, 151 00:07:33,240 --> 00:07:34,559 let's say you, 152 00:07:34,559 --> 00:07:40,330 ask me, just so I won't embarrass you, "How much do you weigh, Meron?" How much do you weigh, Meron?" 153 00:07:40,329 --> 00:07:42,128 I weigh about, you know, 154 00:07:42,129 --> 00:07:45,129 155.632 pounds, okay? 155 00:07:45,129 --> 00:07:47,879 That makes perfect sense, right. If I can put a decimal point, I can put as 156 00:07:47,879 --> 00:07:51,399 many numbers after it, or I could just say 156, right and 157 00:07:51,399 --> 00:07:54,478 those are both valid kinds of things. Now 158 00:07:54,478 --> 00:07:55,680 you could ask me, 159 00:07:55,680 --> 00:07:56,579 "Hey Meron, 160 00:07:56,579 --> 00:08:00,168 how much children do you have?" 161 00:08:00,168 --> 00:08:01,430 I have 162 00:08:01,430 --> 00:08:02,478 2.3 children. 163 00:08:02,478 --> 00:08:06,860 Does that make any sense to you? Yeah, it's like we had three until that grisly 164 00:08:06,860 --> 00:08:09,088 accident. No, 165 00:08:09,088 --> 00:08:12,548 it's just that there's sometimes - I know that's horrible to say. We actually have one, 166 00:08:12,548 --> 00:08:16,589 and he's just fine. 167 00:08:16,589 --> 00:08:19,619 There's sometimes in the world when you care about counting, and when you care 168 00:08:19,620 --> 00:08:23,590 about counting, it doesn't make sense to have fractional values. Those things are integers. They're a 169 00:08:23,589 --> 00:08:25,609 how many kind of value. 170 00:08:25,610 --> 00:08:27,399 When you're thinking about how much, 171 00:08:27,399 --> 00:08:30,338 that's a double value, and you actually want to keep these distinct because if I 172 00:08:30,338 --> 00:08:34,950 ask you what's the next number after 1, you say - 2. 173 00:08:34,950 --> 00:08:35,810 2 174 00:08:35,809 --> 00:08:38,609 because you're thinking integer, and that's perfectly right and if I say 175 00:08:38,610 --> 00:08:40,950 what's the next number after 1.0, 176 00:08:40,950 --> 00:08:43,920 you say - [Inaudible]. Yeah, 177 00:08:43,919 --> 00:08:48,199 you start mumbling and you're like, "Well, I know it's not 2.0. That would have been the obvious answer and that's 178 00:08:48,200 --> 00:08:49,110 probably not it. 179 00:08:49,110 --> 00:08:53,710 How about 1.0000001, and I'm like, "No you missed a couple of zeros in there. Just keep 180 00:08:53,710 --> 00:08:54,790 going. 181 00:08:54,789 --> 00:08:58,469 Wait until the end of the quarter and put that 1 and come back and talk to me." 182 00:08:58,470 --> 00:09:00,990 In real values, there is no next value, 183 00:09:00,990 --> 00:09:04,019 okay, so when you care about counting, it doesn't make sense to use a double. That's 184 00:09:04,019 --> 00:09:06,759 when you want to use an integer, so having these things be a distinct type 185 00:09:06,759 --> 00:09:07,708 actually make sense 186 00:09:07,708 --> 00:09:10,469 and you'll kind of see that as we go along, okay. 187 00:09:10,470 --> 00:09:13,710 So the value is basically just what you're actually gonna put into this box over 188 00:09:13,710 --> 00:09:16,420 here and it's gonna be some of the values that we actually talked about, 189 00:09:16,419 --> 00:09:21,139 so let me show you some examples of how we might use variables, what the 190 00:09:21,139 --> 00:09:24,210 syntax for variables actually is in Java. 191 00:09:24,210 --> 00:09:27,540 So what we need to do with variables before we use them in Java is we need to 192 00:09:27,539 --> 00:09:30,129 declare a variable. It's kind of like declaring your love. If you're gonna 193 00:09:30,129 --> 00:09:32,919 have some box somewhere that's got something stored in it, you need to come tell the 194 00:09:32,919 --> 00:09:34,649 world, "Hey 195 00:09:34,649 --> 00:09:36,850 world, I've got this thing called X. It's cool. 196 00:09:36,850 --> 00:09:38,320 Check it out." 197 00:09:38,320 --> 00:09:41,260 Here's how you do that in Java. You say 198 00:09:41,259 --> 00:09:44,029 INT X, okay? 199 00:09:44,029 --> 00:09:48,230 What that does is you specify a type for your variable, what's the box gonna 200 00:09:48,230 --> 00:09:49,170 hold. 201 00:09:49,169 --> 00:09:52,719 You specify a name for that variable. In this case, the name is X 202 00:09:52,720 --> 00:09:56,540 and because all statements end with a semi colon, just like you saw in Karel's 203 00:09:56,539 --> 00:09:57,909 world, we put a semi colon. 204 00:09:57,909 --> 00:09:59,059 What does this do? 205 00:09:59,059 --> 00:10:02,528 It creates a box somewhere in the computer's memory 206 00:10:02,528 --> 00:10:05,679 that's name is X that's gonna store an integer in it. 207 00:10:05,679 --> 00:10:09,719 Right now, what does it store? [Inaudible]. Nothing. 208 00:10:09,720 --> 00:10:12,069 We haven't told it anything to store in particular. 209 00:10:12,068 --> 00:10:14,849 And so in Java's world, unless you initialize you always want to think 210 00:10:14,850 --> 00:10:18,389 about initializing a variable, giving it some initial value 211 00:10:18,389 --> 00:10:21,519 to start off with and there's some rules that we'll talk about as we go along 212 00:10:21,519 --> 00:10:25,129 regarding when Java will automatically sort of give you a zero value in there and there's 213 00:10:25,129 --> 00:10:27,909 time when it doesn't and sometimes it can get a little bit confusing. So the 214 00:10:27,909 --> 00:10:31,620 general rule you want to remember and it's also good software engineering is give variables 215 00:10:31,620 --> 00:10:34,299 an initial value when it makes sense, okay? 216 00:10:34,299 --> 00:10:37,809 So how might we give it an initial value? Well, on that same line, rather than just putting 217 00:10:37,809 --> 00:10:41,019 the semi colon there, we could say X equals 3 218 00:10:41,019 --> 00:10:44,568 and what that does is it gives it an initial value of 3 to x. 219 00:10:44,568 --> 00:10:47,039 Okay, so you could actually have the 220 00:10:47,039 --> 00:10:49,360 declaration of the variable without the initial value, but you always need to 221 00:10:49,360 --> 00:10:52,180 give it an initial value before you use it, so a lot of times we just give it the 222 00:10:52,179 --> 00:10:54,559 initial value when we actually declare it, okay. 223 00:10:54,559 --> 00:10:57,139 If we want it to have something of type double, maybe something called 224 00:10:57,139 --> 00:11:01,689 Y, we could have something called Double Y equals 5.2 and what that means is 225 00:11:01,690 --> 00:11:04,980 somewhere in memory we have this thing called Y, which has value 5.2 in 226 00:11:04,980 --> 00:11:08,310 it, and it's just a real value. 227 00:11:08,309 --> 00:11:11,339 So the general form for how you do these declarations 228 00:11:11,340 --> 00:11:14,759 is you say type and I'll put little squiggles underneath these just to let you know 229 00:11:14,759 --> 00:11:16,759 that this is the general form, 230 00:11:16,759 --> 00:11:18,889 the name 231 00:11:18,889 --> 00:11:24,340 and then potentially an equal and some 232 00:11:24,340 --> 00:11:28,090 initial value, followed by the semi colon, and that's how you do the 233 00:11:28,090 --> 00:11:30,310 declaration. And so the question that comes up 234 00:11:30,309 --> 00:11:33,649 where do you actually make these declarations and the place you generally 235 00:11:33,649 --> 00:11:37,220 at least for the time being make the declarations of value, 236 00:11:37,220 --> 00:11:40,420 for declarations of variables is inside methods. 237 00:11:40,419 --> 00:11:45,529 So when you create some method like you have public, 238 00:11:45,529 --> 00:11:46,759 void, 239 00:11:46,759 --> 00:11:48,450 run 240 00:11:48,450 --> 00:11:51,569 and you're gonna put everything that's gonna go inside run in there, 241 00:11:51,568 --> 00:11:54,899 you could, for example, have this line in there and this would create X with a 242 00:11:54,899 --> 00:11:58,448 value of 3 and Y with a value of 5.2 and you wouldn't have the 243 00:11:58,448 --> 00:12:02,278 general form there, but for example, you could have those declarations inside 244 00:12:02,278 --> 00:12:06,879 run and what that means is you have those variables created with some values 245 00:12:06,879 --> 00:12:09,340 available to you inside this method called run. 246 00:12:09,340 --> 00:12:12,830 They're only available to you inside that method called run. They're not available 247 00:12:12,830 --> 00:12:16,800 to you in some other method and we'll talk about how you get values passed around 248 00:12:16,799 --> 00:12:20,490 between different methods in a few days, but for the time being when you 249 00:12:20,490 --> 00:12:23,769 have your variables they're only available in that method. Okay, 250 00:12:23,769 --> 00:12:25,759 so any questions about that - 251 00:12:25,759 --> 00:12:32,759 variables or values? Uh-huh? If you have them inside the public run method [inaudible], will that method be able to take that variable? 252 00:12:36,480 --> 00:12:39,970 Well, you won't be defining another method inside run, right; you'll be 253 00:12:39,970 --> 00:12:42,990 defining another method that's separate from run. It's just a separate method. It won't be 254 00:12:42,990 --> 00:12:46,409 available in that separate method. It doesn't matter if it's public or private. 255 00:12:46,409 --> 00:12:50,949 It just not available there. So this public or private thing that you have here doesn't 256 00:12:50,950 --> 00:12:54,240 affect the visibility of your variables. Your variables are always only visible 257 00:12:54,240 --> 00:12:57,240 inside the method in which they're declared and set up, okay? 258 00:12:57,240 --> 00:12:59,448 Now, one other thing that we did when we sort of, 259 00:12:59,448 --> 00:13:02,668 you know, did this equals thing over here, if we wanted to, we could have actually 260 00:13:02,668 --> 00:13:04,559 done this in two lines 261 00:13:04,559 --> 00:13:08,669 rather than setting up X initial value there, we could have said into X and then we 262 00:13:08,669 --> 00:13:12,849 could have set X equals 3 over here and gotten the same effect. 263 00:13:12,850 --> 00:13:17,570 And when we say X equals 3, this equals is not equals in mathematics; it's 264 00:13:17,570 --> 00:13:21,930 actually what's known as an assignment statement in Java or in most programming 265 00:13:21,929 --> 00:13:22,669 languages. 266 00:13:22,669 --> 00:13:26,469 And the idea is we're taking some value over here. That's what's on the 267 00:13:26,470 --> 00:13:30,009 right-hand side of the equals and assigning it to whatever variable is 268 00:13:30,009 --> 00:13:32,419 listed over on the left-hand side of the equals, 269 00:13:32,419 --> 00:13:34,368 so we're assigning that value, okay? 270 00:13:34,369 --> 00:13:37,830 And so the general form for an assignment, 271 00:13:37,830 --> 00:13:40,770 let's just do that over here, 272 00:13:40,769 --> 00:13:43,919 is pretty straightforward, you just saw it, 273 00:13:43,919 --> 00:13:49,379 is variable and this is the name of the variable. So I'll squiggle - equals 274 00:13:49,379 --> 00:13:53,049 some value or some expression for a value and we'll talk about expressions 275 00:13:53,049 --> 00:13:56,399 in just a second with a semi colon at the end of it, and that's how you assign a value. 276 00:13:56,399 --> 00:14:00,279 You can assign values multiple times to a variable, to a variable. It's a variable, the contents change. So you 277 00:14:00,279 --> 00:14:04,809 say, "Hey, X equals ," and then down here somewhere, "Hey X equals 4," 278 00:14:04,809 --> 00:14:08,679 and when you get to this line up until you get to that line from here to here, X will 279 00:14:08,679 --> 00:14:11,878 have had the value 3 and when it gets to this line, it'll stick a 280 00:14:11,879 --> 00:14:13,058 4 in the box. 281 00:14:13,058 --> 00:14:16,129 And that's perfectly fine because X is a variable. As long as everything you stick in 282 00:14:16,129 --> 00:14:20,399 there is of the type of which X is declared, you're okay, okay? 283 00:14:20,399 --> 00:14:21,879 So what that also means 284 00:14:21,879 --> 00:14:25,289 is when you're doing Assignment you can do things that are perfectly fine in 285 00:14:25,289 --> 00:14:28,639 programming, but to mathematicians, they just go nuts; they go crazy like 286 00:14:28,639 --> 00:14:31,299 veins just burst in your head and you hear these popping noises and 287 00:14:31,299 --> 00:14:32,719 people dying in the streets, 288 00:14:32,720 --> 00:14:36,360 which is you can say, "Hey, I have some variable called total. Let me create some 289 00:14:36,360 --> 00:14:39,789 variable in its total," which I'll give some initial value like 10 and that's a good 290 00:14:39,789 --> 00:14:40,799 time. 291 00:14:40,799 --> 00:14:47,189 And then I say, "Hey total equals total plus 1." And if 292 00:14:47,190 --> 00:14:49,690 you're a mathematician, you look at that and say, 293 00:14:49,690 --> 00:14:51,600 "No, man. 294 00:14:51,600 --> 00:14:55,590 Total can't equal total plus 1. That's just not right." And then you got this 295 00:14:55,590 --> 00:14:58,940 whole philosophical question about, you know, if this is like total is equal to 296 00:14:58,940 --> 00:15:01,890 infinity and you're just like, "No, no, no, that's not what we're talking about." 297 00:15:01,889 --> 00:15:05,449 We're talking about assignment. This is into equals, this is an assignment. So what it 298 00:15:05,450 --> 00:15:07,190 says is evaluate 299 00:15:07,190 --> 00:15:09,710 the right-hand side, okay? 300 00:15:09,710 --> 00:15:13,250 Total - what was total's previous value. Go and look it up in the box over here you have some 301 00:15:13,250 --> 00:15:17,090 box for total. Its value was 10. It looks it up. It says, "Oh, that was 10." I'm gonna 302 00:15:17,090 --> 00:15:19,139 add 1 to it, that gives me 11. 303 00:15:19,139 --> 00:15:21,350 What do I do with that 11? 304 00:15:21,350 --> 00:15:22,870 Stick it back in the box 305 00:15:22,870 --> 00:15:26,730 named Total. So it just says okay, and it puts an 11 over here and that's perfectly 306 00:15:26,730 --> 00:15:30,269 fine. So it's perfectly fine to do something like then where you take the old value 307 00:15:30,269 --> 00:15:35,500 for a variable, you do some manipulation on it, you stick it back in the same variable. Okay? 308 00:15:35,500 --> 00:15:39,419 So with that said, now that you know about variables, we can go back to a 309 00:15:39,419 --> 00:15:43,059 couple of the programs that you saw last time and things ill now begin to make a 310 00:15:43,059 --> 00:15:45,789 little bit more sense, so if you come to the computer, 311 00:15:45,789 --> 00:15:49,699 remember our little friend, add two integers that you saw last time. Suddenly 312 00:15:49,700 --> 00:15:53,300 the world opens up a little bit and we say, yeah, add two integers is a console 313 00:15:53,299 --> 00:15:57,000 program, right. So it's gonna write some text out to a console somewhere 314 00:15:57,000 --> 00:16:01,549 and we have our run method. We print out a line; we know that the method print 315 00:16:01,549 --> 00:16:02,909 [inaudible] when we say that, 316 00:16:02,909 --> 00:16:06,139 whatever we put inside the double quotes gets printed to the screen so it 317 00:16:06,139 --> 00:16:06,838 prints out 318 00:16:06,839 --> 00:16:11,040 this program adds two numbers, so we execute that line and after we execute it, it writes it out and 319 00:16:11,039 --> 00:16:12,699 it comes to the next line. 320 00:16:12,700 --> 00:16:18,049 Low and behold, what have we done here? We've declared a variable named N1. 321 00:16:18,048 --> 00:16:21,088 What's the value for that variable that we're gonna assign to it? We're 322 00:16:21,089 --> 00:16:22,819 going to call some method. 323 00:16:22,818 --> 00:16:25,929 The method we're gonna call is something called Read INT and this is 324 00:16:25,929 --> 00:16:28,608 a method that's provided for you in 325 00:16:28,609 --> 00:16:31,160 console programs. All console programs can do this. 326 00:16:31,159 --> 00:16:34,269 The way it works is you give it some 327 00:16:34,269 --> 00:16:37,529 text between two double quotes. It writes that text on the screen just like 328 00:16:37,529 --> 00:16:41,809 you saw last time and asks for input from the user. Whatever value the user 329 00:16:41,809 --> 00:16:43,138 types in and hits 330 00:16:43,139 --> 00:16:48,619 enter, that number is the value that gets what we call return by read INT. It's 331 00:16:48,619 --> 00:16:51,699 the value that [inaudible]. It's kind of 332 00:16:51,698 --> 00:16:55,188 giving it back to you. Do what you want. What 333 00:16:55,188 --> 00:16:58,338 are you gonna do with it? You're gonna assign it to the Box N1, so here's 334 00:16:58,339 --> 00:17:00,380 the box for N1. 335 00:17:00,379 --> 00:17:04,200 We execute this statement. It's coming; it's gonna print to enter N1 on the 336 00:17:04,200 --> 00:17:07,789 screen. It's gonna ask us for a value. Let's say we type in the value 337 00:17:07,789 --> 00:17:12,379 17 and it sticks that 17 so this expression over here 338 00:17:12,380 --> 00:17:15,199 evaluates the 17. It sticks 17 in the box for N1, 339 00:17:15,199 --> 00:17:19,479 goes to the next line, now it declares N2. We have some box. Its initial value 340 00:17:19,479 --> 00:17:23,949 also is gonna come from the user via this read INT method. So we call read INT. It 341 00:17:23,949 --> 00:17:27,279 asks us for another value; we type in 25, 342 00:17:27,279 --> 00:17:29,538 and it sticks that in the box. 343 00:17:29,538 --> 00:17:32,609 Now, we're gonna declare another; we just declare until the cows come home, right? 344 00:17:32,609 --> 00:17:36,250 Declaring is fun; it's cheap. It's easy; do it, do it often, right, so we're gonna declare 345 00:17:36,250 --> 00:17:39,169 another variable total, so we have another box over here for total 346 00:17:39,169 --> 00:17:42,059 and the value of total that es gonna get assigned there is just whatever value is in 347 00:17:42,058 --> 00:17:43,440 N1 plus N2. 348 00:17:43,440 --> 00:17:47,370 So to evaluate this side, it says I need to look up the box N1, so at the 349 00:17:47,369 --> 00:17:51,199 point where it reaches this line, it evaluates whatever is in N1. 350 00:17:51,200 --> 00:17:54,909 This isn't some truth that holds for all time. It's just an assignment that it does 351 00:17:54,909 --> 00:17:56,429 when it reaches that line. 352 00:17:56,429 --> 00:17:58,530 So it says, find out what's in N1, 353 00:17:58,529 --> 00:17:59,899 add it to what's in N2. That'll 354 00:17:59,900 --> 00:18:02,389 give you the value 42; stick that in Total, 355 00:18:02,388 --> 00:18:05,649 which is what you get. And the last line says, 356 00:18:05,650 --> 00:18:08,670 I'm gonna print something to the screen where the total is and you see these 357 00:18:08,670 --> 00:18:10,550 funky plusses and you're like, 358 00:18:10,549 --> 00:18:14,470 "Whoa, whoa, whoa Meron, I thought plus was light lading things together. What's going 359 00:18:14,470 --> 00:18:16,358 on? You're like trying to add - 360 00:18:16,358 --> 00:18:19,369 this thing's got the value of 42. I know that because it's in the box. 361 00:18:19,369 --> 00:18:21,939 You're trying to add 42 to some text? 362 00:18:21,940 --> 00:18:23,590 That's not right." Well, 363 00:18:23,589 --> 00:18:26,970 in most worlds, it would not be right, but it turns out in the Java world it's 364 00:18:26,970 --> 00:18:28,960 perfectly fine and when it sees something 365 00:18:28,960 --> 00:18:30,058 that some words 366 00:18:30,058 --> 00:18:34,859 with a plus sign, this plus sign here is not numerical addition anymore. This is 367 00:18:34,859 --> 00:18:37,579 essentially you can think of as a concatenation operator. It says, "Take 368 00:18:37,579 --> 00:18:38,899 whatever text is here 369 00:18:38,900 --> 00:18:41,059 and add to that text, the 370 00:18:41,058 --> 00:18:42,729 textural form of this value." 371 00:18:42,729 --> 00:18:46,088 Well, the textural form of 42 is just the Characters 4 and 2, 372 00:18:46,088 --> 00:18:47,298 so it sort of 373 00:18:47,298 --> 00:18:50,868 adds that 42 here and then it adds to the end of that a little period, so it 374 00:18:50,868 --> 00:18:52,399 concatenates all those things together 375 00:18:52,400 --> 00:18:55,640 and then prints them out and says the totals 42. 376 00:18:55,640 --> 00:18:59,380 So any questions about the program? 377 00:18:59,380 --> 00:19:02,000 If you're feeling good, like you understand what just went on in the program, 378 00:19:02,000 --> 00:19:03,849 nod your head. 379 00:19:03,849 --> 00:19:06,089 Rock on. That's what I like to see. All right. 380 00:19:06,089 --> 00:19:08,819 So with that said, 381 00:19:08,819 --> 00:19:12,529 we can go back to something else we did last time, right, so now you know how 382 00:19:12,529 --> 00:19:13,950 all this 383 00:19:13,950 --> 00:19:16,419 variables and assignments and types 384 00:19:16,419 --> 00:19:19,360 and you're like, "Hey, Meron, yeah, that's all good and well, but last time you were telling me 385 00:19:19,359 --> 00:19:21,209 about like 386 00:19:21,210 --> 00:19:25,390 classes and objects and drawing stuff on the screen like, what's up with all that." 387 00:19:25,390 --> 00:19:29,040 And this is the time when we actually do sort of the, you know, Reese's 388 00:19:29,039 --> 00:19:30,288 Peanut Butter Cup. 389 00:19:30,288 --> 00:19:33,920 Like it's two great tastes that taste great together. We take some 390 00:19:33,920 --> 00:19:36,900 objects over here and some variables over here and we just slam them together, 391 00:19:36,900 --> 00:19:40,690 and now you're going to have variables that contain objects. They 392 00:19:40,690 --> 00:19:41,950 taste great, all right? 393 00:19:41,950 --> 00:19:45,639 So here are some of the objects that you considered last time, right? We had things 394 00:19:45,638 --> 00:19:50,219 called, like some classes. You have the G label class and the G -rect class 395 00:19:50,220 --> 00:19:53,240 and the G-oval class. These are all these graphics classes like a label 396 00:19:53,240 --> 00:19:57,779 [inaudible] or rectangle or an oval. I'll show you examples of all these things this time. 397 00:19:57,779 --> 00:19:58,859 And these guys 398 00:19:58,859 --> 00:20:00,969 were actually all 399 00:20:00,970 --> 00:20:03,839 classes that 400 00:20:03,839 --> 00:20:08,819 are part of a hierarchy of other classes and the other classes, they're all 401 00:20:08,819 --> 00:20:11,619 part of is G 402 00:20:11,619 --> 00:20:17,019 object? So what G-object is a G-object is a class. Don't let the name fool 403 00:20:17,019 --> 00:20:20,460 you. Some people see G-object and they think, "Oh, that must be an 404 00:20:20,460 --> 00:20:21,860 object." No, G-object is a class. 405 00:20:21,859 --> 00:20:25,949 It just means a graphics object. And guess what, a rectangle or an oval or a line or 406 00:20:25,950 --> 00:20:28,630 a label or all different graphics objects. 407 00:20:28,630 --> 00:20:32,030 So what that actually looks like in terms of the hierarchy just like you saw 408 00:20:32,029 --> 00:20:33,379 last time, 409 00:20:33,380 --> 00:20:37,650 is the classes are represented, kind of like this in the hierarchy, all of these 410 00:20:37,650 --> 00:20:42,950 classes are actually G-objects. Okay, they're all sub classes of G-objects. It means they're 411 00:20:42,950 --> 00:20:43,440 specializations. 412 00:20:43,440 --> 00:20:46,889 So any behavior that G-object has, all of these things have, 413 00:20:46,888 --> 00:20:49,829 but they might have their own specialized behavior, as well. 414 00:20:49,829 --> 00:20:52,569 Now, the interesting thing about all this is that 415 00:20:52,569 --> 00:20:54,389 when you have classes 416 00:20:54,390 --> 00:20:57,900 and you create objects, you can store those objects in a variable. You can say, 417 00:20:57,900 --> 00:21:02,580 "Give me a variable that is a box," and the type that that box holds is an object 418 00:21:02,579 --> 00:21:06,309 that's the type of some class, so all classes can actually be used as type means, 419 00:21:06,309 --> 00:21:07,559 okay? 420 00:21:07,559 --> 00:21:11,569 Now, that's kind of funky. Let me show you an example, okay? 421 00:21:11,569 --> 00:21:13,639 Here is another program you saw last time. 422 00:21:13,640 --> 00:21:14,640 Now, 423 00:21:14,640 --> 00:21:17,870 we can just kind of reveal the mystery about variables. 424 00:21:17,869 --> 00:21:21,589 We have this program, Hello program that extends the graphics program, right, so it's gonna 425 00:21:21,589 --> 00:21:22,639 draw us some pictures. 426 00:21:22,640 --> 00:21:26,509 Well what are we actually doing on this first line? Hey, we're doing a declaration 427 00:21:26,509 --> 00:21:28,648 and assignments on the first line. 428 00:21:28,648 --> 00:21:31,548 What's the type of our variable? It's a G-label. 429 00:21:31,548 --> 00:21:33,920 So we use the class name as the type. 430 00:21:33,920 --> 00:21:37,680 The name is label, so we have a box called label and what that box is gonna 431 00:21:37,680 --> 00:21:39,660 hold is any object 432 00:21:39,660 --> 00:21:41,710 of the class G-label. 433 00:21:41,710 --> 00:21:44,299 Okay, so that's the type. It's gonna hold any object. 434 00:21:44,299 --> 00:21:45,589 And the question comes up, 435 00:21:45,589 --> 00:21:50,278 how do we get an object of type G label. And here's the funky part. The way we get an 436 00:21:50,278 --> 00:21:52,140 object of type G label is 437 00:21:52,140 --> 00:21:53,389 we have to ask 438 00:21:53,388 --> 00:21:55,178 for a new G-label. 439 00:21:55,179 --> 00:21:57,830 So we give the name of the class here 440 00:21:57,829 --> 00:22:01,099 and class is depending on the class, you know, see some examples of this 441 00:22:01,099 --> 00:22:02,228 will take in 442 00:22:02,229 --> 00:22:07,229 or give in some initial values for what we refer to as parameters, 443 00:22:07,229 --> 00:22:09,870 and these parameters are all separated by commas. 444 00:22:09,869 --> 00:22:11,558 But at some initial values, 445 00:22:11,558 --> 00:22:16,178 that this object uses to sort of set itself up. It initializes the objects. So objects 446 00:22:16,179 --> 00:22:19,580 rather than just taking one value, can potentially hold multiple values in 447 00:22:19,579 --> 00:22:20,048 them. 448 00:22:20,048 --> 00:22:23,629 In this case, the G-label is gonna have Hello World as some text that it's gonna 449 00:22:23,630 --> 00:22:27,440 display in some location that's 100,75. 450 00:22:27,440 --> 00:22:31,980 So we say, "Hey G-label, you're the class. I don't have an object of you yet. You're 451 00:22:31,980 --> 00:22:35,999 the class." When I say, "Give me a new G label, it's sort of like going to the factor. 452 00:22:35,999 --> 00:22:39,868 You sort of show up at the G-label factory and you say, "Yo, G-label factory. 453 00:22:39,868 --> 00:22:43,898 Give me a new object," and G-label factory is sort of sitting up there and says, "Well, what kind of new 454 00:22:43,898 --> 00:22:46,898 object do you want? Yeah, I can give you a new G-label, but how is that initial G label 455 00:22:46,898 --> 00:22:47,719 going to look," 456 00:22:47,720 --> 00:22:51,799 and you're providing these parameters that sort of specify what the initial G-label looks like. 457 00:22:51,799 --> 00:22:56,019 So this line is now created a G-label, okay? 458 00:22:56,019 --> 00:22:57,569 And it has some initial, 459 00:22:57,569 --> 00:22:59,549 so when we execute that line, 460 00:22:59,549 --> 00:23:01,190 it has some initial 461 00:23:01,190 --> 00:23:04,460 text that it's gonna display and somewhere it's also storing the value is 175 because 462 00:23:04,460 --> 00:23:06,840 it knows where that's gonna be on the screen. 463 00:23:06,839 --> 00:23:09,928 Now, what we're gonna do is we're going to tell that object 464 00:23:09,929 --> 00:23:12,090 to do some additional stuff. 465 00:23:12,089 --> 00:23:16,869 The way we tell an object to do something is we specify the name of the 466 00:23:16,869 --> 00:23:20,639 variable for the object, okay. So the object name is label. That's the name of 467 00:23:20,640 --> 00:23:21,559 the variable. 468 00:23:21,558 --> 00:23:24,378 And so that's what we say here. We don't say G-label. We say the name of the 469 00:23:24,378 --> 00:23:28,368 variable label. Then we have a dot; then we have the name of the method that 470 00:23:28,368 --> 00:23:32,548 we're going to call for that particular object. So it's a little bit different than 471 00:23:32,548 --> 00:23:34,079 Karel's world, all right. In Karel's world, 472 00:23:34,079 --> 00:23:36,089 every time you call the method, 473 00:23:36,089 --> 00:23:38,519 Karel was the one executing that particular method. 474 00:23:38,519 --> 00:23:42,279 Here when you have multiple objects, you need to specify which object you want 475 00:23:42,279 --> 00:23:43,779 to execute this method. 476 00:23:43,779 --> 00:23:46,690 So you say, "Hey, label, yeah, I'm referring to you, buddy. 477 00:23:46,690 --> 00:23:51,049 Set your fonts to be this particular font, san seraph which you saw last time." It 478 00:23:51,049 --> 00:23:53,450 just makes it big. It makes it 36-point font. 479 00:23:53,450 --> 00:23:56,370 And then you say, "Hey label, yeah, you, same guy, 480 00:23:56,369 --> 00:24:00,179 or gal or it or whatever it may be. Set your color to be red." 481 00:24:00,180 --> 00:24:02,509 And it says, "Okay, I'll set my color to be red, 482 00:24:02,509 --> 00:24:03,989 and then finally 483 00:24:03,989 --> 00:24:08,440 if I want to take this label and stick it up onto my graphics canvas which is 484 00:24:08,440 --> 00:24:11,099 actually the program I'm looking at. Right, I've done all this stuff and my program's still 485 00:24:11,098 --> 00:24:14,269 bringing out a blank screen. It's like here you go, nothing going on here. I 486 00:24:14,269 --> 00:24:16,179 say add and the name of 487 00:24:16,179 --> 00:24:19,940 the object that I want to add, right because now I'm holding this little object called label. This 488 00:24:19,940 --> 00:24:22,759 is my little felt Hello World, and I want to say, "Hey 489 00:24:22,759 --> 00:24:25,899 take the felt hello world that's so cute and just stick it in the world," 490 00:24:25,898 --> 00:24:28,609 so we add it to the 491 00:24:28,609 --> 00:24:30,429 graphics program, basically 492 00:24:30,430 --> 00:24:33,779 and it shows up in the screen where it knows it's supposed to show up at a 493 00:24:33,779 --> 00:24:36,109 494 00:24:36,109 --> 00:24:36,668 100,75. 495 00:24:36,669 --> 00:24:40,240 So the general form for this, the people will refer to when you see something like 496 00:24:40,240 --> 00:24:44,269 this, you have name of the variable and the method name and the 497 00:24:44,269 --> 00:24:47,278 Java-esc names that we give to these, just so we hear other 498 00:24:47,278 --> 00:24:48,029 programmers 499 00:24:48,029 --> 00:24:50,259 refer to them, you know what they're talking about, 500 00:24:50,259 --> 00:24:53,259 is they say the object is the receiver. 501 00:24:53,259 --> 00:24:57,019 So the object here is the one who's receiving or being told to do 502 00:24:57,019 --> 00:24:57,819 something, 503 00:24:57,819 --> 00:25:00,769 okay? So the name of the variable is the receiver 504 00:25:00,769 --> 00:25:04,250 and the method that's being called is called the message and so there's this whole 505 00:25:04,250 --> 00:25:08,569 notion of sending messages, kind of like you could think of programming as I am, right, 506 00:25:08,569 --> 00:25:12,049 and in your little I am you have like your friend, Bob, and your friend, Sally and your 507 00:25:12,049 --> 00:25:14,178 friend, Dave, and your friend, Label, 508 00:25:14,179 --> 00:25:18,140 right. And Label is just sitting there in I am and Label is not very talkative and once 509 00:25:18,140 --> 00:25:21,640 in awhile you can send a message to I am which is a form of message 510 00:25:21,640 --> 00:25:24,519 that label understands and then Label will do something. 511 00:25:24,519 --> 00:25:27,660 So this is called the message that we send to the receiver, 512 00:25:27,660 --> 00:25:31,860 is kind of the nomenclature. But a lot of times, you'll just hear people say, I made a 513 00:25:31,859 --> 00:25:35,449 method call on the object and it means exactly the same thing. Again, we like to give 514 00:25:35,450 --> 00:25:37,730 funky names to simple concepts. Okay, 515 00:25:37,730 --> 00:25:44,730 so any questions about that, uh-huh? If you add the label first, then you start [inaudible]. 516 00:25:45,140 --> 00:25:49,000 Yeah, you can still, after you sort of put the little object up there that says, 517 00:25:49,000 --> 00:25:50,589 "Oh, the object's in the world, and 518 00:25:50,589 --> 00:25:54,169 say, "Object change your color to red," and that's where it's a lot cooler than the felt 519 00:25:54,170 --> 00:25:57,130 animals you had when you were a kid, because it'll just change to red right there on 520 00:25:57,130 --> 00:25:57,919 the screen 521 00:25:57,919 --> 00:26:00,759 because once it's been added, it's up on your canvas, 522 00:26:00,759 --> 00:26:04,190 and now any messages that you send to it will actually change its appearance on 523 00:26:04,190 --> 00:26:06,250 the canvas. Okay. 524 00:26:06,250 --> 00:26:09,778 So with that said, let's kind of go through and understand a little bit more 525 00:26:09,778 --> 00:26:11,679 about what are some of these 526 00:26:11,679 --> 00:26:12,190 527 00:26:12,190 --> 00:26:16,570 messages or methods that we can send to particular kinds of objects. 528 00:26:16,569 --> 00:26:20,859 So to do that, first let's talk about the graphics world that we live in. 529 00:26:20,859 --> 00:26:23,219 There's a couple of things you want to think about the graphics world. How do 530 00:26:23,220 --> 00:26:26,589 we specify all these things like locations and how is it laid out. Well 531 00:26:26,589 --> 00:26:30,490 the origins, the upper left, so the upper left corner of your graphics window is 532 00:26:30,490 --> 00:26:32,460 location 00. 533 00:26:32,460 --> 00:26:36,620 Every thing in your graphics window is measured in pixels. Pixels are the little 534 00:26:36,619 --> 00:26:39,250 dots on your screen, so if you get out a magnifying glass and you go up to your monitor 535 00:26:39,250 --> 00:26:43,099 and you look real close and you get like eye strain and then you want to sue, 536 00:26:43,099 --> 00:26:46,679 don't worry. It's just fine. At least you saw a pixel and it brought you al little closer 537 00:26:46,680 --> 00:26:47,750 to Nirvana. 538 00:26:47,750 --> 00:26:51,679 So a pixel is just a little squares essentially on your screen 539 00:26:51,679 --> 00:26:53,490 that could actually get displayed. 540 00:26:53,490 --> 00:26:56,819 All the numbers that we refer to are in terms of pixels. 541 00:26:56,819 --> 00:27:01,269 So when we think about X and Y coordinates, X is how far you go across, 542 00:27:01,269 --> 00:27:04,460 sort of across the screen and Y is how far you go down. 543 00:27:04,460 --> 00:27:08,990 The X coordinates just increase to the right from 00 and the Y coordinates, 544 00:27:08,990 --> 00:27:12,569 different than what you're used to in mathematics, Y coordinates go down. So 545 00:27:12,569 --> 00:27:16,099 normally, we think of this as a negative Y direction. Not so in Java's world. This is 546 00:27:16,099 --> 00:27:20,488 the positive Y direction going down. Hey that's just the way life is. It's been 547 00:27:20,489 --> 00:27:24,329 like that since I was a wee tike, or actually, since Java came around. Okay, 548 00:27:24,329 --> 00:27:28,429 and so you might wonder, "Hey, if I have some label, like Hello World, and I 549 00:27:28,430 --> 00:27:30,298 specify some location, 550 00:27:30,298 --> 00:27:33,969 where is that really on this Hello World thing and so 551 00:27:33,969 --> 00:27:37,269 the G-label coordinates, the coordinates that 100,75 that we gave it were the X and Y 552 00:27:37,269 --> 00:27:41,259 location of the base line of the first character. So that first pixel right 553 00:27:41,259 --> 00:27:45,919 there on the lower left hand corner of the H, okay? 554 00:27:45,919 --> 00:27:52,919 And so that's 175. That's how things were laid out. Uh-huh Is it possibly to specify [inaudible] using ratios? 555 00:27:56,048 --> 00:27:59,039 Well, everything's in terms of pixels. There's some stuff you'll see later on in the 556 00:27:59,039 --> 00:28:02,809 class where we actually specify some things like in polar coordinates if you're familiar 557 00:28:02,809 --> 00:28:05,278 with polar coordinates, you'll get to that later on. 558 00:28:05,278 --> 00:28:08,038 But you want to think of these numbers as just being absolute pixels and you 559 00:28:08,038 --> 00:28:10,990 can do some math to figure out where those pixels should actually be. 560 00:28:10,990 --> 00:28:14,308 But they're still just pixels, 561 00:28:14,308 --> 00:28:16,269 just way off. All right, 562 00:28:16,269 --> 00:28:17,869 so 563 00:28:17,869 --> 00:28:21,449 remember all of these classes are organized in some hierarchy and everything 564 00:28:21,450 --> 00:28:25,500 that we talked about, right, G-label, G rect, G-oval and G-line are all G-objects, 565 00:28:25,500 --> 00:28:29,359 which means any behavior that a G-object has, all of these puppies have. 566 00:28:29,359 --> 00:28:32,879 So let's start off by thinking about what are the behaviors or methods that are 567 00:28:32,880 --> 00:28:37,049 available on G-objects. So one is set color; you just saw an example of that, 568 00:28:37,048 --> 00:28:40,168 right, you specified the object, you say set color and you give it some color and I'll show you 569 00:28:40,169 --> 00:28:41,538 where the colors come from 570 00:28:41,538 --> 00:28:46,308 and it sets the color of the specified object to the color you just specified 571 00:28:46,308 --> 00:28:48,629 as this thing called a parameter. 572 00:28:48,630 --> 00:28:52,320 So the thing that's inside the 573 00:28:52,319 --> 00:28:53,389 parentheses 574 00:28:53,390 --> 00:28:58,070 in our formal speak, we refer to as parameter. So methods have parameters. 575 00:28:58,069 --> 00:29:01,569 They're the values that you provide inside the parentheses 576 00:29:01,569 --> 00:29:04,179 when you invoke a particular method, okay? 577 00:29:04,180 --> 00:29:06,920 So there's a single parameter there, color. 578 00:29:06,920 --> 00:29:10,769 There's also something there called set location, which takes an X and Y coordinates. 579 00:29:10,769 --> 00:29:12,279 Again X and Y are in pixels 580 00:29:12,279 --> 00:29:16,690 and it sets the location of that object to that XY, so this method has two 581 00:29:16,690 --> 00:29:18,288 parameters, X and Y. 582 00:29:18,288 --> 00:29:22,980 And you might say, "But Meron, when I created the G-label before, didn't I 583 00:29:22,980 --> 00:29:27,130 specify the X and Y location when I told the factory to give me one of 584 00:29:27,130 --> 00:29:29,049 these G-label objects?" Yeah, you did, 585 00:29:29,048 --> 00:29:30,418 but you didn't have to, 586 00:29:30,419 --> 00:29:33,989 and it turns out there's times you don't want to; you want to just say, "Hey give me a 587 00:29:33,989 --> 00:29:37,639 label that's got the words Hello World in it and later on I'll figure out where 588 00:29:37,638 --> 00:29:41,089 it's actually gonna go on the screen, so I'll specify what the X and Y are," but 589 00:29:41,089 --> 00:29:43,368 until you specify the X and Y, it doesn't know, 590 00:29:43,368 --> 00:29:46,368 but you can specify after you've created the object, is the 591 00:29:46,368 --> 00:29:47,859 important thing. And 592 00:29:47,859 --> 00:29:50,269 move, and here's the funky thing. You can actually tell an object, like you have 593 00:29:50,269 --> 00:29:50,979 your little, 594 00:29:50,979 --> 00:29:53,769 you know, Hello World somewhere on the screen and it's kind of like the 595 00:29:53,769 --> 00:29:57,519 furniture idea of objects. You're like, "Yeah, I don't like how it looks there. Move it a little to 596 00:29:57,519 --> 00:29:58,679 the right. 597 00:29:58,679 --> 00:30:00,929 And so there's a move method 598 00:30:00,929 --> 00:30:04,280 and its coordinates if you're sort of a Calculus person or DX and DY, and if you're 599 00:30:04,279 --> 00:30:07,779 not a Calculus person, don't worry, you don't need to be a Calculus. This is the closest we 600 00:30:07,779 --> 00:30:10,740 get to Calculus in this class. Rock on. DX 601 00:30:10,740 --> 00:30:14,970 and DY just means how many pixels in the X direction and the Y 602 00:30:14,970 --> 00:30:15,950 direction should you 603 00:30:15,950 --> 00:30:18,569 move this object from its previous location 604 00:30:18,569 --> 00:30:21,740 and these can be positive or negative, because you can actually have something 605 00:30:21,740 --> 00:30:24,420 negative say in the Y direction and it'll move it up on the screen. 606 00:30:24,420 --> 00:30:27,680 So it's just the offset or how much you want to, so 607 00:30:27,680 --> 00:30:30,580 think of it as the difference in X and the difference in Y, how much do 608 00:30:30,579 --> 00:30:37,329 you want to change where it moved. Uh-huh, question? Do you use a set location XY and then [inaudible]? 609 00:30:37,329 --> 00:30:44,329 Can you do set location and so you - [Inaudible] 610 00:30:44,659 --> 00:30:48,409 later define X and Y. No, if you want to say set location XY, you're 611 00:30:48,409 --> 00:30:51,730 referring to two variables X and Y, so there's variables X and Y and each have already been 612 00:30:51,730 --> 00:30:56,299 declared and have values. [Inaudible]. 613 00:30:56,299 --> 00:31:00,419 Yeah, so anything that's a parameter here, you can actually use a variable rather 614 00:31:00,420 --> 00:31:04,259 than an actual value, like an actual number, and we'll see some examples of that as we go 615 00:31:04,259 --> 00:31:05,619 along. 616 00:31:05,619 --> 00:31:06,829 [Inaudible]. 617 00:31:06,829 --> 00:31:10,429 So where do all these colors come from? It turns out these colors come from a 618 00:31:10,430 --> 00:31:13,750 standard library that someone in the Java world wrote that's called 619 00:31:13,750 --> 00:31:18,179 the Java AWT package, so if you want to use these colors at the top of the 620 00:31:18,179 --> 00:31:22,820 program, and I'll show you and example, that should say, importJava.AWT.star 621 00:31:22,819 --> 00:31:26,599 and these are all the colors, so they're all the name color, dot and then what the 622 00:31:26,599 --> 00:31:27,678 color actually is. 623 00:31:27,679 --> 00:31:31,880 So you just saw an example where we used colored dot red to make Hello World 624 00:31:31,880 --> 00:31:34,559 red, but there's all these different colors and they're all in your book, so you don't need 625 00:31:34,558 --> 00:31:36,240 to scribble them down hurriedly, but 626 00:31:36,240 --> 00:31:39,200 you know, different shades or black or grey or white and 627 00:31:39,200 --> 00:31:42,919 you know, magenta and sienna if you're sort of a color photography sort of person, but 628 00:31:42,919 --> 00:31:46,649 there's a whole set of colors and you can just go to town on them. Okay. 629 00:31:46,648 --> 00:31:51,308 So all G objects, all of these things respond to these methods because 630 00:31:51,308 --> 00:31:55,569 these guys are all objects, so any of those three methods will work on any of these 631 00:31:55,569 --> 00:31:58,329 objects, of objects of any of these types. But 632 00:31:58,329 --> 00:32:01,569 there are some additional behaviors, there are some additional methods 633 00:32:01,569 --> 00:32:05,829 that some of the specialized classes, the sub classes actually implement. So 634 00:32:05,829 --> 00:32:06,359 G-label 635 00:32:06,359 --> 00:32:10,119 has some additional things that it does beyond just what a G-Object in 636 00:32:10,119 --> 00:32:10,998 general would do. 637 00:32:10,999 --> 00:32:12,950 And the big one, 638 00:32:12,950 --> 00:32:16,029 well first of all, it's got what we refer to as the constructor. You can think of 639 00:32:16,029 --> 00:32:19,019 the constructor and I'm gonna do a little violence to the term, 640 00:32:19,019 --> 00:32:22,269 but you can think of the constructor as a factory, okay? 641 00:32:22,269 --> 00:32:26,509 What the constructor says is, "Hey, I want a new one of these guys," so I use the word 642 00:32:26,509 --> 00:32:29,869 new and then I specify the constructor, okay? 643 00:32:29,869 --> 00:32:35,138 The constructor is the name of the class of which you're going to get a new object 644 00:32:35,138 --> 00:32:38,898 and then some initial parameters. So the initial perimeters for a G-label for 645 00:32:38,898 --> 00:32:39,338 example, 646 00:32:39,338 --> 00:32:43,028 could be what you just saw, the text that you wanted to display and its initial X 647 00:32:43,028 --> 00:32:45,329 and Y coordinate, okay. 648 00:32:45,329 --> 00:32:46,779 There's some other ones. 649 00:32:46,779 --> 00:32:50,049 For example, you can change the fonts, right, it makes sense for a label. It 650 00:32:50,049 --> 00:32:53,369 doesn't make sense for a square or rectangle, to say, "Hey, square, you're font's 651 00:32:53,369 --> 00:32:54,959 gonna be Helvetica." And 652 00:32:54,960 --> 00:32:56,420 the square is like, 653 00:32:56,420 --> 00:32:59,539 "I'm four lines, man, I don't have a font. What are you talking about?" 654 00:32:59,539 --> 00:33:03,609 That's why the notion of setting a font isn't something that all G-objects have. 655 00:33:03,609 --> 00:33:07,729 It's only something that G-labels have which means the G-rect and G-oval and G-line 656 00:33:07,730 --> 00:33:09,140 don't have a set font. 657 00:33:09,140 --> 00:33:12,320 This is just something that you can do with a G-label and what it does it says 658 00:33:12,319 --> 00:33:13,710 specify the font 659 00:33:13,710 --> 00:33:16,289 that you want that label to show up in on the screen, 660 00:33:16,289 --> 00:33:20,049 and the general form for specifying the font - this is all in your book and it shows you examples of the 661 00:33:20,049 --> 00:33:22,139 fonts, but the general form is 662 00:33:22,140 --> 00:33:24,880 inside double quotes, because you're gonna give it as a piece of text, you 663 00:33:24,880 --> 00:33:29,190 specify the family of the font. That would be something like times or Helvetica. 664 00:33:29,190 --> 00:33:33,389 The style of the font, which is, you know, plain or bold or italic or bold or 665 00:33:33,388 --> 00:33:36,699 italic, so yeah, now in fact you can do all those funky things your word processor does and 666 00:33:36,700 --> 00:33:40,380 then the size of your font, how big or small you actually want it to be 667 00:33:40,380 --> 00:33:43,580 and you specify that as some texts and that would be 668 00:33:43,579 --> 00:33:47,869 what you send over to this sent font 669 00:33:47,869 --> 00:33:49,588 method to set the text. Okay, 670 00:33:49,588 --> 00:33:55,799 so any questions about that? 671 00:33:55,799 --> 00:34:02,799 Uh-huh? [Inaudible]. No, you get them all through graphics program. Rock on. All right, so 672 00:34:04,000 --> 00:34:11,000 how do we draw some geometrical objects? Have another question? [Inaudible]. 673 00:34:13,659 --> 00:34:17,338 If you're doing a graphics program without any colors, you don't need 674 00:34:17,338 --> 00:34:23,679 Java.awt. But if we're doing one with color, then we need Java.awt in addition? Yeah, 675 00:34:23,679 --> 00:34:26,979 in addition to a graphics program, and Iell give you an example of that in just a second. I 676 00:34:26,978 --> 00:34:29,068 have to show you a program that has it. 677 00:34:29,068 --> 00:34:32,369 So drawing geometrical objects - it turns out some of these other guys have funky 678 00:34:32,369 --> 00:34:35,579 things that you can do specifically with them. So first of all, there's the 679 00:34:35,579 --> 00:34:38,199 constructors, the factories from which you can build new things. So 680 00:34:38,199 --> 00:34:39,729 how do I make a new rectangle? 681 00:34:39,728 --> 00:34:45,348 I say, "Hey, I want a new G-rect." I specify the X and Y location. It's the upper 682 00:34:45,349 --> 00:34:49,028 left-hand corner of that rectangle and then I specify the width and height of 683 00:34:49,028 --> 00:34:51,389 that rectangle in pixels. Okay. 684 00:34:51,389 --> 00:34:55,399 So that's the basic idea is upper left-hand corner and then 685 00:34:55,398 --> 00:34:59,639 width and height of the rectangle. 686 00:34:59,639 --> 00:35:02,958 Similarly, for oval - ovals are kind of funky because you look at this and you're like, 687 00:35:02,958 --> 00:35:07,078 "Hey I have an X and Y and I have a height. I didn't like -width and height like 688 00:35:07,079 --> 00:35:10,789 ovals. I thought an oval was defined by like two [inaudible] and that whole thing 689 00:35:10,789 --> 00:35:14,219 where you have a string. Did you ever do that - you stick the two like nails in the board and you 690 00:35:14,219 --> 00:35:16,400 put the string around it and you draw 691 00:35:16,400 --> 00:35:17,409 anyone? 692 00:35:17,409 --> 00:35:19,989 There's like two people - yeah, like sorry. 693 00:35:19,989 --> 00:35:23,489 Do it; go get a piece of wood, stick two nails in it, put some string around it and go get a 694 00:35:23,489 --> 00:35:25,659 pencil, something you can draw ovals 695 00:35:25,659 --> 00:35:29,248 and you can draw a 1 and if you're like 6 years old, you'll draw like 1,000, 696 00:35:29,248 --> 00:35:34,068 and if you're 18, and draw one, you're like, "Yeah, that was just stupid." 697 00:35:34,068 --> 00:35:37,708 But the basic idea behind an oval is the reason why we specify this way is 698 00:35:37,708 --> 00:35:42,828 you specify sort of an imaginary rectangle for the oval and the oval shows 699 00:35:42,829 --> 00:35:45,010 up as an oval that just happens to touch 700 00:35:45,010 --> 00:35:49,040 the sides of the rectangle, which is how we sort of specify how wide and broad it is. 701 00:35:49,039 --> 00:35:52,349 So think imaginary rectangle and sort of the four ends of the oval would sort of 702 00:35:52,349 --> 00:35:54,538 be touching it, and I'll show you that 703 00:35:54,539 --> 00:35:55,339 momentarily. 704 00:35:55,338 --> 00:35:58,978 G line is very straightforward. G-line, the way we specify line just like 705 00:35:58,978 --> 00:35:59,748 you know, 706 00:35:59,748 --> 00:36:03,039 the days of yore with Euclid, line is defined by two points, 707 00:36:03,039 --> 00:36:06,360 right, that's an infinite line, but we can think of where it ends at the two points. So 708 00:36:06,360 --> 00:36:10,829 we specify an initial XY location and an ending XY location and it will just 709 00:36:10,829 --> 00:36:13,130 draw a line between those two points, 710 00:36:13,130 --> 00:36:20,130 pretty straightforward. Uh-huh? [Inaudible] is the set location defined as the lower left corner of your object, and 711 00:36:25,179 --> 00:36:30,910 the G-rectangle [inaudible] left corner? Well, for set location is the lower left corner of for textural objects, 712 00:36:30,909 --> 00:36:36,068 and then it becomes different for rectangles and ovals and stuff like that, yeah. 713 00:36:36,068 --> 00:36:39,009 So it's a slight variation because that's just because we're trying to 714 00:36:39,009 --> 00:36:43,498 deal with, you know, texts and rectangles symmetrically and it's tough to do, okay. 715 00:36:43,498 --> 00:36:47,389 There's also some additional methods that are just shared by G rect and G-oval. 716 00:36:47,389 --> 00:36:49,489 They don't actually apply to lines 717 00:36:49,489 --> 00:36:52,420 or to labels for that sense, 718 00:36:52,420 --> 00:36:55,710 rectangles and ovals can be filled, so they can either be an outline. You can either have 719 00:36:55,710 --> 00:36:58,720 a rectangle that looks just like this which is an outline 720 00:36:58,719 --> 00:37:02,628 or it can be filled in which means it's just a big solid 721 00:37:02,628 --> 00:37:04,318 chunk of stuff. So 722 00:37:04,318 --> 00:37:08,088 you said if it's filled, you say this is either true or false with the actual 723 00:37:08,088 --> 00:37:12,219 word true or false. So if you've set it to true, then when you draw it on there, 724 00:37:12,219 --> 00:37:15,898 you say, "Hey, put this thing up on my canvas." By adding it to the canvas, it 725 00:37:15,898 --> 00:37:18,228 will show up filled in; it will show up solid. 726 00:37:18,228 --> 00:37:21,308 And if this is set to false, it just shows up as the outline of the object, 727 00:37:21,309 --> 00:37:24,749 whether or not that's an oval or a rectangle. 728 00:37:24,748 --> 00:37:28,719 And almost done here. There's also a notion of a set fill color, 729 00:37:28,719 --> 00:37:32,209 and you might be wondering, you're like, "But Meron, you told me that set color. 730 00:37:32,210 --> 00:37:33,639 Here's where things get a little funky, 731 00:37:33,639 --> 00:37:35,298 you told me that set color 732 00:37:35,298 --> 00:37:38,298 is something that was defined for a G-object, 733 00:37:38,298 --> 00:37:42,130 so if set color is defined over here, doesn't that mean that a 734 00:37:42,130 --> 00:37:45,579 G-rect and a G oval already can set that color?" 735 00:37:45,579 --> 00:37:46,890 Yes, that's true. 736 00:37:46,889 --> 00:37:51,019 "So what's the set fill color all about?" And this is a slight variation. When you 737 00:37:51,019 --> 00:37:52,489 set color for something, 738 00:37:52,489 --> 00:37:54,828 you set the color for the whole thing 739 00:37:54,829 --> 00:37:57,519 if that's solid or if it's an outline, you're setting the color for the whole 740 00:37:57,518 --> 00:38:02,808 thing. When you set fill color, you are just setting the color for this internal 741 00:38:02,809 --> 00:38:06,548 portion. So imagine like this was your rectangle, 742 00:38:06,548 --> 00:38:09,619 what color used to fill it in can actually be different than what color 743 00:38:09,619 --> 00:38:12,879 the line is. So if you want the line, say, to be black and you want the fill 744 00:38:12,878 --> 00:38:15,210 to be red, you can actually do that 745 00:38:15,210 --> 00:38:18,599 by setting the fill color to be red and then the outline is still whatever 746 00:38:18,599 --> 00:38:20,829 color the actual outline was 747 00:38:20,829 --> 00:38:25,420 if you want to do that. Okay, so if the color - if the interior is fill, so the set 748 00:38:25,420 --> 00:38:28,829 fill has to be true, then you can set the fill color to be different. That's a 749 00:38:28,829 --> 00:38:31,898 minor thing, but it's kind of fun if you're drawing stuff, okay? 750 00:38:31,898 --> 00:38:35,568 So any questions about that? Oh, 751 00:38:35,568 --> 00:38:42,568 wait - we have a question over there, uh-huh. [Inaudible]. 752 00:38:43,619 --> 00:38:46,239 Oh, in the place of this word fill? Yeah. 753 00:38:46,239 --> 00:38:49,920 It's either the word true or the word false. 754 00:38:49,920 --> 00:38:53,700 So if you say true, set filled is true which means it will be a 755 00:38:53,699 --> 00:39:00,699 solid thing; it will be filled in. Uh-huh? If your when you're using the [inaudible], you specify a [inaudible], what is the why would you use the set location [inaudible]. 756 00:39:08,009 --> 00:39:09,619 Maybe you wanna do some animation. 757 00:39:09,619 --> 00:39:13,940 Say like you have like your bunny in the world and he's gonna want to move along. [Inaudible]. 758 00:39:13,940 --> 00:39:16,880 Well, it's set somewhere to begin with and then later on, guess what you're 759 00:39:16,880 --> 00:39:20,960 actually gonna be doing a program that involves animation. Rock on. 760 00:39:20,960 --> 00:39:23,349 And you will move things around. 761 00:39:23,349 --> 00:39:27,509 So that's where it comes from. All righty. So 762 00:39:27,509 --> 00:39:30,920 one last thing that might be useful to you, sometimes you want to say, center 763 00:39:30,920 --> 00:39:33,278 something in the graphics window or draw something in the bottom, how do you know 764 00:39:33,278 --> 00:39:35,170 how big the graphics 765 00:39:35,170 --> 00:39:36,639 window actually is. 766 00:39:36,639 --> 00:39:39,299 There's two methods from the graphics 767 00:39:39,300 --> 00:39:40,229 program class, 768 00:39:40,228 --> 00:39:43,918 so these methods you might notice they don't have any objects, that is their 769 00:39:43,918 --> 00:39:47,609 receiver, and the reason for that is that both of these methods, 770 00:39:47,610 --> 00:39:51,099 their receiver is the graphics program, so if you don't specify 771 00:39:51,099 --> 00:39:53,009 the receiver for a method, 772 00:39:53,009 --> 00:39:57,369 the thing that is actually receiving the method is the encapsulating class. Okay, 773 00:39:57,369 --> 00:40:01,099 just like in Karel, when you had Karel and you said move and you didn't specify an object for 774 00:40:01,099 --> 00:40:02,068 the move, 775 00:40:02,068 --> 00:40:04,418 the move is going into Karel, so get width and get height. 776 00:40:04,418 --> 00:40:08,088 We'll return to you the width and the height of the graphics window in pixels. So 777 00:40:08,088 --> 00:40:11,369 these actually give you back a numeric value that you can, for example, assign to a 778 00:40:11,369 --> 00:40:18,369 variable and then do manipulations on it. Uh 779 00:40:22,608 --> 00:40:26,199 huh. If you don't fill in [inaudible] something like set color to change the entire background of the graphics [inaudible]? Well, you can make the huge rectangle the size of the graphics 780 00:40:26,199 --> 00:40:29,180 window and set it to be filled and set it to be a color and then you can 781 00:40:29,179 --> 00:40:35,788 change the whole background that way. Uh-huh? Can you then change the size of the graphics window? 782 00:40:35,789 --> 00:40:39,219 There is some stuff for later on, but for right now, just think of a fixed size 783 00:40:39,219 --> 00:40:40,599 graphics window. Yeah, 784 00:40:40,599 --> 00:40:43,559 so let me push on just a little bit if we can 785 00:40:43,559 --> 00:40:46,650 because what I want to actually show you now is a graphics window that 786 00:40:46,650 --> 00:40:50,189 makes, or a graphics program that makes use or a bunch of this stuff 787 00:40:50,188 --> 00:40:53,828 so over here, here's a little program called fun graphics, okay. 788 00:40:53,829 --> 00:40:57,869 And so we have a comment up at the top and here are all the imports you need. Notice I 789 00:40:57,869 --> 00:40:58,980 [inaudible] for you. 790 00:40:58,980 --> 00:41:02,559 We need acm.graphicstar because we're gonna write a graphics program. 791 00:41:02,559 --> 00:41:06,619 We need acm.program.star because all programs that you write in this class are gonna 792 00:41:06,619 --> 00:41:07,339 need that. 793 00:41:07,340 --> 00:41:12,260 And because we're gonna use colors we additionally need Java.awt.star. 794 00:41:12,260 --> 00:41:15,080 If we weren't gonna use colors, we wouldn't need that, but it's needed for the 795 00:41:15,079 --> 00:41:22,079 colors. Uh-huh. 796 00:41:23,239 --> 00:41:26,068 [Inaudible]. It did; it might have just been hidden 797 00:41:26,068 --> 00:41:29,338 because sometimes it just appears hidden until you expand it out, but it actually 798 00:41:29,338 --> 00:41:30,328 had it in there. 799 00:41:30,329 --> 00:41:33,548 So here's what we're gonna do. Let me just run this program, show you what 800 00:41:33,548 --> 00:41:36,818 it displays on the screen and then show you how we actually get that display, so it's just 801 00:41:36,818 --> 00:41:41,150 like, "Hey Meron, you're just drawing garbage on my screen," no, there's really like 802 00:41:41,150 --> 00:41:43,829 a method to this madness. Not really, 803 00:41:43,829 --> 00:41:47,360 there are multiple methods to this madness [inaudible]. I 804 00:41:47,360 --> 00:41:49,030 805 00:41:49,030 --> 00:41:50,139 know, 806 00:41:50,139 --> 00:41:51,338 sometimes 807 00:41:51,338 --> 00:41:54,409 computer scientists should not be - that was actually not a canned joke. 808 00:41:54,409 --> 00:41:58,679 That just came tome - how horrible is that? 809 00:41:58,679 --> 00:42:00,579 So here is what's going on. 810 00:42:00,579 --> 00:42:03,038 We drew a bunch of stuff on that screen, how did it get there. 811 00:42:03,039 --> 00:42:06,679 Well, hey Hello World; we probably knew how that got there. This should all be familiar to 812 00:42:06,679 --> 00:42:08,838 you now. We got a new G-label. 813 00:42:08,838 --> 00:42:12,759 We had some variable called label that we assigned it in. We set its font. We set its 814 00:42:12,759 --> 00:42:15,900 color to red. Because we set its color to red, that's why we need 815 00:42:15,900 --> 00:42:18,980 www.Java.awt because we're using colors. 816 00:42:18,980 --> 00:42:22,969 We set its color to red and we said, "Hey, I got Hello World, throw it up on the 817 00:42:22,969 --> 00:42:26,389 screen," so we add label. You give it the name of the variable and 818 00:42:26,389 --> 00:42:27,539 there it appears. 819 00:42:27,539 --> 00:42:29,409 Over here we say, "Hey G-rect. 820 00:42:29,409 --> 00:42:33,779 I want a new rectangle that's upper left-hand coordinate is 10,10 and 821 00:42:33,780 --> 00:42:38,360 it's size is gonna be 50,50, which means it is a square. It's not filled 822 00:42:38,360 --> 00:42:41,960 in. I haven't specified its color right," so if you don't say set fill, it's not automatically 823 00:42:41,960 --> 00:42:44,858 filled in. If you don't specify the color to automatically black 824 00:42:44,858 --> 00:42:45,848 and then I add it 825 00:42:45,849 --> 00:42:50,949 and then you get that little box all the way up in the upper left-hand corner. Then, I say, "Hey, 826 00:42:50,949 --> 00:42:54,628 you know what, I want some other G-rect because it's cooler." It's gonna have 827 00:42:54,628 --> 00:42:58,469 colors and it's gonna be bigger and better and it's gonna be nationwide, 828 00:42:58,469 --> 00:43:01,349 and so what it's gonna do is I'm gonna have rect 2 which is 829 00:43:01,349 --> 00:43:05,369 another variable of Type G-rect. It's perfectly fine for me to have multiple 830 00:43:05,369 --> 00:43:08,058 objects of the same type which they knew G-rect 831 00:43:08,059 --> 00:43:12,829 that's upper left-hand coordinates is 300,75 and whose size is 832 00:43:12,829 --> 00:43:16,539 honking - it's 200 by 100, so it's big long thing, 833 00:43:16,539 --> 00:43:19,939 and it's gonna be filled in and I'm gonna set its fill color to be red, so the 834 00:43:19,938 --> 00:43:21,308 whole thing is red, 835 00:43:21,309 --> 00:43:22,819 throw it up there 836 00:43:22,818 --> 00:43:25,358 and so what I get is that big, rectangle. 837 00:43:25,358 --> 00:43:28,969 Just ignore the big oval one in front of it for now; I get this big red 838 00:43:28,969 --> 00:43:29,869 rectangle up there. 839 00:43:29,869 --> 00:43:32,960 And I say, "Well, that's kind of cool, but I want to see what this oval thing is all 840 00:43:32,960 --> 00:43:33,809 about. 841 00:43:33,809 --> 00:43:36,259 So Hey, Meron, 842 00:43:36,259 --> 00:43:37,690 make me an oval." 843 00:43:37,690 --> 00:43:41,920 And I'm like, "All right, well, G-oval will make you an oval," and the dimensions of the oval, 844 00:43:41,920 --> 00:43:45,990 its upper left-hand coordinate and its size are exactly the same as the 845 00:43:45,989 --> 00:43:49,199 rectangle, and the reason for doing that is to show you the oval in relation 846 00:43:49,199 --> 00:43:51,219 to the rectangle that you specified. 847 00:43:51,219 --> 00:43:53,058 It's set filled is true 848 00:43:53,059 --> 00:43:57,380 and it's set fill color, not its set color, but its set fill color 849 00:43:57,380 --> 00:44:00,869 is set to be green, which means the internal dividend will be green, the outline 850 00:44:00,869 --> 00:44:04,359 of the oval will still be black, because we did not set its color and then we 851 00:44:04,360 --> 00:44:04,939 added 852 00:44:04,938 --> 00:44:08,739 and there you get that green oval and you can notice its four ends touch the same 853 00:44:08,739 --> 00:44:10,130 four ends as this rectangle 854 00:44:10,130 --> 00:44:14,349 and if you look real closely there's actually a green line that demarcates the 855 00:44:14,349 --> 00:44:17,660 oval and then the middle fill is actually green. It might be a little hard to see, but 856 00:44:17,659 --> 00:44:18,788 that's the case. 857 00:44:18,789 --> 00:44:22,230 Then we also have a line, so I will just call this my funky line, 858 00:44:22,230 --> 00:44:26,400 and my funky line is a line that starts at 100,150 and it extends to 859 00:44:26,400 --> 00:44:29,099 the location 200,175 860 00:44:29,099 --> 00:44:31,208 and there is my funky line. 861 00:44:31,208 --> 00:44:33,328 Yeah, it's a funky line. All right, 862 00:44:33,329 --> 00:44:35,548 and then I add another thing and you're like, 863 00:44:35,548 --> 00:44:37,248 "Dude where's my line." 864 00:44:37,248 --> 00:44:41,548 And so Dude where is my line is a line that should cut across the entire graphics window 865 00:44:41,548 --> 00:44:45,088 because it starts at 00, well not the entire graphics window. It starts at 00 866 00:44:45,088 --> 00:44:48,288 in the upper left-hand corner and goes to 100,100, and this is a common error which is 867 00:44:48,289 --> 00:44:49,999 why I how it to you. You look up there and you're like, 868 00:44:49,998 --> 00:44:54,308 "But Meron, there's only one line," and that's your funky line or my funky line as the case 869 00:44:54,309 --> 00:44:55,180 may be. 870 00:44:55,179 --> 00:44:57,989 Where is Dude where is my line? 871 00:44:57,989 --> 00:45:01,378 And it's not up there. Why is it not up there? [Inaudible]. 872 00:45:01,378 --> 00:45:05,099 Yeah. You did not add it to the graphics content, okay. 873 00:45:05,099 --> 00:45:08,279 Common error - people will go ahead and create something and add all the colors and set all 874 00:45:08,280 --> 00:45:12,200 the sides and be like, "Ah, I'm rocking," and then they'll run their program and nothing shows up and they 875 00:45:12,199 --> 00:45:16,538 start tearing their hair out and beating their section leader and it's a bad time, and they're sitting there 876 00:45:16,539 --> 00:45:17,930 contemplating life in jail 877 00:45:17,929 --> 00:45:19,298 and then they realize, 878 00:45:19,298 --> 00:45:22,659 "I just should have added it to the canvas, all right." 879 00:45:22,659 --> 00:45:26,769 But you can't give that as an excuse to the judge. All right. So any questions about 880 00:45:26,768 --> 00:45:28,358 that? 881 00:45:28,358 --> 00:45:31,858 All right. Let me very quickly - I need to push on real quickly. So I want to tell you 882 00:45:31,858 --> 00:45:35,068 a little bit about expressions before we 883 00:45:35,068 --> 00:45:37,958 finish up and we'll do more expressions, but 884 00:45:37,958 --> 00:45:42,608 you need to know some basic stuff on expressions. 885 00:45:42,608 --> 00:45:47,548 So all an expression is is you already saw one. And expression is something like INT 886 00:45:47,548 --> 00:45:52,009 total, equals N1 plus N2. This is an expression. 887 00:45:52,009 --> 00:45:55,829 It's just some mathematical expression that tells us how things kind of relate 888 00:45:55,829 --> 00:45:56,729 to each other. 889 00:45:56,728 --> 00:46:01,038 And basically all an expression is it's a bunch of terms and these terms can be either 890 00:46:01,039 --> 00:46:02,440 individual variables 891 00:46:02,440 --> 00:46:07,409 or they can be constant values, like they ca be like the Number 2 or 3 892 00:46:07,409 --> 00:46:11,499 or they could be some method called like Read INT that actually gets a 893 00:46:11,498 --> 00:46:14,178 value from the user, for example, 894 00:46:14,179 --> 00:46:17,898 and there's some operators between them, like the addition operator, and so when 895 00:46:17,898 --> 00:46:21,548 you take some terms and you put operators between them to combine the 896 00:46:21,548 --> 00:46:22,119 terms 897 00:46:22,119 --> 00:46:25,450 that's called an expression, all right. Fairly straightforward stuff - you've probably seen these. 898 00:46:25,449 --> 00:46:27,639 There's a bunch of operators you should know about. 899 00:46:27,639 --> 00:46:31,539 The operators are things like plus, minus, 900 00:46:31,539 --> 00:46:36,778 times, which is actually a star or asterisk, which is how we refer to times. It's not as X - 901 00:46:36,778 --> 00:46:40,838 division and then strangely enough, this thing's that looks like percent and it's 902 00:46:40,838 --> 00:46:44,778 not the percentage operator, although you might think that. This is what's known as 903 00:46:44,778 --> 00:46:50,268 the remainder operator, okay. So these puppies generally work like you 904 00:46:50,268 --> 00:46:54,838 would think, add two numbers, subtract two numbers. There's also a uniary minus. 905 00:46:54,838 --> 00:46:58,009 The uniary minus is the funky mathematical way of saying negative sign. 906 00:46:58,009 --> 00:47:00,009 So you could actually say like 907 00:47:00,009 --> 00:47:05,449 equals negative N1 plus N2. That's like a negative minus. That's perfectly fine. 908 00:47:05,449 --> 00:47:09,579 Okay, or you can use that same thing, you know, as subtraction if 909 00:47:09,579 --> 00:47:11,959 it's between two terms. 910 00:47:11,958 --> 00:47:12,899 Multiplication, division, 911 00:47:12,900 --> 00:47:17,259 the thing about division that is funky is it depends on how division is used, 912 00:47:17,259 --> 00:47:20,009 okay and I'll tell you a little bit more about that next time, but I want to talk 913 00:47:20,009 --> 00:47:21,769 about remainder right now. 914 00:47:21,768 --> 00:47:24,649 And remember back in second grade, when you did like 915 00:47:24,650 --> 00:47:26,340 short division and you said, 916 00:47:26,340 --> 00:47:30,759 "Oh, I had 5 and 2 - how many times does 2 go into 5 - 917 00:47:30,759 --> 00:47:35,838 2 and you were like remainder 1. Anyone remember that? 918 00:47:35,838 --> 00:47:39,048 Wasn't that such good time? I loved remainder - like 919 00:47:39,048 --> 00:47:42,438 that's the online like math. I'm like - and when the remainder was 0, I 920 00:47:42,438 --> 00:47:46,139 got pissed. 921 00:47:46,139 --> 00:47:49,748 But that's what remainder was all about. It's just what's left after you do the 922 00:47:49,748 --> 00:47:53,438 division. So that's the way you should think about percentage. It's the remainder 923 00:47:53,438 --> 00:47:55,408 operator, and you're like, "So how does that work?" 924 00:47:55,409 --> 00:48:00,019 So what that means is if I say 7 remainder 2, 925 00:48:00,018 --> 00:48:05,438 the value of this is 1 because it's after I divide 7 by 2 and I see 926 00:48:05,438 --> 00:48:08,858 how many whole times 2 goes into 7, what's the remainder, 927 00:48:08,858 --> 00:48:09,748 it's 1, okay? 928 00:48:09,748 --> 00:48:13,788 You could say something funky, like, hey what's 7 929 00:48:13,789 --> 00:48:14,770 remainder 20? You're 930 00:48:14,769 --> 00:48:20,380 like well, 20 doesn't go into 7, Meron. So that would be 0 remainder - 931 00:48:20,380 --> 00:48:22,088 rock on. 932 00:48:22,088 --> 00:48:24,058 So that's the way it works. 933 00:48:24,059 --> 00:48:27,309 It's not the modulus operator. If you're a mathematician, or if you've worked with 934 00:48:27,309 --> 00:48:31,119 other languages and you've seen clusters and you're like, "Oh, that's the modular operator." 935 00:48:31,119 --> 00:48:35,189 It's not in Java; it's remainder an dif you try to apply it to negative numbers, 936 00:48:35,188 --> 00:48:38,818 unexpected things, what's not unexpected; it's just not what you would have expected in the 937 00:48:38,818 --> 00:48:41,378 mathematical sense. So as far as you see it in the book and everything we do in 938 00:48:41,378 --> 00:48:44,929 this class, just apply it to positive integers and you can only apply it to 939 00:48:44,929 --> 00:48:48,568 integers so there's no remainder with valued doubles, 940 00:48:48,568 --> 00:48:51,518 there's no remainder. And so when we come back next time we'll talk a little bit 941 00:48:51,518 --> 00:48:53,679 about some of these expressions. 942 00:48:53,679 --> 00:48:56,328 Any questions before we break? All right, and 943 00:48:56,329 --> 00:48:58,999 I'll see you - oh, there's a question. Why don't you 944 00:48:58,998 --> 00:49:00,358 just come up and ask it. Thank you.