Java Programming and Object-Oriented Programming: From Karel to Java, Study notes of Programming Methodologies

The transition from karel programming to java programming, explaining how concepts learned in karel are expanded upon in java. It covers the history of computing machines and the role of ada lovelace and charles babbage, the difference between source code and object code, and the concept of classes and objects in java. Students will gain a better understanding of the relationship between karel and java, as well as the importance of classes and objects in java programming.

Typology: Study notes

2010/2011

Uploaded on 10/04/2011

hollyb
hollyb 🇺🇸

4.8

(44)

431 documents

1 / 21

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Programming Methodology-Lecture04
Instructor (Mehran Sahami):So welcome back. Now we’re officially started with the
class. I hope you had a good weekend. I was just asking people before class what kind of
stuff they did this weekend. So if anyone wants to, come early. We’ll just engage in
random conversation.
So a couple of quick announcements: There’s one handout, which is your section handout
for this week. Make sure you get that. What I’ll do every week that we have a section, is
that week, you’ll get a handout on Monday, which is a problem that you’re going to go
over in section.
This gives you a chance to think about it before section. It’s not required that you do it.
It’s not extra homework; it’s just something and if you want to read through it to get
familiar with what you want to do with section or possibly do it yourself if you want
more practice to be prepared for section, you can do that and then you’ll go to section;
you’ll cover the problem and actually in section, you’ll get solutions to the problem, as
well, so you’ll get examples of code for all the section problems.
Section assignments – hopefully you should have signed up. The deadline for signing up
was Sunday at 5:00 p.m. Section assignments will be mailed out to you tomorrow night
so you’ll know which section you’ve been assigned to and you can go to sections this
week, get your section leader’s email address, complete Assignment 1, at least the email
part by sending email, and I don’t need to mention the sections actually start this week, so
you should go this week once you get your section assignment.
If for whatever reason you didn’t manage to sign up for a section, you should remedy the
problem by emailing cs198@cs and let them know you forgot sign-up for a section for
CS106a and they’ll let you know what times are available and you can sign up.
Don’t email me because I have nothing to do with the sections, other than actually
assigning the problems for the section. Don’t email Ben because he’s not involved in
administration for the sections either. He also worries about the content of the sections.
But the 198 folks handle all the administration. Uh-huh?
Student:[Inaudible]
Instructor (Mehran Sahami):Yeah, you should submit it again after you’ve been
assigned to a section just to clarify. All right, but if you’ve already submitted it, we can
sort figure it out eventually, but it’s in your best interest to re-submit it after you’ve been
assigned to a section. So any questions about anything else before we start? Uh-huh.
Student:[Inaudible].
Instructor (Mehran Sahami):Yeah, the most submission is the one that’s counted if you
submit multiple times. It just lets you kind of submit more if you’d like, if you submit
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15

Partial preview of the text

Download Java Programming and Object-Oriented Programming: From Karel to Java and more Study notes Programming Methodologies in PDF only on Docsity!

Programming Methodology-Lecture

Instructor (Mehran Sahami) :So welcome back. Now we’re officially started with the class. I hope you had a good weekend. I was just asking people before class what kind of stuff they did this weekend. So if anyone wants to, come early. We’ll just engage in random conversation.

So a couple of quick announcements: There’s one handout, which is your section handout for this week. Make sure you get that. What I’ll do every week that we have a section, is that week, you’ll get a handout on Monday, which is a problem that you’re going to go over in section.

This gives you a chance to think about it before section. It’s not required that you do it. It’s not extra homework; it’s just something and if you want to read through it to get familiar with what you want to do with section or possibly do it yourself if you want more practice to be prepared for section, you can do that and then you’ll go to section; you’ll cover the problem and actually in section, you’ll get solutions to the problem, as well, so you’ll get examples of code for all the section problems.

