All righty. Letfs go ahead and get started. A couple of quick announcements before we start today ? so hopefully youfre all busy working away on Karel and life is good. Just quick poll ? how many people have actually finished Karel already? Oh, yeah. I wonft ask how many people have not yet downloaded Eclipse. There are no handouts today. Gettingf a little breather ? no handouts. Donft worry; youfll get some more of that next time. Sections start this week, so hopefully you all should have gotten an email about your section assignment and who your section leader is, so you can actually do Assignment No. 1, the email portion. You should have been able to do the programming portion the whole time. But make sure to go to section this week. And the other thing is the Tresidder Layer, which every once in awhile youfve heard me refer to. This is a computer cluster up in Tresidder. Is staffed by one of six helpers like almost continuously around the clock or most of the times at reasonable times when people are working. So Sunday through Thursday, every day except Friday and Saturday because contrary to popular opinion, computer science people actually do have lives or we actually like to pretend we have lives, but every day from Sunday through Thursday 6:00 p.m. to midnight there will be a staff of helpers on there and actually some of the times there is like two or three or four people there. And theyfre there just dedicated for the 106 classes. Theyfre not like general consultants. Theyfre just there to help you work out problems in this class, and they know like what assignments youfre working on, the whole deal. Theyfre all like, your section leaders and theyfre all be exceptionally trained to do this. The other thing thatfs going on, hopefully you should be doing Assignment No. 1. Ifve actually gotten a bunch of Assignment No. 1 emails that have already come in. In the early days, when I got the first few, I actually tried to respond to them all, but then at some point, I just woke up and I like, you know, went to my computer and was like, gOh, youfve got mail.f And it just [inaudible]. So I couldnft respond to everyone individually. I apologize if I donft respond to you individually, but I do read them all. I guarantee you that I actually read them all and I look at backgrounds. And just to prove to you that I do, herefs some interesting ones that have come in so far ? just to share three. So therefs someone actually spent their time in Taiwan living in a Buddhist monastery, which I thought was interesting, except for the fact they were actually living there as a monk whom I thought was pretty interesting. Someone else used a ? I wasnft quite sure on this concept, but maybe I can provide a clarification. There was a vegetarian who only eats low-quality meat, and so he mentioned that as things like burgers and not steaks. And I would qualify that by saying thatfs not a vegetarian; thatfs called being a grad student. And last, but not least, there was actually someone in here whofs on the Colbert Report, which I thought was actually pretty interesting. I donft know in what context, but come talk to me afterwards. So with that said, any questions about anything before we start? Today wefre actually gonna go over some of the graphic stuff you saw, talk a little bit more about objects and classes and get into variables and values and all kinds of goodies. Any questions? All righty, then letfs just dive right in. So one of the main topics for today is this thing called a variable. And a variable, you know, like variables come up in mathematics and itfs like, oh X and Y are variables, right and there are these things and we do all these manipulations on variables. In the computer science world, theyfre really friendly, right, and you donft have to worry about integration or differentiation or you know, those kind of variables. Variables are kind of your friend, and basically all the variable is in the computer science worlds is itfs a box. Itfs a box where we stick stuff and the stuff we stick into that box can change. Thatfs why we call it a variable because itfs a box that has a variable contents, and you think back you know, in the days of yore in math, and you know, oh yeah, itfs kind of like X can have different values, yeah, itfs basically just like that. So in computer science, what we think of as a variable, is each variable has three things associated with it. Itfs got some name, and thatfs just how we refer to that particular box. It has a type, which is something a little bit different in mathematics, but the type basically says what kind of thing does this box store? Some boxes store numbers; some boxes store letters; some boxes will store other things, like little objects in the world. But a type is just whatfs stored in that box. And then therefs a value, and the value, as you can imagine is just whatfs in the box. What is the actual thing thatfs in there, right? If it stores the number, then it might store, for example, the Value 3 and thatfs just the value, and it may have some name associated with it. And how do we actually name these? Therefs actually a rule and itfs not a very complicated rule, but a very simple rule you need to remember for what are valid names for variables in Java. So a valid name, so this is how you actually name these puppies, has to start with a letter or an underscore. So it starts with a letter like one of the alphabetic letters and can be upper or lower case or the underscore character. Okay, and thatfs kind of you know, underscore. Itfs down at the bottom of the line, okay? And then after you have that initial letter or underscore, then after that, you can have any number of letters, numbers, thatfs like the number digits, like 1, 2, 3, 4, you know, 0, etc. or underscores, okay? So you canft start with a number. You have to start with a letter or underscore, but for most purposes in this class, just thing about them as letters. When not actually using underscores, you might occasionally use numbers. You can actually have numbers after the first letter. There is one slight caveat to this rule which you canft have any variables thatfs name is the same as some which known as a reserved word in Java, which means its name canft be the same as some special word in Java, like the word class is a special name in Java, and therefs actually a page in your book, I think in Chapter 2, that lists all the special names. Itfs like out of the English language it has about 127,000 words. I think there is like 40 in Java that you canft make a variable name. Okay, you have lots of other choices. As a matter of fact, therefs lots of things that donft have to be valid words in English. They can just be any name that follows this rule. The important thing to think about in terms of a name and this is one of the good software engineering principles, is make your name descriptive. If you have a program thatfs maintaining for example, the balance in the bank account, a real good name for the place where you store the value of the balance would be something like gBalance.h A real bad name would be something like gA,h because no one knows what A is. Itfs like hey itfs A ? yeah, I know what A is. And someone says, gYeah, A is balance,h and someone says, gNo, no, no, in my program, A is actually how many miles I bicycle today,h and youfre like, gNo, no, no, man, A was balanced.h Well, if itfs called Balance, therefs just no ambiguity, so give them descriptive names. So thatfs kind of a name part of this. The next thing is what is this type all about. What are the different types that you can actually have. And there are some things that we refer to as primitive types. These are the types that have the smaller developed brains and use knuckles drag on the ground. No, there just the types that are built into Java, okay and some of the basic types we have is something we call an INT, which is short for an integer, but we actually write INT, so INT, I-N-T, is the name of the type, okay. And this is just an integer of value. Itfs just gonna store some whole number basically. It stores a number between minus 2 billion and plus 2 billion, but for all intents and purposes, you can just imagine you can store any integer in there, okay? Therefs also besides integers what other kinds of numeric data do we have? People already know, itfs like I would think it would be like real values, but a bunch of people are already saying double because youfve read ahead and youfve done the assignment the way you should. And that was kind of a mini social, but both went to the same person. Therefs this thing called a double and a double is actually some real valued numeric value, right. Itfs something like 2.3 is a double or even 2.0 can be a double, okay? Why is this thing called a double ? anyone know, as opposed to like a real? Uh-huh? [Student:][Inaudible]. Yeah, therefs this wonderful verb people call the I triple E, which is like the Institute for Electrical and Electronic Engineers and they come up with all these standards for things. Is anyone a member of I Triple E here? No one? Oh, man, join and pay your dues. Itfs a good time. I'm not actually a member myself. But what those folks actually do is they come up with standards for things and one of the standards they came up with is how you represent numbers that are real valued numbers inside a computer, right, because remember a computer only understands ones and zeros, so how do you actually represent a real valued number. And so therefs a standard, and part of that standard has to do with a precision of the number, how many digits in some sense and a rough approximation you store and double stands for a double precision real number. And so for the purpose of this class, all real valued numbers that wefre gonna use are just of type double. Okay, therefs a couple of other types that Ifll just mention now and wefll actually go into them in much more detail in a couple of future classes. One is called Boolean. And if youfve ever heard of Boolean logic, this is a logical value, so this is just essentially true or false, and wefll talk about that in excruciating detail next time, but Ifll just let you know that therefs a type called Boolean. Therefs also a type called care or Car and as you can imagine itfs because we like sort of the first syllable of most things and this is the first syllable of character, okay, and so wefll also talk about this in a couple of weeks time when we actually get into a some things with characters, but thatfs just a character. Itfs a variable; itfs a box, but still is a character. Itfs a box that stores an integer, box that stores a real value, box that stores true or false and a box that stores a character. Those would be the different types of them. So one thing people ? so thatfs kind of types, at least some of the basic types. And then when you think about value that we actually store in this box, people always get uncomfortable when they see INTs and they see double, right, and they sort of say, gBut Meron, like 2 is an integer, right?h And I'm like, gYeah.h And theyfre like, gBut you just told me 2.0 is a double, right?h And I'm like, gYeah, 2.0 is a double.h And so the natural question is why? Why do we have both these things? How come like all integers arenft subsumed by the real values, right, youfre kind of the mathematician type and youfre like, gYeah, therefs like strictly more of these than there are of these. So why are all these like subsumed in here. Why do we have this integer type,h okay? And the reason we do boils down to a simple question and the simple question you want to ask yourself is how much versus how many, okay? So if you ask someone, letfs say you, ask me, just so I wonft embarrass you, gHow much do you weigh, Meron?h [Student:]How much do you weigh, Meron?h I weigh about, you know, 155.632 pounds, okay? That makes perfect sense, right. If I can put a decimal point, I can put as many numbers after it, or I could just say 156, right and those are both valid kinds of things. Now you could ask me, gHey Meron, how much children do you have?h I have 2.3 children. Does that make any sense to you? Yeah, itfs like we had three until that grisly accident. No, itfs just that therefs sometimes ? I know thatfs horrible to say. We actually have one, and hefs just fine. Therefs sometimes in the world when you care about counting, and when you care about counting, it doesnft make sense to have fractional values. Those things are integers. Theyfre a how many kind of value. When youfre thinking about how much, thatfs a double value, and you actually want to keep these distinct because if I ask you whatfs the next number after 1, you say ? [Student:]2. 2 because youfre thinking integer, and thatfs perfectly right and if I say whatfs the next number after 1.0, you say ? [Student:][Inaudible]. Yeah, you start mumbling and youfre like, gWell, I know itfs not 2.0. That would have been the obvious answer and thatfs probably not it. How about 1.0000001, and I'm like, gNo you missed a couple of zeros in there. Just keep going. Wait until the end of the quarter and put that 1 and come back and talk to me.h In real values, there is no next value, okay, so when you care about counting, it doesnft make sense to use a double. Thatfs when you want to use an integer, so having these things be a distinct type actually make sense and youfll kind of see that as we go along, okay. So the value is basically just what youfre actually gonna put into this box over here and itfs gonna be some of the values that we actually talked about, so let me show you some examples of how we might use variables, what the syntax for variables actually is in Java. So what we need to do with variables before we use them in Java is we need to declare a variable. Itfs kind of like declaring your love. If youfre gonna have some box somewhere thatfs got something stored in it, you need to come tell the world, gHey world, Ifve got this thing called X. Itfs cool. Check it out.h Herefs how you do that in Java. You say INT X, okay? What that does is you specify a type for your variable, whatfs the box gonna hold. You specify a name for that variable. In this case, the name is X and because all statements end with a semi colon, just like you saw in Karelfs world, we put a semi colon. What does this do? It creates a box somewhere in the computerfs memory thatfs name is X thatfs gonna store an integer in it. Right now, what does it store? [Student:][Inaudible]. Nothing. We havenft told it anything to store in particular. And so in Javafs world, unless you initialize you always want to think about initializing a variable, giving it some initial value to start off with and therefs some rules that wefll talk about as we go along regarding when Java will automatically sort of give you a zero value in there and therefs time when it doesnft and sometimes it can get a little bit confusing. So the general rule you want to remember and itfs also good software engineering is give variables an initial value when it makes sense, okay? So how might we give it an initial value? Well, on that same line, rather than just putting the semi colon there, we could say X equals 3 and what that does is it gives it an initial value of 3 to x. Okay, so you could actually have the declaration of the variable without the initial value, but you always need to give it an initial value before you use it, so a lot of times we just give it the initial value when we actually declare it, okay. If we want it to have something of type double, maybe something called Y, we could have something called Double Y equals 5.2 and what that means is somewhere in memory we have this thing called Y, which has value 5.2 in it, and itfs just a real value. So the general form for how you do these declarations is you say type and Ifll put little squiggles underneath these just to let you know that this is the general form, the name and then potentially an equal and some initial value, followed by the semi colon, and thatfs how you do the declaration. And so the question that comes up where do you actually make these declarations and the place you generally at least for the time being make the declarations of value, for declarations of variables is inside methods. So when you create some method like you have public, void, run and youfre gonna put everything thatfs gonna go inside run in there, you could, for example, have this line in there and this would create X with a value of 3 and Y with a value of 5.2 and you wouldnft have the general form there, but for example, you could have those declarations inside run and what that means is you have those variables created with some values available to you inside this method called run. Theyfre only available to you inside that method called run. Theyfre not available to you in some other method and wefll talk about how you get values passed around between different methods in a few days, but for the time being when you have your variables theyfre only available in that method. Okay, so any questions about that ? variables or values? Uh-huh? [Student:]If you have them inside the public run method [inaudible], will that method be able to take that variable? Well, you wonft be defining another method inside run, right; youfll be defining another method thatfs separate from run. Itfs just a separate method. It wonft be available in that separate method. It doesnft matter if itfs public or private. It just not available there. So this public or private thing that you have here doesnft affect the visibility of your variables. Your variables are always only visible inside the method in which theyfre declared and set up, okay? Now, one other thing that we did when we sort of, you know, did this equals thing over here, if we wanted to, we could have actually done this in two lines rather than setting up X initial value there, we could have said into X and then we could have set X equals 3 over here and gotten the same effect. And when we say X equals 3, this equals is not equals in mathematics; itfs actually whatfs known as an assignment statement in Java or in most programming languages. And the idea is wefre taking some value over here. Thatfs whatfs on the right-hand side of the equals and assigning it to whatever variable is listed over on the left-hand side of the equals, so wefre assigning that value, okay? And so the general form for an assignment, letfs just do that over here, is pretty straightforward, you just saw it, is variable and this is the name of the variable. So Ifll squiggle ? equals some value or some expression for a value and wefll talk about expressions in just a second with a semi colon at the end of it, and thatfs how you assign a value. You can assign values multiple times to a variable, to a variable. Itfs a variable, the contents change. So you say, gHey, X equals ,h and then down here somewhere, gHey X equals 4,h and when you get to this line up until you get to that line from here to here, X will have had the value 3 and when it gets to this line, itfll stick a 4 in the box. And thatfs perfectly fine because X is a variable. As long as everything you stick in there is of the type of which X is declared, youfre okay, okay? So what that also means is when youfre doing Assignment you can do things that are perfectly fine in programming, but to mathematicians, they just go nuts; they go crazy like veins just burst in your head and you hear these popping noises and people dying in the streets, which is you can say, gHey, I have some variable called total. Let me create some variable in its total,h which Ifll give some initial value like 10 and thatfs a good time. And then I say, gHey total equals total plus 1.h And if youfre a mathematician, you look at that and say, gNo, man. Total canft equal total plus 1. Thatfs just not right.h And then you got this whole philosophical question about, you know, if this is like total is equal to infinity and youfre just like, gNo, no, no, thatfs not what wefre talking about.h Wefre talking about assignment. This is into equals, this is an assignment. So what it says is evaluate the right-hand side, okay? Total ? what was totalfs previous value. Go and look it up in the box over here you have some box for total. Its value was 10. It looks it up. It says, gOh, that was 10.h I'm gonna add 1 to it, that gives me 11. What do I do with that 11? Stick it back in the box named Total. So it just says okay, and it puts an 11 over here and thatfs perfectly fine. So itfs perfectly fine to do something like then where you take the old value for a variable, you do some manipulation on it, you stick it back in the same variable. Okay? So with that said, now that you know about variables, we can go back to a couple of the programs that you saw last time and things ill now begin to make a little bit more sense, so if you come to the computer, remember our little friend, add two integers that you saw last time. Suddenly the world opens up a little bit and we say, yeah, add two integers is a console program, right. So itfs gonna write some text out to a console somewhere and we have our run method. We print out a line; we know that the method print [inaudible] when we say that, whatever we put inside the double quotes gets printed to the screen so it prints out this program adds two numbers, so we execute that line and after we execute it, it writes it out and it comes to the next line. Low and behold, what have we done here? Wefve declared a variable named N1. Whatfs the value for that variable that wefre gonna assign to it? Wefre going to call some method. The method wefre gonna call is something called Read INT and this is a method thatfs provided for you in console programs. All console programs can do this. The way it works is you give it some text between two double quotes. It writes that text on the screen just like you saw last time and asks for input from the user. Whatever value the user types in and hits enter, that number is the value that gets what we call return by read INT. Itfs the value that [inaudible]. Itfs kind of giving it back to you. Do what you want. What are you gonna do with it? Youfre gonna assign it to the Box N1, so herefs the box for N1. We execute this statement. Itfs coming; itfs gonna print to enter N1 on the screen. Itfs gonna ask us for a value. Letfs say we type in the value 17 and it sticks that 17 so this expression over here evaluates the 17. It sticks 17 in the box for N1, goes to the next line, now it declares N2. We have some box. Its initial value also is gonna come from the user via this read INT method. So we call read INT. It asks us for another value; we type in 25, and it sticks that in the box. Now, wefre gonna declare another; we just declare until the cows come home, right? Declaring is fun; itfs cheap. Itfs easy; do it, do it often, right, so wefre gonna declare another variable total, so we have another box over here for total and the value of total that es gonna get assigned there is just whatever value is in N1 plus N2. So to evaluate this side, it says I need to look up the box N1, so at the point where it reaches this line, it evaluates whatever is in N1. This isnft some truth that holds for all time. Itfs just an assignment that it does when it reaches that line. So it says, find out whatfs in N1, add it to whatfs in N2. Thatfll give you the value 42; stick that in Total, which is what you get. And the last line says, I'm gonna print something to the screen where the total is and you see these funky plusses and youfre like, gWhoa, whoa, whoa Meron, I thought plus was light lading things together. Whatfs going on? Youfre like trying to add ? this thingfs got the value of 42. I know that because itfs in the box. Youfre trying to add 42 to some text? Thatfs not right.h Well, in most worlds, it would not be right, but it turns out in the Java world itfs perfectly fine and when it sees something that some words with a plus sign, this plus sign here is not numerical addition anymore. This is essentially you can think of as a concatenation operator. It says, gTake whatever text is here and add to that text, the textural form of this value.h Well, the textural form of 42 is just the Characters 4 and 2, so it sort of adds that 42 here and then it adds to the end of that a little period, so it concatenates all those things together and then prints them out and says the totals 42. So any questions about the program? If youfre feeling good, like you understand what just went on in the program, nod your head. Rock on. Thatfs what I like to see. All right. So with that said, we can go back to something else we did last time, right, so now you know how all this variables and assignments and types and youfre like, gHey, Meron, yeah, thatfs all good and well, but last time you were telling me about like classes and objects and drawing stuff on the screen like, whatfs up with all that.h And this is the time when we actually do sort of the, you know, Reesefs Peanut Butter Cup. Like itfs two great tastes that taste great together. We take some objects over here and some variables over here and we just slam them together, and now youfre going to have variables that contain objects. They taste great, all right? So here are some of the objects that you considered last time, right? We had things called, like some classes. You have the G label class and the G ?rect class and the G-oval class. These are all these graphics classes like a label [inaudible] or rectangle or an oval. Ifll show you examples of all these things this time. And these guys were actually all classes that are part of a hierarchy of other classes and the other classes, theyfre all part of is G object? So what G-object is a G-object is a class. Donft let the name fool you. Some people see G-object and they think, gOh, that must be an object.h No, G-object is a class. It just means a graphics object. And guess what, a rectangle or an oval or a line or a label or all different graphics objects. So what that actually looks like in terms of the hierarchy just like you saw last time, is the classes are represented, kind of like this in the hierarchy, all of these classes are actually G-objects. Okay, theyfre all sub classes of G-objects. It means theyfre specializations. So any behavior that G-object has, all of these things have, but they might have their own specialized behavior, as well. Now, the interesting thing about all this is that when you have classes and you create objects, you can store those objects in a variable. You can say, gGive me a variable that is a box,h and the type that that box holds is an object thatfs the type of some class, so all classes can actually be used as type means, okay? Now, thatfs kind of funky. Let me show you an example, okay? Here is another program you saw last time. Now, we can just kind of reveal the mystery about variables. We have this program, Hello program that extends the graphics program, right, so itfs gonna draw us some pictures. Well what are we actually doing on this first line? Hey, wefre doing a declaration and assignments on the first line. Whatfs the type of our variable? Itfs a G-label. So we use the class name as the type. The name is label, so we have a box called label and what that box is gonna hold is any object of the class G-label. Okay, so thatfs the type. Itfs gonna hold any object. And the question comes up, how do we get an object of type G label. And herefs the funky part. The way we get an object of type G label is we have to ask for a new G-label. So we give the name of the class here and class is depending on the class, you know, see some examples of this will take in or give in some initial values for what we refer to as parameters, and these parameters are all separated by commas. But at some initial values, that this object uses to sort of set itself up. It initializes the objects. So objects rather than just taking one value, can potentially hold multiple values in them. In this case, the G-label is gonna have Hello World as some text that itfs gonna display in some location thatfs 100,75. So we say, gHey G-label, youfre the class. I donft have an object of you yet. Youfre the class.h When I say, gGive me a new G label, itfs sort of like going to the factor. You sort of show up at the G-label factory and you say, gYo, G-label factory. Give me a new object,h and G-label factory is sort of sitting up there and says, gWell, what kind of new object do you want? Yeah, I can give you a new G-label, but how is that initial G label going to look,h and youfre providing these parameters that sort of specify what the initial G-label looks like. So this line is now created a G-label, okay? And it has some initial, so when we execute that line, it has some initial text that itfs gonna display and somewhere itfs also storing the value is 175 because it knows where thatfs gonna be on the screen. Now, what wefre gonna do is wefre going to tell that object to do some additional stuff. The way we tell an object to do something is we specify the name of the variable for the object, okay. So the object name is label. Thatfs the name of the variable. And so thatfs what we say here. We donft say G-label. We say the name of the variable label. Then we have a dot; then we have the name of the method that wefre going to call for that particular object. So itfs a little bit different than Karelfs world, all right. In Karelfs world, every time you call the method, Karel was the one executing that particular method. Here when you have multiple objects, you need to specify which object you want to execute this method. So you say, gHey, label, yeah, I'm referring to you, buddy. Set your fonts to be this particular font, san seraph which you saw last time.h It just makes it big. It makes it 36-point font. And then you say, gHey label, yeah, you, same guy, or gal or it or whatever it may be. Set your color to be red.h And it says, gOkay, Ifll set my color to be red, and then finally if I want to take this label and stick it up onto my graphics canvas which is actually the program I'm looking at. Right, Ifve done all this stuff and my programfs still bringing out a blank screen. Itfs like here you go, nothing going on here. I say add and the name of the object that I want to add, right because now I'm holding this little object called label. This is my little felt Hello World, and I want to say, gHey take the felt hello world thatfs so cute and just stick it in the world,h so we add it to the graphics program, basically and it shows up in the screen where it knows itfs supposed to show up at a 100,75. So the general form for this, the people will refer to when you see something like this, you have name of the variable and the method name and the Java-esc names that we give to these, just so we hear other programmers refer to them, you know what theyfre talking about, is they say the object is the receiver. So the object here is the one whofs receiving or being told to do something, okay? So the name of the variable is the receiver and the method thatfs being called is called the message and so therefs this whole notion of sending messages, kind of like you could think of programming as I am, right, and in your little I am you have like your friend, Bob, and your friend, Sally and your friend, Dave, and your friend, Label, right. And Label is just sitting there in I am and Label is not very talkative and once in awhile you can send a message to I am which is a form of message that label understands and then Label will do something. So this is called the message that we send to the receiver, is kind of the nomenclature. But a lot of times, youfll just hear people say, I made a method call on the object and it means exactly the same thing. Again, we like to give funky names to simple concepts. Okay, so any questions about that, uh-huh? [Student:]If you add the label first, then you start [inaudible]. Yeah, you can still, after you sort of put the little object up there that says, gOh, the objectfs in the world, and say, gObject change your color to red,h and thatfs where itfs a lot cooler than the felt animals you had when you were a kid, because itfll just change to red right there on the screen because once itfs been added, itfs up on your canvas, and now any messages that you send to it will actually change its appearance on the canvas. Okay. So with that said, letfs kind of go through and understand a little bit more about what are some of these messages or methods that we can send to particular kinds of objects. So to do that, first letfs talk about the graphics world that we live in. Therefs a couple of things you want to think about the graphics world. How do we specify all these things like locations and how is it laid out. Well the origins, the upper left, so the upper left corner of your graphics window is location 00. Every thing in your graphics window is measured in pixels. Pixels are the little dots on your screen, so if you get out a magnifying glass and you go up to your monitor and you look real close and you get like eye strain and then you want to sue, donft worry. Itfs just fine. At least you saw a pixel and it brought you al little closer to Nirvana. So a pixel is just a little squares essentially on your screen that could actually get displayed. All the numbers that we refer to are in terms of pixels. So when we think about X and Y coordinates, X is how far you go across, sort of across the screen and Y is how far you go down. The X coordinates just increase to the right from 00 and the Y coordinates, different than what youfre used to in mathematics, Y coordinates go down. So normally, we think of this as a negative Y direction. Not so in Javafs world. This is the positive Y direction going down. Hey thatfs just the way life is. Itfs been like that since I was a wee tike, or actually, since Java came around. Okay, and so you might wonder, gHey, if I have some label, like Hello World, and I specify some location, where is that really on this Hello World thing and so the G-label coordinates, the coordinates that 100,75 that we gave it were the X and Y location of the base line of the first character. So that first pixel right there on the lower left hand corner of the H, okay? And so thatfs 175. Thatfs how things were laid out. Uh-huh [Student:]Is it possibly to specify [inaudible] using ratios? Well, everythingfs in terms of pixels. Therefs some stuff youfll see later on in the class where we actually specify some things like in polar coordinates if youfre familiar with polar coordinates, youfll get to that later on. But you want to think of these numbers as just being absolute pixels and you can do some math to figure out where those pixels should actually be. But theyfre still just pixels, just way off. All right, so remember all of these classes are organized in some hierarchy and everything that we talked about, right, G-label, G rect, G-oval and G-line are all G-objects, which means any behavior that a G-object has, all of these puppies have. So letfs start off by thinking about what are the behaviors or methods that are available on G-objects. So one is set color; you just saw an example of that, right, you specified the object, you say set color and you give it some color and Ifll show you where the colors come from and it sets the color of the specified object to the color you just specified as this thing called a parameter. So the thing thatfs inside the parentheses in our formal speak, we refer to as parameter. So methods have parameters. Theyfre the values that you provide inside the parentheses when you invoke a particular method, okay? So therefs a single parameter there, color. Therefs also something there called set location, which takes an X and Y coordinates. Again X and Y are in pixels and it sets the location of that object to that XY, so this method has two parameters, X and Y. And you might say, gBut Meron, when I created the G-label before, didnft I specify the X and Y location when I told the factory to give me one of these G-label objects?h Yeah, you did, but you didnft have to, and it turns out therefs times you donft want to; you want to just say, gHey give me a label thatfs got the words Hello World in it and later on Ifll figure out where itfs actually gonna go on the screen, so Ifll specify what the X and Y are,h but until you specify the X and Y, it doesnft know, but you can specify after youfve created the object, is the important thing. And move, and herefs the funky thing. You can actually tell an object, like you have your little, you know, Hello World somewhere on the screen and itfs kind of like the furniture idea of objects. Youfre like, gYeah, I donft like how it looks there. Move it a little to the right. And so therefs a move method and its coordinates if youfre sort of a Calculus person or DX and DY, and if youfre not a Calculus person, donft worry, you donft need to be a Calculus. This is the closest we get to Calculus in this class. Rock on. DX and DY just means how many pixels in the X direction and the Y direction should you move this object from its previous location and these can be positive or negative, because you can actually have something negative say in the Y direction and itfll move it up on the screen. So itfs just the offset or how much you want to, so think of it as the difference in X and the difference in Y, how much do you want to change where it moved. Uh-huh, question? [Student:]Do you use a set location XY and then [inaudible]? Can you do set location and so you ? [Student:][Inaudible] later define X and Y. No, if you want to say set location XY, youfre referring to two variables X and Y, so therefs variables X and Y and each have already been declared and have values. [Student:][Inaudible]. Yeah, so anything thatfs a parameter here, you can actually use a variable rather than an actual value, like an actual number, and wefll see some examples of that as we go along. [Inaudible]. So where do all these colors come from? It turns out these colors come from a standard library that someone in the Java world wrote thatfs called the Java AWT package, so if you want to use these colors at the top of the program, and Ifll show you and example, that should say, importJava.AWT.star and these are all the colors, so theyfre all the name color, dot and then what the color actually is. So you just saw an example where we used colored dot red to make Hello World red, but therefs all these different colors and theyfre all in your book, so you donft need to scribble them down hurriedly, but you know, different shades or black or grey or white and you know, magenta and sienna if youfre sort of a color photography sort of person, but therefs a whole set of colors and you can just go to town on them. Okay. So all G objects, all of these things respond to these methods because these guys are all objects, so any of those three methods will work on any of these objects, of objects of any of these types. But there are some additional behaviors, there are some additional methods that some of the specialized classes, the sub classes actually implement. So G-label has some additional things that it does beyond just what a G-Object in general would do. And the big one, well first of all, itfs got what we refer to as the constructor. You can think of the constructor and I'm gonna do a little violence to the term, but you can think of the constructor as a factory, okay? What the constructor says is, gHey, I want a new one of these guys,h so I use the word new and then I specify the constructor, okay? The constructor is the name of the class of which youfre going to get a new object and then some initial parameters. So the initial perimeters for a G-label for example, could be what you just saw, the text that you wanted to display and its initial X and Y coordinate, okay. Therefs some other ones. For example, you can change the fonts, right, it makes sense for a label. It doesnft make sense for a square or rectangle, to say, gHey, square, youfre fontfs gonna be Helvetica.h And the square is like, gI'm four lines, man, I donft have a font. What are you talking about?h Thatfs why the notion of setting a font isnft something that all G-objects have. Itfs only something that G-labels have which means the G-rect and G-oval and G-line donft have a set font. This is just something that you can do with a G-label and what it does it says specify the font that you want that label to show up in on the screen, and the general form for specifying the font ? this is all in your book and it shows you examples of the fonts, but the general form is inside double quotes, because youfre gonna give it as a piece of text, you specify the family of the font. That would be something like times or Helvetica. The style of the font, which is, you know, plain or bold or italic or bold or italic, so yeah, now in fact you can do all those funky things your word processor does and then the size of your font, how big or small you actually want it to be and you specify that as some texts and that would be what you send over to this sent font method to set the text. Okay, so any questions about that? Uh-huh? [Student:][Inaudible]. No, you get them all through graphics program. Rock on. All right, so how do we draw some geometrical objects? Have another question? [Student:][Inaudible]. If youfre doing a graphics program without any colors, you donft need Java.awt. [Student:]But if wefre doing one with color, then we need Java.awt in addition? Yeah, in addition to a graphics program, and Iell give you an example of that in just a second. I have to show you a program that has it. So drawing geometrical objects ? it turns out some of these other guys have funky things that you can do specifically with them. So first of all, therefs the constructors, the factories from which you can build new things. So how do I make a new rectangle? I say, gHey, I want a new G-rect.h I specify the X and Y location. Itfs the upper left-hand corner of that rectangle and then I specify the width and height of that rectangle in pixels. Okay. So thatfs the basic idea is upper left-hand corner and then width and height of the rectangle. Similarly, for oval ? ovals are kind of funky because you look at this and youfre like, gHey I have an X and Y and I have a height. I didnft like ?width and height like ovals. I thought an oval was defined by like two [inaudible] and that whole thing where you have a string. Did you ever do that ? you stick the two like nails in the board and you put the string around it and you draw anyone? Therefs like two people ? yeah, like sorry. Do it; go get a piece of wood, stick two nails in it, put some string around it and go get a pencil, something you can draw ovals and you can draw a 1 and if youfre like 6 years old, youfll draw like 1,000, and if youfre 18, and draw one, youfre like, gYeah, that was just stupid.h But the basic idea behind an oval is the reason why we specify this way is you specify sort of an imaginary rectangle for the oval and the oval shows up as an oval that just happens to touch the sides of the rectangle, which is how we sort of specify how wide and broad it is. So think imaginary rectangle and sort of the four ends of the oval would sort of be touching it, and Ifll show you that momentarily. G line is very straightforward. G-line, the way we specify line just like you know, the days of yore with Euclid, line is defined by two points, right, thatfs an infinite line, but we can think of where it ends at the two points. So we specify an initial XY location and an ending XY location and it will just draw a line between those two points, pretty straightforward. Uh-huh? [Student:][Inaudible] is the set location defined as the lower left corner of your object, and the G-rectangle [inaudible] left corner? Well, for set location is the lower left corner of for textural objects, and then it becomes different for rectangles and ovals and stuff like that, yeah. So itfs a slight variation because thatfs just because wefre trying to deal with, you know, texts and rectangles symmetrically and itfs tough to do, okay. Therefs also some additional methods that are just shared by G rect and G-oval. They donft actually apply to lines or to labels for that sense, rectangles and ovals can be filled, so they can either be an outline. You can either have a rectangle that looks just like this which is an outline or it can be filled in which means itfs just a big solid chunk of stuff. So you said if itfs filled, you say this is either true or false with the actual word true or false. So if youfve set it to true, then when you draw it on there, you say, gHey, put this thing up on my canvas.h By adding it to the canvas, it will show up filled in; it will show up solid. And if this is set to false, it just shows up as the outline of the object, whether or not thatfs an oval or a rectangle. And almost done here. Therefs also a notion of a set fill color, and you might be wondering, youfre like, gBut Meron, you told me that set color. Herefs where things get a little funky, you told me that set color is something that was defined for a G-object, so if set color is defined over here, doesnft that mean that a G-rect and a G oval already can set that color?h Yes, thatfs true. gSo whatfs the set fill color all about?h And this is a slight variation. When you set color for something, you set the color for the whole thing if thatfs solid or if itfs an outline, youfre setting the color for the whole thing. When you set fill color, you are just setting the color for this internal portion. So imagine like this was your rectangle, what color used to fill it in can actually be different than what color the line is. So if you want the line, say, to be black and you want the fill to be red, you can actually do that by setting the fill color to be red and then the outline is still whatever color the actual outline was if you want to do that. Okay, so if the color ? if the interior is fill, so the set fill has to be true, then you can set the fill color to be different. Thatfs a minor thing, but itfs kind of fun if youfre drawing stuff, okay? So any questions about that? Oh, wait ? we have a question over there, uh-huh. [Student:][Inaudible]. Oh, in the place of this word fill? [Student:]Yeah. Itfs either the word true or the word false. So if you say true, set filled is true which means it will be a solid thing; it will be filled in. Uh-huh? [Student:]If your when youfre using the [inaudible], you specify a [inaudible], what is the why would you use the set location [inaudible]. Maybe you wanna do some animation. Say like you have like your bunny in the world and hefs gonna want to move along. [Student:][Inaudible]. Well, itfs set somewhere to begin with and then later on, guess what youfre actually gonna be doing a program that involves animation. Rock on. And you will move things around. So thatfs where it comes from. All righty. So one last thing that might be useful to you, sometimes you want to say, center something in the graphics window or draw something in the bottom, how do you know how big the graphics window actually is. Therefs two methods from the graphics program class, so these methods you might notice they donft have any objects, that is their receiver, and the reason for that is that both of these methods, their receiver is the graphics program, so if you donft specify the receiver for a method, the thing that is actually receiving the method is the encapsulating class. Okay, just like in Karel, when you had Karel and you said move and you didnft specify an object for the move, the move is going into Karel, so get width and get height. Wefll return to you the width and the height of the graphics window in pixels. So these actually give you back a numeric value that you can, for example, assign to a variable and then do manipulations on it. Uh huh. [Student:]If you donft 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 window and set it to be filled and set it to be a color and then you can change the whole background that way. Uh-huh? [Student:]Can you then change the size of the graphics window? There is some stuff for later on, but for right now, just think of a fixed size graphics window. Yeah, so let me push on just a little bit if we can because what I want to actually show you now is a graphics window that makes, or a graphics program that makes use or a bunch of this stuff so over here, herefs a little program called fun graphics, okay. And so we have a comment up at the top and here are all the imports you need. Notice I [inaudible] for you. We need acm.graphicstar because wefre gonna write a graphics program. We need acm.program.star because all programs that you write in this class are gonna need that. And because wefre gonna use colors we additionally need Java.awt.star. If we werenft gonna use colors, we wouldnft need that, but itfs needed for the colors. Uh-huh. [Student:][Inaudible]. It did; it might have just been hidden because sometimes it just appears hidden until you expand it out, but it actually had it in there. So herefs what wefre gonna do. Let me just run this program, show you what it displays on the screen and then show you how we actually get that display, so itfs just like, gHey Meron, youfre just drawing garbage on my screen,h no, therefs really like a method to this madness. Not really, there are multiple methods to this madness [inaudible]. I know, sometimes computer scientists should not be ? that was actually not a canned joke. That just came tome ? how horrible is that? So here is whatfs going on. We drew a bunch of stuff on that screen, how did it get there. Well, hey Hello World; we probably knew how that got there. This should all be familiar to you now. We got a new G-label. We had some variable called label that we assigned it in. We set its font. We set its color to red. Because we set its color to red, thatfs why we need www.Java.awt because wefre using colors. We set its color to red and we said, gHey, I got Hello World, throw it up on the screen,h so we add label. You give it the name of the variable and there it appears. Over here we say, gHey G-rect. I want a new rectangle thatfs upper left-hand coordinate is 10,10 and itfs size is gonna be 50,50, which means it is a square. Itfs not filled in. I havenft specified its color right,h so if you donft say set fill, itfs not automatically filled in. If you donft specify the color to automatically black and then I add it and then you get that little box all the way up in the upper left-hand corner. Then, I say, gHey, you know what, I want some other G-rect because itfs cooler.h Itfs gonna have colors and itfs gonna be bigger and better and itfs gonna be nationwide, and so what itfs gonna do is I'm gonna have rect 2 which is another variable of Type G-rect. Itfs perfectly fine for me to have multiple objects of the same type which they knew G-rect thatfs upper left-hand coordinates is 300,75 and whose size is honking ? itfs 200 by 100, so itfs big long thing, and itfs gonna be filled in and I'm gonna set its fill color to be red, so the whole thing is red, throw it up there and so what I get is that big, rectangle. Just ignore the big oval one in front of it for now; I get this big red rectangle up there. And I say, gWell, thatfs kind of cool, but I want to see what this oval thing is all about. So Hey, Meron, make me an oval.h And I'm like, gAll right, well, G-oval will make you an oval,h and the dimensions of the oval, its upper left-hand coordinate and its size are exactly the same as the rectangle, and the reason for doing that is to show you the oval in relation to the rectangle that you specified. Itfs set filled is true and itfs set fill color, not its set color, but its set fill color is set to be green, which means the internal dividend will be green, the outline of the oval will still be black, because we did not set its color and then we added and there you get that green oval and you can notice its four ends touch the same four ends as this rectangle and if you look real closely therefs actually a green line that demarcates the oval and then the middle fill is actually green. It might be a little hard to see, but thatfs the case. Then we also have a line, so I will just call this my funky line, and my funky line is a line that starts at 100,150 and it extends to the location 200,175 and there is my funky line. Yeah, itfs a funky line. All right, and then I add another thing and youfre like, gDude wherefs my line.h And so Dude where is my line is a line that should cut across the entire graphics window because it starts at 00, well not the entire graphics window. It starts at 00 in the upper left-hand corner and goes to 100,100, and this is a common error which is why I how it to you. You look up there and youfre like, gBut Meron, therefs only one line,h and thatfs your funky line or my funky line as the case may be. Where is Dude where is my line? And itfs not up there. Why is it not up there? [Student:][Inaudible]. Yeah. You did not add it to the graphics content, okay. Common error ? people will go ahead and create something and add all the colors and set all the sides and be like, gAh, I'm rocking,h and then theyfll run their program and nothing shows up and they start tearing their hair out and beating their section leader and itfs a bad time, and theyfre sitting there contemplating life in jail and then they realize, gI just should have added it to the canvas, all right.h But you canft give that as an excuse to the judge. All right. So any questions about that? All right. Let me very quickly ? I need to push on real quickly. So I want to tell you a little bit about expressions before we finish up and wefll do more expressions, but you need to know some basic stuff on expressions. So all an expression is is you already saw one. And expression is something like INT total, equals N1 plus N2. This is an expression. Itfs just some mathematical expression that tells us how things kind of relate to each other. And basically all an expression is itfs a bunch of terms and these terms can be either individual variables or they can be constant values, like they ca be like the Number 2 or 3 or they could be some method called like Read INT that actually gets a value from the user, for example, and therefs some operators between them, like the addition operator, and so when you take some terms and you put operators between them to combine the terms thatfs called an expression, all right. Fairly straightforward stuff ? youfve probably seen these. Therefs a bunch of operators you should know about. The operators are things like plus, minus, times, which is actually a star or asterisk, which is how we refer to times. Itfs not as X ? division and then strangely enough, this thingfs that looks like percent and itfs not the percentage operator, although you might think that. This is whatfs known as the remainder operator, okay. So these puppies generally work like you would think, add two numbers, subtract two numbers. Therefs also a uniary minus. The uniary minus is the funky mathematical way of saying negative sign. So you could actually say like equals negative N1 plus N2. Thatfs like a negative minus. Thatfs perfectly fine. Okay, or you can use that same thing, you know, as subtraction if itfs between two terms. Multiplication, division, the thing about division that is funky is it depends on how division is used, okay and Ifll tell you a little bit more about that next time, but I want to talk about remainder right now. And remember back in second grade, when you did like short division and you said, gOh, I had 5 and 2 ? how many times does 2 go into 5 ? 2 and you were like remainder 1. Anyone remember that? Wasnft that such good time? I loved remainder ? like thatfs the online like math. I'm like ? and when the remainder was 0, I got pissed. But thatfs what remainder was all about. Itfs just whatfs left after you do the division. So thatfs the way you should think about percentage. Itfs the remainder operator, and youfre like, gSo how does that work?h So what that means is if I say 7 remainder 2, the value of this is 1 because itfs after I divide 7 by 2 and I see how many whole times 2 goes into 7, whatfs the remainder, itfs 1, okay? You could say something funky, like, hey whatfs 7 remainder 20? Youfre like well, 20 doesnft go into 7, Meron. So that would be 0 remainder ? rock on. So thatfs the way it works. Itfs not the modulus operator. If youfre a mathematician, or if youfve worked with other languages and youfve seen clusters and youfre like, gOh, thatfs the modular operator.h Itfs not in Java; itfs remainder an dif you try to apply it to negative numbers, unexpected things, whatfs not unexpected; itfs just not what you would have expected in the mathematical sense. So as far as you see it in the book and everything we do in this class, just apply it to positive integers and you can only apply it to integers so therefs no remainder with valued doubles, therefs no remainder. And so when we come back next time wefll talk a little bit about some of these expressions. Any questions before we break? All right, and Ifll see you ? oh, therefs a question. Why donft you just come up and ask it. Thank you. Duration: 52 minutes