Pages

Saturday 11 June 2011

Mehran Sahami Handout #45 CS 106A December 3, 2007


Mehran Sahami Handout #45
CS 106A December 3, 2007
Section Handout #9: Objects and Data structures
Parts of this handout by Eric Roberts and Patrick Young
1. Primitive vs. Objects
Let's say a student writes the following line of code in a predicate method (i.e., a method that returns a boolean):
public boolean IsNameQ() {
String name = readLine("Enter name: ");
return (name == "Q");
}
The author of this code thinks that the program will return true if the player’s name is "Q". What’s the problem here?
Now consider if the code were written as:
public boolean IsNameQ() {
String name = readLine("Enter name: ");
char ch = name.charAt(0);
return ((ch == 'Q') && (name.length() == 1));
}
How is the code above different with respect to checking for equality with the value "Q"?
Continued on next page
– 2 –
2. Data structure design
So far in CS106A, we've worked a good deal with arrays and ArrayLists. While arrays have fixed sized, ArrayLists grow as more elements are added (usually, to the end). We could think of potentially an even more powerful idea: an expandable array. The idea here is that we could think of an array that dynamically expanded to accomodate whatever index we try to access. For example, if we started with an empty array, and tried to add an element at index 14, the array would automatically grow large enough, so that elements 0 through 14 all existed (and we could store the given value at index 14). All the elements of the array that had not previously been given a value would have the value null. Then if we tried store a value at, say, index 21, the array would again grow automatically to have space for elements up to an including index 21. Note that the value at index 14 would still appear to be at index 14 in the expanded array.
Being able to expand an array dynamically is useful enough that it might be worth creating an abstraction to implement it. Such an abstraction is shown in Figure 1 (on the next page), which shows the structure of an ExpandableArray class that allows the client to call set on an index even if it doesn’t currently exist in the array. When such a situation occurs, the implementation of the ExpandableArray class has to allocate a new internal array that is large enough to hold the desired element and then copy all of the existing elements into the new array. The point of this problem is simply to make that operation general by creating a new class that provides the functionality, and we make the class sufficiently general by having it be able to store any type of object (hence the use of the type Object for the array elements). To create an expandable array, and set some of it's values (whih in this case are Strings), you might have code such as:
ExpandableArray myList = new ExpandableArray();
myList.set(14, "Bob");
myList.set(21, "Sally");
When you wanted to retrieve the value at a particular element of the expandable array, you could do something like the following:
String value = (String) myList.get(14); // Note the cast
if (value != null) {
println("Got value: " + value);
}
In writing your solution, you should keep the following points in mind:
• The underlying structure in your implementation must be an array of objects. Although using a HashMap might well be easier, it will be less efficient than the array in terms of the time necessary to look up a specific index.
• Notice that the definition of the abstraction explicitly states that the get method should return null if the specified index value does not exist. That means that you will need to check whether the index is out of bounds before trying to select that element.
• You may assume that all of the index values supplied by the client are nonnegative and do not need to check for such values in the implementations of get and set.
– 3 –
Figure 1. Proposed structure of the ExpandableArray class
/**
* This class provides methods for working with an array that expands
* to include any positive index value supplied by the caller.
*/
public class ExpandableArray {
/**
* Creates a new expandable array with no elements.
*/
public ExpandableArray() {
. . . You fill in the implementation . . .
}
/**
* Sets the element at the given index position to the specified.
* value. If the internal array is not large enough to contain that
* element, the implementation expands the array to make room.
*/
public void set(int index, Object value) {
. . . You fill in the implementation . . .
}
/**
* Returns the element at the specified index position, or null if
* no such element exists. Note that this method never throws an
* out-of-bounds exception; if the index is outside the bounds of
* the array, the return value is simply null.
*/
public Object get(int index) {
. . . You fill in the implementation . . .
}
}

Mehran Sahami Handout #45A CS 106A December 5, 2007


Mehran Sahami Handout #45A
CS 106A December 5, 2007
Solution to Section #9
Parts of this handout by Eric Roberts and Patrick Young
1. Primitive vs. Objects
In the first example, the student is thinking a little too literally about the expressions they’ve written them, seeing them as what they want them to mean as opposed to what they in fact do mean. The problem lies in the comparison:
(name == "Q")
The correct English translation of this statement is: compare the address of the object name to the address of the constant string "Q". In other words, name is a reference to a String object. Since name was read in from the user, this comparison will always return false, as it cannot be the same underlying object as the constant string "Q". If we actually want to compare the values held in those String objects, we should write:
name.equals("Q")
For comparing values, the == operator should only be used with primitive types, such as int, double, boolean, and char. Variables that represent objects (like String) are always references (addresses to some location in memory).
In the second example the code actually works as intended. In the expression:
(ch == 'Q')
we are using the == operator to compare the primitive type char. Se we are comparing primitives (and not object references), the == operator is comparing actual char values rather than memory addresses. This works just as we would want it to.
Continued on next page
– 2 –
2. Data structure design
/*
* File: ExpandableArray.java
* --------------------------
* This class provides methods for working with an array that expands
* to include any positive index value supplied by the caller.
*/
public class ExpandableArray {
/**
* Creates a new expandable array with no elements.
*/
public ExpandableArray() {
array = new Object[0]; // Allows us to check length of array
// even when no elements exist
}
/**
* Sets the element at the given index position to the specified.
* value. If the internal array is not large enough to contain that
* element, the implementation expands the array to make room.
*/
public void set(int index, Object value) {
if (index >= array.length) {
// Create a new array that is large enough
Object[] newArray = new Object[index + 1];
// Copy all the existing elements into new array
for (int i = 0; i < array.length; i++) {
newArray[i] = array[i];
}
// Keep track of the new array in place of the old array
array = newArray;
}
array[index] = value;
}
/**
* Returns the element at the specified index position, or null if
* no such element exists. Note that this method never throws an
* out-of-bounds exception; if the index is outside the bounds of
* the array, the return value is simply null.
*/
public Object get(int index) {
if (index >= array.length) return null;
return array[index];
}
/* Private instance variables */
private Object[] array;
}

Programming Methodology-Lecture27 Instructor (Mehran Sahami)