Section assignments – hopefully you should have signed up. The deadline for signing up was Sunday at 5:00 p.m. Section assignments will be mailed out to you tomorrow night so you’ll know which section you’ve been assigned to and you can go to sections this week, get your section leader’s email address, complete Assignment 1, at least the email part by sending email, and I don’t need to mention the sections actually start this week, so you should go this week once you get your section assignment.

If for whatever reason you didn’t manage to sign up for a section, you should remedy the problem by emailing cs198@cs and let them know you forgot sign-up for a section for CS106a and they’ll let you know what times are available and you can sign up.

Don’t email me because I have nothing to do with the sections, other than actually assigning the problems for the section. Don’t email Ben because he’s not involved in administration for the sections either. He also worries about the content of the sections. But the 198 folks handle all the administration. Uh-huh?

Student: [Inaudible]

Instructor (Mehran Sahami) :Yeah, you should submit it again after you’ve been assigned to a section just to clarify. All right, but if you’ve already submitted it, we can sort figure it out eventually, but it’s in your best interest to re-submit it after you’ve been assigned to a section. So any questions about anything else before we start? Uh-huh.

Student: [Inaudible].

Instructor (Mehran Sahami) :Yeah, the most submission is the one that’s counted if you submit multiple times. It just lets you kind of submit more if you’d like, if you submit

something and you’re like, “Oh, I made a mistake.” Then you can go back and submit again. We don’t feel like, “Oh, every time I like save, I need to submit.” Don’t worry; just submit when you feel like you’re done and if you need to go back and make changes, that’s fine. Uh-huh?

Student: [Inaudible].

Instructor (Mehran Sahami) :If it’s really necessary and the main thing is if there’s space in other sections. Yeah, you would email cs198@cs and let them know. All righty.

So let’s go ahead and get started with the actual content. And so today is the day where we make a little bit of a transition. It’s like one of our transition points in life. It’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, “But I'm still doing Karel.” Yeah, in lectures, we’re going to start to wave bye bye to Karel and Karel’s like, “Yeah, I’ll be there in your dreams when you’re actually programming, right.”

So Karel’s not really gone until you’re done with the Assignment No. 1. But now, we take – we sort of graduate into the world of Java and you’ll actually see that a lot of things that exists in Karel’s world will come back because we give them to in a gentle form in Karel’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. Okay, 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 one we think of is computing device was made available, but then you want to know what that was? Advocates.

The first computing device 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. That was someone by the name Charles Babbage. Anyone heard of Charles Babbage, by the way, a just quick show of hands. We can just call him Chuck.

He was actually a very well learned and well-known person in his day. He had the Lucasian chair, which is the same chair at university that actually Steven 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 is a way of being able to automatically.

And in those days, right, they didn’t have silicon. Well, they had silicon, but it was in the form of sand, and they didn’t have computers in the way we think of computers, so he wanted to build a mechanical device like with real, actual, you know, mechanics and pieces of woods that’s been around, and in his time, he thought is would be powered by

There was a computer that was built at Iowa State by Atanasoff and Berry. Their names are actually not – well, they’re important if you’re a lawyer because it turned out there were actually lawsuits at the time by who patented it in 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 Elkhart and McCauley at U Penn which was standard for, if I can remember this, for electronic, numerical integrator and calculator.

And basically with one of these big things, 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 electro magnetic computer that we think about, again, you know, modular 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 microprocessors 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 Hawk, is actually 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 a single chip microprocessor was going to be that interesting.

He sort of designed this thing and it was originally going to be going over for use in some machine in Japan and they were just going to give them some design and the patent for it and the folks were getting like, “Well, we’re not really interested. Yeah, sure it’s an interesting chip, but we’re not really interested 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 in the last 35 years or so, there’s just been an astounding amount of development in computing and computer science, right, so really this field is like hundreds or thousands 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 this acceleration phase, right.

Think about what’s going to happen, like all the stuff that happened in the last 36 years, you’re gonna be around to see the next 36 years and you’re gonna 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, like why do we – we’ve been talking about Karel programming and a lot of this class we’re gonna 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 a major just called, you know, computer programming and why is there really a science to it in some sense.”

