Lecture 4 | 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 so let's go ahead and get started with the actual contents and so today is the day where we make a little bit of a transition it's like one of our transition points and life's kind of like coming to college and the transition point we make is you now come into the java world and you're like what I'm still doing Carol yeah well in lecturers we're going to start a wave bye-bye to Carol and Carol's like yeah I'll be there in your dreams when you're actually programming right so Carol is not really gone until you Donna you're done with assignment number one but now we take we sort of graduate into the world of job and you'll actually see a lot of the things that exist in Carol's world will come back because we give them to you in the gentle form enjoy in craig-carroll's world and then the Java world they come full steam so before we dive into that I want you to get a little notion of the history of computing and we'll go over this very briefly but computing actually has a very long history and it dates back about 4,000 years okay so roughly 4,000 years ago the first what we think of as computing device was made available which anyone know what that was abacus was one of the first computing device and allowed you to do some fairly rudimentary arithmetic on it but it actually was something that allowed people to compute a lot faster than they could by keeping track of stuff in their head the real sort of advances in what we think of as computing actually came around in the 1800's there was a someone by the name of Charles Babbage anyone heard of Charles Babbage by the way quick show of hands we can just call him Chuck here's actually a very well very well learned and well-known person in his day he had the Lucasian chair which is the same chair at university that actually Stephen Hawking has now that Sir Isaac Newton had at one point so pretty smart dude and he came up with this notion of something called the difference engine which was a way of being able to automatically in those days right they didn't have silicon they didn't well they had silicon but it was in the form of sand and they didn't have computers in the way we think of compute so he wanted to build a mechanical device like with real actual you know mechanics and piece of wood that would spin around they would in his time he thought it would be powered by steam right because this is sort of pre thinking about electronic computers that would do automated calculations and he designed something called the difference engine and then he designed something called the analytic engine which were supposed to be even more powerful and interestingly enough neither one of them were ever actually built during his lifetime it turns out that years later that difference engine was actually built from a bunch of wheels and rotors and stuff and now the thing that you know he would have liked to think of is the analytic engine we think of as these puppies it actually turned out in the 1800s he had a lot of the same ideas that are in our laptops or in our computers today strangely enough but so that's kind of where computing we think of the notion of computing is really coming from and it turns out the first programmer was a woman by the name of ADA Byron actually Augusta ADA Byron and if the byron looks familiar too it's because she was the daughter of Lord Byron the English poet and she was actually very taken by the designs of Charles Babbage's machines and actually wrote programs the machines were actually written to have sort of cards sort of like the jacquard loom if you've ever heard of the jacquard loom and if you haven't it's not important that actually would have programs for the kind of computations that it would do and see she actually devised various programs for Charles Babbage's analytical engine and you might be sitting there and you might think but Marilyn you just told me that the analytical engine was never actually built in their lifetime so what's she doing writing programs for computers it doesn't exist and that's actually something that happens today people write programs for computers that don't exist and you might wonder why you know what that's like not a very good use of your time well guess what if the next generation of computers is going to come out while it's still being designed before it's actually manufactured and built someone needs to figure out what kind of programs you want to run on that machine so there are actually programmers today who write programs for machines that aren't built and they simulate them by hand and they go through and try to figure out what they would do and it's a perfectly reasonable thing to do but as a result ADA Byron is in some sense the first programmer right because she was actually writing programs for in some sense a general-purpose computational device over a hundred years ago which is kind of an astounding thing if you sort of think about it now it turns out cuting actually got its first real you know what we think more closely of as computing devices in the 1930s she put nineteen here just so we're clear 1930s and 40s there were sort of the first prototypes of electromechanical computers that were actually built at the time there was a computer that was built at Iowa State by add an ass off and bury the names are actually not well they're important if you're a lawyer because it turned out there were actually lawsuits at the time about who patented the computer but we won't talk about that started sort of in this era with sort of prototypes and then the one machine you may have actually heard of by 1946 there was a machine called ENIAC which was actually built by Eckert and Macaulay at UPenn which was standard for if I remember this electronic numerical integrator and calculator and basically was one of these big things that you know sat in a big warehouse a few tons but it actually did computational you know the kinds of things we would think of as computation and it was really in some sense the first large-scale electromagnetic computer that we think about again you know modulo lawyers and then really it was in 1971 that the first microprocessor came around so we come to a fairly modern time right so it's not all that long ago like we're talking 36 years the micro processors have actually been around and so the first microprocessor and one of the folks who was on that team who built the first microprocessor Ted Hoff is actually a Stanford alum interestingly enough it was built at Intel the Intel 4004 was the name of that microprocessor and at the time no one actually thought that having the single-chip microprocessor was going to be that interesting he sort of designed this thing and I was originally going to be going over for use in some machine in Japan and they were just going to give them sort of the design and the patent for it and the folks who were getting it were like well we're not really sure it's an interesting chip but we're not actually interested in in you know owning the patent we don't think there's anything interesting necessarily here and so they kind of ran with it and the rest is history as they say and as you can imagine the last 35 years or so there has just been an astounding amount of development in computing computer science right so really this field is like hundreds or thousands of years old if you think about it but really it's kind of in your lifetime that all the interesting stuff is really happening which is also why it's exciting to be alive now and be a computer scientist because you're in the acceleration phase right think about what's going to have all the stuff that happened last like 36 years you're going to be around to see the next 36 years and you're going to be doing the next 36 years so that's just an exciting thing that I think about but another thing that comes up when you think about this what is computer science right why do we like we've been talking about Carroll programming and a lot of this class we're going to learn about software development and Java programming and so a lot of people tend to equate computer science with programming and they think well why isn't the major just called you know computer programming and why is it why is there really a science to in some sense and that's something that people actually have would use to debate they don't really debate it so much anymore is there real science of computing or is it just programming and in fact the difference between the two is that computer science or CS as well just affectionately refer to it really is the science or in some sense the study of problem solving with computers and you don't need to write this all down it's just for your edification with computers or I should say computational devices computational I'll even say methods because some people actually look at you know theoretically what's possible to compute without even necessarily thinking that it's realized in Hardware right turns out it actually surprises people there are some functions that you can prove are not computable now like what really you can yeah and it's good to know about them before you go and try to write a program to actually compute them because in theory they're not computable and you can prove that and if you're actually interested in that take CS 103 or CS 154 and we'll talk about it all the time but that's really what the science is is thinking about problem solving and approaches to problem solving how you analyze how fish in a solution is to a problem how solvable actually is the different approaches you take to it so notice the word programming doesn't show up here programming is an artifact it's something we do to realize that particular part problem solving technique in a computer or some computational method it's just something we do as part of the process right it's not what the whole thing is really about in some sense there's actually this famous quote that computer science is just about just as much about programming as astronomy is about building telescopes right astronomy is about far more than building telescopes really sort of the science of celestial bodies telescopes is the mechanism by which you actually look at them uh-huh question in the back well if you think about computer engineering part of its just kind of the definition that a particular school wants to have for it right and some places actually our computer science program is in the School of Engineering someplace that's actually in humanities and Sciences it depends on the view and it also depends on the particular program that's there so some places may actually there's no way I'm an inch in the back may just be training programmers right and the science of computing is not what they're actually interested in and so it depends on the program but we really think of computer sciences of science and it's an interesting philosophical argument we can certainly talk about it more in class but I just want to kind of push on all right so with that said that's kind of the quick tour of a couple hundred years of computer science history it turns out when we get into modern day when we actually think about computing what does a computer really understand and what is the promoter of programs that you write on and how do they turn into something the computer understand so it turns out the computer actually only understands zeros and ones and so we like to think of that as binary right binary is just a number system that only has zeros and ones it's base to numbers basically and that's all computers understand zeros and ones on and off so how do we take these things like move or turn left and turn them into ones and zeros and so there is something called machine language that is what a computer understands or in some sense what the microprocessor understands right the machine language is defined by what chip you have inside your computer that's eventually going to boil down into a bunch of ones and zeros people are real bad at writing ones and zeros in a form the computers can understand so we program in what's known as a high-level language okay and there's all kinds of high-level languages so one of the ones that you're going to learn in this class is Java on there some other ones you may have heard like C or C++ or basic or Fortran whatever the case may be these are all high-level languages because they're at a higher level than what the Machine understands and so there's this question that's actually part of the science of computing is how do you go from this high-level language into something the Machine understands right what is this translation process because there actually is some translation that needs to happen and this process going from the high-level language to the machine language is something we refer to as comp relation right so compilation is something strangely enough that's done by a compiler and Eclipse which you've been using this whole time is in fact a compiler it takes the instructions that you write in some high-level language and converts them into some form that the machine understands now it turns out the process for doing this in some languages is actually slightly different than other languages so I want to show you a quick little overview of how that looks in Java just so you can get a real sense of what's going on between the time you write a program and the time the machine executes it so you sort of understand the low-level as programmers you write what's called source code you do know it learn it live it love it source code that's what you're going to write when you were writing Karel programs you're writing source code when you write Java you're going to write source code what the Machine understands the zeros and ones here is something that's called object code an object code is essentially just the low-level instructions the machine instructions that the computer understands and so going from source code to object code is basically what the compiler does it's the translation from this to this now in regular languages that might look if we got the overhead I shouldn't say regular languages in some classical languages it might look something like this you write some source code in what we refer to as the source file a source file is just a file that contains source code so you write your program that goes into some compiler say Eclipse which is the compiler you're using and what might come out of it is an object file because what's contained in that file is just essentially object code or in this case a bunch of zeros and ones that the computer understands and there might have been other programs that someone wrote along the way like some libraries you might make use of like you did with Carol right all the basic carol functionality might be in some other library and so there's other files that contain object code and all of this stuff gets linked together into one big set of object code which we refer to as the executable or the application that's the thing that say when you double-click on your word processor on your computer and it runs you're running some executable file which is just basically a file of a bunch of ones and zeros that eventually or some time ago someone wrote a source code and got compiled down in this executable file and for a lot of languages like C or C++ this is the process that actually happens now the people who did Java thought of things slightly differently and here is kind of where things get funky in Java okay in Javas world part of what's going on is actually run on a virtual machine what does that mean it means you write some source file in Java okay that goes through some compiler and what comes out of it is not an object file but something called a class file it takes all of the high-level stuff that you write in Java or in this case you could say Carol because it's the same thing and turns it into some set of numerical instructions that are not yet ones and zeros that the computer understands but are some intermediate language that's just a numeric language okay sometimes you refer to this as Java bytecode but the name is actually not important at some intermediate language and guess what there's other classes just like there were before the contain instructions in this intermediate language and those all get linked together in some big file that we call a jar archive which just stands for jar just means Java archive strangely enough so it's actually redundant to say jar archive people just do but it means Java archive archive all right and then this whole thing is now said of this intermediate language to something that neither the human really understands okay there's a few humans in the world that might and they're a little weird we won't talk about them most people don't understand this and the computer doesn't understand it directly either so you say well that's the most useless thing ever why would I ever do that these instructions go to something called the Java Virtual Machine the JVM and what the java virtual machine says it says hey guess what I'm going to be pretending like I'm a machine that understands this as my object code I understand this stuff as the basic language I speak and I take that and when I run it I do something on your computer and you might say why would you want to have this extra process and the reason why you want to have this extra process the fact that guess what in the world there's things like Macs and there's pcs and there's Linux computers and there's all kinds of machines out there which means if you sort of do things in either the good old way or the bad old way the compiler needs to understand what is the what are the ones and zeros that your computer speaks and the ones and zeros that a Macintosh speaks are different than the ones and zeros a PC speaks and they're different than the ones and zeros sex machine speaks so the compiler needs to know all that if it's actually going to generate this kind of code for all of those different sorts of machines and most compilers don't most the time you get a compiler and says hey I'm a PC compiler I'm just going to generate stuff for the PC and that's all I do in Javas world things are a little bit more interesting because the compiler doesn't need to know what kind of computer you're running on it says hey I'm producing this intermediate language and the intermediate language is the same for all computers the only thing that has to be different on your computer is you need to have this little thing called a JVM that knows how to translate this intermediate language into what's going on on your computer so if you have a JVM for your Mac or a JVM for your PC or a JVM for your Linux machine they can all run this same low-level intermediate code and do the right thing on those computers okay and so remember when you set up a clip sure if you haven't set up Eclipse yet you'll get there soon enough but when you were setting up Eclipse we asked you to download and install something called the Java runtime environment how many people remember that raise your hand if you remember that few folks guess what the Java runtime environment provided it provided this thing for your computer ok so you write the program once you compile it once and now that class information that you have that class file can be run on any computer that has this java virtual machine and java virtual machines are sort of ubiquitous they exist for a lot of different computing platforms ok so that's why they actually do it that way and it's kind of funky but it's a little bit different now with that kind of said that's the low-level stuff that's kind of what's happening at the low level and you don't really need to know that low level intimately I just want you to understand what's happening from the programs you write to what happens when they get executed on the computer now what you do need to know is how do we begin to write programs in Java and Java you'll see some examples of it today is what we refer to as an object-oriented language ok so Java is object oriented and not all languages are object oriented and what is this whole object-oriented thing about so you should know what this whole notion is about before you start programming in a language that's kind of seeped in the idea the idea basically is that a program is written as a bunch of classes right so if you think about let's back up for a second if you think about Carol right when you were writing general programs or you saw Carroll programs you would actually create some Carroll program by saying public class Carroll program extends super Carroll let's just say we're using the super Carroll model and then you put some stuff in here you have like your run method in here maybe some other methods and what you did was you were creating a class that was basically the information for your program what instructions you were actually going to execute as part of that program so what a Java program is it's just a set of classes you may have more than one class like I'm Carol right now you always just have one class and inside that class you may have a bunch of different methods but you always have one class a Java program can actually have multiple classes associated with it when we start in this class we'll start with some simple ones that are all just one class but the idea to think about is that you can have more than one class okay and what these classes are which you can think of a class as okay a class is just basically an encapsulation of some behavior that the program does just like in Java or in Carroll's world you had some behavior of a bunch of different methods you defined for Carol to actually be able to execute right those are just different behaviors but you also have some data so it's behavior and data because in the Java world you're now actually going to keep around a tract of information that are not just beepers in some world but they're actually like numbers that you're going to store somewhere and that's your data and so what we do is we think of having all the behaviors all the kind of manipulations you might want to do with data along with the data and put that all into this thing called the class so just like in Carol where you had a class and you had behaviors you could just imagine yeah we're going to have some behaviors which means in Java we're going to have some class and define a bunch of methods in that class but we're also going to have some data associated with it that whole thing together is going to be a class now it turns out the thing that's interesting about classes so any questions about the notion of a class it's just kind of this abstract concept right now the thing that makes class is really powerful is the classes actually get organized into hierarchies let me just draw it here okay and you've actually already seen this in a very small way with Carol so all the things that you've seen and Carol are going to translate over remember we talked about the basic Carroll model and when you were writing a program you could say extends Carolyn when you did the basic Carroll model you got like move and turn left and pick beeper and put beeper and that's all you got and then we said hey you know what you really want to do turn right and turn around well there's this thing called super Carol and super Carol is just a souped up version of Carol well guess what Carol was just a class and super carol was also a class but super carol is a class that extends the functionality of the basic carol class okay so we say that's super Carol the way we would draw things if we were going to put box around them and draw them like the book is say that super Carol is a subclass of Carol and Carol is the superclass so this is where it's going to get confusing the superclass are the things in some sense that are more general and the subclass are the things that take the basic idea of some class and extend it so super Carol does everything Carol does but it also does turn right and turn around okay so super Carol is a subclass of Carol or alternatively Carol is a superclass of super Carol which sounds a little bit backward but the super here and the super here mean two different things super here means sort of above and super here means cooler than okay so don't think of them as like they mean the same thing it's just an overloaded term all right so you might say okay Marilyn this get a whole little abstract for me well let me put it in terms that you understand you're a human being right at this point you should say right and if you're not come talk to me hi it wouldn't be the first time so we have humans okay humans are just a class okay and all humans are primates primates are a superclass sorry yeah or a superclass of humans because all humans are primates all primates are mammals and all mammals are animals so there's actually a much deeper and richer hierarchy say in biology than there is with Carol and when you actually see job a job we'll eventually have sort of a richer hierarchy stated with it now the interesting thing about this is not only are humans primates but monkeys are also primates ok humans are not monkeys and monkeys are not human so there's not a strict rule you might say well we don't know about that but there is an a strict relationship between humans and monkeys other than they're both primates and that makes the mammals and that makes them animals and the whole notion of having this hierarchy of classes right so you could think of being an animal is just a class and it has some behaviors what does it mean to be an animal well to be an animal that means you digest food right that's like not something like Sun Road light and actually one of the technical terms I'm not a biologist but this what my friends tell me is that embryos plant pass through the blastula stage any biologists in here is that right ok good good because I was like asking a friend of mine like what differentiate animals from plants he's like well of course the embryo is passed to the blastula station I'm like alright on and no idea I was like how do you spell blastula um so I'll just put blastula down here right and so that means guess what all and all mammals inherit those same properties all mammals also have some internal digestion of food and also pass through this blastula stage but mammals additionally we know are warm-blooded in general and they have mammary glands which is what they're named mammal where the named mammal comes from so mammary glands or it's just kind of a generalization of sweat glands but that's what mammals have and then all primates are mammals which means all primates have all of these same properties plus they have more and so primates interesting enough have five fingers I didn't know all primates have five fingers but evidently I'm told that they do they have opposable thumbs and they also have fingernails but fingernails we just won't put up there it's not a big deal ok and then we get down to humans and monkeys right and humans it turns out in theory anyway we're supposed to have highly developed brains I will just put brains in culture because other animals have brains too for suppose to have highly developed brains and we have a wrecked body carriage that's just what we are we're erect brains and monkeys have other things that are going on some evidently that brains are not as highly developed and they sort of like their knuckles dragged on the ground sometimes I do that in the morning but that's not important but that's the whole point this is the class hierarchy okay so these are all classes and things that are subclasses of some other class means they inherit all of the behaviors of the things above them all the way up the chain plus they may have their own additional behaviors and that's the key concept here of organizing things into classes and you'll see that in more specific instances when you get into Java you saw it already with Carolyn just a really simple example right there was four commands for Carol super Carol gave you those four plus an additional two that was it okay now besides this idea there is another key idea that comes up besides this notion of classes okay and that's the notion of the instance of a class okay and what the instance of a class is is humans or a class there is no one person that is oh you are humanity right you are a person right so some in there what's your name Wow Eduardo all right so I hope I'm spelling this right ed do all right oh is that right is an instance of human I would hope right so what that means is you're going to have things in your Java programs that you create which are objects and what an object is to differentiate it from a class so Eduardo is an object okay I'm sorry sorry to break it to you that's just you're an object an object is an instance of a class it is a particular example of a class there will be multiple objects or multiple instances potentially of some class and that instance of a class has all of the characteristics of the class and all of the other classes above it in the class hierarchy so by looking at this I can say hey Eduardo guess what you went to the blast chiller phase when you were an embryo good times because you're an animal right and I know that and this is inherited all the way up so that's the important thing to keep in mind is you're going to be writing classes you're going to be writing things that define some set of behavior and along with that you will be creating instances so this is an instance of the class and the instances are what we refer to as objects so all of your instances in the world are just things that we think of as objects and your classes are in some sense the templates for those objects okay so any questions about the general concept I know there are a little bit high level but it's important for you to kind of understand them so with that said let's actually look at begin to look at the notion this notion in Java okay and so the way that this is going to work uh-huh so in Carol it turned out what you're actually just creating was a single Carroll object was being created that contained your program and that's what was actually run so you created the class Carol and when that puppy actually sort of fire it up and you saw a little Carol running around in the world that was a Carol object that was an instance of your class now you actually didn't see multiple object or multiple instances there there was only one Carol instance so there it's very it's a good question because it's very easy to get confused between the instance and the class in that case because there was only one button here you'll actually see will create classes and will have multiple instances of them but you're running Carol was basically the instance of that object okay question so basically like it'd have like Oh Carol found in like one program you could if we sort of set it up that way in the Carol the way it's kind of set up is it only allows you it only sort of behind the scenes crates for you this one instance of Carol but in in some you know alternative Carol universe we could have actually just taken your class and say hey guess what we're going to just create multiple Carol instances from your class and have them all run around in the same world we could up and guess what that's what seemed to have happened in this room right somewhere along the way there was this class human and someone came along and created multiple instances of you wouldn't say just necessarily one person but now there's multiple instances of you right and you're all running around the world doing your thing and you interact with each other and guess what classes actually do they interact or objects actually do they interact with each other okay so with that said there's going to be some functionality we're going to use that was written by someone else so just like Carol the basic version of Carol was written by someone else when we start with Java we're going to start with some set of scaffolding or libraries that were written by someone else that are going to allow us to do a bunch of powerful things in Java very early on okay and this is something that's called the ACM program hierarchy so it sounds all complicated but all this is right in much the same way that I drew that picture over there with animals mammals primates humans and monkeys this is a hierarchy of classes that exist in Javas world or at least are provided by the ACM and you might say who is the ACM anyone know who the ACM is they're big they're bad they're nationwide they're the Association for Computing Machinery it's the oldest computing Society they've been around actually for about 60 years or so on your life but Marilyn I thought you said hundreds of years yeah before then people called it math so Association for Computing Machinery has this programming hierarchy and what that means is the kind of programs that we write in this class just like when you wrote your Carroll programs you extend Carroll or you extend super Carol you're going to be extending different kinds of programs either a console program which is something that produces textual output a dialog program which brings up little dialog boxes that ask for information or a graphics program that actually draws some pretty funky stuff on the screen all of those classes are classes that are inheriting from a class a superclass called program so all of the things that you write is a dialog program or graphics program are all something that our programs and all programs are something of type J applet which just means Java applet and all Java applets are something of some type called applet anyone ever heard of an applet few folks an applet is something you can run on your web page interestingly enough that's kind of where the term comes from it's like a lightweight application it's an applet it's like an app application but small and so it turns out since all of the programs that you're writing are actually inherit the properties of being an applet they actually will be applets which means you can put them on your web page and run your programs on your web page if you want later on the class we'll talk about how to do that okay but you're going to go and in this class at least you're not going to write anything that directly ends like jail plate or a plate or program everything you write is going to extend down this level but it's important to know there's different kinds of programs that you can actually write at that level so let's look at an example of a Java program and I'll show your first Java program today so we can feel good about Java no it's not that one it's this one it's small it's tiny it's fun it's Java all right so let me expand this whole thing out so you can see the whole program still fits on one screen okay and you might suddenly notice there's a bunch of things in this program that look very similar to Karel that's because carol was implemented in Java so the first thing you want to look at here is we have a file called hello program dot Java just like you're Carol programs are written with a dot Java file you were creating a Dajjal file here this is a source file because it has source code in it up at the top we have a comment ji uses the exactly the same structure as carol comments yeah because carol comments were actually Java comments comments and Carol and Java exactly the same just like you imported Stanford carol dot star now you're going to be using the stuff that the lovely folks at the ACM have provided for you so you're going to import ACM graphics dot star an ACM dot program dot star what are these things these are just if we bit well I won't back up because we're not on the slides these are just remember I said you write your classes and someone else may have written some classes and they all get linked together before they're executed these are just some other classes that someone else wrote that are going to get linked in to what you do right they provide you the definition for things like what a graphics program actually is so now what you're going to do as we talked about all Java programs are just a collection of classes so just like in Carol we have public class and some name for your program here we'll call it hello program and a hello program is a particular kind of program it doesn't extend super Carol because it's not a super carol program anymore it's going to be a graphics program it's actually going to draw some stuff on the screen so it's going to extend the graphics program but all the boilerplate should look the same right that's why we had you do Carol because all the stuff from Carol just translates directly over to Java except now we're sort of cooking with gas now we're doing the real thing ok so it extends graphics program and guess what inside here we have a run method just like Carol and that's where the program begins executing now where things get funky is you're like oh man what happened to pick beeper and put beeper and move like life was so good it was easy you know turn right was like the extent of it what is it like add new G label hello world like what is this all about hey we'll go through the step-by-step all this is saying is when you create a graphics program we'll go through the details in just a second you're going to get a blank screen you're going to get a empty canvas in some sense you're going to be an artist and you're going to draw a stuff on that campus and the thing that you're going to draw on that campus is some label and all that label is just basically words and the words are going to be hello world because you want to write hello world on the screen and you want to write it at a particular location on the screen the locations are you on the screen you want to write it out is 100 comma 75 and I'll tell you where that is on the screen in just a second and then once you create this little label you're going to add it to your canvas you're going to say I have some canvas plop that puppy onto my canvas okay so let's run the program and I'll just plop it onto the canvas for you and you'll see what's going on life will be good so we run this program all right so we want to do hello program we're getting excited we're running we're running the disks just turning away and there's your first Java program you're all Java programmers now what did you do you created a graphics program which brought up this big window and said I'm I'm blank canvas draw on me and you said all right hello world hello and I'm done because that's all you did you said here's a little world put it put it up on the canvas for me at a location 100 comma 75 and thanks for playing that's all I'm going to do but now you've actually gone through the whole process of compiling your Java program and turning it into the same immediate code the intermediate code gets executed and you actually did something and that's like half the battle right there the next nine weeks is the other half of the battle but half the battle is like getting this up I kid you not really if you can make that happen you're you're you're just most of the way there so let's do something a little bit more exciting then let's actually do some interaction with you let me show you another program remember this one is a graphics program I told you there's a bunch of different kinds of programs you can have when I showed you the picture right so we had a graphics program let's look at something called the console program over here uh-huh I know not in Java so you're always extending one kind of program in a graphics program we'll actually as you saw you can put up text a console program is just meant to be a textual program so you're not going to do any drawing but here's another program right again a lot of the same things as before you have some comment up at the top you have some libraries that you're going to import in this case you're not doing anything with graphics so you don't need to import ACM graphics dot star you just need to import ACM graphics top or in ACM dot program dot star which just says get me all the standard stuff for a program if you're doing graphics you also need to have a second line for ACM graphics dot star if you're not doing graphics you don't so again public class this one's called add two integers because guess what it's going to ask the user very exciting program for two integers and add them together but in fact it's going to do something interactive with the user which is pretty exciting in itself this is going to extend a console program because it's not going to have any graphics oh yeah I know it's sad times but sometimes text is very powerful what's it going to do when it runs it's going to write out to the screen and this time we're going to use something called print line' which is just we like to drop some vowels here and there which stands for print line and it prints a line this program adds two numbers then it's going to read an integer from the user and we'll go through all this stuff in more detail don't worry this is just the high-level overview so you get some basic idea of what Java looks like it asks the user for n1 right because we have to be very scientific so rather than give me the first number we say enter n1 and suddenly we're we're much more formal enter n2 right and so what this is actually doing is asking the user for a number and whatever number the user gives us we stick in this location called n1 and whatever number the user gives us here we're going to stick in a location called n2 and we'll actually talk about what these locations are and how they get set up and everything next time but I just want you to see something before we got to go into all the details because we got to start somewhere then we're going to add n1 and n2 together and store them in a place called total and then we're going to write out the total is whatever the total is and then a period right so this is in some sense the world's most expensive calculator that adds two numbers and will go ahead and run it do to do to add two integers or compile in we're feeling good and that's the program we want to run and now this comes along and notice now we have some window that rather than putting graphics all over and telling it where to display some words like hello world we just said write some line and so what it's going to do is just sequentially write lines on the screen because this what we think of is a console all a console really is the way you can think about it is it's a window that contains text and can potentially interact with the user so there's places where we might ask the user for input so we said this program adds two numbers I hope you can see this in the bags tiny tiny font enter an one and it's sitting there with a blinking cursor and anytime you see the blinking cursor that means hey I'm expecting some value so give me some value with some integer twelve someone said twelve someone said one so I'll take 12 as n1 then it asks for n2 I'll give it one and lo and behold it says the total is thirteen period end of program okay at this point there's a blinking cursor but the program is done but now you've just seen a real Java program that actually takes in values from the user compute some does some computation on them and displays output okay and this is a console program as opposed to a graphics program because all the stuff we're doing here is textual okay so any questions about the basic idea of classes or objects or these different programs or writing the programs that are running one they run they are being their objects their instances that are being run for you what they're doing as instances their objects that are inheriting all the behavior from the class template and then when we run an instance is actually created in its run because if you say ACMs ostrov strangely enough you won't necessarily get everything underneath a cm as well you'll get everything at that level so there's actually a package for a cm and there's a package a cm graphics and there's a package a CM program and what you really want is everything inside a CM program and a CM graphics not just the things they're in the a CM package by itself yeah it's kind of a it's an interesting technical question and if you don't need to worry about the details just to add both of them and you'll be fine uh-huh no the console program you just print out that's one of the differences with the graphics price should actually say you both up with the graphics program as the graphics program you have to tell it where stuff goes console program is just writing out line by line uh-huh sorry yeah every time you run a carol program you can think of what it's doing is creating an instance of Carol which is that little guy you see on the screen and he's going around doing stuff so and Carol there was always only one instance of the object here you'll actually see will create some objects where we can have multiple instances of an object in the same world and I'll show you some examples of that in just a second but we just haven't gotten there yet okay so what we're going to do now is we're going to think a little bit about graphics that we can actually draw because in the graphics world is easiest to see multiple objects you're going to put multiple stuff up inside our little graphics canvas so what's this whole graphics thing about let me tell you a little bit about the graphics window and then we'll put a couple different objects up in the graphics window and you can see what I'm talking about so in terms of graphics all graphics that we're going to do are a model called a collage madlyn if you remember in the days of yore when you are a wee tyke did anyone have like a little felt board of little felt animals that you put on it yeah that was a collage or if you had a big piece of paper and you had some like crazy we're not crazy glue as well as I was glue stick don't smell that stuff too much but you like would take some piece of paper and you're like oh here's a picture of a cat and you would like put some glue on the back of it and put it up on there and then you say alters picture a dog and put some glue on there and put it up on there that's the model for graphics in Java we're going to have these little pictures or little objects and we're going to say show up here and show up over here and show up over there so what you have is a collage which starts off with a blank canvas and you're going to put objects onto that canvas okay and so what we can do is create various kinds of shapes or pieces of text and put them up in various places on the canvas and you just saw an example of that with her la HelloWorld but I'll show you some of the other things that you can create the different kinds of objects that exist in the graphics world one you just saw it's called a G label for a graphic label this is just text in some sense it's text that you can put up somewhere in the world there's also a G rect which is a rectangle so oddly enough a G oval which is an oval or if you actually make the height and width the same it becomes a circle right so that's why we don't have historical we have oval and same thing with rect versus square because all square rectangles ng line which just means a graphic line so these are the different kinds of objects we can have these are classes they are templates for objects we're going to create particular instances of these objects and put them up in the collage but these just tell us the kinds of things we can have there the general class so if we come back if we close this guy and we come back over here to fund to hello program what we've done is we've said I want a G label G label is the class when I say new G label what that actually does it says give me a new instance of that class give me an object that is a G label and the things that you're going to specify about that particular instance or it has some text associated with it which is hello world we put all the text inside double quotes okay so hello world without the double quotes is actually the text that's associated with this label and we give it some location 100 comma 75 so now this little object okay which is an instance of a label it says hey I'm one particular label in the world and the kind of label I am I say hello world and I'm at location 107 and 175 and it's like okay you have this label where you going to do with it and I'm like put it on the canvas and the way you put it on the canvas as you say add so in all graphics programs when you say add there is something that you will specify to add the things that you are specifying to add are instances they are individual objects that you're going to put on the canvas and the way you get those individual objects is you create a new version of some particular class okay so this creates that new label at location 175 and it gets when it gets added it gets put up there now there's other kinds of things we could do with this so if we kind of return over here Q oh this is just the add two integers program let's do add two integers real quickly at 17 25:42 writes out 42 same thing you just saw except now in excruciating detail let's say we want to do something that makes a G label more funky than we originally had okay so again we're going to have our Hello program that's going to extend the graphics program and here we're going to do things slightly differently what we're going to do here is we're going to create a new instance of Ag label object so we say new G label and again we're going to name it hello world and 100 comma 75 so I have some label that's hello world at location hundred 75 and I say hey you know what label I want you to look different than you actually are okay so now that I have this object I have this little label you can think of it as little object that sits somewhere in the world it's not being displayed yet because I haven't added it yet to my canvas it's being stored somewhere which I have named label okay and we'll talk about this notion of G label label next time but something I've created called label which is just a new instance of this hello world label first thing I want to say is hey you know I want to change what the font looks like anyone know what a font is right you probably use different fonts in your word processor it's just the way characters actually appear on the screen it's a term that comes from typesetting I want the font to be sans-serif thirty six so what happens when I start off is I say create for me a new label that's name is hello world and so it says okay I have some label object the label the words that are associated with that label hello world notice there's still nothing on the screen here's my screen that's my canvas this is all in the computer's memory somewhere it's just saying yeah you've created some label you haven't displayed it yet you've just created it and I say hey I want to tell that label to change its font to sans-serif thirty six the way I do that is I have an object that I've created the object is named label I use the name of the object followed by a dot followed by a method that I'm going to call on that object remember in Carroll's world we had methods the way we called Carol's methods we just gave the name of the commands to go execute that method now all methods are associated with objects at least for the time being so when I have label and I say set font what that actually means is set the fonts for this particular label object to be sans-serif thirty six and so when I execute that what happens is it becomes thirty six point font in sans-serif which means serifs are just a little things at the ends of if you can see the font up there has a little you know Swiss cheese at the end of the letters those are serifs so font like this doesn't have any of the squishies at the end of the letters its sans serif sans means not okay then we say you know what I'd really like you to be red because black is just so blase and red is really the new black so set your color to be the red color okay so I execute that and it turns itself red there's still nothing on the screen this is all kind of with the label and now that I've taken this label and I've taken its text and made it big and turn to red I finally say hey graphics program add this label add this object to your collage and it says okay and it adds it at the location at which this label was created which was 100 comma 75 so then it finally shows up on the screen it only shows up on the screen when I actually do the add up to that point all I've done is modify some of the properties of this thing okay so starting next time what we're going to actually get into is thinking about what is this G label thing about what does it mean when we actually specify a method for an object and go into all that in a little bit more detail but hopefully you've seen an example of what your first Java program looks like all right I'll see you on Wednesday
Info
Channel: Stanford
Views: 501,280
Rating: undefined out of 5
Keywords: computer, science, technology, grades, programing, software, language, java, lecture, source, code, object, engineering, stanford, Eniac, microprocessor, computational, methods, binary, karel, machine, compiler, classes, consoleprogram, dialog
Id: nWheM30THaY
Channel Id: undefined
Length: 48min 28sec (2908 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.