Programming Methodology-Lecture05

Instructor (Mehran Sahami):All righty. Letís go ahead and get started. A couple of quick announcements before we start today Ė so hopefully youíre all busy working away on Karel and life is good. Just quick poll Ė how many people have actually finished Karel already? Oh, yeah. I wonít ask how many people have not yet downloaded Eclipse. There are no handouts today. Gettingí a little breather Ė no handouts. Donít worry; youíll 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 youíve 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 theyíre there just dedicated for the 106 classes. Theyíre not like general consultants. Theyíre just there to help you work out problems in this class, and they know like what assignments youíre working on, the whole deal. Theyíre all like, your section leaders and theyíre all be exceptionally trained to do this.

The other thing thatís going on, hopefully you should be doing Assignment No. 1. Iíve 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, ďOh, youíve got mail.í

And it just [inaudible]. So I couldnít respond to everyone individually. I apologize if I donít 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, hereís some interesting ones that have come in so far Ė just to share three.

So thereís 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 wasnít 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 thatís not a vegetarian; thatís called being a grad student.

And last, but not least, there was actually someone in here whoís on the Colbert Report, which I thought was actually pretty interesting. I donít know in what context, but come talk to me afterwards.

So with that said, any questions about anything before we start? Today weíre 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 letís 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 itís 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, theyíre really friendly, right, and you donít 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 itís a box. Itís a box where we stick stuff and the stuff we stick into that box can change.

Thatís why we call it a variable because itís 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, itís kind of like X can have different values, yeah, itís basically just like that.

So in computer science, what we think of as a variable, is each variable has three things associated with it. Itís got some name, and thatís 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 whatís stored in that box.

And then thereís a value, and the value, as you can imagine is just whatís in the box. What is the actual thing thatís in there, right? If it stores the number, then it might store, for example, the Value 3 and thatís just the value, and it may have some name associated with it.

And how do we actually name these? Thereís actually a rule and itís 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 thatís kind of you know, underscore. Itís 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, thatís like the number digits, like 1, 2, 3, 4, you know, 0, etc. or underscores, okay?

So you canít 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 canít have any variables thatís name is the same as some which known as a reserved word in Java, which means its name canít be the same as some special word in Java, like the word class is a special name in Java, and thereís actually a page in your book, I think in Chapter 2, that lists all the special names. Itís like out of the English language it has about 127,000 words. I think there is like 40 in Java that you canít make a variable name.

Okay, you have lots of other choices. As a matter of fact, thereís lots of things that donít 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 thatís 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 ďBalance.Ē

A real bad name would be something like ďA,Ē 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 someone says, ďNo, no, no, in my program, A is actually how many miles I bicycle today,Ē and youíre like, ďNo, no, no, man, A was balanced.Ē Well, if itís called Balance, thereís just no ambiguity, so give them descriptive names.

So thatís 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. Itís 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?

Thereís also besides integers what other kinds of numeric data do we have? People already know, itís like I would think it would be like real values, but a bunch of people are already saying double because youíve read ahead and youíve done the assignment the way you should. And that was kind of a mini social, but both went to the same person.

Thereís this thing called a double and a double is actually some real valued numeric value, right. Itís 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?


Instructor (Mehran Sahami):Yeah, thereís 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. Itís 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 thereís 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 weíre gonna use are just of type double. Okay, thereís a couple of other types that Iíll just mention now and weíll actually go into them in much more detail in a couple of future classes.

One is called Boolean. And if youíve ever heard of Boolean logic, this is a logical value, so this is just essentially true or false, and weíll talk about that in excruciating detail next time, but Iíll just let you know that thereís a type called Boolean.

Thereís also a type called care or Car and as you can imagine itís because we like sort of the first syllable of most things and this is the first syllable of character, okay, and so weíll also talk about this in a couple of weeks time when we actually get into a some things with characters, but thatís just a character.

Itís a variable; itís a box, but still is a character. Itís 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 thatís 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, ďBut Meron, like 2 is an integer, right?Ē And I'm like, ďYeah.Ē And theyíre like, ďBut you just told me 2.0 is a double, right?Ē And I'm like, ďYeah, 2.0 is a double.Ē

And so the natural question is why? Why do we have both these things? How come like all integers arenít subsumed by the real values, right, youíre kind of the mathematician type and youíre like, ďYeah, thereís 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,Ē 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, letís say you, ask me, just so I wonít embarrass you, ďHow much do you weigh, Meron?Ē

Student:How much do you weigh, Meron?Ē

