ProgrammingMethodology-Lecture26.pdf

(51 KB) Pobierz
Programming Methodology-Lecture26
Instructor (Mehran Sahami): All right. Welcome back to – what kind of day is it going
to be in 106a? Anyone want to – fun-filled and exciting. It always is. Thanks for playing
along. So a couple quick announcements before we start. One announcement is that there
is one handout today, so several people have asked in the past, these programs that I
make in these classes are kind of cool. I would like to be able to share them with my
friends and relatives and whoever else. We're going to talk a little bit about how you do
that today and what that means underneath the hood, but the handout actually explains it
all. It's this notion of a jar file. We'll talk more about a jar file as we go along.
The graphics context, for those of you who are doing it, is due today. Just wondering,
quick show of hands, how many people entered the graphics contest. Wow. Not as many
as I would've thought. There could be a couple people who are at home, even if you don't
win of getting 100 on the final in a random drawing. So that's a good sign.
One thing I do want to check, I just heard a little bit before class that some folks were
having some trouble submitting their graphics contest because there actually might have
been an issue with the server that takes submissions. So if you submitted to the graphics
contest, whether you're in here or you happen to be watching the video, email me, and let
me know what the name of your contest entry was. That way, I know for sure that we
actually got all the contest entries that we think we had, and if we didn't get one, I can
email you back. The thing I would ask you is, if you can't email me any time this
weekend because this weekend is actually, when we're going to make the first pass
looking over all the contest entries, and then we're going to have a small pool that we'll
take to the section leaders. They will vote and give the winner. I'll announce the winner
in class next week.
I might show a demo of the winning two as well. We might do the random drawing in
class as well to see who actually gets the third coveted random drawing spot, even if you
don't win. So please email me if you entered the graphics contest, just to make sure.
One other thing with email for SCPD students, I know it's still a little too early to think
about final exams, but if you're an SCPD student, it's not too early. If you're not an SCPD
student, it's not too early, either. But for SCPD students, if you're taking the final exam, if
you plan of taking it at your sites and you're not going to come on campus to take it,
email me by 5:00 p.m. December 5, letting me know that you're taking it at your site and
the name and email of your site coordinator, just like the midterm. That way, I can get the
information to your site coordinator for the final well before the final.
If you're planning on coming on campus to take the final, you can feel free to send me an
email to say you're coming on campus. If I don't hear from you, I will assume you're
coming on campus. So you only need to email me if you're taking it at your site, so please
do that if you're an SCPD student and you plan on taking it at your site.
Any questions about anything we've done so far before we dive into our next great topic?
All right.
One of the things that we've done the whole time in this class is we use these things
called the ACM libraries. The ACM libraries are a set of libraries that are actually created
by a task force of people. The ACM is the Association of Computing Machinery. We
talked about them at the very beginning of the class when we talked about these libraries.
They put together some nice libraries of stuff that are really useful for teaching, which is
why we use them.
Today, what I'm going to do is lift a little bit underneath the hood and talk about standard
Java, which is what you would get if you didn't use the ACM libraries and you just used
the standard Java libraries. Now, there's no reason why you can't continue to use the
ACM libraries after this class. They're just another set of libraries that were written by a
group of people that you're certainly welcome to use.
So there's no reason why you should stop using them, but there were a couple important
issues related to standard Java. Now it's time for you to know. So the first thing that's
related to thinking about standard Java is when you're running your programs, when you
go into Eclipse and you click on the little running guy to compile your programs. It give
you a list of what classes you actually might want to run.
If you only have one project, you may only get one choice, but one of the things you kind
of think about is in the name surfer program, I actually have four or five different classes.
How come it always knew which class to run? How come it always knew the name surfer
class was the class that I actually should run? Anyone want to venture a guess?
Student: [Inaudible].
Instructor (Mehran Sahami): It's the only class with [inaudible] which is very related to
an underlying issue. It's the only class that actually was extending programs. So one of
the extended programs, what actually was happening in these ACM libraries is you were
getting a method called main. Main is actually the place – you're old enough to see main.
Main is actually the method at which Java classes actually start running.
So one of the things you should think now, you never wrote a method called main. I
never saw a method called main, and you're telling me that's where Java programs
actually start running. Yeah, in fact it is. It's because programs provided this main method
for you. What this main method did in the program was essentially get the rest of you
program running by getting a few things set up and then kicking off your run method. So
you didn't actually need to worry about this.
But now you're sort of old enough to actually see what that main method is all about. So
if we think about what this main method does, the header for the main method is also
kind of weird. This is part of the reason why we never showed you the main method
before. The header for the main method is actually public static void main, but we're not
done yet. Main actually has some arguments. It has an array of strings called args and
arguments, and then something in here happens inside of me.
If we showed this to you on the first day, we would've had to go through and explain
what all these words meant before we explained what main even was, before we
explained how you write your first program. That would've been a pain. Now we can just
tell you. Public mains is a public method. You know that. You probably recall the other
public methods you've written. Static means that this is actually a method that belongs to
the class – it's not something that you would actually call on a particular object.
So you never have some object – like, here's my object X, and I call X .main. Main is just
something that gets called. It's a class method as opposed to being a method that gets
called on an instance. Void means it just returns nothing.
What is getting passed in here is an array of strings. Where is that array of strings coming
from? This actually harks back to when computers weren't all nice and graphical and
everything. When people wrote programs, the wrote program and were typing on what's
called a command line. They wrong the name of the program out. They actually typed it,
and then they typed a bunch of things that they wanted to be passed into the program
such as initial information to start that program. That was the initial thing, so if you had
some program like name surfer, you might actually start off by giving the name of the
program.
Then after name surfer, you might give it the name of the data file, like data dot text. You
might've given it some other things as well that were separated by spaces. This list of
stuff is essentially what gets passed in here as arguments. They're strings, and this is how
the program would actually know what came in on the command line when the program
was kicked off. Java's not that old of a language. It sort of came around and gaining
popularity in 1995. People weren't doing a lot of this in 1995. I already had my mouse
and my folders and all this other stuff, even if you were six years old. You probably did.
You're like, I never typed this stuff, so why do I care about it? The reason why Java's
derived from another language called C, and there's a variation called C++ that was
created when people were writing programs in the days of yore. The whole notion of
main and having some arguments to get passed to main kind of came along with the
baggage of actually having a program language that matches the same style programming
languages when they did do this.
So a lot of the times in real Java programs these days, there aren't really any arguments. If
there are arguments, there's some system parameters or something like that. We don't
usually worry about them. So when you go and look at some other Java program that isn't
using the ACM libraries and you see this main thing, and you're wondering what it's all
about, you can think of main analogously to run. It's just where the whole time you've
been thinking of run as where you're execution starts, main is really where execution
started.
If you think about execution actually started in main, so how did this thing actually kick
off my run method? Now you're sort of old enough to see that, too. So what it actually
did – let's say this was the main method is something like name surfer. So somewhere
inside of a program, inside of the ACM libraries for program, we had this main method
that figured out what the name of your class was. Essentially, it had a one-liner in it that
would've been equivalent to this.
New name surfer dot start cards. So it's a one liner. Now you know what this means.
What was it actually doing? When main started, no objects exist in the world. It's a static
method. So there's no object that you're giving the main message to. Main just wakes up
and says, hey, I'm main. What am I going to do? Why don't I create some object of this
particular type, name surfer, which happens to scan an object, which is your program.
Remember your program, as we kind of talked about, implements the run method, and
actually a program underneath the hood implements the runable interface that we talked
about last time, with threads. We talked about the runable interface.
All programs implement the runable interface. How do you kick off something that's
runable? You say start. So what it basically did was created and object of your class,
which was name surfer, and told it to start. It happened to start pass along these
arguments, but you never needed to see those arguments. As a matter of fact, you never
did see them because when your object was in stand shaded, it didn't expect any
arguments. So the arguments actually got passed to this thing called start, which just
ignored them, basically, and then started your run methods to kick everything off.
Last time when we talked about start, we talked about this in the context of threads. So
we said, oh, you create a new thread, and the object that's you're going to start running,
we put inside a thread. We kick the thread off with start. In this case, we're not actually
creating a new thread. We're just saying we want to start executing this object I just
created. It implements runable, so you'll start running from the method run, but I'm not
creating it in thread. So this thing is going to execute in the same thread of execution as
the entire class.
So I don't suddenly kick off something that's running in parallel with this guy. It's
actually going to sequentially start name surfer, and that's the last thing this guy does.
Run your whole program. Any questions about that? Kind of a funky concept, but that's
basically what's going on. You should see it. We were creating an instance of your
program and then just kicking it off. That's why, this whole time, we had this thing called
the run method that had to be public because it was implementing the runable interface.
But now you've seen main.
We could've just had main in your program to begin with and included all this code. The
only reason we didn't put it in there before is because we didn't want to explain any of
this stuff. In week two of the class, right after [inaudible] all holding hands and singing
Kum Ba Ya. We're like, oh, it's [inaudible] the robot. Let's give Java, public static void
main. Straightening args. You're like, what is going on?
We hadn't done arrays. We hadn't done classes worrying about static. We certainly hadn't
done methods. We hadn't even done parameter passing. So we just waited until the end.
Now you see it.
Now that we have that idea, now we can think about, okay, if this is kind of what the
standard Java world is, let me think about taking this idea and using it to help me take my
existing programs that I've written and pack them up into a form so I can share them with
family and friends. So that's what we're going to do next. The basic concept of doing this
is something that's called a jar file. You've actually seen jar files before because you've
been working with something this whole time in your projects called ACM dot jar. This
was just a jar file that contained all the ACM libraries.
Basically, all a jar file is, where does it get its name. It's not a big mason jar, although you
can think of it like that. It stands for Java archive. That's where the name comes from.
The basic idea behind the jar file is this can contain a whole bunch of different stuff.
Most of the time, what it contains, is a bunch of classes in Java. You can think of this as
the complied version of the classes.
You can actually put source code inside the jar if you want, but most of the time when
you get a jar, it doesn't have the source code associated with it. I just has the actual dot
class files, which are the compiled version of the files. So you could put source files in
here if you wanted. You could put data files in here if you wanted. You could put a bunch
of things in here if you want, but we're really going to focus on the case of putting classes
in here.
So one of these already existed for you. It was ACM dot jar, and we're going to figure out
how to actually create some of these ourselves and use them because you can actually
think of them as something that's executable.
So if we come to the computer, here's name surfer. This is an actual working version of
name surfer, so I'm not going to show you the rest of the files in case you're taking
[inaudible]. All the code's in here, so the first thing I did in the name surfer program is I
thought about, hey, I want to think of this in the standard Java world now, even though
you're still using the ACM libraries. When I want to build this jar, I want to build it in a
way that sort of makes it maximally portable. I can give it to someone who's over here on
this PC and someone who's over here on this Mac. They don't need to have a Eclipse or
anything like that. They can just run it. That's the whole point.
So the first thing I'm going to do is to introduce my friend, the main method. So basically
I put in the code you just saw. I add a method, public static void main has this array of
strings called args that sets the parameters. That's just the way main is always defined to
be. What it's going to do is create a new name surfer object and kick it off. That's the only
thing I need to add to my program. So anywhere you had some class that extended
program, you would add these three lines of code to get it compliant with standard Java.
Zgłoś jeśli naruszono regulamin