That’s something that people actually would use to debate. They don’t really debate it so much anymore. Is there a real science to computing or is it just programming and in fact, the difference between the two is that computer science or CS as we’ll 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 his 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 without even necessarily thinking that it’s real life in hardware, right.

Turns out it actually surprises people there are some functions that you can prove are not computable. I'm like, “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 the approaches to problem-solving, how you analyze how efficient a solution is to a problem, how solvable it 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 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 is actually this famous quote that computer science is just as much about programming as astronomy is about building telescopes. Right, astronomy is about far more than building telescopes. It’s really sort of a science of celestial bodies. Telescopes is the mechanism by which you actually look at them. Uh-huh, question in the back?

Student: [Inaudible].

Instructor (Mehran Sahami) :Well, if you think about computer engineering, part of it is just kind of a 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. Some places it’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 gonna hit you 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 the computer science as a science, and it’s an

you were writing source code. When you write Java, you’re going to write source code. What the machine understands, these zeros and ones here, it’s something that’s called objects 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. Is shouldn’t say regular language. In some classical languages, it might look something like this, you write some source code in what we refer to as the source file.

The 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 is 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 Karel, right, all the basic Karel 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 as some time ago, someone wrote and source code and it 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 Java’s 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 Karel 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, which is just a numerical language.

And sometimes you refer to this as Java bite code, but the name is actually not important. It’s some intermediate language, and guess what, there’s other classes just like there were before that 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. All right, and then this whole thing is now instead of this intermediate language, it’s something that neither the human really understands – okay, there’s a few humans in the world that might and they’re a little weird and 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 is 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 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 that a PC speaks.

And they’re different than the ones and zeros that the Linux machine speaks. So the compiler needs to know all that if it’s actually gonna generate this kind of code for all of those different sorts of machines and most compilers don’t. Most of the time you’ve got a compiler that says, “Hey I'm a PC compiler. I'm just gonna generate stuff for the PC and that’s all I do.”

In Java’s 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 the 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 Eclipse or if you haven’t set up Eclipse yet, you’ll get there soon enough, but when you’re wetting up Eclipse, we asked you to download and install something called the Java run-time environment and how many people remember that? Raise your hand if you remember that? You folks – guess what the Java runtime environment provided? It provided this thing for your computer, okay.

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

So just like in Karel where you had a class an you had behaviors, you can just imagine, yeah, we’re gonna have some behaviors, which means in Java we’re gonna have some class and define a bunch of methods in that class, but we’re also gonna have some data associated with it and that whole think together is gonna be a class.

Now, it turns out, the thing that’s interesting about classes. Is there any questions about the notion of a class? It’s just kind of this abstract concept right now. The thing that makes classes really powerful is the classes actually get organized into hierarchies, I mean, just draw it here, okay.

And you’ve actually already seen this in a very small way with Karel, so all the things that you’ve seen in Karel are gonna translate over. Remember we talked about the basic Karel model and when you are writing a program you could say extends Karel and when you did the basic Karel 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 really want to turn right and turn around, well there’s this thing called Super Karel and Super Karel is just a souped up version of Karel.” Well, guess what, Karel was just a class, and Super Karel was also a class, but Super Karel is a class that extends the functionality of the basic Karel class, okay.

So we say that Super Karel, the way we would draw things, if we were gonna put boxes around them and draw like the book is say that Super Karel is a sub class of Karel and Karel is the super class, this is where it’s gonna get confusing, but super class are the things in some sense that are more general and the sub class are the things that take the basic idea of some class and extend it.

So Super Karel does everything that Karel does, but it also does turn right and turn around, okay. So Super Karel is a sub class of Karel or alternatively Karel is a super class of Super Karel, 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 want to think of them as they mean the same thing, it’s just an overloaded term, all right? So you might say, “Okay, Marion, this is getting 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. All right, 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 super class 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 then there is with Karel and when you actually see Java, Java will eventually have sort of a richer hierarchy associated with it.