Instructor (Mehran Sahami):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, ďHey Meron, how much children do you have?Ē I have 2.3 children. Does that make any sense to you? Yeah, itís like we had three until that grisly accident. No, itís just that thereís sometimes Ė I know thatís horrible to say. We actually have one, and heís just fine.

Thereís sometimes in the world when you care about counting, and when you care about counting, it doesnít make sense to have fractional values. Those things are integers. Theyíre a how many kind of value. When youíre thinking about how much, thatís a double value, and you actually want to keep these distinct because if I ask you whatís the next number after 1, you say Ė


Instructor (Mehran Sahami):2 because youíre thinking integer, and thatís perfectly right and if I say whatís the next number after 1.0, you say Ė


Instructor (Mehran Sahami):Yeah, 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 probably not it. How about 1.0000001, and I'm like, ďNo 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.Ē

In real values, there is no next value, okay, so when you care about counting, it doesnít make sense to use a double. Thatís when you want to use an integer, so having these things be a distinct type actually make sense and youíll kind of see that as we go along, okay.

So the value is basically just what youíre actually gonna put into this box over here and itís 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. Itís kind of like declaring your love. If youíre gonna have some box somewhere thatís got something stored in it, you need to come tell the world, ďHey world, Iíve got this thing called X. Itís cool. Check it out.Ē Hereís how you do that in Java.

You say INT X, okay? What that does is you specify a type for your variable, whatís 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 Karelís world, we put a semi colon.

What does this do? It creates a box somewhere in the computerís memory thatís name is X thatís gonna store an integer in it. Right now, what does it store?


Instructor (Mehran Sahami):Nothing. We havenít told it anything to store in particular. And so in Javaís world, unless you initialize you always want to think about initializing a variable, giving it some initial value to start off with and thereís some rules that weíll talk about as we go along regarding when Java will automatically sort of give you a zero value in there and thereís time when it doesnít and sometimes it can get a little bit confusing.

So the general rule you want to remember and itís 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 itís just a real value.

So the general form for how you do these declarations is you say type and Iíll 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 thatís 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 youíre gonna put everything thatís 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 wouldnít 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.

Theyíre only available to you inside that method called run. Theyíre not available to you in some other method and weíll 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 theyíre 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?

Instructor (Mehran Sahami):Well, you wonít be defining another method inside run, right; youíll be defining another method thatís separate from run. Itís just a separate method. It wonít be available in that separate method. It doesnít matter if itís public or private. It just not available there. So this public or private thing that you have here doesnít affect the visibility of your variables. Your variables are always only visible inside the method in which theyíre 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; itís actually whatís known as an assignment statement in Java or in most programming languages. And the idea is weíre taking some value over here. Thatís whatís 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 weíre assigning that value, okay?

And so the general form for an assignment, letís just do that over here, is pretty straightforward, you just saw it, is variable and this is the name of the variable. So Iíll squiggle Ė equals some value or some expression for a value and weíll talk about expressions in just a second with a semi colon at the end of it, and thatís how you assign a value.

You can assign values multiple times to a variable, to a variable. Itís a variable, the contents change. So you say, ďHey, X equals ,Ē and then down here somewhere, ďHey X equals 4,Ē 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, itíll stick a 4 in the box. And thatís perfectly fine because X is a variable. As long as everything you stick in there is of the type of which X is declared, youíre okay, okay?

So what that also means is when youíre 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, ďHey, I have some variable called total. Let me create some variable in its total,Ē which Iíll give some initial value like 10 and thatís a good time.

And then I say, ďHey total equals total plus 1.Ē And if youíre a mathematician, you look at that and say, ďNo, man. Total canít equal total plus 1. Thatís just not right.Ē And then you got this whole philosophical question about, you know, if this is like total is equal to infinity and youíre just like, ďNo, no, no, thatís not what weíre talking about.Ē

Weíre 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 totalís 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, ďOh, that was 10.Ē 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 thatís perfectly fine.

So itís 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 itís 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? Weíve declared a variable named N1. Whatís the value for that variable that weíre gonna assign to it? Weíre going to call some method. The method weíre gonna call is something called Read INT and this is a method thatís 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. Itís the value that [inaudible]. Itís kind of giving it back to you. Do what you want. What are you gonna do with it? Youíre gonna assign it to the Box N1, so hereís the box for N1.

We execute this statement. Itís coming; itís gonna print to enter N1 on the screen. Itís gonna ask us for a value. Letís 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, weíre gonna declare another; we just declare until the cows come home, right? Declaring is fun; itís cheap. Itís easy; do it, do it often, right, so weíre gonna declare another variable total, so we have another box over here for total and the value of total that Ďs 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 isnít some truth that holds for all time. Itís just an assignment that it does when it reaches that line. So it says, find out whatís in N1, add it to whatís in N2.

