ProgrammingMethodology-Lecture22.pdf

(54 KB) Pobierz
Programming Methodology-Lecture22
Instructor (Mehran Sahami): Howdy! So welcome back to yet another fun-filled,
exciting day of CS106a. We’re getting close to that Thanksgiving recess, which is always
a good time. In the days of yore, it used not be a whole week. It used to be you got like
one or two days off. You got like Thursday and Friday, which means you would have
gotten only one day off from this class and now you get a whole week to mellow in style
or catch up on all your other work as the case may be.
So there’s one handout today. It’s Assignment No. 6, which is the next assignment, name,
circle, watch. They talk a lot about that assignment in this class today. As a matter of fact,
we’re very fortunate for coming today because I will give you so much gratuitous
information that presents that assignment. It’s just almost laughable.
And then Assignment No. 5, as you know, is due today, so we can do painful to see how
much time it actually took you. Just wondering, I heard from my little sources that exist
was that last night, a lot of people were at the Lair and the computers at the Lair. Just
wondering how many people were at the Lair last night. A fair number. And some of the
computers at the Lair just mysteriously didn’t have Eclipse anymore. Anyone run into
that problem? One person, okay.
Yeah, there was some reimaging that went on, which I had nothing to do with and didn’t
know about until some of the section leaders said, hey, Eclipse is missing on these
machines and, you know, so I sent a very anxious email, I should say, to academic
computing, and there had been some mix up and Eclipse is now reinstalled on all the
machines, so I’m glad it didn’t affect too many people, but if it affected you, I apologize,
although we really didn’t have anything to do with it, but I’m sorry that you had to
experience that pain.
So in terms of the painful, how much time it actually should give you the assignment on
the Yahtzee assignment? Just wondering, how many of you actually liked the Yahtzee
assignment? Just a quick show of hands. And how many thought it was just more pain
than it was worth? Anyone? You folks, all right. And that’s good to know. I always try to
– actually try to gauge these things.
So anyone in the zero to two-hour category? You just did it and it worked and life was
good. Two to four? A couple people; that’s always good to see. Four to six? A reasonable
contingent. Six to eight? Good times. Eight to ten? Wow, pretty healthy distribution
there. Ten to twelve? Okay, we’re falling off a bit. Twelve to fourteen? You folks.
Fourteen to sixteen? Maybe a couple. Sixteen to eighteen? Eighteen plus? Taking a late
day? Yeah.
As you may have read – I should refresh your memory because it pertains to Assignment
No. 6, but as you may have read on Assignment – on Handout No. 1 at the very
beginning of class, because Assignment No. 7 is due the last day of the quarter, you can’t
use late days on that assignment. So if you have late days and you’re like, hey, I was just
saving them up for No. 7, well, seven’s due the last day so there’s no classes beyond
seven and you don’t say, hey, I’m taking a late day. That gives me an infinite amount of
time to turn it in because there are no more classes. No, it doesn’t work like that.
So Assignment No. 7 you can’t use late days on. So again, I would discourage you from
using your late days, but should you have them and you’re like, hey, I really wanna use
my late days, Assignment No. 6 is kind of your last chance to actually use your free late
days. Again, not that I would encourage you to use them, but just so you know up here
that you don’t get Assignment 7 and say, hey, I thought I could use my late days for this.
You’re getting plenty of advanced warning that you actually can’t.
One other quick announcement; CS Career Panel is today, so after today, I’ll stop
mentioning it because I believe most of you can’t actually time travel, so today, 5:30 p.m.
in Packard 101. Be there. We have a stellar panel of folks from academia, from industry,
a lot of people who actually – every one is a computer science major and four of the five
people graduated from Stanford’s department. The fifth is actually a faculty member here
that didn’t graduate from my department, but is a faculty member here.
And you’ll actually see there’s a very broad range of stuff to do. Out of those five people,
though, actually, I think only one, maybe one and a half who actually do programming as
their sort of fulltime job. But all of them are computer science undergraduates, so you get
sort of a breadth of what computer sciences can do.
So I’m so happy to see the fifth region. You’ll see the variance gets larger, right. As the
quarter goes on, that kind of happens, but it’s good to see that the mean is still where we
generally want it to be.
So now it’s time to talk a little bit about your next assignment. You just turned in your
last assignment. You’re like, no, no, it’s not time to talk about the next assignment. I’m
still working on the last assignment. Well, just pay attention.
Here’s what you’re going to do for your next assignment. It’s a little program called
NameSurfer, okay? And the idea of this program is it turns out that the government
actually keeps track of statistics of the popularity of names over time, okay. So every
decade when they do the census, they kind of keep track of the number of names and they
rank the top 1,000 names in terms of popularity for boys’ names and for girls’ names, and
they actually make this data available. We thought wouldn’t it be cool if you actually
were to display names to the user in terms of how that popularity changed over time.
So that’s what NameSurfer does. Here’s the program. What it does is it shows you the
decades. You can notice that this is a graphics, or involves graphics, right, has the
decades from 1900 up to 2000 and so relatively recent date up to the last census. And
what we can do is we can type in a name and then click graph, and what it will do is it
will show the rank of that name over time, on a decade-by-decade basis. So let’s try Fred,
for example. If you graph Fred, you can see Fred real popular at the turn of the prior
century and then Fred just had this quick decent into oblivion, where now he’s the 974th
most popular boys name, at least in the last Census 2000. We can do multiple graphs on
here, so if I type in someone else – so one thing that’s also interesting is you’ll notice
popular culture comes in, and so if I type in Britney, the alternative non-traditional
British spelling, it didn’t exist until 1980, in which case it totally shot up to like the top
100 and maybe with the most recent things that are going on, it’s dropped a little bit.
But through this whole region, what we have in our data, and I’ll show you the data in
just a second, is that Britney did not rank in the top 1,000 until the 1980’s and then it
jumped up. Because it started ranking in the top 1,000 in the 1980’s, it still shows up in
our data file because at some point it started ranking higher than that. And it turns out that
there are some people who just have names that, well, are not so popular. So we do
Mehran and nothing comes up because contrary to popular opinion, Mehran is not in the
top 1,000 most popular names. In a few years, you can change that if you have children.
Your choice. Not that I would advocate it, man. It’s a rough childhood. Mehran, doesn’t
that sound like moron? Yeah, I haven’t heard that one in the last two hours. Yeah, I
know. I’m just – rough childhood. Not that it affected me at all. So in terms of actually
doing this assignment, you might say, oh, that’s kind of interesting. Now there are a
couple of things going on. You might look at that and say, yeah, I could do that, Mehran.
I could do that in the zero to two-hour category, right, except for one little thing and the
one little thing is check out this bad boy. Whoa. As I resize the window – I will continue
to resize the window until I get you in a hypnotic trance – everything in the graphics
display automatically resizes to display for that window size, okay? And what you’re
going to find out today is how do you actually do this kind of thing to keep track of as the
windows resizing, how do you get events that come to your program that tell you, hey,
some resizing happened. I need you to redisplay what’s going on.
The other thing that’s interesting about this is I cannot only when I resize, it keeps track
of all the information that’s on there, Fred and Britney. If I ask someone else like, oh, my
son, William, who’s always been a popular choice – ah ha ha – just like in the top ten
dips briefly over here because maybe it was getting over used and then it comes back up.
Keeps them all. They’re in different colors. The color sort of cycles, so if we add
someone else, like Ethel – Ether, Ethel – yeah, Ethel, sorry, not so popular anymore.
When Lucille Ball – when “I Love Lucy” was on or earlier, it was a popular time. The
cycles of colors eventually will go back to black, so if we put in something like Bob, it
goes back to black. Then you’ll see red again, then blue, then magenta. That’s all given to
you in the handout.
If you hit clear, everything goes away, except for the gridlines, and you can sort of go
again from there, like here’s George and you can see George has a slight decline. Here’s
four back here and now he’s like somewhere in the hundreds. It’s okay if it goes off the
window occasionally, okay? But the resizing is kind of the key to this and that’s what
we’re going to – one of the things we’re gonna look at today. Okay.
So that’s what you want to keep in mind for the assignment, resizing, because the demo
that’s on the website because it runs in a web browser, doesn’t resize and it sort just has it
as the little warning down at the bottom. It just shows you how the program actually
works so you can try out typing in different names and see the behavior. But it doesn’t
resize because its size it stuck by the browser and changing the browser window doesn’t
actually change the size of the applet. That’s just life in the city, okay? But that’s the way
NameSurfer works.
So what we wanna think about is actually building programs that have this kind of
property and to understand how programs that have this kind of property work, what we
need to think about is something known as a component and related to that is a container.
So first of all, I’ll show you all the classes in Java that pertain to the components and
containers. Not all of them, but a good number of them. So here’s the classes in the
component hierarchy and some of these will look familiar to you. So programs of which
Console program and Graphics program that you’ve been using the whole time are j-
applet, which is an applet, which is a panel, which is a container, which is a component.
So somewhere all the way up the food chain, right, the food chain before we didn’t go
that high. We just went up to applet when we showed you sort of this picture before.
Turns out all your programs are components and containers. Every component is a
container, or every container is a component. And so you may ask, well, what are
containers and components? So the way you wanna think about this is at some generic
level, a component is anything that can appear in a window. It’s anything that can in
some sense be displayed. So your programs are appearing in a window that’s getting
displayed. That’s why they’re components, okay? Now, that also means that a graphics
program and a console program are also components because they’re subclasses of
program. Now at the same time, there’s a notion of a container. What’s a container? A
container is simply something that contains other components, so a container just
contains a set of components and you might say, hey, Mehran, that’s really weird. A
container is a component, but you’re telling me it contains a set of components? What’s
that all about? And the simplest way to think about it is it’s this, okay? This is a
component, this bag. It turns out this bag is also a container because it contains things and
it’s perfectly fine for something to be a container and also to be a component.
So here’s what I mean, right. Your programs, as it turns out, are all components, which
means they display in the screen, but they’re also containers because your programs
contain other things. For example, in the display of your program, it may contain a
button. Right? It may also contain, say, a text field. And the other thing that it may
contain, if you have a graphics program besides these kind of components, which buttons
and text fields are also components – I’ll show you an example of that in just a second –
it may contain another container. And it’s perfectly fine for containers to contain
containers. As a matter of fact, you’ve been using them for the whole time. Your graphics
program, especially if you have a graphics program that has some interactors on it. It
could have buttons. It could have text fields. It could have a G-canvas, and gee, guess
what? A G-canvas is a container and a component. Because it’s a component, it can be
contained inside some other container, and because it itself is a container, it may have
certain things in it.
Oh, like here’s our friend, the frog image from the mid-term. Yeah, that was just
something that was in another container, okay? So there’s no reason why something that
is a container can’t be a component, which means it can be contained in some other
container, that’s fine, and all containers themselves are components, so they can be stuck
in somewhere else. That’s the basic idea. You should think of it as bags and you can put
bags inside of other bags, and bags contain things. So even though you see the hierarchy
this way, the concept itself is not actually that difficult. Now, the thing you wanna think
about this is I just mentioned to you your programs are actually components and
containers. G-canvas is a container and a component, but components that we talked
about before, j-components, like our friends, say, oh, j-button – you actually saw this
slide a couple classes ago, j-button or j-label or j-combo box, all these things you saw
before – are actually j-components. They all are subclasses of j-component, which means
by being subclasses of j-component, they were also containers, strangely enough, and
they are also components. We just think of them generally as components and we try to
put them in other containers, like we put them inside the display for a program and that’s
its container. But that’s the general idea that’s kinda going on here. So when you wanna
think about containers and components, what you also wanna think about is that these
components themselves, before we thought about like action listeners, right, like
something happens like I click on some component and some particular event happens,
like I clicked on it, something like that. We can think of the whole component as also
listening for other events because a component is something that display stuff on the
screen.
Stuff can happen to be components, like they can be resized and when a component’s
resized, some event can get generated and sent to that component that says, hey, guess
what? You got resized. You need to do something. Like the canvas that shows all the
graphics, it can get some event that says, hey, canvas, yeah, you were just resized. Do
whatever you wanna do. Up until now, you didn’t do anything. Someone came and
resized your canvas. You said, yeah, you’re resizing my canvas. All of the pieces I have
in my canvas stay where they are. Nothing’s changing. Now what you’re going to get is
the ability to have the canvas be told, hey, you got resized or something else happened to
you. You can respond to it however you want, which means you can redraw everything
that’s in your canvas, so that, for example, you can have all the layout come out just fine
when people resize your window. So what does that actually mean in terms of these
components and containers? What it means is that we can have listeners in the same way
that you kinda thought about listeners from mouse events and listeners for j-components.
You can have listeners for components, okay, like we talked about resizing the window.
Now, here’s a little simple example of something that can go on with one of these
components. So I’ll come back to Eclipse, I’ll run a little program here – I gotta compile
– and I’ll show you the very sort of simple program that makes use of this concept of a
component having a listener and being able to get certain events. So it’s just called My
Programming. Here’s what my program does. It draws a square. That’s all it does is it
draws in a filled-in square. You’re like why are you showing me this, Mehran? I could’ve
done this in the first week of class and I would say, no, you were doing terrible in the first
week of class. And then you say, no, I could’ve done this in the second week of class and
Zgłoś jeśli naruszono regulamin