ProgrammingMethodology-Lecture05.pdf

(62 KB) Pobierz
Programming Methodology-Lecture05
Instructor (Mehran Sahami) :All righty. Let’s go ahead and get started. A couple of
quick announcements before we start today – so hopefully you’re all busy working away
on Karel and life is good. Just quick poll – how many people have actually finished Karel
already? Oh, yeah. I won’t ask how many people have not yet downloaded Eclipse. There
are no handouts today. Getting’ a little breather – no handouts. Don’t worry; you’ll get
some more of that next time.
Sections start this week, so hopefully you all should have gotten an email about your
section assignment and who your section leader is, so you can actually do Assignment
No. 1, the email portion. You should have been able to do the programming portion the
whole time. But make sure to go to section this week.
And the other thing is the Tresidder Layer, which every once in awhile you’ve heard me
refer to. This is a computer cluster up in Tresidder. Is staffed by one of six helpers like
almost continuously around the clock or most of the times at reasonable times when
people are working.
So Sunday through Thursday, every day except Friday and Saturday because contrary to
popular opinion, computer science people actually do have lives or we actually like to
pretend we have lives, but every day from Sunday through Thursday 6:00 p.m. to
midnight there will be a staff of helpers on there and actually some of the times there is
like two or three or four people there.
And they’re there just dedicated for the 106 classes. They’re not like general consultants.
They’re just there to help you work out problems in this class, and they know like what
assignments you’re working on, the whole deal. They’re all like, your section leaders and
they’re all be exceptionally trained to do this.
The other thing that’s going on, hopefully you should be doing Assignment No. 1. I’ve
actually gotten a bunch of Assignment No. 1 emails that have already come in. In the
early days, when I got the first few, I actually tried to respond to them all, but then at
some point, I just woke up and I like, you know, went to my computer and was like, “Oh,
you’ve got mail.’
And it just [inaudible]. So I couldn’t respond to everyone individually. I apologize if I
don’t respond to you individually, but I do read them all. I guarantee you that I actually
read them all and I look at backgrounds. And just to prove to you that I do, here’s some
interesting ones that have come in so far – just to share three.
So there’s someone actually spent their time in Taiwan living in a Buddhist monastery,
which I thought was interesting, except for the fact they were actually living there as a
monk whom I thought was pretty interesting.
Someone else used a – I wasn’t quite sure on this concept, but maybe I can provide a
clarification. There was a vegetarian who only eats low-quality meat, and so he
mentioned that as things like burgers and not steaks. And I would qualify that by saying
that’s not a vegetarian; that’s called being a grad student.
And last, but not least, there was actually someone in here who’s on the Colbert Report,
which I thought was actually pretty interesting. I don’t know in what context, but come
talk to me afterwards.
So with that said, any questions about anything before we start? Today we’re actually
gonna go over some of the graphic stuff you saw, talk a little bit more about objects and
classes and get into variables and values and all kinds of goodies. Any questions?
All righty, then let’s just dive right in. So one of the main topics for today is this thing
called a variable. And a variable, you know, like variables come up in mathematics and
it’s like, oh X and Y are variables, right and there are these things and we do all these
manipulations on variables.
In the computer science world, they’re really friendly, right, and you don’t have to worry
about integration or differentiation or you know, those kind of variables. Variables are
kind of your friend, and basically all the variable is in the computer science worlds is it’s
a box. It’s a box where we stick stuff and the stuff we stick into that box can change.
That’s why we call it a variable because it’s a box that has a variable contents, and you
think back you know, in the days of yore in math, and you know, oh yeah, it’s kind of
like X can have different values, yeah, it’s basically just like that.
So in computer science, what we think of as a variable, is each variable has three things
associated with it. It’s got some name, and that’s just how we refer to that particular box.
It has a type, which is something a little bit different in mathematics, but the type
basically says what kind of thing does this box store? Some boxes store numbers; some
boxes store letters; some boxes will store other things, like little objects in the world. But
a type is just what’s stored in that box.
And then there’s a value, and the value, as you can imagine is just what’s in the box.
What is the actual thing that’s in there, right? If it stores the number, then it might store,
for example, the Value 3 and that’s just the value, and it may have some name associated
with it.
And how do we actually name these? There’s actually a rule and it’s not a very
complicated rule, but a very simple rule you need to remember for what are valid names
for variables in Java. So a valid name, so this is how you actually name these puppies,
has to start with a letter or an underscore. So it starts with a letter like one of the
alphabetic letters and can be upper or lower case or the underscore character.
Okay, and that’s kind of you know, underscore. It’s down at the bottom of the line, okay?
And then after you have that initial letter or underscore, then after that, you can have any
number of letters, numbers, that’s like the number digits, like 1, 2, 3, 4, you know, 0, etc.
or underscores, okay?
So you can’t start with a number. You have to start with a letter or underscore, but for
most purposes in this class, just thing about them as letters. When not actually using
underscores, you might occasionally use numbers. You can actually have numbers after
the first letter.
There is one slight caveat to this rule which you can’t have any variables that’s name is
the same as some which known as a reserved word in Java, which means its name can’t
be the same as some special word in Java, like the word class is a special name in Java,
and there’s actually a page in your book, I think in Chapter 2, that lists all the special
names. It’s like out of the English language it has about 127,000 words. I think there is
like 40 in Java that you can’t make a variable name.
Okay, you have lots of other choices. As a matter of fact, there’s lots of things that don’t
have to be valid words in English. They can just be any name that follows this rule. The
important thing to think about in terms of a name and this is one of the good software
engineering principles, is make your name descriptive. If you have a program that’s
maintaining for example, the balance in the bank account, a real good name for the place
where you store the value of the balance would be something like “Balance.”
A real bad name would be something like “A,” because no one knows what A is. It’s like
hey it’s A – yeah, I know what A is. And someone says, “Yeah, A is balance,” and
someone says, “No, no, no, in my program, A is actually how many miles I bicycle
today,” and you’re like, “No, no, no, man, A was balanced.” Well, if it’s called Balance,
there’s just no ambiguity, so give them descriptive names.
So that’s kind of a name part of this. The next thing is what is this type all about. What
are the different types that you can actually have. And there are some things that we refer
to as primitive types. These are the types that have the smaller developed brains and use
knuckles drag on the ground. No, there just the types that are built into Java, okay and
some of the basic types we have is something we call an INT, which is short for an
integer, but we actually write INT, so INT, I-N-T, is the name of the type, okay.
And this is just an integer of value. It’s just gonna store some whole number basically. It
stores a number between minus 2 billion and plus 2 billion, but for all intents and
purposes, you can just imagine you can store any integer in there, okay?
There’s also besides integers what other kinds of numeric data do we have? People
already know, it’s like I would think it would be like real values, but a bunch of people
are already saying double because you’ve read ahead and you’ve done the assignment the
way you should. And that was kind of a mini social, but both went to the same person.
There’s this thing called a double and a double is actually some real valued numeric
value, right. It’s something like 2.3 is a double or even 2.0 can be a double, okay? Why is
this thing called a double – anyone know, as opposed to like a real? Uh-huh?
Student: [Inaudible].
Instructor (Mehran Sahami) :Yeah, there’s this wonderful verb people call the I triple
E, which is like the Institute for Electrical and Electronic Engineers and they come up
with all these standards for things. Is anyone a member of I Triple E here? No one? Oh,
man, join and pay your dues. It’s a good time. I'm not actually a member myself.
But what those folks actually do is they come up with standards for things and one of the
standards they came up with is how you represent numbers that are real valued numbers
inside a computer, right, because remember a computer only understands ones and zeros,
so how do you actually represent a real valued number.
And so there’s a standard, and part of that standard has to do with a precision of the
number, how many digits in some sense and a rough approximation you store and double
stands for a double precision real number.
And so for the purpose of this class, all real valued numbers that we’re gonna use are just
of type double. Okay, there’s a couple of other types that I’ll just mention now and we’ll
actually go into them in much more detail in a couple of future classes.
One is called Boolean. And if you’ve ever heard of Boolean logic, this is a logical value,
so this is just essentially true or false, and we’ll talk about that in excruciating detail next
time, but I’ll just let you know that there’s a type called Boolean.
There’s also a type called care or Car and as you can imagine it’s because we like sort of
the first syllable of most things and this is the first syllable of character, okay, and so
we’ll also talk about this in a couple of weeks time when we actually get into a some
things with characters, but that’s just a character.
It’s a variable; it’s a box, but still is a character. It’s a box that stores an integer, box that
stores a real value, box that stores true or false and a box that stores a character. Those
would be the different types of them.
So one thing people – so that’s kind of types, at least some of the basic types. And then
when you think about value that we actually store in this box, people always get
uncomfortable when they see INTs and they see double, right, and they sort of say, “But
Meron, like 2 is an integer, right?” And I'm like, “Yeah.” And they’re like, “But you just
told me 2.0 is a double, right?” And I'm like, “Yeah, 2.0 is a double.”
And so the natural question is why? Why do we have both these things? How come like
all integers aren’t subsumed by the real values, right, you’re kind of the mathematician
type and you’re like, “Yeah, there’s like strictly more of these than there are of these. So
why are all these like subsumed in here. Why do we have this integer type,” okay? And
the reason we do boils down to a simple question and the simple question you want to ask
yourself is how much versus how many, okay?
So if you ask someone, let’s say you, ask me, just so I won’t embarrass you, “How much
do you weigh, Meron?”
Student: How much do you weigh, Meron?”
Instructor (Mehran Sahami) :I weigh about, you know, 155.632 pounds, okay? That
makes perfect sense, right. If I can put a decimal point, I can put as many numbers after
it, or I could just say 156, right and those are both valid kinds of things.
Now you could ask me, “Hey Meron, how much children do you have?” I have 2.3
children. Does that make any sense to you? Yeah, it’s like we had three until that grisly
accident. No, it’s just that there’s sometimes – I know that’s horrible to say. We actually
have one, and he’s just fine.
There’s sometimes in the world when you care about counting, and when you care about
counting, it doesn’t make sense to have fractional values. Those things are integers.
They’re a how many kind of value. When you’re thinking about how much, that’s a
double value, and you actually want to keep these distinct because if I ask you what’s the
next number after 1, you say –
Student: 2.
Instructor (Mehran Sahami) :2 because you’re thinking integer, and that’s perfectly
right and if I say what’s the next number after 1.0, you say –
Student: [Inaudible].
Instructor (Mehran Sahami) :Yeah, you start mumbling and you’re like, “Well, I know
it’s not 2.0. That would have been the obvious answer and that’s probably not it. How
about 1.0000001, and I'm like, “No you missed a couple of zeros in there. Just keep
going. Wait until the end of the quarter and put that 1 and come back and talk to me.”
In real values, there is no next value, okay, so when you care about counting, it doesn’t
make sense to use a double. That’s when you want to use an integer, so having these
things be a distinct type actually make sense and you’ll kind of see that as we go along,
okay.
So the value is basically just what you’re actually gonna put into this box over here and
it’s gonna be some of the values that we actually talked about, so let me show you some
examples of how we might use variables, what the syntax for variables actually is in Java.
Zgłoś jeśli naruszono regulamin