Now, the interesting thing about this, this is not only humans primates, but monkeys are also primates, okay. Humans are not monkeys and monkeys are not humans, so there’s not a strict – you might say, “Well, we don’t know about that.” But there isn’t a strict relationship between humans and monkeys other than they’re both primates, and that makes them mammals, and that makes them animals, and the whole notion of having this hierarchy of classes, right, so you could think of being animal as just a class and it has some behaviors. What does it mean to be an animal?

Well, to be an animal means you digest food, right, it’s not something like something like [Inaudible] actually, and one of the technical terms – I'm not a biologist, but just what my friends tell me, embryos pass through the blastula stage. Any biologists in here? Is that right?

Student: Yeah.

Instructor (Mehran Sahami) :Okay, good, good – because I was like asking a friend of mine, like, “What differentiates animals form plants,” and he was like, “Oh, of course, the embryo was passed through the blastula stage and I was like, “Oh, right on.” I had no idea, and I was like, “How do you spell blastula?”

So I’ll just put blastula down here, right. So that means, guess what, all mammals inherit those same properties. 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 where the name mammal comes from.

So mammary glands or 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 interestingly enough have five fingers. I didn’t know all primates have all 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, okay?

And then we get down to humans and monkey, right, and humans it turns out in theory anyway were supposed to have highly developed brains. I’ll just put brains in quotes here because other animals have brains, too, but we’re supposed to have highly developed brains and we have an erect body carriage.

That’s just what we are. We’re wrecked brains. And monkeys have mother things that are going on, so evidently their brains are not as highly developed and they’re sort of like their knuckles drag 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. These are all classes and things that are sub classes 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

that’s what was actually run. So you created the class Karel, and when that puppy actually sort of fired up and you saw little Karel running around in the world, that was a Karel object; that was an instance of your class.

Now, you actually didn’t see multiple objects or multiple instances there. There was only one Karel instance. So there it’s very – it’s a good question because it’s easy to get confused between the instance and the class in that case because it was only one.

But in here you’ll actually see we’ll actually create classes and we’ll have multiple instances of them, which are running Karel with basically the instance of that object. Okay, question?

Student: So, basically like you can have, like several Karels running around in like one program?

Instructor (Mehran Sahami) :You could if we sort of set it up that way. In the Karel, the way it’s kind of set up is it only allows you, it only sort of behind the scenes creates for you this one instance of Karel, but in some, you know, alternative Karel universe, we could have actually have just taken her class and said, “Hey, guess what, we’re gonna just create multiple Karel instances from your class and have them all run around in the same room.”

We could have, 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. Well, I wouldn’t just say just one person, but now there’s multiple instances of you, right, and you’re all running around in the world doing your thing and you interact with each other and guess what objects actually do; they interact with each other, okay?

So with that said, there is gonna be some functionality we’re gonna use that was written by someone else. So just like Karel, the basic version of Karel was written by someone else. When we start with Java, we’re gonna start with some set of scaffolding or libraries that were written by someone else that are gonna allow us to do a bunch of powerful things in Java very early on, okay? And this is something that is called the ACM program hierarchy. So it sounds all complicated.

But all this is, right, and 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 Java’s world or at least are provided by the ACM and you might say, “Who is the ACM?” Anybody 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 for actually for about 60 years or so. And you’re like, “But Marion, I thought you said 100 years. Yeah, before then, people called it math.

So Association for Computing Machinery has this program hierarchy and what that means is the kind of programs that we write in this class, just like when you wrote your Karel programs you extend Karel or you extend Super Karel, you’re going to be extending different kinds of programs, either a console program which is something that produces textural output, a dialog program which brings up little dialogue 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 super class called program. So all of the things that you write as a dialogue program or a graphics program are all something that are programs. And all programs are something uptight J-Applet, which just means Java Applet and all Java Applets, are of some type called Applet.

