ProgrammingMethodology-Lecture02.pdf
(
60 KB
)
Pobierz
Programming Methodology-Lecture02
Instructor (Mehran Sahami)
:Alrighty, welcome back to CS106A. If you're stuck in the
back, just come on down, have a seat. Originally, I thought maybe we would have
slightly fewer people today than last time, but that appears not to be the case. So while
we're waiting, everyone loves babies, so I decided to put – that's Karel, the Robot, the
early days.
No, this actually – my son, and yeah, I know. He's a little bit older now but, like, he's got
these little robot pajamas and he runs around all the time. So I'm like, oh, it's Karel, The
Robot, and my wife looks at me like, it's your son. But that's a whole different issue.
Anyway, a few administrative announcements before we start. A couple things, there are
four more handouts today, just because we like consistency. Four last time, there's four
this time. They're all in the back. If you didn't already pick them up, you can pick them
up after class, but they have all the information about downloading Eclipse, which is the
environment that you're going to use for programming in this class, both for Karel and for
Java.
There is also a special handout in there just on using Karel, so it talks about how Karel
works in the Eclipse environment, and so you can get all set up with that. And so after
today, you'll know how Karel works and you'll have the environment to use it. So
surprisingly enough, you also get your first assignment today, which is actually in two
parts.
So the assignment, the real assignment is the programming part, which is due on Friday
of next week, October 5th. There is also an email part to it, where the email part is, we
ask all of you kind folks to send an email to Ben, the head TA, myself, and also your
section leader. So Ben and I get a whole bunch of mail, your section leader will hopefully
get a little bit less mail.
But you won't actually know your section leader until after you go to your first section
next week, which is why that part of the assignment is not actually due until basically one
minute before midnight on Sunday. So I didn't make it midnight, just because then there's
always confusion, midnight, which night? So 11:59 p.m. on Sunday, October 7th is when
you should send that email.
That's not really the critical part of the assignment. The critical part of the assignment is
the programming problems, which are actually all due in class on Friday. Okay, and then
there's one last assignment, or one last handout, which is about how you actually submit
your work in this class. You'll submit your work both electronically and in hard copy.
The electronic copy, so that your section leader can run it and verify it. The hard copy is
so that they can actually write comments on it, and then you'll do interactive grading with
them as well.
A couple other quick announcements. The website, in case you weren't here on Monday
and you don't know what handouts I'm talking about, and you don't know where to get
them, go to the class website, cs106A.stanford.edu. There will be electronic copies in
PDF format of all the handouts there, so you can get caught up if today is your first day.
You also need to sign up for a section. As we mentioned last time, section signups start
tomorrow, 5:00 p.m. Sign up early if you want to have the most flexibility in terms of
time, because look around, there's a whole bunch of people in this class. It's going to fill
up quickly. So if you have constraints, sign up quickly, and the place you sign up is CS
198, not CS 106A, but CS198.stanford.edu/section. The 198 folks are the folks who
lovingly run all of the sections and coordinate the whole program.
Last, but not least, just a word on the readings that I didn't mention last time. On the
syllabus, you'll notice that pretty much for every day, or most days in the quarter, there is
some sort of reading associated with it. That's the reading assignment that you should
have done by that day's lecture, because that's what we will cover in that day's lecture.
So for today it should be the first three chapters of the Karel book, or the course reader.
And if you're like, oh my God, I'm already three chapters behind, don't worry, it's like 20
pages or something. It's pretty lightweight, but you should do the readings by that time in
class. So any questions about anything we covered last time, which is mostly logistics, or
any of the sort of administrivia?
All righty, then let's get started on the real content. So you remember from the time
before, we talked a little bit about Karel, the Robot, and here is Karel in that world that I
showed you before. And there were avenues that run north-south, and streets that run
east-west, and little beepers in the world. And Karen can face different directions, and
there's walls.
And so now, we want to think about how do we actually program Karel. How do we get
this little guy to do something interesting in the world? Okay, and it turns out there are
four commands that Karel understands, okay, and those are pretty straightforward.
So here they are. You're going to get all of Karel's vocabulary in, like, one minute.
There's a command called move, and move basically moves Karel one spot forward in the
direction he's facing. So if he's on corner one, one, and he moves one spot forward, he's
facing east. So he'll move over to two, one, basically. So one corner forward in the
direction he's facing.
Karel also started being a good democrat, knows how to turn left. So he turns left. This is
a lower case T, this is an upper case L. So turning left changes his direction by 90 degrees
in the left hand of whichever way he's facing. So if he's facing east now, and he turns left,
he will be facing north. And then he can turn left again and he'll be facing west.
Okay, now, question?
Student:
[Inaudible]?
Instructor (Mehran Sahami)
:There is no space here. These are all one word. Good
question, and I love it when you make it easy. All right, so besides turning left, there's
also B-person Karel's world, and if he couldn't do anything with beepers, they wouldn't
be interesting. So he can pick up beepers, which interestingly enough is called
pickBeeper, again lower case P, upper case B, all one word, and there is putBeeper.
And what these do is pickBeeper, if Karel happens to be on a corner that has a Beeper on
it, he picks up that Beeper, and stuffs it in his Beeper bag, in which case the count in his
Beeper bag goes up by one. Or if it's infinite, it remains infinite, because he's got a real
big Beeper bag, and sometimes he can infinitely many Beepers in there. It's just – we can
talk about the infinity of Karel's Beeper bags some other time, but if you're interested, it's
fascinating.
And he can also putBeeper, which means he can go to a corner and be like, "Hey, I'm just
feeling happy. I'm going to put a Beeper down, say, on that corner right there." Except
you can't throw the beepers, right. Karel's kind of limited. He's got, you know, if you look
at him he's got no arms. He's got legs. No arms, so basically all he can do is he sort of
like drops the little Beeper where he's at. So this puts it on the corner that he's at.
And these things are what we refer to as methods. Okay, methods are basically some
instruction that we can call, okay, or use, like move, or turn left, or pickBeeper, or
putBeeper. And what we say is Karel responds to this method. What we're doing is we're
invoking, or we're calling a particular method on Karel, and he takes some action, which
is basically what that method specifies to do.
Okay, so if we kind of think about this program, or if we kind of think about this world,
maybe we want Karel to do something. So here's the initial configuration of the world.
Maybe what we want Karel to do is pick up this beeper, drop it off at this corner, and end
up at this corner facing to the east. And you can think about how we might do that with
some of the instructions we have.
So what we want the final configuration of the world to look like, and I'll just put Karel at
blinding speed. You can actually control Karel's speed here with this little slider. So I'm
going to just show you the end configuration by running Karel so fast you can't even see
it. It's just – he's blindingly fast. He's just that good, okay.
And so that's what we want to get to. Notice he's still facing east, but he's picked up that
beeper that was on that corner at two, one, and moved it over to the corner at four, two.
Okay, and so how might we do this? Right, so one thing we could consider is, what was
the initial state of the world again?
So I'm just going to run this program again, and here I have a clip set up, which is your
handout number five explains how to get this. But if I want to run a program, there is
these two little running person icons, or as you might notice, we even have our own menu
in Eclipse, because Stanford's just that much fun, okay.
So under the Stanford menu, we have these two options for run. Import project is
explained in the handout, is what you'll use. We'll give you some initial stuff for Karel
that you'll start with, and you'll use import project to get it into Eclipse. But once it's
there, we can run it, and so what we're going to do is say run this particular Karel
program. So it's get – and we'll go through all this – it's get and go.
And this is our first Karel program that we want to run, and it's sort of, here's the initial
world again, all big and happy. So I'm going to shrink this down a little bit, so we can see
it over here on the side, while we think about what commands Karel would need to
execute to kind of get to that final state we just talked about.
And over here, happily enough, let me resize a little bit, is our first file, which is empty
right now, that we're going to write our first Karel program. So what commands might we
consider Karel actually doing from the list you have here, to sort of affect what we want
to happen in the world?
Student:
[Inaudible].
Instructor (Mehran Sahami)
:Yeah, so we want to move. Okay, and then what do we
want to do?
Student:
[Inaudible].
Instructor (Mehran Sahami)
:PickBeeper, then move. Notice at that point we could
have actually done something else. This is part of the art of programming. There's
actually many ways to solve the problem. We're just going to happen to pick one. So
we've moved. We've picked the beeper. We've moved again, and now what do we do?
Student:
Go to the left.
Instructor (Mehran Sahami)
:Turn left. What happens if we turn left?
Student:
[Inaudible].
Instructor (Mehran Sahami)
:Right, then what are we going to do?
Student:
Move.
Instructor (Mehran Sahami)
:Move. And now what do we want?
Student:
[Inaudible].
Instructor (Mehran Sahami)
:Yeah, conceptually what we'd like to do is turn right,
right? At this point, Karel's like, "No, man. I'm just, you know, I'm left wing. What can I
do? I turn left. That's what I do." And you kind of think about it, and you say, "That's all
right, because really the world is just one big spectrum, and if you go around far enough
to one side, you end up on the other." So if we make you turn left three times, that's
equivalent to essentially turning 90 degrees to the right.
So here, we turn left, turn left, and turn left. All right, now what do we do?
Student:
Move.
Instructor (Mehran Sahami)
:Move. So we sort of go up the step. PutBeeper and move
to the last spot. Now, at this point we'd like to think, oh good times, we can just run this
and it's a Karel program, and life is happy. In fact, this is not a valid Karel program. What
this is, is an algorithm.
This is a recipe for doing something, and we'll talk in a lot more detail about different
algorithms on Friday. But the way you can think of distinguishing between an algorithm
and a program is an algorithm is essentially the recipe for doing something. The program
is something that is valid syntactically according to the rules of the language.
And so Karel actually has some specific rules to its language that we have to apply to
these statements to make them look a little bit different, so they follow the valid syntax of
Karel. And that's what we're going to do now. We're just going to kind of go through this
step by step, okay.
So one of the things we'd like to be able to do is, first of all, we want these to be valid
commands. Right now, they're not valid commands. To turn a command into a valid
command, or what we would refer to as a method call in Karel, after the name of the
command we put an open paren and a close paren, and then a semicolon.
Okay, so move, open paren, close paren, semicolon is actually the valid move command,
or move method invocation for Karel, and we do that for all these things. So to save a
little bit of time, I'm just going to copy this and just go through and paste until the cows
come home. Let's do a little paste there. Oh, it didn't copy. All right, let's try that again.
We do a little paste, a little paste.
So we're going to go and turn all of these into valid Karel commands. Okay, so now you
might think, okay they're all valid Karel commands. Are we ready to actually run this
Karel program? And it turns out no, we're not yet ready to run – this is not yet a valid
Karel program.
What makes it a valid Karel program is we need a few more things. One of those things
is, we need to tell Karel where to start running. And you look at this and you're like, what
does that mean? Like, doesn't he just start at the top? Does Karel start somewhere in the
middle?
Plik z chomika:
m_r_k
Inne pliki z tego folderu:
code.zip
(296 KB)
wy1.pdf
(284 KB)
CSS.XHTML.video.kurs.rar
(582334 KB)
2008. Wrox Professional CSS Cascading Style Sheets for Web Design 2nd Edition.pdf
(18144 KB)
0131473816_book.pdf
(6963 KB)
Inne foldery tego chomika:
Pliki dostępne do 01.06.2025
Pliki dostępne do 19.01.2025
Android Programming
c#
c_c++
Zgłoś jeśli
naruszono regulamin