Thatíll 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 youíre like, ďWhoa, whoa, whoa Meron, I thought plus was light lading things together. Whatís going on? Youíre like trying to add Ė this thingís got the value of 42. I know that because itís in the box. Youíre trying to add 42 to some text? Thatís not right.Ē

Well, in most worlds, it would not be right, but it turns out in the Java world itís 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, ďTake whatever text is here and add to that text, the textural form of this value.Ē

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 youíre feeling good, like you understand what just went on in the program, nod your head. Rock on. Thatís 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 youíre like, ďHey, Meron, yeah, thatís all good and well, but last time you were telling me about like classes and objects and drawing stuff on the screen like, whatís up with all that.Ē

And this is the time when we actually do sort of the, you know, Reeseís Peanut Butter Cup. Like itís 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 youíre 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. Iíll 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, theyíre all part of is G object? So what G-object is a G-object is a class. Donít let the name fool you. Some people see G-object and they think, ďOh, that must be an object.Ē 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, theyíre all sub classes of G-objects. It means theyíre 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, ďGive me a variable that is a box,Ē and the type that that box holds is an object thatís the type of some class, so all classes can actually be used as type means, okay?

Now, thatís 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 itís gonna draw us some pictures.

Well what are we actually doing on this first line? Hey, weíre doing a declaration and assignments on the first line. Whatís the type of our variable? Itís 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 thatís the type. Itís gonna hold any object.

And the question comes up, how do we get an object of type G label. And hereís 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 itís gonna display in some location thatís 100,75.

So we say, ďHey G-label, youíre the class. I donít have an object of you yet. Youíre the class.Ē When I say, ďGive me a new G label, itís sort of like going to the factor. You sort of show up at the G-label factory and you say, ďYo, G-label factory. Give me a new object,Ē and G-label factory is sort of sitting up there and says, ďWell, 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,Ē and youíre 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 itís gonna display and somewhere itís also storing the value is 175 because it knows where thatís gonna be on the screen.

Now, what weíre gonna do is weíre 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. Thatís the name of the variable. And so thatís what we say here. We donít 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 weíre going to call for that particular object. So itís a little bit different than Karelís world, all right. In Karelís 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, ďHey, label, yeah, I'm referring to you, buddy. Set your fonts to be this particular font, san seraph which you saw last time.Ē It just makes it big. It makes it 36-point font. And then you say, ďHey label, yeah, you, same guy, or gal or it or whatever it may be. Set your color to be red.Ē

And it says, ďOkay, Iíll 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, Iíve done all this stuff and my programís still bringing out a blank screen. Itís 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, ďHey take the felt hello world thatís so cute and just stick it in the world,Ē so we add it to the graphics program, basically and it shows up in the screen where it knows itís 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 theyíre talking about, is they say the object is the receiver. So the object here is the one whoís receiving or being told to do something, okay?

So the name of the variable is the receiver and the method thatís being called is called the message and so thereís 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, youíll 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].

Instructor (Mehran Sahami):Yeah, you can still, after you sort of put the little object up there that says, ďOh, the objectís in the world, and say, ďObject change your color to red,Ē and thatís where itís a lot cooler than the felt animals you had when you were a kid, because itíll just change to red right there on the screen because once itís been added, itís 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, letís 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 letís talk about the graphics world that we live in. Thereís 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, donít worry. Itís 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 youíre used to in mathematics, Y coordinates go down. So normally, we think of this as a negative Y direction. Not so in Javaís world. This is the positive Y direction going down.

Hey thatís just the way life is. Itís been like that since I was a wee tike, or actually, since Java came around. Okay, and so you might wonder, ďHey, 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 thatís 175. Thatís how things were laid out. Uh-huh

Student:Is it possibly to specify [inaudible] using ratios?

Instructor (Mehran Sahami):Well, everythingís in terms of pixels. Thereís some stuff youíll see later on in the class where we actually specify some things like in polar coordinates if youíre familiar with polar coordinates, youíll 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 theyíre 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 letís 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 Iíll 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 thatís inside the parentheses in our formal speak, we refer to as parameter. So methods have parameters. Theyíre the values that you provide inside the parentheses when you invoke a particular method, okay?

So thereís a single parameter there, color. Thereís 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, ďBut Meron, when I created the G-label before, didnít I specify the X and Y location when I told the factory to give me one of these G-label objects?Ē