Anyone ever heard of an Applet? 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, and applet – 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 and later on in the class we’ll talk about how to do that, okay?

But in this class, as least, you’re not gonna write anything that directly extends like J- Applet or Applet or program. Everything you write is gonna extend down to this level, but it’s important to know that 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 you 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 Karel was implemented in Java. So the first thing you want to look at here is we have a file called helloprogram.java, just like your Karel programs are written with a .Java file, you are creating a .java file here. This is a source file because it has source code in it.

Up at the top, you have a comment, “Gee, it actually uses the exactly same structure as Karel comments.” Yeah, because Karel comments were actually Java comments. Comments in Karel and Java are exactly the same. Just like you imported stanford.karel.star, now you’re gonna be using the stuff that the lovely folks at the ACM have provided for you, so you’re going to import acm.graphics.star and acm.program.star.

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 blank canvas. Draw on me.” And you said, “All right, Hello World – woohoo and I'm done,” because that’s all you did. You said here’s Hello World, put it up on the canvas for me at a location 100,75 and thanks for playing; that’s all I'm gonna do.

But now you’ve actually gone through the whole process of compiling your Java program and turning it into this intermediate code and this 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 getting this up. I kid you not, really. If you can make that happen, you’re just most of the way there.

So let’s do something a little more exciting than this. Let’s actually do some interaction with the user. Let me show you another program. Remember this one is a graphics program. I told you there was a bunch of different kinds of programs you can have when I showed you the picture. I said, “We have a graphics program.” Let’s look at something called the console program over here. Uh-huh.

Student: [Inaudible].

Instructor (Mehran Sahami) :No, not in Java. You’re always extending one kind of program and a graphics program will actually, as you saw you can put up text. A console program is just meant to be a textural program, so you’re not gonna do any drawing.

But here’s another program, right, again, a lot of the same things is before. You have some comment up at the top. You have some libraries that you’re gonna import. In this case, you’re not doing anything with graphics, so you don’t need to import acm.graphics.star, you just need o import acm.program.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.star. If you’re not doing graphics, you don’t. So again, public class. This one called add-to integers because guess what, it’s gonna ask the user [Inaudible] programs, put tow integers and add them together, but in fact, it’s gonna do something interactive with the user which is pretty exciting in itself. This is gonna extend the console program because it’s not gonna have any graphics.

Aw, yeah I know, it’s sad times, but sometimes text is very powerful. What’s it gonna do when it runs? It’s gonna write out to the screen and this time we’re gonna use something called print lin, we just like to drop some vowels here and there, which stands for print line, and if print a line, this program adds two numbers, then it’s gonna read an integer from the user and we’ll go through all this stuff in more detail. Don’t worry; this is jus a high-level overview so you get some basic idea of what Java looks like.

It asks the user for N1, right, because you have to be very scientific, so rather than give me the first number, you say enter N1, right, and suddenly we’re much more formal.

Enter N2, right, and so what this is actually doing it’s asking the user for a number and whatever the number the user gives us, we stick in this location called N1.

And whatever the number the user give us here we’re gonna stick in a location called N 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 can kind of go into all the details because we gotta start somewhere.

Then we’re gonna add N1 and N2 together and store them in a place called total and then we’re gonna 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 we’ll go ahead and run it, add two integers. We’re compiling; 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 it and telling it where to display some words like Hello World, we just said, “Write some line,” and so what it’s gonna do is just sequentially write lines on the screen because this is what we think of as a console.

All a console really is, the way you can think about it is it’s a window that contains texts and can potentially interact with the user, so there’s places where we might ask the user for input. So we said if this program adds two numbers, I hope you can see this in the back. It’s tiny, tiny font. Enter N1 and it’s sitting there with a blinking cursor, and any time you see the blinking cursor, that means, “Hey, I'm expecting some value.” So give me something of value. What’s some integer?

Student: [Inaudible].