Programming Methodology-Lecture27
Instructor (Mehran Sahami): So welcome back. Wow. That’s a little loud. To our last
week of cs106a. Of course, it is another fun filled exciting day despite it being our last
week. We’re getting down to the end. We have class today, there’s class on Wednesday,
there’s no class on Friday. So next time will be our last day. But a few announcements.
There’s actually just a load of announcements because we’re so close to the end of the
quarter. First announcement, there’s one handout, which is your section handout for this
week. There are still sections this week, so despite the fact that we don’t have class on
Friday, still go to your sections this week.
There’s a couple problems on the section handout, as well as the sectional will just be a
general review for the final exam in case you have any questions. That’s a good place to
ask them. Also it would be a good place if you want to ask some questions say, about
[inaudible] for example, the last assignment. Just wondering, how many have started
assignment number seven? Wow, good to see. Anyone done with assignment number
seven? A couple of folks. That’s good to know. I might talk to you afterwards as to how
much time it actually took you, but hopefully, it wasn’t too painful. The graphics contest
was due last Friday. The winners will be announced in next class, so Ben and I actually
took a first pass already over all the contest entries. There was actually some very
impressive entries in the contest. Things you were just kind of jaw dropping, like, go and
show them to other faculty in the department because they’re just that cool. But this
afternoon we are having our staff meeting with all the section leaders and they will
actually be the ones voting and deciding on the winners in each category. So we’ll give
them the short list and they’ll make the final determination, and then on Wednesday, I’ll
announce it to and I’ll check with the winners of the contest to see if they’re okay
demo'ing it. But if they’re okay demo’ing it, then I’ll show you the winning contest
entries as well, plus on Wednesday we’ll have the random drawing to give away the last
sort of grand prize. But if you didn’t happen to win, just for entering you still get an entry
into the grand prize or you could just get a free 100 percent on any assignment in the
class including the final exam. So assignment number seven, we just talked a little bit
about. Since it’s due the last day of the quarter on Friday, but we don’t have class that
day, it’s just electronic submission. So if you’re wondering about what do I do with the
hard copy, you don’t need to do anything with the hard copy. We just need electronic
submissions.
For the other assignments in this class, we requested that you turn in a hard copy because
when you did interactive grading or if you’re a section leader to write comments on, we
actually had something that they could mark up. For assignment number seven, because
it’s due the last day of the quarter, there will be no interactive grading for the last
assignment so the down side is there’s no interactive grading for it, the plus side of that is
that you don’t need to turn in a hard copy because we can just take a look at your online
submission to figure out functionality and other kinds of things. And just as a reminder,
even though it wasn’t clear that was on the handout, like, right on the front the last couple
days, no late days on assignment number seven. Just in case you’re wondering. So final
exam, it’s time to start thinking about the final exam. Finals are next week. You probably
know that, but just in case you didn’t, the final exam is just like the mid-term. It’s open
book and open notes, so you can bring in your text book for the class. Feel free to bring
in all print outs of all your programs, all the notes you’ve taken in the class, all your
handouts. That stuffs all open, but just like the mid-term, it is a closed computer exam. So
if you have a laptop or a PDA or whatever, you can’t use that during the exam. Same
rules basically applies to the mid-term. And we’ll talk a little bit more about the final
when we actually do review for it next class.
The regular final is scheduled for Thursday of finals week. That’s December 13th, 12:15
to 3:15 P.M. in Kresge Auditorium, which is the same place we had the mid-term. One of
the only rooms large enough to actually accommodate us, and the alternate has been
scheduled. So alternate final is December 12th, that’s Wednesday of finals week, 3:30 -
6:30 in the afternoon, also in Kresge Aud. So this time seemed like a relatively unpopular
time for other final exams. And you’re free to take either one. So you don’t need to send
me an email saying you have a conflict with the regular exam or whatever. If you just
want to get done with your finals earlier and you want to take the alternate exam, you’re
just welcome to take the alternate exam. But only take one exam. So you can pick one,
and just one. That’s just life in the city. Okay. And if you’re an SEPD student, I
announced this last time, but I’ll announce it again. I’ve already gotten email from one of
you, which is a good thing. To email me by 5:00 P.M. December 5th, that’s Wednesday
if you’re planning on taking the exam at your site. If you’re gonna come in for the exam,
you don’t need to email me. You can, feel free to email and say, “Hey, man, I’m gonna
come into campus and take the exam.” And you’re welcome to take it at either one of
these times as well as if you’re out in SITN. But if you plan on taking it at your site, send
me an email. Also, let me know the name and email address of your site administrator so
I can send the site administrator your exam to administer to you. So that was just a load
of announcements.
Any questions about anything before we delve into our next great topic? All right. You’re
feeling okay. Good. So a lot of today’s class is actually about life after this class because
we’re getting pretty close to the end of this class. So one of the things I want you to just
kind of know about and so you can think about it, are what are some of the options that
are available to you afterwards. Whether or not you’re just thinking about declaring a
major or if you’ve already declared a major or you just want to get sort of a lay of the
land of what’s this whole computer science thing all about. Because probably the biggest
thing I would stress, despite the fact that you just spent the last nine weeks programming,
is computer science is not computer programming. Okay? A lot of time the two get
equated, but if it was called computer programming, this class wouldn’t be called
programming methodology, we’d just call it something like programs that work, right,
and we wouldn’t worry about style and all this other stuff, and good software engineering
principles, and at the same time, computer science wouldn’t be called computer science,
it would called something like programming. Right? And it’s, like, “Oh, what did you
major in?” “Oh, I majored in programming,” and that’s like, when you say, “Oh, I’m
sorry. I think you can get shots for that kind of thing now.” Because it’s not just about
programming. There is programming in computer science, but there’s actually a science
to the field and there’s a lot of things that go on outside of programming and that’s what
it’s important to, in some sense, appreciate. So if we think about life after this class, let’s
first kind of deal with some of the short-term logistical kinds of things. Like, you just
took this class; you might think, well, there’s probably a couple things you think. You
think, “Hey, Miron, that was kind of interesting, I might consider taking 106b.” You
might consider, “Hey, Miron, that was interesting, I might actually considering minoring
or majoring in computer science.” And you might say, “Hey, Miron, that was interesting,
in the same way, for example, that dropping a brick on my head is interesting, and I’m
gonna run screaming.”
And if you’re thinking the third option, I apologize, because that was not the point of this
class, but here’s a few things that you can potentially think about, even if you’re in the
third option and definitely some things to think about for the first two options. And I
guess there was also that option of the, “Oh, I got the general education requirements out
of the way and now I will go on figuring out what to do with the rest of my life,” and if
that’s the case, you should pay attention as well. So what happens after 106a? So here’s
cs106a, this is where we’re all sort of happy, and we’re scrappy and we’re making social
networks, and life after this kind of, you know, your next immediate step is actually
pretty clear. There’s a class cs106b, that’s called Programming Abstractions, which is the
next class to take. And that class is on a language called C++, so you’ll learn a whole
new language, although, you’ll realize when you actually CC++, that a whole bunch of
things in it are just the same as Java. Whole notions of parameter passing and methods,
and decomposition and objects, all those same things exist in here. Okay. But you also
will get with this class called Programming Abstractions because, so far, what we’ve
done is used a lot of extractions. There you get into a whole bunch of tradeoffs with how
you can make things run more quickly versus perhaps using more memory versus
different kinds of programming techniques that actually come up. There’s also some
really cool ideas that come up in here, which are just sort of mind blowing ideas, which is
the notion – for example, one of them is called recursion, which is so far we have
methods and methods call other methods and they call other methods. What if a method
called itself? That’s kind of weird, Miron. Why would a method call itself? Because some
functions are defined in terms of themselves. Right?
If you kind of think about the factorial function – anyone remember this function? The
“N” function. Right? This is N factorial. And all this really is – sorry if I just shattered
your ear drums, is N times N minus 1 times N minus 2 times….times 1. You just multiply
everything together. That’s where [inaudible] all about. You can define a function in
terms of itself. And it turns out, yeah, a factorial, that’s kind of a simple way to
understand it. It turns out that this is a hugely powerful concept that allows you to do all
kinds of things, and this is kind of another cool thing you get in cs106b. Okay. Now, you
might say, “Okay, Miron, that’s still sounding like programming to me, even though I’m
learning these cool concepts, isn’t that just a programming class,” and in some sense,
yeah, this is a programming class. There’s other options that are also available to you
now that kind of fall into the category of being part of the CS major or the CS minor. A
set of classes called cs103. And cs103 come in a different couple different flavors, like,
vanilla, grape and pork – no, they come in those in a, b sequence and there’s [inaudible] –
I can’t think of anything in the world that would come in those three flavors.
And this is really a class that in some sense is about discrete math. And you might say,
“Oh, gee, Miron, besides your class, I’m taking calculus and that’s about as much fun as
sliding down a 50 foot razor blade. Why would I want to do that again? Not on the sharp
side, right, just imagine the other side, like, the flat side of the razor blade and it’s been
made slick and it’s like a big slide. It’s fun. Wait until all my friends in the math
department see that. Anyway, why would I care about this discrete math thing? Well, first
of all, this is an operative word here, which means this little symbol that you have grown
to know and love, our friend the integral, just nod around, right, this is all discrete, this is,
like, “Hey, you know what, what we want to think about our some things that are useful
to us in a computer science context,” and computers at the end of day are digital objects.
Right. They have ones and zeros, which means there’s a whole bunch of things, like, sets
for example and logic that come up in these things. But there’s also interesting ideas that
come up in here, like, computability. In these classes, you get exposed to some things like
some of the biggest open problems in computer science.
Now, there isn’t time to go into what some of the biggest open problems in computer
science are, but there’s a problem called the P = NP problem. Right. And this is a big
question mark. Basically, we just don’t know if these two things, one of them named P
and the other one named NP are equal to each other or not. And you’ll find out what
those are in the class and you might say, “Okay, Miron, why do I care about that?”
Because it turns out this little problem here, has a $1 million prize associated with it. And
it’s simple enough to explain that after having had 106a when you take these next two
classes you’ll actually get exposed to this problem It’s one of things that’s, like, a minute
to understand, a lifetime to master. And no one’s mastered it yet. But in some sense, this
is also a problem that’s only about 35 years old. Maybe just slightly older than 35 years
old. So it’s not like this problem that’s existed for, like, hundreds of thousands of years
and, you know, cave people were writing does P = NP on stone tablets. This problem
actually came to the floor and people realized it was an important problem in the 70s,
which means it’s possible that it’ll be solved in your lifetime, and it’s possible that you
may be the one, presumably, solving it in your lifetime because it would be difficult to
solve it if it wasn’t in your lifetime.
So even if programming, by itself, doesn’t necessarily turn you on, but you think, you
know, programming is interesting, is there also some deeper science or some
mathematics because for a lot of people, they didn’t necessarily get exposed to computer
science earlier on, but they did get exposed to mathematics, this might be the kind of
thing that really turns you on. Now, you might say, “Okay, Miron, math doesn’t
necessarily turn me on, programming turns me on.” Besides that 106 class, what other
options are there? There’s two other classes, cs107 and 108. And these classes, basically,
look at building, in some sense, larger scale systems, so this involves object oriented
systems and in some sense, building larger applications. So you build some things here
which are outside the scope of a one or two-week project, like, you might spend four
weeks on a large project in this class by the end and actually build a fairly substantial
application, and 107 looks at a whole bunch of issues, that in some sense, we like to think
of as lower level kinds of issues, but it involves a lot of programming and it gets into the
nuts and bolts as to how does the software sit on top of the hardware of your machine and
how do these things interact and getting into understanding memory better and whole
bunch of other things.
And if you think about this set of class, like 106a, b, 103a, b and 107, 108, if you were to
take that set of classes and add to it 2cs electives, that’s the minor. Okay. So the minor is
basically these six core classes. You need to take math up to math51 I should say as a
little side note. Just in case you’re wondering. That’s just something, that, you know,
we’re not responsible for that, it’s just kind of required. And then two cs electives beyond
this kind of stuff and then you’re getting a minor. Okay. So if you want to kick it up a
notch beyond a minor and think about the major – actually, I’ll just leave this up. Two cs
electives, you sort of add that all together and it equals the cs minor, which is kind of fun.
Okay. Now, if you want to think about besides just a cs minor, potentially, actually
majoring in cs, you might want to think about, “Okay, first of all, what are some other
things that I can do in computer science beyond the introductory classes,” and there’s a
whole bunch of things. There’s something that we call artificial intelligence, or just AI,
for short. And there’s a whole bunch of aspects of artificial intelligence. That’s sort of the
highest level.
It’s the notion of trying to make your computer work more intelligently, and in some
sense, appear to be more intelligent, sort of on the order of the intelligence of a person.
But really this has a whole bunch of sub fields to it, for example, robotics and various
other things such as computational biology, there’s a lot of computational biology that’s
ground in artificial intelligence. Data analysis and I’ll show you some examples of these
as we go along. And this is today, and there’s a whole bunch of people in the world who
are wondering what happens tomorrow. And if you can do slightly better than 50 percent
predicting what happens tomorrow based on analyzing all the data from today and before,
you make tons of money. Okay. And if you wondered is this really the case? Yeah, in
fact, anyone heard of a company called D.E. Shaw? Yeah. Anyone. A few folks. Yeah.
It’s David Shaw. He was actually a grad student at Stanford in computer science. And
this whole – I wouldn’t say he started this whole thing, this actually existed long before
that, but there’s whole companies whose entire business is based on the notation of
quantitative analysis and guess who are a bunch of people that they employ? Computer
scientists who go and do the data analysis and actually figure this out. Okay.
So the application and understanding what are all the variables that you care about and
the information that exists in the stock market that you can extract and model with
different kinds of algorithms to make your prediction, is all part of what computer
science is all about. Besides, AI, there’s various other kinds of little areas. I’ll show you
some more pictures, like, robotics. Anyone heard of Darpa Grand Challenge or a little
robot called Junior or Stanley? Yeah. Oh, Junior, he’s so cute. Because it’s a robot, that
in some sense, is a car. Right. And there’s no reason why a car can’t be a robot. Just think
if Carol had wheels on it, and instead of move…you had move at 60 miles an hour, you’d
be doing the same thing, except you’d be doing it in a simulation. This is Stanford’s car,
Junior, and this is a car that’s basically a robot. It doesn’t have a human driver, at least
most of the time. Right? It has things like various kinds of sensors on it, various sorts of
radar and other kinds of laser range finding that sense what’s going on in the world and
then it makes decisions. Okay. And so let me show you a little example of that. So here’s
a little video of Junior actually involved – the joy of software.
That’s another thing you can do as a computer scientist. You can fix other people’s bugs.
Here’s what’s actually going inside Junior when it’s actually running along. It’s sensing a
bunch of things about its environment, and you can actually see it’s driving along – this is
where it has some uncertainty or some distribution over where it thinks it is, where it
thinks different lane markers in the road are and it’s doing all this by actually taking
pictures of the road, analyzing them in real time and then making various kinds of
decisions about where to steer and where to go. And this is all happening in real time.
Right. This isn’t, like, “Oh, we had to load all this data and figure it out on some super
computer,” there’s just a little bank of computers inside of Junior that is actually figuring
this out as he goes along. It figures out certain places to stop or how it’s going to remaneuver
itself. Let me show you the set of computers that are actually doing this.
They’re just sitting in the back of the computer. Yeah, there’s a few different machines in
here, but it’s sort of computational power on par with what you’re gonna have in your
dorm room by the time you graduate basically. Let me show you one more quick example
of Junior actually parking. Okay.
So these little red marks over here are actually cars and it’s basically sensing that these
areas are blocked and what it wants to do is get to a parking spot that’s between two cars
right here. So it plans this little path and it looks like it’s gonna rear end this other car
over here, but really all it’s doing is repositioning itself so it can re-plan to be able to
back up and then pull into the parking spot. Right. And if you think about all of the
dynamics that need to be going on to do this, all the low level stuff to sense where things
are, the high level planning to figure out how sharp of a turn it can make and now it’s
gonna back out and drive off. All of this stuff is basically just software. It’s a computer
science problem. And that’s how the junior team actually views this robotic car. They
view it as there’s a bunch of sensors in the car and there’s some actuators, like, they can
hook up computers to the steering wheel to turn it and really the whole problem is solved
in software. How to do the planning, when to turn the steering wheel, by how much,
when to figure out if lanes are blocked, stuff like that. Okay. So that’s a little bit of AI.
Let me show you a few other fields. Okay. So besides AI, and there’s a class related to
this, cs121 or 221, you sort of have your choice.
This is kind of a survey of artificial intelligence and this is kind of, in some sense,
modern techniques for artificial intelligence. If you really want to go and build robots, I
sort of suggest you take 221. If you want to get a lay of the land, of what’s in artificial
intelligence, you can take cs121. Okay. Some other things that you take along the way are
a class like cs140, Operating Systems. Right. And if you’ve ever wondered about things,
like, “Hey, I have my Mac, how does my Mac actually do all this stuff for me, how does
it take care of a file system for me, how does it take care of the fact that there’s multiple
things running at the same time, how does it deal with the fact that I may actually be
running more applications than I actually have real ram in my computer?” There’s a
notion of virtual memory, for example, where it uses your disk for part of memory.
That’s all stuff that’s covered in Operating Systems class, and if you’re interested in
systems kinds of things, there’s just a ton of things that you’ll in here that you can kind of
build on. Right? Graphics is a big area that’s in cs, and it turns out, interestingly enough,
of our graphics faculty, Pat Hanrahan is one of the faculty here. He actually has, not one,
but two academy awards. All right. Interestingly enough, he’s actually got Oscars. Right.
And you might wonder, “Why does he have Oscars, Miron?”
Well, because guess what, there’s all these animated movies these days, there’s a system
called Render Man that was actually responsible for being able to do a lot of the
rendering for original computer graphic movies. He was on the team that built that
system. And he’s done a bunch of other stuff since then, which is why they gave him a
second one in 2004. Okay. There’s a guy named Ron Fedkiw, and I’ll show you a little
animation that his group developed. So here’s what looks like a lighthouse and water, and
here is basically, a realistic computer animated waveform crashing over the lighthouse.
Right. This was all done. This wasn’t like scanned over some real lighthouse when there
was flooding. This is all basically done as a computer simulation. All right. That’s the
kind of stuff his group does. And as a matter of fact, for doing stuff like this, it doesn’t
just show up in little animations to show in 106a, if you happen to see Star Wars 3, he
was in the credits for it, if you happen to see – what were some of the other movies he
was in – anyone see Terminator 3? Horrible movie. Don’t see it. But he was in the screen
credits for that as well. Evan Almighty, yeah, so there’s serious movies that involve
major computer graphics where the stuff that’s being done here is actually at the cutting
edge of that to be able to figure out new ways of actually doing things with computer
graphics and actually doing the animation. But there’s other things you can do. Like,
here’s a mis-focused camera, you just bring the picture into focus automatically. Here’s a
really blurry one. Awe – pretty hardcore. And here’s focusing through a splash of water.
So it doesn’t just have to be a picture of some solid object. I hope you can actually see
that re-focusing while it’s happening.
Then we get into the audio part. I won’t share the audio part. It’s kind of more of the
same. But that’s the basic idea. They’re actually starting a company around this idea of
light field photography where you have a camera and just the way the lenses is
constructed and the amount of light that you sample at various kinds depths of fields
allows you to take this image and then be able to refocus on different parts of it later or
clean things up or whatever. That’s just another thing that’s kind of based on graphics
that you wouldn’t necessarily think of right, but photography really is taking some
sample of the world, turning it into a graphical image and then doing manipulations on
that image.
So a lot of the things that happen in graphics, apply directly to photography as well.
Okay. So besides graphics and robotics, we talked a little bit about those. There’s folks
that worry about stuff like databases, like, handling large volumes of data on streaming
data, on different kinds of things you could do with data and I was kind of thinking about
this and I was, like, what’s a demo I could show having to do with large volumes of data
because that’s not something you can actually draw a picture of real easily. And then I
just thought I’d show you this. Because Google came out of Stanford. It came out of a
group of folks who did things like understanding data structures and the algorithms
associated with them and who understand how to keep track of large volumes of data and
be able to do manipulations on that kind of data. And in the early, early days, most people
don’t know this now, but if you went to google.stanford.edu was the web address for
Google. Okay. And it turned out at some point this was actually eating up so much of the
entire bandwidth on campus that some folks said, “You really need to go and move this
somewhere else,” and then they actually created the company Google, which is based on
a misspelling.
Right. The actual – does anyone know what a Google is, which is the correct spelling of
Google, is ten to the hundredth power, it’s 10 with a hundred zero’s after it. And so Larry
Page and Sergey Brin were grad students here and they wanted to think of same name
that captured the largeness of Google or of the web search that they were doing so they
went off and registered Google because that’s how they thought it was spelled, or at least
one of them, and I won’t tell you which one thought that. When they were grad students,
and then when the other one of them came back to the room and looked at it he said,
“You misspelled it,” but two things transpired. One was that this .com was already taken,
and the second one was when you’re a grad student and at the time it was, like, $50 or
$70 to actually register the name, that’s kind of spendy when you’re living on Ramen. So
that’s what it was. Okay. But it just shows you the kinds of things that get done by taking
basic ideas in computer science and building them to a larger scale. Other things that go
on. I’ll just give you a brief sampling.
Cryptography, which is big for web security. Right? It turns out a lot of the web is
actually pretty insecure. Much more less secure than you would actually imagine.
Anyone ever had a credit card number stolen? A few folks. Yeah. When you get your
credit number stolen, then you think twice about a lot of the transactions you make. I had
it happen, actually, a couple times and I still, like, you know, Christmas time rolls
around, I’m just like online shopping until the cows come home. But it’s important to
actually think about what’s secure and what’s not secure. And there’s actually a group
that deals with cryptography, especially security in the context of the web.
Other kinds of things that go on. We talked about AI, and sort of a sub field of AI, which
is growing into a whole area of its own, is machine learning, and I talked a little bit about
things like biology or predictive data analysis. There’s actually also machine learning that
affects your life on a daily basis, whether or not you know it. How many people have a
spam filter on their email? Anyone? Yeah, did you know that chances are probably in all
likelihood that your spam filter is actually based on machine learning? It’s seen a whole
bunch of email, some mail that was spam, some mail that wasn’t spam. And it learned, no
one told it what was spam and what wasn’t spam. It learned to figure out how to
distinguish between what’s spam and what’s not spam. Now, it’s not perfect. Right?
People aren’t perfect either, so sometimes you get messages in your inbox that are spam,
and every once in a while, rarely, but it happens, someone sends you a message and you
never hear about it, and they’re like hey, I sent you this email and the you go check your
spam folder and it shows up in there. But spam filtering is another one of these things that
in the last oh, ten years or so, is another something we take for granted and don’t think
about the fact there’s actually a bunch of science under the hood as to how to do this and
people continue to do research how to improve it.
And there’s a bunch of other things based on this, like, robot and navigation. Some of the
stuff you just saw with Junior, is actually based on learning landmarks of the road or
learning where lanes are on the road or what obstacles actually are. There’s a ton of other
things. I’m just giving you a sampling. Now, if any of this has interested you at all,
there’s a guy you need to go see. One guy you could go see is me, and I’d be happy to
talk to you about any of this, but there’s another guy you can go see whose name is Dave
Koslow. And Dave, is what we refer to as the CS course advisor. I’ll just put the CS
advisor up here. He’s in G160. He’s the guy you see when you want to declare a
computer science minor or major. Not that I’d be putting in a plug, but he’s an interesting
guy to talk to about some of the different possibilities in the field, but open invitation. So
this class is gonna end, like, after Wednesday or after the end of the week or after you
take the final depending on how you look at it. Don’t be a stranger. Right. Come on by. If
you want to talk computer science, if you want to talk about what’s possible to do in the
field, come by. My office hours will be on the web or send me an email to set up a time
to talk, and I’d be more than happy to take you through a bunch of this stuff. So besides,
Dave, there’s me.
Now, last but not least, and I shouldn’t say, last but not least, you might say, Miron,
computer science is kind of interesting, but are there other related majors that I should
consider. So in the sense of full disclosure on fair play, there’s computer science, there’s
some other possibilities. There’s electrical engineering if you’re more interested in the
hardware side of things. There’s math and computational science. And math and
computational science is more if you’re interested in the mathematical side of computing.
You’ll still get a lot of math if you do computer science major, but if you sort of are
really kind of immersed on the mathematical side, math computational science is
something to consider. And there’s also a major called symbolic systems or just sym sis.
And sym sis is also a fun major. It’s actually a combination of linguistics, computer
science, philosophy and psychology. I always forget the last one. Except, it’s always
different every time which one I forget. And the basic idea here is to think of both
humans and machines are symbol processors, right? People are symbol processors, in
some sense, because they take in symbols of the world, namely, language or visual
[inaudible] that they actually see and they make some sense of it, and then they act in the
world.
But now you might say, okay, that’s interesting. You’ve told me about all these fields,
but you told me that computer science was more than just programming and so far, it’s
unclear what I might be doing other than programming all these cool application you’re
showing me. So let me tell you about a few of them. This is the one I refer to as kind of
the peanut butter cup version of computer science, which is you can take computer
science and a whole bunch of other things and mix them together and they’re just two
great tastes that taste great together, and I’ll show a lot of examples of that. So there’s CS
and business. Okay. If you’re interested in sort of the business side of thing, product
management is a whole field or a whole area that people go into, especially in high tech
product management which are people who don’t necessarily program, but they have
technical backgrounds to be able to define what products are going to do and how people
are gonna interact with them. So if you look at a lot of high tech companies, people who
are product managers, who are taking more of a managerial role and defining a role for
product, many of them, in some sense, I’d actually say most of them, probably have a
technical background. In a lot of cases, it’s computer science even though they do know
programming. They do product definition.
Beyond that, and this is kind of a popular one around here. Entrepreneurship. Yeah.
That’s good enough. I always get nervous writing that. And that’s the whole notion of
you think about people who are doing startup companies. There’s been a ton of startup
companies. I can’t name them all because over the last few years, there’s been over 2,500
companies that have come out of Stanford. Some of them are big and you know about,
like, Google and Yahoo and Cisco and Sun and HP and all these other ones, and there’s a
whole bunch of smaller ones out there that also did pretty well. Anyone ever remember
Evite? Anyone ever send an Evite? Yeah, that was started by a guy I lived next door to
many years ago. And they did pretty well. It got acquired eventually, but life was all
good. And the whole notion here of thinking about startup companies – now, one thing
that’s interesting is a lot of people think, “Oh, well, if I want to do entrepreneurship, I
should go do business, right?” Well, what I’d actually challenge you to do, if you think
that, is go find out about the backgrounds of people who are things like successful
venture capitalists and see what they did when they started.
And one of the things that you’ll actually find, which is surprising, is most of these
people didn’t start as business people, they started as technical people who actually went
and did interesting technical work and at a certain point, realized there was a need and
then moved on into the business realm. Tons of examples of that. I’ll just give you a
quick one. Eric Schmidt, who happens to be the CEO of Google, PhD in computer
science. Right, now an MBA. And that’s not to say an MBA is a bad route. It’s just to say
that, realistically, if you look at what a lot of people have done, the route to actually
getting there, in many cases, actually, flows through a technical area. Okay. There’s also
finance, in the sense of computational finance. All right. Again, not only in predicting the
stock market, but there’s a whole bunch of people that what they do is they worry about
different kinds of modeling algorithms or managing different kinds of funds, basically, by
thinking of financial markets as a computational problem that they model with different
kinds of data structures and different sorts of algorithms to potentially make predications
on or just to get insight into. If you’re interested in this kind of stuff, there’s actually a
program called the Mayfield Fellow’s Program. If you do a search for Mayfield Fellow’s
Stanford, in your favorite search engine, you can find out more about it, which is actually
a program that you learn about entrepreneurship. You go into an internship with a startup
company to learn more about it, but you actually get immersed in thinking about the
different issues of starting a company. We’ll just leave the CS up here.
And biology. This has become a hugely popular area these days. Okay. So there’s a
whole bunch of things like bioinformatics, and bioinformatics – there’s kind of different
flavors of CS and biology, is thinking about the information systems that keep track of
biological data, or they keep track of medical data. Right. So if you think about if there’s
a whole bunch of medical data that’s being kept on, like, your medical records and results
for tests and a whole bunch of things that I want to be able to slice and dice in different
ways, or understand how, for example, symptoms that you have might be related to some
other symptoms or some other diagnosis that happened in the past. These are the kinds of
information systems that deal with that, and we have a whole program here called the
BMI program, Biomedical Informatics that just deals with that. But beyond that, there’s
also fun things, like, genomics and proteomics, and doing things like being able to look at
gene expression data and DNA and be able to determine what kind of diseases do you
hereditarily have more of a disposition to because of your genetic makeup. And if you’re
interested in this kind of stuff, there’s actually a program also on bioengineering. They
don’t, right now, have an undergraduate program. They have a graduate program.
They’re gonna form an undergraduate program. That’s something you could be interested
in, or it’s something we actually have sort of a sub areas of the computer science major
that you can also do this sort of stuff in.
Now, one thing that’s kind of interesting, which also sometimes surprises people is they
say, “Oh, I want to be a patent lawyer. I want to go and deal with all these issues like
making sure that file copying of music is legal for everyone, so I’m gonna go and be a
political science major cause that’s what I should do to go to law school.” Right. Turns
out that if you actually want to be an intellectual property lawyer, you need to have a
technical background. There’s a list of approved areas that you could’ve done for your
undergraduate degree that allow you to become an intellectual property or copyright
lawyer. Computer science got added to that list about 15 years ago. Political science, not
on that list. Okay. So it’s something you should probably know now. If this is the area
that you’re thinking about going to, you need to understand the technology to understand
how intellectual property and copyright issues apply. You need to understand what an
algorithm is. What parts of an algorithm are obvious versus what parts of an algorithm
are not obvious? That’s what allows people to do this work. Okay.
And then last, but not least, CS plus CS. So you can just do – you don’t have to mix
computer science with something else. You can just do computer science, and obviously,
programming is part of this. There’s a lot of people who are very happy being software
engineers and there’s lots of jobs in software engineering and life is good. But there’s
also people who go into engineering management. Most managers, in computer science,
are not professional managers. They are people who at one time were programmers or
engineers, and worked their way up through the ranks and eventually became managers
and became senior managers and became VPs and the whole deal. Right. So it started by
having a technical background. It didn’t start by saying, “Hey, I want to be a manager,”
and having someone hire you to be a manager. Okay. And there’s also, and this is near
and dear to my heart, so I’m just gonna sort of wrap up quickly, teaching. Right. So you
could think about computer science as a field that you go into because you want to teach
it to other people, in addition, to perhaps doing some stuff in it yourself because you find
it interesting, but if teaching at all is something that’s interesting to you or, like, when
you were in your section, you were, like, “Hey, section leading is kind of cool, this is
something I might consider,” there’s the cs198 program. And this is a program that I’ve
talked about in the past, but I just want to spend a little bit more time talking about.
And what you need to go into cs198 is you need cs106a and b, or you could’ve taken X,
but at this point, it’s kind of too late to take X. So what you really need is cs106b, after
one more class, you’re eligible to become a section leader. And being a section leader,
you might say, “Oh, well, what does that involve?” And it turns out to involve a whole
bunch of things. One is that you actually teach a section, which is kind of cool in itself
because you get to learn the material a whole lot better when you teach it to someone
else. There’s always some new little nuance about something that you learn somewhere.
So you learn the material that much better by teaching the section. You also get to know
other section leaders. So there’s kind of a social aspect to it, and especially if you want to
go into computer science, this is a great way to meet project partners and other people
who you know are really interested in computer science and motivated. You also get to
meet faculty. So when it comes time for getting letters of recommendations, which is
something that people don’t necessarily think about early on in their program, but then
later on how many people are thinking about letters of recommendations now, like, it’s
getting to that grad school application time, and how many people wish you had thought
about it earlier. Yeah. Mostly the same hands.
This is a good way to do that. Is to actually get to know people who are involved in
teaching and we have regular staff meetings, and it’s a good way to sort of think about
that. And at the same time, and one last side point I would put in, is that there’s a huge
network of people who went through this program who are out there. So the cs198
program is actually a program that’s not just known at Stanford, but it’s actually known
nationally. Like, if I go to other companies or something like that, there’s people that
come to visit, for example, from Microsoft and they’re, like, “Yeah, tell me where the
198 meeting is, and what’s going on there,” and they come and recruit from that group of
people, and this happens for a whole bunch of companies across the board. So, with that
kind of said, hopefully this has given you a little bit of a taste for what computer science
can be about. Not just thinking about programming per se, which is what we’ve done a
lot of in this class, but programming is really just the first step that opens up a whole
bunch of other venues. And hopefully you got a sense of some of the other classes you
can take that will broaden your horizons even more and some of the different areas you
can go into potentially with a computer science or related major that can open up all these
possibilities. Any questions about any of that? You’re all set? All right. Then I will see
you on Wednesday.
[End of Audio]
Duration: 47 minutes

