ProgrammingMethodology-Lecture06.pdf
(
60 KB
)
Pobierz
Programming Methodology-Lecture06
Instructor (Mehran Sahami)
:All righty, welcome back. If you haven’t turned in the
assignment yet and you, at some point want to, turn it into that box up there.
So a couple quick announcements before we start. First of which, first of which is the
quarter is already 1/5 of the way over, right? After today it’s like two weeks of ten week
are over, so that’s hard to believe.
But there’s three handouts in the back including your next assignment because the fun
never stops; when one assignment’s due the next assignment goes out, and a couple other
handouts. Assignment one, as you know, is due today so please drop it off in the box in
front and, congratulations, you’ll all – well, assuming you didn’t take a late day you’re all
programmers, right? Because hopefully you all did, Karel, you got him to run around the
world and do stuff. Hopefully figured out some interesting out rhythms and now you can
turn it in. Question?
Student:
I didn’t know if you needed a hard copy [inaudible].
Instructor (Mehran Sahami)
:Un huh, you want to get a hard copy in as soon as you
can, like right after class. But hard copies are important because we want you to turn in
both because we use the electric submission to be able to actually run it and your section
leader makes comments on the hard copy and so it’s important to have both.
But just because you asked…
All righty. So I want to take a quick pain pole before we start. So let’s actually dive into
the real sort of meaningful things. What the pain pole really is – remember I asked you to
think about how much time it actually took you to do the assignment? So total it up over
all the Karel problems; how many total hours; think about it, it took you to actually do the
assignment. Right? And we’re just going to go through and do a quick show of hands.
How many people actually got through the assignment in zero to two hours? All right.
Maybe like a couple people. I’ll make a small bar. How about two to four? A fair
number. Four to six? That’s good to see. Six to eight; eight to ten; ten to 12; 12 to 14; 14
to 16; 16 plus? Rock on. Thanks for admitting it. It’s a good time. Hopefully it was a
good time.
But that’s – first of all, one thing to note is the world is surprisingly normal. Right? Like
everything in the world is just normally distributed, that’s just the way it is.
The second thing is that computer programming, right or software engineering is a pretty
high variance event, right that you can go from less than two hours to 16 plus. I don’t
joke when I say it’s actually very high variance. But hopefully what this give you is
again, what we shoot for, right, is about ten hours per week outside of class for, you
know, work. We shoot for here and as I mentioned, you know, it looks like you’re all
doing real well because you’re sort of below the average point. The truth of the matter is
kind of as the quarter goes on, the assignments tend to get a little bit harder which means
you’ll actually see this curve kind of move down a little bit more into that range. This is
good times to actually see it here right now.
It also hopefully gives you a chance to gage for yourself how you’re doing sort of relative
to expectations. Right? If you’re sort of doing real – you know, hopefully you put in your
comments and it was good software engineering and everything and I’m totally willing to
believe it was and you just wrote your code and it just all worked and it was beautiful
and, sort of, if you’re done in this end, as long as you’re still feeling like you’re
understanding the concepts and you’re plugging away, that’s important and just keep
plugging and you will do just fine, trust me.
There have been times when I have been down here myself and it wasn’t fun when I was
there, but you just keep plugging away and it works out.
And but the important thing is if you were sort of in a particular range, even if you’re in
this range and things just worked but you didn’t understand why, that’s more dangerous
than being in this range and understanding why because all the concepts in this class will
build on top of each other. So make sure you understand the concepts not just that, oh
Karel happened to do the right thing. Yes, he got to the right spot in the middle of the
world, but now he’s just spinning around. That’s fine if he’s just spinning around, he just
got that middle spot, right. We’re kind of a lock, like you just throw in enough
instructions until I kind of did the right thing; that’s not real good understanding and you
want to talk to me, talk to your section leader, talk to the TA to try to clear that up.
All right, so with that said we’re just going to dive in because there’s a question in the
back.
Student:
Is it an honorable violation if you look at someone else’s code once you already
both handed you assignments in and gotten it back?
Instructor (Mehran Sahami)
:Once you’ve gotten it back and it’s already graded, it’s
fine to actually be able to look at someone else’s code because at that point you can just
kind of, you know, share ideas.
All right, any other questions?
All righty, so a couple things to cover real quickly. Last time we talked all about methods
and some more about objects. There’s two things you should know in the programs that
you’re going to be doing, is we talked a little bit about one of them last time in terms of
how to get input from the user. There’re these functions that you should know about.
One is called READ INT and there’s some prompt inside double quotes that you give and
what that does is ask the user basically for an integer and gives you back some value that
you can say, assign to an integer. There’s also a version of this to get doubles, which
surprisingly enough is called RE DOUBLE and has exactly sort of the same properties.
So it’s called RE DOUBLE; it has some string here as it’s parameter or some text here in
its parameter inside double quotes which it displays to the screen and then gets you back
a value which is a double one you can assign to a double. Those are just two things off
the bat that you should know about because that’s how you’re going to get input, at least
for the time being, from the user in a lot of cases.
Now, one thing you want to do once you actually get some input from the user is, you
want to do some manipulation on it like some expressions that we talked about last time.
We talked about some of the different operators like addition, subtraction or unary minus,
it’s the same symbol, multiplication, division and my favorite, the remainder. And so we
talked about all those except for this little guy last time. All of the operators kind of work
the way you would expect them to, okay. And we’ll talk a little bit more about division in
just a second. The interesting thing about division – so all of these things work with both
– or I should say – all of these work with both integers and doubles. The remainder, as we
talked about, only works with integers, right because it doesn’t make sense to have a
remainder when you have real values.
These three guys work exactly the same for integers and double, just the way you would
expect addition, multiplication, all that happy stuff, to work. Division kind of rears its
ugly head because it actually works slightly differently if you’re doing division for
integers versus doubles. Okay? The whole point of that is, if you’re doing a division and
the two arguments that you’re dividing, right if both of these things are integers; in this
case I have integer constant which is what I mean, the values, right. If both of these
integers, what it does is integer division which means it does the division and throws
away any remainder. So what you get back is an integer. So 5 divided by 2 when these
are integers gives you back the Value 2. That little remainder thing is just gone. If you
want to get the remainder you use this guy. Okay?
If either one of these particular values happens to be a real value, like a double, then it
will do real-value division and give you back a real value. So if you happen to divide 5,
even if 5 is an integer, by the Value 2.0 and so it knows it’s a real value because it’s got a
decimal point in it, this will give you back 2.5 as a double and so you can assign that to a
double. Okay?
So if either one of the arguments is a double, you get real-value division; if they’re both
integers, you get back the integer portion. Un huh?
Student:
I’m a little confused about the double; the double is just a real number?
Instructor (Mehran Sahami)
:It’s just a real number. Yes.
So another thing that kind of comes up when you do expressions – yeah, sometimes
you’re taking notes and you just don’t know; it’s like candy raining from the sky.
The other thing to keep in mind is just like arithmetic, sometimes you want operators to
evaluate in different order. There’s an order precedent for how these things actually
evaluate in case you have to have some big honking expression. The order of precedent is
you can have parentheses. Parentheses are the highest precedent. That means you
evaluate everything in parentheses first, then multiplication, division and the remainder
operator have the same level of precedents. And so if you have multiple of them; they’re
evaluated from left to right and then addition and subtraction. Again, if you have
multiple, evaluate left to right.
So it’s just like regular rules of precedent in algebra, which hopefully you’re familiar
with, but to make that concrete let’s say you have some integer X and we say X equals 1
plus 3 times 5 divided by 2. How does that actually evaluate?
Well first of all, we say do we have any parens? No we don’t have any parens. That
would be the highest level of precedence. You can always force something to evaluate
more highly by putting it in parens. So these guys are all at the same level, so we evaluate
left to right. So we come across and we say here, here’s multiplication, we evaluate this
thing as 15, right? We don’t do this addition first; this 3 times 5 becomes 15. Then we
divide it by 2. And, you remember what I just said? Hey, this is an integer, this is an
integer so this is integer division, so 15 divided by 2 in integer division is?
Student:
Seven.
Instructor (Mehran Sahami)
:Seven. Rock on.
So this whole thing is 7 and then we add the 1 to it because addition has the lowest level
of precedence of all the operators here and what we get at the end of the day is that X is
equal to 8.
Okay, hopefully you can see that and if you can’t, I we’ll just tell you X is equal to 8. So
those are the rules of precedent. They’re exactly the same as, hopefully you know from
algebra. Un huh?
Student:
Isn’t it true [inaudible] should be in parentheses?
Instructor (Mehran Sahami)
:It’s nice to clarify; it’s nice to always put in parentheses.
Sometimes you have to put in parentheses to get the right thing to compute and I’ll show
you that in just a second, but it’s nice now to just to fully parenthesize everything. Uh
huh?
Student:
Where do exponents fall in there?
Instructor (Mehran Sahami)
:There is no built-in exponent operator. Okay? So if you’re
sort of like a mat-lab person or something like that there’re functions that we’ll get to
later on that compute exponents but there’s no build-in primitive operation for exponents.
Okay?
So with that said, sometimes there’s times in life when you say hey, but Marilyn, I have
these two integers but I really want to get some value back which is some real value.
Right? So what you can do is – let’s say I have INT X, okay? And let me give X some
initial value; so I can say X equals 5. Then I want to take like half of that and assign it to
some double Y. So if I say double Y equals X divided by 2, you might think, hey, this is a
double right, isn’t it going to do the right thing and give me back 2.5? No. It evaluates the
right hand side first and then assigns it for left hand side. So you have 5 here. That’s an
integer. This 2 is also an integer. It does integer division which means you get 2. Once
you get that 2 it says hey, but that two into a double and says, okay, it’s 2.0. So you’re
like, huh? That was totally weird, but that’s because it was doing integer division. So
what you need to do is, you need to tell it – you need to say, I want you to do real-value
division by temporarily treating one of these things as a double. That’s something that we
refer to at a cast. It’s kind of like you were making a movie and you need to come up
with a cast and you know, you get someone who’s going to like, play Harry Potter for
you, but he doesn’t really wear glasses so you say, for the purpose of being in the cast,
I’m going to put some glasses on you. For this one thing, you’re going to wear glasses
and then we’re going to take them off. It doesn’t change intrinsically who you are, it just
makes you appear different for this one operation.
And the way we do that is we specify the type that we want to cast to in front of the thing
that we want to cast. So we would say DOUBLE X divided by 2. All this means is, take
this thing, which is not normally a double, and for the purpose of this operation, treat it as
though it were a double. It does not change X from being an integer intrinsically; X
remains an integer after this operation, but now it becomes 5.0 and when we do the
division, we do real-value division, we get 2.5 and that’s what goes into Y.
Okay? So that’s what’s called a cast. You can also do that, interestingly enough – let’s
say you did that and now Y is some box somewhere that has the value 2.5 in it and you
say, you know what, I really like the integer part of Y. And so I’m going to have some
integer Z and I’m just going to set that to be equal to what I would get if I cast Y to be an
integer.
Well, if I cast Y to be an integer, you might say, oh Marilyn, does it round? Like do I get
3 because I remember if it’s a .5 we round up, that was always the way it was when life
was good. No. You don’t round up. As a matter of fact, it could be 2.9; it could be
2.9999, you still don’t round up. This is computer science. It’s not necessarily forgiving.
We always round down. You take the integer of a real value, it truncates it; it drops
anything after the decimal point; it doesn’t matter how big it is, sorry. It’s just like the
dollar; we just devalued your currency, right. It just – drop everything after the decimal
point. That’s life in the city. Okay?
So, if we go to the computer for a second, we can put this all together in a little program
and actually see what’s going on. So I wrote a little program that averages two numbers
and it’s running right now. I’ll show you the text for that program over here.
Plik z chomika:
m_r_k
Inne pliki z tego folderu:
ProgrammingMethodology-Lecture01.html
(61 KB)
ProgrammingMethodology-Lecture01.pdf
(63 KB)
ProgrammingMethodology-Lecture11.html
(61 KB)
ProgrammingMethodology-Lecture04.pdf
(61 KB)
ProgrammingMethodology-Lecture03.pdf
(66 KB)
Inne foldery tego chomika:
Zgłoś jeśli
naruszono regulamin