Yeah, you did, but you didnít have to, and it turns out thereís times you donít want to; you want to just say, ďHey give me a label thatís got the words Hello World in it and later on Iíll figure out where itís actually gonna go on the screen, so Iíll specify what the X and Y are,Ē but until you specify the X and Y, it doesnít know, but you can specify after youíve created the object, is the important thing.

And move, and hereís the funky thing. You can actually tell an object, like you have your little, you know, Hello World somewhere on the screen and itís kind of like the furniture idea of objects. Youíre like, ďYeah, I donít like how it looks there. Move it a little to the right. And so thereís a move method and its coordinates if youíre sort of a Calculus person or DX and DY, and if youíre not a Calculus person, donít worry, you donít 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 itíll move it up on the screen.

So itís 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]?

Instructor (Mehran Sahami):Can you do set location and so you Ė

Student:[Inaudible] later define X and Y.

Instructor (Mehran Sahami):No, if you want to say set location XY, youíre referring to two variables X and Y, so thereís variables X and Y and each have already been declared and have values.


Instructor (Mehran Sahami):Yeah, so anything thatís a parameter here, you can actually use a variable rather than an actual value, like an actual number, and weíll 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 thatís called the Java AWT package, so if you want to use these colors at the top of the program, and Iíll show you and example, that should say, and these are all the colors, so theyíre 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 thereís all these different colors and theyíre all in your book, so you donít need to scribble them down hurriedly, but you know, different shades or black or grey or white and you know, magenta and sienna if youíre sort of a color photography sort of person, but thereís 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, itís 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, ďHey, I want a new one of these guys,Ē so I use the word new and then I specify the constructor, okay?

The constructor is the name of the class of which youíre 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.

Thereís some other ones. For example, you can change the fonts, right, it makes sense for a label. It doesnít make sense for a square or rectangle, to say, ďHey, square, youíre fontís gonna be Helvetica.Ē And the square is like, ďI'm four lines, man, I donít have a font. What are you talking about?Ē Thatís why the notion of setting a font isnít something that all G-objects have. Itís only something that G-labels have which means the G-rect and G-oval and G-line donít 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 youíre 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?


Instructor (Mehran Sahami):No, you get them all through graphics program. Rock on. All right, so how do we draw some geometrical objects? Have another question?


Instructor (Mehran Sahami):If youíre doing a graphics program without any colors, you donít need Java.awt.

Student:But if weíre doing one with color, then we need Java.awt in addition?

Instructor (Mehran Sahami):Yeah, in addition to a graphics program, and IĎll 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, thereís the constructors, the factories from which you can build new things. So how do I make a new rectangle? I say, ďHey, I want a new G-rect.Ē I specify the X and Y location. Itís the upper left-hand corner of that rectangle and then I specify the width and height of that rectangle in pixels. Okay. So thatís 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 youíre like, ďHey I have an X and Y and I have a height. I didnít 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? Thereís 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 youíre like 6 years old, youíll draw like 1,000, and if youíre 18, and draw one, youíre like, ďYeah, that was just stupid.Ē

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 Iíll 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, thatís 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?

Instructor (Mehran Sahami):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 itís a slight variation because thatís just because weíre trying to deal with, you know, texts and rectangles symmetrically and itís tough to do, okay.

Thereís also some additional methods that are just shared by G rect and G-oval. They donít 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 itís just a big solid chunk of stuff. So you said if itís filled, you say this is either true or false with the actual word true or false.

So if youíve set it to true, then when you draw it on there, you say, ďHey, put this thing up on my canvas.Ē 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 thatís an oval or a rectangle.

And almost done here. Thereís also a notion of a set fill color, and you might be wondering, youíre like, ďBut Meron, you told me that set color. Hereís 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, doesnít that mean that a G-rect and a G oval already can set that color?Ē

Yes, thatís true. ďSo whatís the set fill color all about?Ē And this is a slight variation. When you set color for something, you set the color for the whole thing if thatís solid or if itís an outline, youíre 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. Thatís a minor thing, but itís kind of fun if youíre drawing stuff, okay? So any questions about that? Oh, wait Ė we have a question over there, uh-huh.


Instructor (Mehran Sahami):Oh, in the place of this word fill?


Instructor (Mehran Sahami):Itís 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 youíre using the [inaudible], you specify a [inaudible], what is the why would you use the set location [inaudible].

Instructor (Mehran Sahami):Maybe you wanna do some animation. Say like you have like your bunny in the world and heís gonna want to move along.


Instructor (Mehran Sahami):Well, itís set somewhere to begin with and then later on, guess what youíre actually gonna be doing a program that involves animation. Rock on. And you will move things around. So thatís 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.