Mehran Sahami Handout #44 CS 106A November 30, 2007


Mehran Sahami Handout #44
CS 106A November 30, 2007
Packaging Your Program into a Distributable JAR File
Based on a handout by Eric Roberts and Brandon Burr
Now that you’ve written all these wonderful programs, wouldn’t it be great if you could package them up and send them to your mom, dad, friends, and pets so that she could see what you’ve done? Because we here in CS106A feel that no parent should be spared the joy of sitting through a simplified version of a game he or she has undoubtedly played a million times before, here’s a short guide to making an executable JAR file in Eclipse!
If your program uses no external JAR files
Eclipse makes it easy to package your code up into a JAR file that you can double-click to run, especially for those programs that aren’t using other JAR files. In other words, if your program is using the ACM libraries (or some other non-standard Java libraries), you’ll have to do the slightly more complicated method that uses a manifest file. Both examples are described below.
Using the ACM libraries, Step 1
Our programs that have used the ACM libraries have started running via the public void run() method. We changed Eclipse to allow this, to make things easier for you. But in reality, a Java program needs to start at a particular method in a class, the public static void main(String[] args) method. If your program uses the ACM libraries, you’ll need to edit your code to have a main(). You can do this easily by simply adding the following code to the class that has the public void run(), substituting the name of that class for „Yahtzee‟ below.
public static void main(String[] args) {
new Yahtzee().start(args);
}
If you remember at the beginning of the quarter, we said that you needed the special Stanford version of Eclipse to run your programs, this is because of the edit (mentioned above) that we made to Eclipse. But if you add this main() method your program should run fine in any Java compiler.
Using the ACM libraries (or using any external JAR files), Step 2
Now that we have a normal running Java program, let’s package it up into a JAR file. A JAR file is simple a Java ARchive – a file that contains a set of Java class files as well as potentially other files that will be used by the program. One important issue to point out here is if we need to specify other libraries that our program will need to reference. The easiest way do this in Eclipse is by using a manifest file when creating your JAR file. The manifest file allows you to specify things like which is the main class to run when the JAR file is double-clicked, or what the external libraries are, or even security information about the JAR file. If you aren’t using other JAR files, you don’t need to use the manifest file. Eclipse provides a straightforward way of exporting your program. In either case, you can create a JAR file as follows.
– 2 –
Go through the process of Exporting your project to a JAR file as shown below. In Eclipse, highlight (click on the name of) the project you wish to create a JAR file from:
Go to the File menu and select the Export... command. This should give you the following window:
Click on Java to expand the list of options, and then click on the option JAR file. Hit the „Next >‟ button in the window.
– 3 –
You will see the JAR Export window:
Click on the name of your project (Assignment5, in this case) to make sure the (default package) is selected, and select the destination of the JAR file using the „Browse...” button. Then hit „Next‟.
– 4 –
You will then see the following window:
This screen isn’t too important for our purposes. Just hit the „Next >‟ button again.
– 5 –
You will then see the following window:
Okay, now here’s where the important stuff happens. First, near the bottom of the window, select the Main class using the „Browse...‟ button. The main class (Yahtzee, in this case) should show up in the list if you correctly added the main() method described above.
If your program doesn’t reference other JAR files (i.e., it does not use the ACM libraries or any other libraries), that’s it. You’re done! You don’t need to worry about the manifest file stuff. Just hit the „Finish‟ button, and go double-click the JAR file you just created. Make sure you see the last section of this handout on data files and distribution, though.
– 6 –
If you do need to reference other JAR files (i.e., like the ACM libraries), then you need to create a manifest file. To do this, we will go through this exporting process twice. The first time is to generate a manifest file, and the second time is to use that file when exporting our program. So, from here, make sure „Generate the manifest file‟ radio button near the top of the window is selected, and that check box for „Save the manifest in the workspace' is checked. Use the „Browse…‟ button associated with the „Manifest file‟ text box to select the destination of the manifest file. Click the Assignment5 folder (or whatever your project is named), and then in the box type in the name “manifest”. This screen should look something like the image above when you’re done (i.e., the Manifest file will likely be '/Assignment5/manifest'). Now hit the 'Finish' button.
You should see the manifest file show up in the package explorer:
If you double click on the manifest file, you should see its contents. You need to edit the manifest file to add the line "Class-Path: " followed by the names of all the JAR files that this program uses, separated by spaces. In this case, that would include acm.jar and yahtzeelib.jar. When you’re done the manifest file will look something like this:
– 7 –
Make sure to save the updated manifest file. Now that we have this manifest file, repeat the entire above process of exporting a JAR file (i.e., click on your project name, pick Export... from the file menu, select the JAR file option for exporting, etc.). However, this time you will do something different when you get to the last window (shown below):
When you get here, make sure to click the radio button for “Use existing manifest from workspace”.
– 8 –
You should then have a screen that looks like this:
Now, hit “Finish” button. Eclipse will use the manifest file we just created previously to make our yahtzee.jar. If it asks you to overwrite the old yahtzee.jar, just say “Yes”.
We’re almost there!
– 9 –
Distributing your program
Now you have your yahtzee.jar file, containing your Yahtzee code, but you can’t simply send the yahtzee.jar to your friends. This jar doesn’t contain the code in the other two JAR files (acm.jar and yahtzeelib.jar), nor does it contain any data files your program might use (text files with data, or even sounds or images). What you’ll want to do is create a new folder, place your yahtzee.jar file in it, along with any other JAR files your program uses (like acm.jar and any other the ones you added to the manifest file) and data files you use. Once you have all of these files in a single folder, you should be able to just double-click your yahtzee.jar file, and have it run your application. You will need to distribute this entire folder to anyone that you want to share your program with. Usually the easiest way is to just zip the folder up into a single file, and then email that – just make sure that your friends know how to unzip the file!

