ProgrammingMethodology-Lecture07.pdf
(
62 KB
)
Pobierz
Programming Methodology-Lecture07
Instructor (Mehran Sahami)
:Alrighty. Welcome back to yet another fun-filled exciting
day of CS106a. So a couple quick announcements before we start – first announcement,
there are two handouts in the back. Hopefully, you already got them. If you didn’t already
get them, you can get them later.
Second announcement, just a little reminder, microphones, if you go ahead and pick up
those microphones right now and keep them in your lap. They’re warm. They’re cozy.
They’re fun. Use them to ask questions. Just because, looking at the videos, got a, kind
of, general reminder that the questions aren’t actually coming through. Whoa, or I can
just breath real heavily into my microphone.
All right. So one real quick point before we dive into the main meat of the lecture, it’s
just a clarification on something we did last time called The Cast. So remember last time
where we had a cast, which was this thing that allowed us to say treat this one data point,
or this one data item, this one variable as a different type for one particular operation.
And so last time I told you you could actually do something like int X equals – let’s say
we had some double up here, like YY, and maybe Y got some value like 3.5, and I said
you could do a cast like this, and we put Y here, right? And so that’ll take Y. It’ll truncate
it, right? It drops everything out of the decimal point and assigns it to an int.
And I, sort of, said, misspeaking, actually, that you could drop this part. It turns out that
in Java, any time you do a cast of something, that loses information. Like, when you go
from a double to an integer because it truncates, it drops everything after the decimal
point, you have to put in the cast, so you have to make this explicit.
If you go the other way around where you’re not losing information, like, if you were to
have Y equals X at some point where you know that X is an int and Y is a double, you’re
not gonna lose information by assigning an int to a double. So there you’re okay.
You can do the explicit cast if you want, and actually I would encourage you, actually, to
use the explicit cast, but in this case, you don’t need it. In this case over here you actually
do. So when you lose information, you need to make an explicit cast. For most of you, it
probably won’t affect your day-to-day life, but that’s just something you should know.
Alrighty. So any questions about anything we’ve done up to now?
We’re just gonna, kind of, press ahead with something that we’ve talked about last time.
Remember last time we’re talking about our friend the while loop? And the while loop
was something that you used when you don’t know how many times you’re gonna do
something. This is what we refer to as an indefinite loop – not an infinite loop, right? It’s
important that you have “de” in there. Infinite loop keeps going forever, but an indefinite
loop is one that when it starts out, you don’t know how many times you’re gonna do
something. You’re just gonna do it some number of times.
Well, it turns out there’s times in life where you’re gonna do something some number of
times. You don’t know how many, but you figure you probably want to do it at least
once, okay? This was, kind of, the case of my brother going to college. Like, he figured,
“I’m gonna go to college.” But when he got there, he didn’t know how many degrees he
was gonna get, and he just keep going and going, and five degrees later, he was, kind of,
like, “Okay. I think I’m done now.” So that was his while loop, right? He didn’t know
how many times, but he knew he wanted to do it at least once.
Now, the interesting thing is that in programming, this actually happens fairly commonly.
So I’ll show you a quick example of this. If we go over to the computer, there’s
something we call the loop and a half because in some sense, you want to half of the loop
at least once, okay? Think about it this way. Let’s say we want to take some program that
reads in a bunch of numbers from the user until the user enters zero to stop, and it, kind
of, computes the sum of all those numbers, so it adds them all together.
So we might have, basically, some sentinel value, right? We’re gonna use our constant,
our little friend last time that we talked about, the constant, to define what is that value
that the user enters in which point they want to stop entering values. This, is in
programming speak, is often referred to as a sentinel. It’s, kind of, the last thing I’m
gonna give you that tells you I’m done; I’m not gonna enter any more values. But until I
give you that sentinel, I’m gonna give you a bunch of values that I want you to add
together, for example.
So that sentinel we might define as a constant, and so we could have some loop that says
well, if I’m gonna keep track of some total or some sum of all the values you enter, that
home’s gonna start at zero. I’m gonna ask you for some value, so I’m gonna read in some
integer and store it in the little box named Val, right? So I just declare it a variable, and
then I’m gonna have a loop, and what my loop says is if the value you gave me is not the
sentinel, that means you don’t want to stop yet, right?
So I have a logical condition in here that’s checking against the constant, and that’s a
perfectly fine thing to do. So we’re taking a lot of those concepts you saw last time and
just, kind of, sticking them all together into one bigger program. If I haven’t seen that
sentinel yet, then take the value you just gave me, and add it to my total, and store it back
in the total using the little plus equals shorthand you saw last time, okay?
Then what I need to do is after I’ve done this, I need to ask you for a value again. So I say
value equals read it. Notice I already declared the value up here, right? I already declared
this variable up here, and this guy’s lifetime is until it gets to the closing brace at the level
at which it’s declared. It’s not declared inside the while loop, so this closing brace
doesn’t make it go away. This closing brace makes it go away, which means it’s actually
alive the whole time in the while loop, and it’s perfectly fine.
So val, I read in some other value from the user, and, again, I go back through this loop.
Did they give me zero? No, add it in. Did they give me, zero? No, add it in. And when
they give me zero, then I say, oh, the value is equal to the sentinel, so this test becomes
false, and I come down here, and I write out the total as total, and you might say, oh,
okay, Mehran, that’s fine. What’s wrong with that?
Well, in computer science or actually in computer programming, we really hate
duplicated code. If we can avoid duplicating code, even if it’s one line, we generally try
to do it, and you can see there’s a duplication here, right? What’s happening is there’s
something we want it to do at least once, which was to ask the user for some value, but
we need to keep doing that over and over in the loop, even after that first time. So we,
kind of, run into this conundrum where we say are we gonna have to repeat code?
And there’s a way around this. So the way around this is we pop out that piece of code,
and we’re gonna pop in our friend the loop and a half, okay? And so what the loop and a
half says, the first thing that looks funky about it is we say while true, and you see that
and you go, “Oh, my God.” Right? Any time you see ‘while true’ you’re thinking bad
times, right? “I’m never gonna break out of the loop. Every time I come here and
evaluate the condition, it’s always true. Isn’t this an infinite loop, Mehran?”
And it would be except for one caveat, and here’s the caveat. We’re gonna ask the user
for a value, right? Notice we declared the int val inside here. We could’ve declared it
outside if we wanted to, as long as we didn’t do the read into outside. We could’ve just
said int val and declared it out here, but we’re only gonna use it inside the loop, so we’re
just gonna declare it here.
We read an integer from the user. We ask if the value is the sentinel. If it is, we break.
What a break statement does is it will break you out of your closest encompassing loop.
What does that mean? It means it finds whatever loop is the loop you’re currently in and
jumps you out of just that loop. So if you hit a break statement, it will jump out to,
essentially, the place right after the closing brace for that loop and keep executing.
So what it allows you to do, essentially, is to check the condition to break out of the loop
in the middle of a loop, which is, kind of, a funky thing, rather than at the very beginning
or every time you iterate through. So we get the value from the user. If the value is the
sentinel, we break, which means we never execute this line or the rest of the loop. We
jump down here. If it is not the sentinel, then we add it to the total. While is true, so we
execute the loop again and go and read another value from the user, right?
So notice that this read int line is no longer repeated. We only do it once, but because of
the structure of this, we’re always guaranteed that this portion of the loop up here is
always executed at least once because we had a while true, and we’re checking the
condition to break out of the loop in the middle, okay? Now, one caveat with that is in
terms of programming style, it’s generally a bad time to have multiple of these kind of
breaks inside a loop because it makes it very difficult for a programmer to figure out what
condition has to be true to break out of the loop.
If there’s only place you see a break, then there’s only one condition you need to check to
break out of the loop. If you have multiple statements, the multiple if, you know, blah-
blah-blah break in your loop, that means the programmer has to keep track of multiple
possible places you could’ve broken out of the loop in their head, and that gets pretty
dicey. So you generally want to avoid that when you’re doing it, okay? So any questions
about the loop and a half? Uh huh.
Student:
Is it okay to redeclare a variable in a loop like that over and over again?
Instructor (Mehran Sahami)
:Yeah, it’s fine to actually declare that variable inside the
loop. You don’t need to worry about, like, efficiency issues or anything like that.
So let me show this to you in action in a larger program, right? So here’s that loop and a
half in the context of an actual run method. It prints something on the screen. It says total
to be equal to zero, and then it comes here, while true will always execute, right, because
the condition’s true.
So it asks the user for some value. We enter one; one is not equal to the sentinel. So we
add that to the total, and you can see here it’s keep track of value and total. Here’s just
my two declared variables and the little boxes for them, and they get updated as I go
along.
So while it’s still true, I read another value. Let’s say the user gives me two. I add it to
the total, which is now three, and I come back up here. I’ll go through this a couple more
times, okay? User gives me zero.
Now, notice when the user gave me zero here, I hit the break because my value is zero,
and that’s equal to the sentinel. So it says, hey, the if part is true, so do the break, and it
immediately jumps out to the end of a loop. It did not do – even though it [inaudible].
So I feel a little like Roger Daltrey when I’m doing this. Anyone know Roger Daltrey,
lead singer of The Who? Go look it up on Wikipedia, and watch the video. Man, I’m
feeling old. All right. And then it says the total is, so this total plus equals value. The
value just doesn’t get added that last time, even though it would’ve been a zero and
wouldn’t affect the things.
It would be different, right, if we said the sentinel equaled a negative one. Then you
would actually see that the total didn’t get one subtracted from it, and we could’ve set the
sentinel to whatever we wanted, but in this case, we used zero, okay? So that’s our little
funkiness, and it writes out the total of zero for actually doing the loop and a half.
Now, in terms of doing all this stuff, okay, you might think, “Well, hey, Mehran, you
showed me about the for loop last time. You showed me about the while loop. It turns out
I can write the same loop in equivalent ways using both of them.” So you remember the
for has the initialization, the test, and the step initialization happens once. Then the test
happens every time through the loop, and then the step happens, kind of, every time at the
end of the loop?
That would be exactly equivalent if I wrote it like this. The int happens once before the
loop. I have some while loop that checks the same test. I have the same set of statements
that would be in the for loop body, and I do the equivalent of the step in the for loop at
the very end of the loop. So these two forms are exactly equivalent to each other, and you
might say, “Hey, Mehran, if they’re exactly equivalent to each other, why do I have these
two loops, and when do I use one versus the other?”
Well, for loops, you want to think about what we refer to as Definite Iteration. That
means we generally know how many times we want to do something, and that number of
times we want to do something is how we, sort of, count in our test. When we don’t know
how many times we actually want to do something, that’s an indefinite loop, or indefinite
iteration as I just talked about, that’s when we use a while loop, when we generally don’t
know how many times we’re gonna do something, okay?
So that, kind of, why we give you both forms, and most programming languages actually
have both forms, but to a programmer, it’s more clear between seeing the difference
between for and a while, it’s, kind of, the meaning of it, right? Are you counting up to
something some number of times, or are you just gonna do something until some
condition is true? That’s, kind of, the differentiator there. So any questions about loops?
So let’s put this all together in the context of a bigger program, okay? So I will show you
a bigger program here. So we’ll come over here. Here’s a little program; let me just run it
for you real quickly. Come on. So we run along, and what this program’s gonna do is
draw a checkerboard, right? Just like you’re used to in the game of checkers or chess, a
little checkerboard on the screen.
So this is gonna be a graphics program. It’s gonna draw a bunch of G rects, which are just
rectangles – in this case, they’re gonna be squares, to draw a little checkerboard, and you
should think, “Huh, this might have some similarities to, maybe, some of the stuff you
were doing in your current assignment. Yeah, so let’s go through this code and see what’s
actually going on, and we can put a bunch of things we learned together like constants,
and loops, and blah-blah-blah.
So what’s going on over here is, first of all, we start off with a couple constants, right?
And the constants we’re gonna have tell us how many rows and columns are gonna be in
our checkerboard. We want to draw a standard checkerboard, which is an 8x8
checkerboard. So we have private static final int, end rows is eight, and private static final
int, end columns is also eight.
Notice that these two constants are not defined inside a method. They’re defined inside
the class but not inside a particular method, which is generally where your constants are
gonna be defined, right, in a class but not inside a particular method, and then for the run
part of the program, first thing we need to know is we need to figure out – because we
want this thing to be general, right? No matter how big our window is, we want to draw a
nice little checkerboard.
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