ProgrammingMethodology-Lecture03.pdf

(66 KB) Pobierz
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
Programming Methodology-Lecture03
Instructor (Mehran Sahami) :Couple quick announcements before we dive into things.
There's one handout, which, hopefully, you should have gotten. It will contain the Karel
example we did in class last time, the steeple chase, as well as some more examples that
we're gonna go over this time. But I encourage you to actually pay attention to what we
do in class rather than sort of looking at it on the handout, because one thing that's always
true about programs is once you see the solution to a program, it's easy to lull yourself
into thinking oh, I could have done that, it seems so easy.
But when you're actually sitting there staring at sort of a blank page on your monitor, it's
a lot harder to generate the program. And we'll actually generate a couple of programs
together in class today, so I'd encourage you to sort of take part in that that we do in class,
and then you'll have the actual listing for the code to take home with you so you don't
need to worry about scribbling everything down quickly.
A couple quick announcements, the class website, one more time – every time I look on
Access there's more and more people enrolled in this class, which begins to frighten me
after a while but that means there's people who haven't gotten any of the previous
handouts.
So if you need to know where to get all the previous handouts if you just joined the class,
CS106A.Stanford.edu is the website for the class, you can get all the handouts there, and
there's a whole bunch of stuff you should read right away.
Section sign-ups. How many people have already signed up for a section? Oh, yeah, that's
a good time. If you haven't at this point, you should have looked around at all those hands
that went up and just realized that sections are just filling up to the left and right of you,
literally.
Section sign-ups are open, and if you go to CS106A.Stanford.edu/section, you can sign
up for a section. The section sign-ups close on Sunday at 5:00 p.m. You need to sign up
for a section in addition to enrolling on Access to actually be enrolled in the course –
important thing.
Another thing just real quickly is just for your convenience, I mentioned last time sort of
the conditions of the [inaudible] about the world. For your convenience, we actually
posted them up off a link on the announcements on the CS106A website, so if you're
programming somewhere and you forgot your little Karel book and you're like what are
all those things that Karel can check in the world, like is front is clear, or is facing north
or whatever, you can just go to a link off the website and you'll find them all listed there.
One other thing I would encourage you to do is we haven't been picking up the audio on
questions that are asked, so one of the things I'd encourage you all to do right now is
you'll notice there should be microphones on the fronts or on the backs of the seat in front
of you.
Pick up the microphone right now, just pull it out of the slot and just keep it in your lap.
Don't worry, it won't damage anything on you personally. It's just kind of fun. You can
keep you there, it'll keep you warm, and that way if you ask a question it'll remind you to
use the microphone, all right?
So with that said, I wanna take a quick poll, because last time you got your first
assignment and the download instructions and everything. How many people have
already downloaded Eclipse? Oh, rock on, good time. How many people have gotten the
first assignment filed that you needed to get? And how many people have started on the
first assignment? Wow, I love that. Oh, that just warms the cockles of my heart.
How many people are done with the first assignment? Oh, yeah, just [inaudible] and
you're like oh, yeah, I got Karel to run around, and it's just a good time. So you’ve still
got a week to do it, but that's a good thing to keep in mind.
So a little bit of additional background on Karel before we dive into the real meat of
things, okay? One thing you may have noticed is all the Karel code that you’ve either
started writing, if you're already working on assignment number one or all the code that
we write in class is all in file that ends with dot Java. If you haven't noticed that before,
you'll – now you know, it ends with dot Java.
Because in fact, Karel is all implemented in Java. So one of the things you might be
wondering is hey, I know a little Java; can I use some Java in conjunction with Karel?
And the answer is no. For the purpose of the Karel assignments, you should just use the
constructs that you’ve been shown in class and in the Karel reader, just keep it to those
constructs. That still gives you plenty of stuff to do with Karel, it's a good time, but keep
it to that, and actually starting on Monday we're gonna, like, sort of leave Karel behind
and say bye-bye, Karel, and I'll be, like, bye-bye, I love you, call me. But we'll get into
Java, so if you know Java now, just use the Karel stuff in the Karel assignments that we
actually do.
All right, so with that said, any questions to start off with before we dive into something
new? Um-hm?
Student: How do you stop the program? Like, I had a problem, it goes up into the corner
and just kind of like it says an error message.
Instructor (Mehran Sahami) :And it kept looping there forever?
Student: Yeah, and I tried making, like, an empty program called Stop, but that didn’t
really work.
Instructor (Mehran Sahami) :No, just go up to the little icon – or the little thing in the
top of the window that allows you to close the window and just close the window. Karel
will be okay, he knows how to deal with that.
As a matter of fact, that's – I am so glad you asked that question. It's just a wonderful
thing, because it actually leads into the first topic that I want to talk about, which is
common errors. And so there are some common errors that may come up, and these are
good things to kind of know about. And one of them is the thing you just ran into.
But before we actually talk about that at sort of the level of Karel, I wanna ask you a
question: [inaudible] another one of these strange questions. How many people have
actually ever read the instructions on a bottle of shampoo? A few folks – oh, wow. Man,
that's more than – I was, like, you’ve really gotta get out more often, right?
But I'm surprised that many. And what do those instructions say? Rinse, right, that's
where you rinse your hair, and then you lather, and then you repeat. And if anyone
actually followed these instructions, you would still be in the shower now. As a matter of
fact, you'd be in the shower for the rest of your life. Why? Because you rinse, you lather,
and you repeat, which means you rinse, you later, and you repeat.
And you just keep doing this, and it's like Karel just taking a shower, right? And he's like
you keep telling me to rinse – to linse, rinse and lather together. Rinse, lather, and repeat,
right? And this is what we refer to in program-speak as an infinite loop. Which as you
can guess is a loop that keeps going forever. And this may come up in your Karel
programs. Here is an example of an infinite loop. You might be standing somewhere and
you might say while front is clear.
Oh, well, if your front is clear, I want you to turn left, because, you know, I have this idea
that eventually you'll turn and you'll be facing something and your front won't be clear
anymore. And that might be a great idea to have, but what happens is if here's our friend
Karel, happens to be standing in the world and guess what? There's no walls around him.
What does he do? He rinse, lathers, and repeats, right?
He just keeps sitting there, turning left on this corner, and his front will never become
blocked so he'll never get out of that loop, okay? That's one of the places where the
syntax of the program is perfectly valid, but in terms of our intention for what we wanted
the program to do, it didn’t work as intended.
This is the more common thing that you're actually going to need to deal with fixing in
your programs as opposed to syntax-type problems. As a matter of fact, I won't mention
the name of the company but there was a company years and years ago that put out this
advertisement that was, like, our processors are so fast they can execute an infinite loop
in 2.5 seconds.
And you kind of looked at that and you were like ah, ha, ha, funny computer scientists.
But the thing that was actually funny about it was the company that put out these chips,
the chips actually had a flaw in them where if they executed things really fast they would
warm up and then they would melt. So in fact, they would execute an infinite loop in 2.5
seconds, and then the processor would catch fire and it would stop.
So sometimes, that happens. Hopefully, that won't happen to your machine. Don't worry,
even if you get Karel going real fast, it's okay. If your processor melts, come talk to me,
it's never happened. But it'll make for a wonderful story in class, and it'll cost you about
$1,800.00. Fine.
So another kind of error that comes up fairly often also has a little affectionate name to it.
And this is something that we refer to in programming speak as an off-by-one bug. Or if
you take the first letter of that, it becomes an OBOB. And sometimes you'll hear people
refer to OBOB, and if you're name is Robert they're not saying, like, oh, Bob. No, that's
just referring to the off-by-one bug.
And what that means is you forgot to do something one more time than you really needed
to, even though logically it didn’t seem like you needed to. So the best way to understand
that is with a little concrete example. So let me actually give you a concrete example by
going over to the computer.
So here's Karel sitting in his world, and one of the things we might wanna do with Karel
– whoa – Karel in his world is to say that we actually want Karel to lay down a line of
beepers, right? Just whatever row he's in, just lay down a line of beepers till you hit the
wall. And that's a fairly common thing that you might wanna do, okay?
Now one way we might be inclined to actually do this if we were to write some code is to
say something like hey, I'm gonna, you know, write a new class, I'll call it fill row that
extends super Karel. I import all the Karel stuff, so I'm doing all the crazy stuff that
[inaudible] told me to do, and hey, I want to keep putting down beepers until I fill the
row. So wouldn’t it make sense to say while front is clear, right? There's no wall in front
of me. Put down a beeper and move to the next corner.
And as long as my front is clear I'll put down another beeper and keep taking another step
and doing this over and over, and this seems like – hey, that seems like reasonable code
to lay down a line of beepers. What happens when I do this? Yeah, so let's actually run it.
Let's bring up our buddy Karel. Where'd you go, Karel? Here you are. And run that
program. And so what Karel does, he puts down a beeper, his front is clear, so he moves,
puts down a beeper, front is clear, puts down a beeper, front is clear, puts down a beeper,
front is clear.
He moves here, front is no longer clear. Before he puts down the beeper, he says hey, my
front isn’t clear anymore, I stop. He's off by one because there's one more thing that he
needed to do, which was to put down another beeper in this last spot. This is another very
common logical error that comes up – you're off by one in terms of what you're thinking,
and so lo and behold if we go into the program over here and we look at this comment,
and I'll tell you about comments in just a second, it says this is buggy, it's off by one. You
need to add a final put beeper.
And so what we do is we say hey, after you got out of your loop, your front is no longer
clear, but you haven't put down a beeper on this corner yet. Put down a beeper, right?
And if we save that puppy off and we close this off and just so we know that it's in fact
running, doo doo doo, we run our little program and rock on, all right? No more off-by-
one bug, life is good. So any questions about the OBOB or the off-by-one bug? That's
another common thing.
These are just a couple of common things I wanna show you, so, you if they come up in
your programs you don't feel like oh, you're out there adrift, all alone. Like, these have
been done millions of times by other very qualified programmers. Don't worry, it happens
to everyone.
All right, so one of the things I just mentioned is this thing called the comment. This
thing that's up here in green. What's that all about? So as I mentioned to you last time,
one of the key software engineering principles is to write programs that are
understandable by people, not just by machines.
And so what a comment is is a way of being able to put something in your program that
another human being reading your program can actually read and understand, and it
actually has no impact on the execution of the program at all.
And so the way the comment works is it starts off with a slash and a star, and now
everything you put – it can even span multiple lines – until you put a star and a slash is a
comment. It's there just for the person to read, and it doesn’t affect the program at all.
And so what I would encourage you to do as part of good programming style is all of
your programs up at the top should have a comment that says what the name of the file is
and has a little bit of an explanation about what your program actually does.
And if you're wondering how much of an explanation or how in-depth it should be, be it
as in-depth as you want, but in the handout you got today you see examples of a bunch of
programs, and they're all commented, so you can see examples of comments in there.
There is also shorthand you can do. There is a comment that's just slash, slash, and that
just means everything on the remainder of this line is a comment. So it actually has no
close, in some sense, for the comment like this does explicitly. Everything else to the rest
of the line, when you hit a return, that's how it knows it's done with the comment.
So sometimes if you wanna have a one-liner comment somewhere, you can just put a
slash, slash.
Student: [Inaudible.]
Instructor (Mehran Sahami) :Yeah, when you get assigned to a different section leader,
you'll actually put your assignments in with your name and your section leader, and that's
how it'll get differentiated. So wait until you get your first section, and then you'll see
how submission stuff works. Um-hm?
Zgłoś jeśli naruszono regulamin