ProgrammingMethodology-Lecture12.pdf

(51 KB) Pobierz
Programming Methodology-Lecture12
Instructor (Mehran Sahami): So welcome back to yet another fun filled, exciting day of
CS106A. A couple quick announcements before we start. There’s actually no handouts
for today, and you’re like if there’s no handouts for today, why are there two handouts in
the back? If you already picked up the two handouts from last class, you might want to
double check to make sure you don’t already have them, but if you don’t have them, feel
free to pick them up. We just don’t want to cut down any more trees than we need to, so
if you accidentally picked them up, you can put them back at the end of the class, pass
them to a friend, whatever you’d like to do.
As per sort of the general consensus last time, everyone wanted to have class slides on
the web, so now all the class slides are posted on the web and after every class when we
have some slides in class, they’ll be posted on the web as well. There’s a little link called,
strangely enough, class slides that is now on sort of the left hand navigation bar on the
106A website, and so you can get all the class slides form there. In some cases, what I
actually did was if we covered some slides over multiple days but they were all about the
same topic, I might have aggregated them all into one set of slides or one deck, so you’ll
see them in there as just one topical kind of thing.
Just wondering – how many people have started break out? Good. How many people are
done with break out? You folks get to see good times. You’re well ahead of the curve. I
won’t ask how many people have not started. I assume it’s just the compliment of that. It
is a fairly beefy assignment. It would be in your best interest to start soon. It’s due on
Wednesday. With that said, it’s time to launch into an entirely new topic, and the entirely
new topic is something that we refer to as enumeration. Enumeration is a pretty basic
idea, and it comes from the word enumerate, as you can kind of imagine.
When you enumerate something, you basically just have some way of referring to
something through numbers. So if we wanted to enumerate, for example, the year that
someone is in college, we might have freshman and sophomores and juniors and seniors
– that’s the enumeration of the year you might be. And so the basic idea is we just want
to have some set of things that we enumerate or give a set of numbers to, essentially, and
you don’t necessarily want to think of it as having to be a set of numbers, but it’s
basically just some set of items that go together.
We generally give them some numbers or some listing as a way of keeping them all – a
way we might keep track of them. One way we might do this in java, for example, is just
have a series of constants that are integers and so just to save myself a little bit of time in
writing, constants. Yeah, a beautiful thing. So we might have some constant public static
final [inaudible], and so if we’re going to do enumeration, oftentimes we just use integers
to refer to each of the individual items and we just count them up. So frosh would be one,
sophomores two, juniors three, seniors four, and grad is five.
That's just what year you might be in school. Oftentimes, computer scientists actually
start counting from zero, but sometimes it actually makes sense to have these things be
numbers that start at one. For example, if you want to know which year someone is in
school or if you’re doing months of the year, January is generally number one as opposed
to number zero, so just to keep with common [inaudible], we might actually number it
this way.
Now, there’s something that was introduced in one of the later versions of java, java 5.0,
which is kind of the second to latest version, which is something called enumerated types,
and the book talks about them briefly. I’m not going to talk about them here sort of for
the same reasons the book doesn’t talk about them. The book actually talks about them
and then says the advantages versus disadvantages of doing enumerations using this thing
called enumerated type versus just listing them out as integers. This way, at least for the
time being in sort of the development of java’s world, seems to win out.
We’re just going to teach you this way. As a matter of fact, in the old school, anything
before java 5.0 had to do it this way, so it’s just best that you see it this way, because
most code these days is written this way and it probably will continue to be until at some
point this new enumerated type thing takes off. As you see in the book, it talks about
enum type. Don’t worry about it. We’re just gonna do it this way.
The only problem that comes up with doing something like this, though, is that you want
to figure out, well, how do I read in these things and display them? Well, these are all just
integers, so if I actually want to ask someone their year in school, I would have to keep
track of that with some ints that I might call year, and so I would read in an int, and I
might ask the person for their year, for example. And when I read that in, that’s all good
and well. The only problem is this thing is just an integer. The user gives me some
number, hopefully between one and five.
I might want to actually have some logic in here that checks to make sure they gave me a
number between one and five, ‘cause if they gave me a six, I don’t know what that
corresponds to. Maybe that’s the dreaded other student category. I need to do something
to guarantee that it’s in this list one through five. The other problem is if I actually want
to print out someone’s year, there’s no way for the computer to know that year one
should print out the word frosh, so if I do something like print lin here and I just write out
year, that’s gonna write out an integer.
It’s gonna write out a value, whatever the value the user gave me, presumably between
one and five. So if I actually want to have some nicety in there and say, oh, if the year is
one, I actually want to write out frosh as opposed to writing out a one, I need to do that
manually. So somewhere in my program, I need to have some function that I do a switch
statement or I can do cascaded ifs and I switch, for example, on year.
I might say, well, in the case where year happens to be frosh, then what I’m going to do is
actually print out print lin frosh in quotes, because that’s the only way the computer
knows about it, and then I would have a break, the funky syntax from our fun, the switch
statement, and then I might have some case over here for sophomore. I need to do this
manually, and that’s just the way life is in the city. These things are just integers. We
have some enumeration.
So in the program, the program can read a little bit more text because we can refer to
frosh, sophomore, junior, senior and grads as constants in the program, but when it comes
to displaying them on the screen, we need to actually write it out manually because the
compute has no other way of knowing that a one means frosh.
Well, case one is the same thing as case frosh, ‘cause if these are in the same program,
frosh is just the constant one, and so in fact that’s why we want to refer to it that way
because it’s more clear for someone reading it. They see oh, what do you do in the case
where you’re a frosh? Well, I’m gonna write out frosh. It’s fairly straightforward enough.
But I’m just using these constants. If someone else wants to come along and say you
know what? I love frosh and they’re all great, but I’m a computer scientist. I start
counting from zero.
It’ll just change everywhere in your program as long as you’ve used the constants if
you’re referring to zero. They might just say, well, actually frosh are the coolest. They’re
a six. That’s fine. You can do whatever you want. The computer really doesn’t care. Most
people probably won’t care, either, so we just start counting from zero or one most of the
time. Any questions about the general idea of enumeration?
Well, that’s the thing. In your program, you want to set up the expectation that they’re
entering a number. If they were to enter the string frosh, because read in does error
checking, it’s going to say that’s not the right format. So one thing you could actually do
is rather than reading an int, you can read in a line, which would read in a string, and then
you’d need to have some logic to convert that over to one. So you’d sort of do this
process but backwards. That’s why enumerations are something that are useful to have
when you’re writing real programs, but they can get a little bit bulky to use because you
have to do the conversions.
Right now, I’m just making the constants public because I might want to refer to them in
some other class. If I have some other class, they can also refer to these constants. If I
was only going to refer to these constants within this class, I’d make them private. With
that said, it’s time for something completely different. I know the sun isn’t out much right
now, but I sort of had this thrill where I wanted to barbecue one last time, and I figure if I
can’t barbecue outside, I’m going to barbecue inside. Now I wanted to light a fire in here,
but as you can imagine for various reasons, that was frowned upon by the university.
So I can’t actually light the fire, but I can basically do everything else that would be
involved with grilling, which doesn’t really turn out to be that exciting when you don’t
have a fire. But the basic idea is if I leave some things on the grill for a while, they’ll get
a little hot. Just pretend they were on the fire for a while, okay? If you leave something
on the grill too long, what happens to it? It gets kind of burned. It accumulates
something. It accumulates something we refer to as char. It turns out as a computer
scientist, you get a different kind of char than other people get on their burgers when
they’re grilling them, or, as happens to be the case, on their ding dongs.
Turns out they don’t make ding dongs. How many people know what a ding dong is?
Good times. If you don’t, come and look at the box. It turns out they don’t actually make
ding dongs. I’m sure this is copyright Hostess 2007. I went so many places last night
trying to find ding dongs, but you can eventually find them. The basic idea, though, is we
want to think about something that we want to refer to in a program that isn’t always just
a number. So far, we’ve been dealing with a whole bunch of numbers. We had doubles.
We had integers. Life was good.
I told you about this thing called string, but we didn’t really explore it, and now it’s time
to explore it a little bit further to figure out what these little puppies, this little char or
little characters are all about. So what we’re gonna do is we’re gonna explore the world
of char. There’s a type called CHAR, which actually is a character, and so the way we
refer to this type, even though I just called it char, is we don’t call it a char. We call it
either a char like the first syllable in character. Some people call it a car, even though it’s
not called a character because they just look at it and they’re like oh, if it was one
syllable, it would just be car.
And some people look at it and say no, Mehran, if it was just one syllable, it would be
char. Don’t call it char. I will beat you. That was a joke, by the way. I’ll be trying to
explain that to the judge when I’m in jail and he’s like, well, the video I saw, it didn’t
appear to be a joke. CHAR is just a character. We can declare variables of this type by
just saying CHAR and I’ll call it CH. I have these little CHAR CH, and what do I assign
to these puppies? What I assign to them is something that’s inside single quotes. That’s
the key.
I can say something like CH equals little a, and I put the a inside single quotes. Not to be
confused with double quotes. If I put it in double quotes, that’s a string. That’s not the
same thing as a CHAR. So I put it inside single quotes. That indicates that it’s a
character. The other thing that differentiates a character from a string is that a character
always is just a single character. You can’t put more than one character there. It can’t be
like CH equals AB. That’s not allowed. It’s just a single character. The funky thing, and
you might say, so Mehran, why are you telling me about this thing called characters right
after you talk about enumeration?
Because it turns out in the computer, the computer really doesn’t know about characters
in some deep way that you and I know about characters. All characters inside the
computer are actually just numbers. At the end of the day, we can refer to them as these
characters in their niceties for us, but to the computer, they’re actually an enumeration. A
happens to be some number and B happens to be some number and C happens to be some
number, and so now it’s time for you to know what those numbers are. So if we just look
at the slides real briefly, there’s this thing called ASCII.
Anyone know what ASCII stands for? It’s American standard code for information
interchange, and we’ll just do it as a social. The basic idea for this is that somewhere,
some time ago, someone came along and said we’re gonna create a standard enumeration
of all the characters that exist, and so here’s the first 127, as it turns out, character
enumeration, which is the part that’s mostly been standardized. All the rest of the stuff,
there’s still some debate about, but this one, everyone’s standardized on. Now this little
diagram that’s up here is actually in octal numbers, and you’re like octal numbers? It’s
base eight.
Computer sciences think that way. We think in base two. We think in base eight and we
think in base 16. Most people think in base ten. Yeah, that’s why most people aren’t
computer scientists. Here is base eight. I had this roommate at Stanford who thought
everyone should count in base 12, because base 12 was divisible not only by two but by
also three and four, and ten wasn’t, and he would just try to convince people of this all
the time. I was like that is so wrong. Now he works for the Senate, which I wouldn’t be
surprised if we have some resolution someday. The United States will now count in base
12.
But anyway, the basic idea here, as you can see in this, is that first of all, the character A
is not the number one, and there’s actually a distinction between the uppercase A and the
lowercase a. Another thing that’s also kind of funky is you might notice the numbers up
here, like zero, one, two, three – the digits. The number zero is not the same thing as the
character zero. The character zero just has some enumeration that’s some funky value.
Who knows what that is. It would actually be 60 in octal notation, which turns out to be
48.
That’s not the same thing as the number zero, so it’s important to distinguish that the
number zero that we think of as an integer is not the same thing as the character zero
which we would put in single quotes, and that’s true for all digits. You don’t need to care
about what the actual numbers are. If you really do care about memorizing it, the way
you can read the chart is the row on the chart is like the first two digits, and then the third
digit is given by the column.
So A is actually 101 in octal notation, if you really care about reading this, which makes
it the value 65, but we don’t really care. We just think of it as A inside single quotes.
There’s a couple things that that gives you, though, that are useful, and the couple things
that it gives you that are actually useful is it guarantees that the letters little a through
little z are sequential. It guarantees that uppercase A through uppercase Z are also
sequential, and it guarantees that the digit zero through the digits nine are also sequential.
That’s all you really need to care about.
Other than the fact that these guarantees are given to you, you don’t care about what the
actual numbers are up there. There’s a couple other things that look a little weird up here
that I’ll just kind of point out. There are some characters that are like these backslash
characters that are \c and \n. The backslashes are just special characters that are treated by
the computer as a single character, so \n is new line. It’s like a return. \c is the tab
Zgłoś jeśli naruszono regulamin