Instructor (Mehran Sahami) :Twelve; someone said 12; someone said one, so I’ll take 12 as N1 and then it asks for N2. I’ll give it one and low and behold, it says the total is 13 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, computes 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 textural. Okay, so any questions about the basic idea of classes or objects or these different programs we’re writing? The programs that are running, when they run, they are being, they’re objects. They’re instances that are being run for you. What they’re doing as instances, they’re objects that are inheriting all the behavior from the class template and then when we run, an instance is actually created in this run. Uh- huh?

Student: [Inaudible].

Instructor (Mehran Sahami) :Because if you say acm.star, strangely enough, you won’t necessarily get everything underneath ACM as well. You’ll get everything at that level.

We’re gonna have these little pictures or little objects and we’re gonna say, “Show up here and show up over there.” So what you have is a collage which starts off as 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 texts and put them up in various places on the canvas and you just saw an example of that with Hello World, but I’ll show you some of the other things that you can create. Two different kinds of objects that exist in the graphics world, one you just saw. It’s called a GLabel for a graphic label. This is just text in some sense.

It’s text that you can put up in the world. There’s also a GRect, which is a rectangle, oddly enough, a GOval, 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 circle; we have oval and same thing with rect versus square because all squares are rectangle, and GLine, 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 are 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 in the general class.

So if we come back, if we close this guy, and we come back over here to fund the below program, what we’ve done is we’ve said, “I want a GLabel. GLabel is the class. When I say new GLabel, what that actually does is says, “Give me a new instance of that class. Give me an object that is a GLabel,” 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’ve put all the texts inside the double quotes, okay. So Hello World without the double quotes is actually the text associated with this label and we give it some location, 100,75.

So now this little object, okay, which is an instance of a label 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 100,75,” and it’s like, “Okay you have this label, what are you gonna do with it.”

And I'm like, “Put it on the canvas,” and the way you put it on the canvas is you say, add. So all graphics programs, when you say add, there is something that you will specify to add, and the things that you were 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, location 175 and when it gets added, it gets put up there. Now, there is other kinds of things we could do with this, so if we’re kind of return over here, this is just the add-to integers program, let’s do add-to integers real quickly – 17, 25, 42, writes up same thing you just saw except now in excruciating detail.

Let’s say we want to do something that makes a GLabel more funky than we originally had, okay? So again, we’re gonna have our Hello Program that’s gonna extend the graphics program. Then we’re gonna do things slightly differently. What we’re gonna do here is we’re going to create a new instance of a GLabel object.

So if we say new GLabel and again, we’re gonna name is Hello World and 100,75, so I have some label that’s Hello World and location 100,75, and I say, “Hey, you know what label, I want you to look different than you actually are, okay?

So now what do I have this object, I have this little label, you can think of it as a little object that sits somewhere in the world that’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 GLabel, label next time, but it’s something I’ve created called Label which is just a new instance of this Hello World label.

The first thing I want to say is, “Hey, you know, I want to change what the font looks like.” Anyone know what font is. All right, you’ve probably used different fonts in your word processors. It’s just the way characters actually appear on the screen. It’s a thing that comes from type setting.

I want the font to be san seraph 36, 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 with the words associated with that label Hello World.”

Notice there’s still nothing on the screen. Here’s my screen. Here’s my canvas. This is all in the computer’s memory somewhere. It’s saying, “Yeah, you’ve created some label. You haven’t displayed it yet. You’ve just created it,” and you say, “Hey I want to tell that label to change it’s font to sans seraph 36.”

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 Karel’s world, we had methods. The way we called Karel’s methods was 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 seraph 36, and so when I execute that, what happens is it becomes 36 point font in sans seraph which means seraphs are just little things of if you can see the font up there has you know, swishes at the end of letters, those are seraphs, so a font like this doesn’t have any of the squishes at the end of the letter. San seraph, sans means not, Okay?

Then we say, “You know what, I’d really like you to be red because black is just so blasé and red is really the new black, so set your color to be the red color.” Like, okay, and so I execute that and it turns itself red. There’s still nothing on the screen.