Thereís two methods from the graphics program class, so these methods you might notice they donít 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 donít 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 didnít specify an object for the move, the move is going into Karel, so get width and get height. Weíll 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 donít fill in [inaudible] something like set color to change the entire background of the graphics [inaudible]?

Instructor (Mehran Sahami):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?

Instructor (Mehran Sahami):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, hereís 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 weíre gonna write a graphics program. We need because all programs that you write in this class are gonna need that. And because weíre gonna use colors we additionally need If we werenít gonna use colors, we wouldnít need that, but itís needed for the colors. Uh-huh.


Instructor (Mehran Sahami):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 hereís what weíre 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 itís just like, ďHey Meron, youíre just drawing garbage on my screen,Ē no, thereís 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 whatís 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, thatís why we need www.Java.awt because weíre using colors.

We set its color to red and we said, ďHey, I got Hello World, throw it up on the screen,Ē so we add label. You give it the name of the variable and there it appears.

Over here we say, ďHey G-rect. I want a new rectangle thatís upper left-hand coordinate is 10,10 and itís size is gonna be 50,50, which means it is a square. Itís not filled in. I havenít specified its color right,Ē so if you donít say set fill, itís not automatically filled in. If you donít 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, ďHey, you know what, I want some other G-rect because itís cooler.Ē Itís gonna have colors and itís gonna be bigger and better and itís gonna be nationwide, and so what itís gonna do is I'm gonna have rect 2 which is another variable of Type G-rect.

Itís perfectly fine for me to have multiple objects of the same type which they knew G-rect thatís upper left-hand coordinates is 300,75 and whose size is honking Ė itís 200 by 100, so itís big long thing, and itís 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, ďWell, thatís kind of cool, but I want to see what this oval thing is all about. So Hey, Meron, make me an oval.Ē And I'm like, ďAll right, well, G-oval will make you an oval,Ē 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.

Itís set filled is true and itís 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 thereís 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 thatís 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, itís a funky line.

All right, and then I add another thing and youíre like, ďDude whereís my line.Ē 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 youíre like, ďBut Meron, thereís only one line,Ē and thatís your funky line or my funky line as the case may be. Where is Dude where is my line? And itís not up there. Why is it not up there?


Instructor (Mehran Sahami):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, ďAh, I'm rocking,Ē and then theyíll run their program and nothing shows up and they start tearing their hair out and beating their section leader and itís a bad time, and theyíre sitting there contemplating life in jail and then they realize, ďI just should have added it to the canvas, all right.Ē

But you canít 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 weíll 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. Itís just some mathematical expression that tells us how things kind of relate to each other.

And basically all an expression is itís 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 thereís 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 thatís called an expression, all right.

Fairly straightforward stuff Ė youíve probably seen these. Thereís 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. Itís not as X Ė division and then strangely enough, this thingís that looks like percent and itís not the percentage operator, although you might think that. This is whatís known as the remainder operator, okay.

So these puppies generally work like you would think, add two numbers, subtract two numbers. Thereís 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. Thatís like a negative minus. Thatís perfectly fine. Okay, or you can use that same thing, you know, as subtraction if itís between two terms.

Multiplication, division, the thing about division that is funky is it depends on how division is used, okay and Iíll 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, ďOh, I had 5 and 2 Ė how many times does 2 go into 5 Ė 2 and you were like remainder 1. Anyone remember that? Wasnít that such good time? I loved remainder Ė like thatís the online like math. I'm like Ė and when the remainder was 0, I got pissed.

But thatís what remainder was all about. Itís just whatís left after you do the division. So thatís the way you should think about percentage. Itís the remainder operator, and youíre like, ďSo how does that work?Ē So what that means is if I say 7 remainder 2, the value of this is 1 because itís after I divide 7 by 2 and I see how many whole times 2 goes into 7, whatís the remainder, itís 1, okay? You could say something funky, like, hey whatís 7 remainder 20? Youíre like well, 20 doesnít go into 7, Meron. So that would be 0 remainder Ė rock on.

So thatís the way it works. Itís not the modulus operator. If youíre a mathematician, or if youíve worked with other languages and youíve seen clusters and youíre like, ďOh, thatís the modular operator.Ē

Itís not in Java; itís remainder an dif you try to apply it to negative numbers, unexpected things, whatís not unexpected; itís 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 thereís no remainder with valued doubles, thereís no remainder.

And so when we come back next time weíll talk a little bit about some of these expressions. Any questions before we break? All right, and Iíll see you Ė oh, thereís a question. Why donít you just come up and ask it. Thank you.

[End of Audio]

Duration: 52 minutes