Lecture 5 | Programming Methodology (Stanford)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
this presentation is delivered by the Stanford center for professional development alright today we're actually going to go over some more 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 alrighty 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 you don't need 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 world is at the 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 variable because it's a box that has variable contents and you think back you know in the days of you're in math and you're like oh yeah it's kind of like X you could 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 than in mathematics but the type basically says what kind of thing does this box store some box store nut some boxes store numbers some boxes store letter some box will store other things like little objects in the world but a type is just what's stored in that box and then there is a value and the value as you can imagine is just what's in the box it's what's the actual thing that's in there right if it stores a number 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 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 in the upper or lowercase 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 an underscore then after that you can have any number of letters numbers that's like the number digits like one two three four you know zero etc or underscores okay so you can't start with a number you have to start with a letter or an underscore for most purposes in this class just think about it as letters we're not actually use underscores you might occasionally use numbers you can actually have numbers after the first letter there's one slight caveat to this rule which is you can't have any variables that's name is the same as some what's 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 there's actually a page in your book I think in Chapter two that lists all the special names it's like out of the English language that has about 127 thousand words I think there was 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 are 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 the name and this is one of the good software engineering principles is make your names descriptive if you have a program that's maintaining for example the balance and a 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 there's like hey it's a yeah I know what a and someone says yeah a is balance 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 was just no ambiguity so give them descriptive names so that's kind of the name part of this the next thing is what's this type all about what are 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 these smaller developed brains and whose knuckles dragged on the ground no there are just the types that are built into Java okay and some of the basic types we have is something called an int which is short for an integer but we actually write int so int int is the name of the type okay and this is an integer value it's just going to 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 numerical 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 read ahead you've done the assignment the way you should and that was kind of a mini social that both wants the same person there's a 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 yeah it's a double but there's this wonderful group of people called the I Triple E which is like the Institute for electronic and electrical and electronic engineers they come up with all these standards for things anyone a member of the I Triple E here no one man join and pay your dues it's a good time um 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 us how do you represent numbers that are real value numbers inside a computer right because remember computer only understands ones and zeros so how do you actually represent a real value number and so there's a standard and part of that standard has to do with the precision of the number how many digits in some sense in a rough approximation can 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 going to use you're just of type double okay there was a couple other types that I'll just mention now I'll actually go into them in much more detail in a couple 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 this 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 and oh a couple weeks time when we actually get into some things with characters but that's just a character it's a variable it's a box that's was a character this is a box the stores an integer box 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 we think about values what we actually store in this box that people always get uncomfortable when they see in and they see double right and they sort of say but Marylin like too is an integer right I'm like yeah and they're like but you just told me 2.0 is a double right like yeah a 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 a mathematician type and you're like yeah there's like strictly more of these than there are of these so why aren't all of these are subsumed in here why do we even have in this integer type okay and the reason we do cut boils down to a very 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 Mehran way about you know one hundred and fifty five point six three two 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 Marin 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 that there are some times I know that's just horrible to say we have we actually have one and he's just fine there are some times in the world where you care about counting and when you care about counting it doesn't make sense to have fractional values those things are integers there are 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 one you say two because you're thinking integer and that's perfectly right and if I say what's the next number after 1.0 you say yeah you start mumbling and you're like well I know it's not a 2.0 I would have been the obvious answer and that's probably not it it's how about 1.000000 one and I'm like no you missed a couple zeroes and they'll just keep going wait till the end of the quarter then put that one 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 have an integer and so having these things be a distinct type actually makes sense and you'll kind of see that as we go along okay so the value is basically just what you're actually going to put into this box over here and it's going to be some of the values that we actually talked about so let me show you some examples of how we might use variables how 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 variables it's kind of like declaring your love if you're going to have some box somewhere that's going to store something in it you need to come tell the world hey world I 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 going to hold you specify a name for that variable in this case the name is X and because all statements end with a semicolon just like you saw in Carroll's world we put a semicolon what does this do it creates a box somewhere in the computer's memory that's name is X that's going to store an integer in it right now what does it store nothing we haven't told it anything to store in particular and so in Javas world unless you initialize you always want to think about initializing a variable giving it some initial value to start off with and there are some rules that we'll talk about as we go along regarding Wednes Java will automatically sort of give you a zero value in there and there's times when it doesn't and sometimes can get a little bit confusing so the general rule you want to remember and it's also good software engineering is give variables and 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 semicolon there we could say x equals 3 and what that does is it gives an initial value of 3 to X ok 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 ok if we want it to have something of type double maybe something called Y we could have double y equals 5.2 and what that means is somewhere in memory we have this thing called Y which has a value 5.2 in it it's just a real value ok so the general form for how you do these declarations is you say type and I'll put little squiggles underneath these to let you know that these are that's the general form the name and then potentially an equal sign and some initial value some initial Vale some initial value followed by the semicolon 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 will generally at least for the time being make declarations of values or declarations of variables is inside methods so when you create some method like you have public void run and you're going to put everything that's going to go inside run in there you could for example have this line in there and this would create X for the value of 3 in Y with the value 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 ok so any questions about that variables or values are 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's 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 set into X and then we could have set X equals three over here and gotten the same effect and when we say x equals three 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 it equals so we're assigning that value okay and so the general form for an assignment we do 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 label equals some value or some expression for a value and we'll talk about expressions in just a second the semicolon at the end of it that's how you assign a value you can assign values multiple times to a variable it's a variable the contents change so you say hey x equals three and then down here somewhere hey x equals four and when you get to this line up until you get to that line from here to here actual have have the value three and when it gets to this line it'll stick a four in the box and that's perfectly fine because X is the 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 that two mathematicians they just go nuts they go crazy like veins just burst in their head and you're these popping noises and people dine in the streets which is you can say hey I have some variable called toad let me create some variable int total which I'll give some initial value like 10 and that's a good time and then I say hey all equals total plus 1 and if you're a mathematician you look at that and you say no man it total can't equal total plus 1 that's just not right unless and then you get in this whole philosophical question about you know if this is like total is equal to infinity and you just like no no no that's not we're talking about we're talking about assignment this isn't equals this is assignment so what it says is evaluate the right-hand side okay total what was total freebie style you go and looks it up in the box over here you have some box for total its value is 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 name 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 this where you take the old value for a variable you do some manipulation on it you stick it back into 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 will now begin to make a little bit more sense so if we 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 as a console program right so it's going to write some text out to a console somewhere and we have our run method we print out a line we know the the method print line' when we say that whatever we put inside the double quotes gets printed to the screen so it's prints out this program adds two numbers so we execute that line and after we execute it it writes it down it comes to the next line low and behold what have we done here we've declared an a variable named n1 what's the value for that variable that we're going to assign to it we're going to call some method the method we're going to 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 returned by read int it's the value that we have to read in to execute says hey I got this number from the user here here's that number it's kind of giving it back to do what you want are you going to do with it you're going to assign it to the box and one so here's the box for n1 we execute this statement it's coming it's going to print enter n1 on the screen it's going to ask us for a value let's say we type in the value 17 and it's fixed at 17 so this expression over here evaluates the 17 it's 617 in the box for n1 go to the next line now it declares n2 we have some box it's initial value is also going to come from the user via this read in smething 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 going to declare another we're 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 going to declare another variable total so we have another box over here for total and the value of told that's going to get a sign they're just whatever values in n1 + n2 so to evaluate this side it says I need to look up the box and one so at the point where it reaches this line it evaluates what end whatever's in n1 this isn't some truths 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 an n1 add it to what's an end to that'll give you the value 42 stick that into total which is what you get and then the last line says I'm going to print something to the screen where the total is and then you see these funky pluses and you're like whoa whoa man I thought plus was like adding things together what's going on you're like trying to add this things got the value 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 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 textual form of this value well the textual form of 42 is just the characters 4 + 2 so it sort of this adds that 42 here and then adds to the end of that a little period so it concatenates all those things together and then prints the mountains as the totals 42 okay so any questions about the program you're all if you're feeling good like you understand what just went on 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 hold this all the variables and assignments and types and you're like hey Merrill 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 this is the time where 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 we just slam them together and now you're going to have variables that contain objects they're just they taste great all right so here are some of the objects that you considered last time right we had things called like it or some classes you have the G Label class and the G rect class and the G Oval class these were all these graphics classes like a label that wrote some text out 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 is a G object is a class don't let the name for 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 subclasses of G objects 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 names okay now that's kind of funky let me show you an example okay here's 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 this going to draw some pictures well what are we actually doing on this first line hey we're doing a declaration and an assignment 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 labeled so we have a box called label and what that box is going to hold is any object of the class G label okay so that's the type it's going to 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 objective type G label is we have to ask for a new g label so we give the name of the class here and classes depending on the class and you'll see some examples of this we'll take in or are given some initial values for what we refer to as parameters and these parameters are all separated by commas but it's some initial values that this object uses to sort of set itself up it initializes the object so object rather than just taking one value can potentially hold multiple values in them in this case a G label is going to have hello world to some text that's going to display in some location that's 100 comma 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 sounded like going to the factory you sort of show up at the G label factory you say yo G label factory give me a new object in G label factory sort of sitting up there and says well what kind of new object you want yeah I can give you a 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 going to display in somewhere it's also storing the values 175 because it knows where that's going to be on the screen now what we're going to 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 that's a little bit different than Carol's world right and care world's every time you call the method Carol 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 sans-serif what you saw last time just makes it big it makes a 36 point fun and then you say hey label yeah you same guy or gal or it as 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 out right I've done all this stuff my program still bring 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 my little felt hello world and I want to say hey take the felt hello world it'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 one hundred comma 75 okay so the general form for this that people refer to when you see something like this you have name of the variable and the method name and the Java esque names that we give to these things just so when you 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 so there's this whole notion of sending messages kind of like you can think of programming as I am right and in your little IM you have like your friend bob and your friend Sally and your friend dave and your friend label right and labels you sitting there on IM and label is not very talkative but every once in a while you can send a message to IM which is a form of message that 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 yeah you can still after you sort of put the little object up there it says Oh the objects in the world then you can say hey object change your colour 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 sent 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 methods or messages that we can send to particular kinds of objects okay so to do that first let's talk about the graphics world that we live in there's a couple 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 origin is the upper left to the upper left corner of your graphics window is location 0 0 everything 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 eyestrain and then you want to sue don't worry it's just fine at least you saw a pixel and it brought you a little closer to Nirvana okay so pixel are just the little squares essentially on your screen that could actually get displayed and 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 increased to the right from 0 0 and the y coordinates different than what you're used to in mathematics Y coordinates go down so normally we think of this is the negative Y direction not so in Javas world this is the positive Y Direction going down ok that's just the way life is that's been there been like that since I was a wee tyke or actually since Java came around and and so you might wonder hey so 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 hundred comma 75 that we gave it where the X&Y location of the base line of the first character so there that first pixel right there on the lower left lower left-hand corner of the H okay and so that's 175 that's how things are laid out uh-huh well everything's in terms of pixels there are 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 is just being absolute pixels and you can do some math to figure out where those pixels should actually be but there's 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 labeled G rec 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 specify the object you say set color and you give it some color I'll show you where the colors come from and it sets the color of the specified objects to the color you just specified as this thing called a parameter so the thing that's inside the braces or the not the braces 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 their color there's also something 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 meirin didn't when I created the G label before didn't I specify the X&Y location when I kind of 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 going to go on the screen so I'll specify with the x and y or but until you specify that x and y it doesn't know but you can specify after you've actually created the object is the important thing and move and here's a funky thing you can actually tell an object like you have your little you know hello world somewhere on the screen is your kids kind of like the furniture ID of objects you're like yeah I don't like how it looks then move it a little to the right and so there's a move method and it's coordinates if you're sort of a calculus person or DX and dy and if you're not a cow this person don't worry you don't need to be a cab this is the closest we get to calculus in this class we rock on the X and D Y it 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 be negative say in the Y Direction that will move it up on the screen so it's just the offset or how much you want to say think of it as the difference in X and the difference in Y how much do you want to change where it moves my question can you do set location as show you know if you want to say set location X Y the very you're referring to two variables x and y so those variables x and y need to have already been declared and have values 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 clip once again so where do all these colors come from it turns out the 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 your program and I'll show you an example let's you'd say import java.awt dot star 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 color 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 of black or gray or white and you know magenta and cyan 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 object of objects of any of these types but there are some additional behaviors or some additional methods that some of the specialized classes the subclasses actually implement so G label has some additional things that it does beyond just what the 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 going to do a little violence the term but you can think of the constructor as a factory okay what the constructor says is hey I want to knew 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 parameters for AG label for example could be what you just saw the text that you want to display and its initial x and y-coordinate okay there are some other ones for example you can change the fonts right it makes sense for a label like it doesn't make sense for a square or rectangle to say hey square your font is going to be Helvetica and the squares like I'm four lines man I don't have a phone what are you talking about right 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 G rect and G oval and G line don't have a set font this is just something that you can do with the 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 fonts this all in your book and it shows you an example of fonts but the general form is inside double quotes because you're going to give it as a piece of text so you specify the family of the font that would be something like x or Helvetica the style of the font which is is it you know plain or bold or italic or bold italic so yeah now in fact you can do like all those funky things your word processor does and then the size of your fonts how big your small you actually want it to be and you specify that as some text and that would be what you send over to this sent font method to to set the text okay so any questions about that uh-huh um no you can you get them all through graphics program rock on alright so how do we draw some geometrical objects so another question if you're doing the graphics program without any colors you don't need java.awt yeah in addition the graphics program and I'll give you an example of that in just a second so I'll actually show you a program that hasn't so drawing geometrical objects it turns out some of these other guys have funky things that you can do specifically with them so the 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&Y location that'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 ideas upper left-hand corner and then X Y and then sorry height or width and height of the rectangle similarly for oval ovals are kind of funky because you look at this you're like hey I have an x and Y and a width and a height I didn't like width and height like ovals I thought an oval was defined by like two foci right and that whole thing like when you have where string you ever do that you stick the two like nails in a board and you put the string around it and you draw on any one there's like two people yeah sorry do it go get a piece of wood stick two nails and it put some string around and get a pencil on one side you can draw ovals and you can chalk like one and if you're like six years old you'll draw like a thousand and if you're you know 18 you 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 actually be touching it and I'll show you that momentarily g-line is very straightforward gene lying the way we specify a line just like you know in the the days of your 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 the wall for set location it's the lower left corner of for textual objects and then it becomes different for rectangles and ovals and stuff like that yeah so it's a slight variation but that's just because we're trying to deal with you know text 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 just is you know 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 you 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 is also a notion of a set fill color and you might be wondering you're like but Marilyn you told me that set color here's where things get a little funky you told me that set color was something that was defined for a G object so if set color is defined over here doesn't that mean that a G wrecked in a G oval already can set their 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 lines 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 the color of the actual object was if you want to do that okay so it's if the color is if the interior is filled so 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 I'll wait my question over there huh Oh though in the place of this word fill it's either the word true or the word false so if you say true set filled is true which means it sets it will be a solid thing it will be filled in uh-huh uh-huh maybe you want to do some animation so you have like your bunny in the world and he's going to like move along well it says start somewhere to begin with and later on guess what you're actually doing a program that involves animation rock on and you will 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 object 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 was going to Karel so get with and get height will 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 huh you while you can make the a huge rectangle that's the size of the graphics window and fill it say that it set it to be filled and set it to be a color and then you can change the whole background that way uh-huh there is some stuff 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 it's because what I want to actually show you now is a graphics window that makes or a graphics program that makes use of 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 all the imports you need notice I commented these for you we need a cm graphic star because we're going to write a graphics program we need a cm dot program dot star because all programs that you write in this class are going to need that and because we're going to use colors we additionally need java.awt ARF we weren't going to use colors who wouldn't need that but it's needed for the colors uh-huh it did it might have just been hidden because sometimes it just appears hidden until you expand it out but it actually had in there so here's what we're going to 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 Mara you're just drawn garbage on my screen no there really is a method to this madness not really there's there are multiple methods to this madness damage I know sometimes he resigns a tsunami that was that was actually not a canned joke that just came to me 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 said it's font we set its color to red because we said it's color to red that's why we need W java.awt because we're using colors we said it's color to red 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 common and it saw is going to be fifty comma fifty 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 automatically not 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 grx that's cooler because it's going to have colors and it's going to be bigger and badder and it's going to be nationwide and so what it's going to do is I'm going to have rect to which is another variable of type G rec that's perfectly fine for me to have multiple objects of the same type which they new G Rex that's upper left hand coordinate is 300 comma 75 and whose size is hongcun it's 200 by 100 so it's this big long thing and it's going to be filled in and I'm going to 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 green oval in front of it for right 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 meirin make me an oval and I'm like all right well G oval we'll make you an oval and the dimensions of the oval its upper left hand coordinate and it's size are exactly the same as the rectangle and the reason for doing that is so 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 it's set color but it's set fill color is set to be green which means the internals of it will be green the outline of the oval will still be black because we did not set its color and then we add it and there you get that green oval and you can notice it's for ends touch the same for 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 comma 150 and extends to the location 200 comma 175 and there's 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's my line is a line that should cut across the entire graph window because it starts at zero zero well not the entire graphics window starts at zero zero in the upper left corner and goes to 100 100 this a common error which is why I show to you look up and you're like but mara there's only one line and that's your funky line or my funky line is the case maybe where's dude where is my line and it's not up there why is it not up there yeah you did not add it to the graphics content okay common error people go ahead and create something and I add all the colors and I'll set all the sizes and they'll be like oh I'm rocking and then they run their program and nothing shows up and they start tearing their hair out and beating their sexually it's a bad time they're sitting there contemplating life in jail and then they realize them I just should added it to the canvas alright but you can't give that as an excuse to the judge alright so any questions about that alright 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 all right so all an expression is is you already saw one an expression is something like int total equals n 1 plus n 2 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 either be individual variables or they can be constant values like they can be like the number 2 or 3 or they could be some method call like read into that actually gets a value from the user for example and there are 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 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 and asterisk is how we refer to times it's not an X Division and then strangely enough this thing 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 so these puppies all generally work like you would think add two numbers subtract two numbers there's also a unary minus unary minus is the funky mathematical way of saying negative sign so you could actually say like equals negative and 1 plus n2 that's a unary 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 how division is used okay 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 a 2 going to 5 2 and then you were like remainder 1 anyone remember that that wasn't us such good times I got warm like I love to remainder like that's the only like math I was like oh there and when the remainder was 0 I got pissed but that's what remainder was all about was 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 if I say seven remainder two the value of this is one because it's after I divide 7 by two and I see how many whole times two goes into seven what's the remainder it's one okay you could say something funky like hey what's seven remainder twenty you're like well twenty doesn't go into seven Marylyn so that would be zero remainder rock on so that's the way it works it's not the modulus operator if you're a mathematician or you've worked with other languages and you've seen plus you're like oh that's the modulo operator it's not in Java it's remainder and if you try to apply to negative numbers unexpected things what's not unexpected it's just not what you would have expected in a mathematical sense so as far as you'll see it in the book and everything we do in this class just apply it to positive integers and you can only apply to integers because there is no remainder with real valued values with doubles there's no remainder and so when we come back next time we'll talk a little bit more about some of these expressions any questions before we break all right I'll see you well oh there's question why don't just come up on ask it thank you
Info
Channel: Stanford
Views: 375,387
Rating: undefined out of 5
Keywords: computer, science, technology, grades, programing, software, variable, names, types, language, java, lecture, binary, machine, compiler, classes, graphics, objects, values, real, stanford
Id: NPzPnycCFuE
Channel Id: undefined
Length: 49min 15sec (2955 seconds)
Published: Thu Jul 03 2008
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.