Programming Methodology-Lecture26 Instructor (Mehran Sahami


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.
Once we do that, we need to create the jar file, which is the thing we're actually going to
be able to execute. One thing you might be saying to yourself is that the ACM libraries
already provide this. So why am I putting it explicitly in there? As a matter of fact, it's
not super required that it be in there. The real reason why we put it in there is to try and
maximize portability. Even though Java's supposed to have this property where we write
the Java code once and it can run on PCs and it can run on Macs and it can run on Vista
and Tiger and all this other stuff, in reality, there's some little differences between these
operating systems. So little problems creep up here than there, every once in a while. You
actually saw a few of those in class on occasion. Some of you experienced them in the
Lair.
By putting in this line explicitly rather than kind of relying on the code that's in the ACM
libraries, this is actually doing a bunch more complicated stuff. It needs to infer what the
name of your class was. You never told it explicitly, hey, ACM library, I need a new one
of these guys. Java has a facility called reflection where it can go and say, oh, let me take
a moment to reflect. I'm going to go and look at the names of your classes and then do
something to actually generate some code based on the names of your classes, which is
what it was doing.
But that kind of stuff can get a little bit frosty, so we're just going to say, hey, ACM, don't
worry about it. I'm just giving it to you. I'm a nice guy. So just put it in to make it
explicit. It maximizes portability is the main reason we put it in.
So how do we create the jar file? What we're going to do – these steps are all in
excruciating detail in your handout, so you don't need to worry about scribbling down
notes quickly. What we're going to do is you first click – that's the most difficult part of
the whole thing. You select the project that you want to create the jar file from. So name
surfer. Then we go to the file menu, and we pick export. So this whole time you were
doing imports when you were bringing stuff in, now it's finally time to give something
back in the form of an export.
So what you want to do when you click export is it brings up a little dialogue box.
Sometimes this is closed. Sometimes it's open. It's not a big deal if it's closed. Open it.
Inside Java, you click on jar file. That's what you want to create. It has this nice little jar
icon there to remind you it's a jar file. You click next. I'm just going to take you through
all the steps.
We need to specify a couple things here. First we need to specify what's in the jar file. So
I'm going to open up name surfer, and what I'm going to put inside the jar file – I don't
want to have all of name surfer in the jar file. In terms of if you throw everything in there
including this stuff called dot project and dot class pass, it actually gets a little bit
confused. Some of those things are not germane to what we want to pack up in our jar.
They're just kind of other administrative information.
What we really want to have is everything inside the default package. So I just click on
default package. I can double click on this to make sure that all of my Java files were
checked. That's what I want in my jar. I want basically all of my Java files or the
compiled version of all of my Java files. I make sure that this is clicked. It's clicked by
default. Just make export generated class files and resources. So what it's going to do is
compile those Java files into their corresponding class files. That's what it's going to put
in the jar.
There are other options, like I can export the Java source files if I wanted to. Then I give
someone the actual source code. Most of the time, you don't want to do this if you don't
want peoples sniffing through your source code. You're like, here, take the compiled –
the ACM libraries, we're not going to give you the source code. Just take the ACM
libraries. They're good for you.
So once we have this, we need to specify where we want to save this jar file. So that's
what select export destination. They should've just said, where do you want to save it.
That's the export destination. We can browse around. Basically, I've already created a
folder that has all my name surfer code in it over here. I'm going to save the jar file in that
same folder. So this is just in the same folder for my project called name surfer that has
all of my class files in it. You can put it wherever you want, just don't forget. That's kind
of the key.
I’m going to save it here. You don't need to worry about the options down here. They're
just fine in the defaults that they're at. You click next. Then you come to the screen that
seems like, what's going on. Export files with compile warnings. Yeah, we just want to
export everything. So we just click next.
This is the most interesting part of the whole thing. What we want to do when we actually
create this jar file, and this is something we only need to do when we have other jar files
like the ACM libraries, we need to generate a manifest file. Basically, all a manifest file
is, it's a complicated name. It sort of sounds formal. It sounds like they're on a boat, and
they're like, oh, where's the passenger manifest? Really, all a manifest is – I'll show you
the manifest file. It's two lines long, and then you add one line to it. It's basically just a
little bit of administrative information that's kept around with your jar file so it knows
what kind of stuff you're using with this jar file. That's all it is.
So what we want to do is generate the manifest file. Make sure you save the manifest in
the workspace is checked, which it should be. Then you need to specify where you want
to save the manifest file. The place I want to save it is basically in the same folder for my
name surfer project in the name manifest. You could browse around if you wanted to, but
usually the name you give it is the name of the folder that all your project stuff's in, and
then the name manifest, which is going to be the actual name of the file. Any questions
about that? Manifest file?
The other thing you need to do when you're specifying a jar is a jar's just a bunch of
classes. It needs to know in some sense if someone ends up running this jar – you'll see
that in just a second. You can run a jar – where should it start? Which class should I call
its main method. That's what you specify here. Select the class of the application entry
point. It's kind of a very formal way of saying, where should I start running? So the place
I want to start running, it lists to you all the classes that have a main method. Those are
all the ones that can start running. Name surfer is the only one.
So I click okay. So it says the main class is name surfer. Now there's no more next
buttons. Now all I can do is click finish, and I just created a jar file. You're like, oh,
[inaudible] double click. Double click. We're not there yet.
There's two things that you'll notice if you look over here in the package explorer. You'll
notice now we have something called a manifest file because that's where I saved it. I
also have name surfer dot jar, the jar file I just created. They were both put in the same
folder with my other files. That's were I wanted to save them so they happened to show
up here in the package explorer. Here's the funky thing. Even though we created this
manifest, and we created this jar, they don't have quite the right information that we want.
So what we do is we double click on the manifest file to open it up. Here's the whole
manifest file.
It's got a version, which is 1.0, and it's got the main class, which is name surfer. Why
does it know the main class is name surfer? Because I told it. That's the application entry
point. Just for letting me know that I told it, that's where the application starts.
There's one other thing I need. What I need to do is say, you know what, that's a good
time, but you also need to use the ACM libraries. It says, you didn't tell me about the
ACM libraries. You say, well, now I'm going to add the coveted third line that was talked
about in the manifest file. So I'm actually going to modify the manifest file and add
something called a class path.
All a class path is, it actually looks just like this, C-P. Class path just tells basically the
application what other stuff were you using. Are there other jar files that you're using? So
what you specify here is the name of any jar files that you're going to be using as part of
your program, separated by spaces.
So I'm going to use ACM dot jar. Here's ACM dot jar over here. I'm going to use the
ACM libraries, and those are all in some jar file. I'm also going to use the jar file I just
created, name surfer dot jar. If I had three or more jar files, I'd list them all on the same
line. So if you go and write some application some day where you're like, hey, I got this
jar file for my friend. Here's this jar file I downloaded from the web, which I wouldn't
encourage you to do, and there's this other job file from somewhere else. You just list
them all here with space in between.
Then we save the file. That's probably the most difficult thing that people forget. So you
save the file. You save the manifest file. What do you do after you do all this? Create a
jar all over again. Why? Just trust me.
Here's how it's going to work. I'm going to do it a little bit different this time. Rather than
going to the file menu and picking export, I'm going to take the advanced course. I'm
going to right click on the project name and click export. It brings up exactly the same
window. I'm going to export a jar file. Come over here. What do I want to export? Oh,
yeah. It's all this [inaudible] again. What I want to export is not all this stuff. I just want
to export everything in my default package. Where do I want to save it? I want to save it
in the same place I had before. You might say, but aren't you going to replace the one that
already exists? Yeah, I need to replace the one that already exists because I updated my
manifest.
I need to say it's not just about you anymore. Now it also involved ACM jar. You can
replace the old one. So I'm going to put it in the places before. Then I click next. Here's
the thing about export warnings, export affairs. I don't care. I'm just going to export. Here
is the only place where things are different. The second time I go through this whole
thing, I don't want to generate another manifest file. I generated a manifest file the first
time, and then I modified it. What I want to do is use that modified manifest file. So I say
use existing manifest from workspace, which is again, just a formal way of saying use the
manifest file that's already there.
It asks me, where is the manifest file? Strangely enough, if you look, this is exactly the
same text as here. No coincidence. You just saved it. That's the one you want to use. It's
going to be the same name. Now notice which class of the entry point is grayed out. It's
grayed out because I told it before, your entry point is name surfer. That's in the manifest
file. If that's already in a manifest file, I'm using it, it doesn't need to know it again. So it
doesn't even let me specify it again. Just say that's the difference. Then I click finish.
They give me one last one. Are you sure you want to override that last jar file? He was
your friend. I say yes. We didn't know each other that well. Now I've created this jar file.
What do I do now? Well, I kind of come over here, and I say, here's the jar file I just
made. Now one thing I can do with the jar file is I can double click it. If I double click on
the jar file, it just starts kicking off the name surfer application. So I don't need Eclipse
anymore to go and run and have a little running dude. I have a little application.
I'm like, oh, yeah. How popular was that? Bob's kind of fallen off in the meantime. There
are some other interesting ones. I was looking at these the other night. Verna. After the
'60s, done deal. Thanks for playing. So name your children Verna. Make a comeback.
So name surfer's just kind of running here. It's fun. It's just a little stand-alone application.
If you wanted to pack this up and send it to a friend of yours, you wouldn't just send
name surfer dot jar. What you would do is say, hey, you know what I want to do? I want
to create – I'll just do it in here – some new folder. So I'm going to create some new
folder, and I'll call this my program or whatever you want to call it. I'm just not going to
call it name surfer again because I already have a folder called name surfer. My program.
What I'm going to put in my program, that folder, is the jar of my program. I'm also going
to put in the jar of the ACM library because I also need that jar. That's separate.
I also need in here any data files like names dot data. That's all the data that gets read
when the program starts. It's not like magically that's just going to be there now. It's still
going to go and try to find that file. So I still need to put all that stuff here. Now this
folder is something I can zip up and send it to a friend of mind. When my friend gets it,
they would be like, oh, name surfer dot jar. Then they get going.
So now you can package up any program that you've written in this class. Remember to
put in main. You got to go through this two-part [inaudible] the jar, export it, process,
modify the manifest, create another jar, put it out there again. But you're good to go. Any
questions about that? Now you can package up and share with friends.
What's even cooler than sharing with friends that you can email stuff to is sharing with
friends on the web. You just have millions of friends on the web. Most of them you just
don't know about, but they're probably on in the late hours of the night, looking at your
web pages and things. You can actually take any files that you create here, like jar files,
and make them available in a web browser.
There's one other thing I should mention, and this is kind of a little thing. Before you go
and create all this stuff and send it off to your mom and dad and be like, mom, dad,
breakout. Go play. It's a good time. In order for them to run your jar files, they need to
have the Java run time environment installed. Remember on the second week of class
where we said go to the CS106 web site. You need to download a clip. There's this thing
called the JRE you also need to download.
If you have a make, the JRE in most cases is already installed. If you have Windows, it's
not installed. They need to go down, and you can send them to the CS106 page and say
download the JRE. Here's a copy of handout No. 5. Go ahead and install it, and then they
can run your programs. Your program can't run if the computer doesn't have Java, right?
It's a bunch of stuff that's going to execute Java byte code, and your computer's like,
what's Java byte code? I don't know what to do with it, so it won't run. They need the
Java Runtime Environment.
Now assuming someone has the JRE, they can go to a web page. You might put some
page on the web that allows you to load your applet. Your applet is just a webified
version of your program. So here look. It's running inside a web browser. This isn't the
actual application. This is my web browser. I could go to some search engine from here.
I'm sitting in the web browser. I'm not just running a regular application on my desktop.
This guy's actually running in my web browser.
Here's how I make it happen. I create a web page. If you don't know about HTML and
creating web pages, unfortunately I can't explain that to you in five minutes. What I can
show you is basically what this file's going to look like. So if you know a little bit of
HTML or you just want to essentially copy and paste this idea, this will work for you. So
all you do is say – the entire page that allows your applet to run. You say this page is
HTML. There's these little things called tags.
The name of that page is name surfer. I want to create a little table. I'm going to create a
little border around my application. What's my application? My whole application is right
here. I have an applet. What's the name of the archive, which is a Java archive that
contains my applet? It's name surfer dot jar. Code is what's the entry point. This guy no
long has a manifest file available to us. What's my entry point? Name surfer dot class.
That's where you start running. So it says I'm going to go name surfer dot class, find its
name. That's where I start running.
The space I'm going to give you to run on the screen is 700 by 500. That little snippet of
code is what goes on whatever web server this is serving on. It looks for those jar files,
slaps them into the page and then they're good to go to run more java program inside their
web browser. How many people know HTML? You folks. There's some number of folks
that this might be a reasonable thing to do. If not, you just don't need to worry about it.
It's not a big deal.
The one other thing you should know if you create a web page is that when something's
running in the web page, it doesn't have access to the rest of the file system. What that
means is if I actually happen to be over here – notice I have index dot HTML. I have
name surfer dot jar, and I have ACM dot jar. What happens in my names dash data file?
It doesn't exist. Why doesn't it exist? Because I couldn't read it anyway. Once I'm running
on the web browser, for security reasons, it doesn't let you go in and pull stuff out of your
file system.
If it did, people could do really bad things to your computer. So what you need to do if
you want to find that data, you let us run name surfer on the web. What did you do?
Here's the dirty little secret. I actually created a giant array that had all the data in it and
made it part of the program. So sometimes you can do stuff like that if you don't actually
want to read from a file. The other thing you can do is you can take those files and
include them in the jar file because the jar file cannot only have compiled [inaudible]. It
can also have data files. That's another way of doing it if you want to do it that way.
We don't have time to go into all the details. It's kind of the same process. When you're
exporting stuff to the jar file, you include some data files in there. So what's creating the
executable. Now that we know all this funky stuff about executables put on a web page if
I want. I'm feeling happy. I'm good to go. It's time to come back to our friend, standard
Java.
Standard Java's what allowed us to think about doing this stuff because we learned about
main. I want to show you a couple examples of programs that actually don't use the ACM
libraries at all to show you why we use the ACM libraries. One of the things you might
be wondering is why weren't we just doing the standard Java thing the whole time? Part
of the reason is things are just so much easier and cooler when you have the ACM
libraries.
So if you go back over to Eclipse, we're kind of done with name surfer and all this
manifest stuff. Here's a program that's written in standard Java that writes out hello world
on the screen. This is something you kind of did in the first class by saying public class
hello world extends console program in printlin. Hello world out to the screen, and you
would've gotten it in the console.
So what's different here? What's different is we have public class hello world, and it
doesn't extend anything. It doesn't extend program. It doesn't extend console program.
There are no imports for the ACM libraries. We're not using any of the ACM stuff. So we
don't have a console program. We don't have a nice console that we write stuff out to that
displays in a nice little window. What we do have is something called the system output
console. If we want to put stuff on that, it looks real similar to what you had before. We
used printlin, which is why we made the method that you used called printlin to match
this. But we need to say system dot out dot prinlin and then the text we want to print out.
Again, here I have a main method. My main method can have whatever I want in it. This
is just where execution starts. So if I compile this and run it – let me show you why. This
world is not all that cool. Here's hello world. It just ran. You're like, I don't see anything.
You don't get a cool window that comes up and is like, hello world. You have the system
console. The system console if you happen to be using a development environment like
Eclipse or something else, it's basically just a window in that development environment
that shows messages that you print out. If you happen to be in the bad old days that I
think I erased over here where you have command line text where you actually type stuff
in, the console would just be that same window where the text would appear where you
type stuff.
If you don't get it in a separate window, it's not that cool. A lot of times, this window's
close anyway. If you wanted to kick it up a notch, why use the console? Why not do
something graphical. So here's graphical hello world. What we want to do is create a
window that is going to have some title associated with it. We're going to put the text
hello world in that window. What do we need to do? Again, execution starts at main. We
need to create something called a J frame, which you never had to worry about before.
What's a J frame? It's actually a frame that's going to hold the window.
We're going to start running our application. It's going to create a little window for us that
we're going to display stuff in. What are we going to put in that window? We're going to
put a label. J labels you've seen before. This is just like you've seen. We're going to create
a J label that's called hello world, and we want this label to be center justified as opposed
to left justified or right justified.
So I need to say J label dot center to center justify it. If I don't give it a justification, it'll
by default by left justified and look kind of ugly because we want it center. Then I add
this label to my frame. So similar to the idea of having a canvas and adding stuff to the
canvas, it's exactly analogous. We want to make it just as easy. So when you saw
standard Java, all the same concepts would apply. Here, we're just adding the J label,
which as you know now, is a J component in the big Java hierarchy. J components can be
added to J frames.
So we have a J label that gets added to a J frame. I set the size for that J frame, which is
500 by 300. That tells me how big the window's going to be when it starts. Then there's
this other stuff that I need to do. You would think why do I need to do that? It doesn't
make any sense that I would not otherwise want to have it this way. Here's what I need to
do.
If someone clicks close on the window, I need to say, hey, if someone clicked that, then
you need to close yourself. Otherwise, the window goes away and the application keeps
running. It seems odd, but we need to have that there for this application to stop running.
Then we say window, I know I created you and everything. You need to make yourself
visible. Otherwise, no one will be able to see you.
You're like, why would I create a window that I wasn't going to make visible? Yeah,
that's why we use the ACM libraries. So we need to make sure that this guy's visibility's
true. If we run this, here's graphical hello. So after all this stuff, here's graphical hello.
Yeah, would you want all that explained to you so you can get hello world? You're like,
I'm writing a social network. I don't need to worry about hello world in the middle of my
screen. That's because you have the ACM libraries. So you're like, okay, let's kick it up
even one more notch. You're like, [inaudible] all this mouse stuff and interaction, right?
That should be something I get some benefit from having standard Java. So we'll have an
interactive version of hello.
Center the interactive version of hello. Now some of this stuff should begin to get a little
more repetitive in the sense that you have your main. You have your J frame. The frame
is called interactive hello. That's the title of that window. What we're going to add to this
J frame is a new class that we're going to create called a moving label. I'll show you what
a moving label down in just a second, but we need to set the size of the window, set
default close operation exit on close. Set visibility to true.
Basically all this does is create this window of a particular size. It's going to add this
thing called a moving label to it. Moving label's just another class I create. What's a
moving label? A moving label is a J component. It needs to be a J component because I'm
going to add it to a frame. To display something on a frame, I can only display
components on the frame. This is going to extend component.
It's going to implement our friend, mouse listener. It's going to listen for the mouse.
You're like, oh, I remember mouse listener. That where the mouse got clicked and
dragged. That stuff's exactly the same. So I have my constructor. What my constructor
has, it has some starting text for this label at its starting X and Y location. That should
look kind of familiar to you. Basically I just store off the text. I store it at X and Y. This
guy wants to listen for mouse events.
So it says add a mouse listener, and it needs to say if you get some mouse event, send
them to this. So this is a little bit different than you've written in your programs before
where you just said add mouse listener. You just had an open [inaudible]. That's because
we kind of took care of the rest of this stuff for you.
Here's where things get a little bit funky. The difference between this and thinking about
having some sort of label that you just display on a canvas, some text that just sits there,
is that this guy now has to worry about what's known as painting himself. That means it
needs to draw itself on the screen.
Well, when I had labels before, they just knew how to draw themselves. Yeah, that's
because we gave you a label that was a little bit smarter and knew that it was a label that
should draw itself. This guy needs to be told to draw himself. There's a method called
paint component that gets called whenever this guys gets displayed on the screen. There's
some other thread that's going to call it for you automatically. Here's the graphics context
in which your going to call yourself.
Within that graphics context, I'm going to draw some string. The actual method name and
everything is not important here. It just shows you that there is extra stuff you need to
worry about, which is why we didn't want to do all this stuff to begin with. This stuff
you've seen before. What happens if a mouse is clicked? I get the new XY location. I
repaint. What does repaint mean? It means redraw yourself. I'm going to redraw myself at
this new XY location. When I call repaint, someone comes along and says, hey, to repaint
this area, I'm going to call your paint component method so you can repaint yourself.
Whoa, this is really weird. I'm over here. I get a mouse click, and I know that I want to
redraw myself. But rather than telling myself directly to redraw myself, I go and tell the
system, I need to get repainted. Then the system says, that's the start of Jabba the Hutt
kind of sitting there fat and happy. Some day, if you actually – we won't get into it.
You go and say I need to repaint myself. It says, okay, you need to repaint yourself. Well,
when I'm ready for you to repaint yourself, I'll call your paint component method. Until
then, you don't repaint yourself. So it delays other stuff in the system to worry about, and
then oh, yeah. There was you. You asked to get repainted. I'll call your paint component.
You say, okay, well, now I'm going to draw myself at the new XY location.
So it kind of convoluted the whole notion of you're doing something here, and you're
asking someone else to do something for you. Then they're going to call you back to do
what you originally intended to do. Now it makes a little bit more sense after we talked
about threads and after you've seen all this stuff. Third day of class, not so hot.
So if we run this, this is called interactive below. Basically, what it does is it just brings
up our particular message, CS106A rocks in the middle of the screen. Now every time I
click the mouse button because this is the mouse click event over here. Every time I click
the mouse button, the XY location of the mouse becomes the new base point for the text
that gets drawn. So it just moves around the screen.
Any questions about that? So this is standard Java. You've seen all the concepts using the
ACM libraries. The notion of adding things, having mouse listeners. As a matter of fact,
the whole mouse listeners concept, we just took the standard Java ideas and used them
sort of in conjunction with the ACM libraries. But there's a lot of things in the ACM
libraries that just made it so much easier to, for example, to graphics contest entries or to
write a social network.
So you're welcome to continue to use the ACM libraries after this class. Some people
were wondering why we use these libraries, and this is the reason why. There's a whole
lot of stuff you'd have to worry about otherwise. Questions?
Student: [Inaudible].
Instructor (Mehran Sahami): You'd be using your favorite word processor, like
notepad. There's actually some places which I won't name, but it's actually reasonable
that some schools in your first programming class, what you do is they say, you need to
have notepad, or you need to have some text editor. Then we're going to do command
line stuff. You're going to type in name surfer, names dash data dot text to run your
program. Then everything is going to be [inaudible].
So one thing I want to leave you with now, in our final few moments together, is sort of a
notion – we're still going to meet next week, talk about life after this class, but if you
want to go on in terms of learning more about Java, especially standard Java, we sort of
just started things off by giving you this book, which talks all about the ACM libraries.
This, I think, is a great book to actually learn everything with and use the ACM libraries.
But if you want to go on, what are some other resources you can use?
Student: [Inaudible].
Instructor (Mehran Sahami): It's not pretty. If you go to the Java section of any
bookstore, just be prepared to stay a while. One book that I would recommend, not that I
get any kickbacks from these folks, called Learning Java. It's actually a pretty good time.
Some of the examples you actually saw here were based on this book. It's a little big.
That's why we don't use it as a textbook in this class. Will we break the 1,000-page mark.
It's so close. No, 950. Sorry, close.
There are some other books. Actually, the original specification of the Java programming
language. This is an older version of the book. This was when I was a wee tyke in the
days of yore and got the old version. I forget what version they're up to now. This was
second edition. I think now they're up to three or four. Something like that. The books are
a little bit bigger, but for a book that specifies the language, Java, it's actually very wellwritten
as a reference. So I'd recommend this as well.
If you're into oh, I want it all, Big Java. It's big. I think it might actually cross the 1,000
page – oh, yeah. It's like 1,200. If you really want to get hardcore and you're all about
web-based Java, Java Server Programming. Everything you would want and a lot of
things you don't want to know. Everything you want to know and more. That's just a
small set.
So these are just a few books I'd recommend if you want to go on beyond this class. But
you can go into any bookstore, and you get inundated with that kind of stuff. Now you
have a context for putting all the pieces together because you've seen all the things that
you actually need to know to be able to work with the huge set of tools that Java actually
has. Any questions about any of this stuff?
You're good to go? All right. I'll let you go a couple minutes early because most of the
time, I let you go a couple minutes late. Have a good weekend.
[End of Audio]
Duration: 45 minutes

Search This Blog