ProgrammingMethodology-Lecture09.pdf

(58 KB) Pobierz
Programming Methodology-Lecture09
Instructor (Mehran Sahami): Alrighty, welcome back. Wow, that's pretty loud.
Welcome back to CS106a. I hope I didn't just shatter your eardrums. And thanks for
making it out in the rain. I think the rain might have stopped a few people from making it
today. But, actually, today is one of the most important lectures of the whole quarter, so
it's too bad that it would happen to happen that way.
So a couple of quick announcements: One is that there's two handouts, one on coding
style, and one on the use of variables. I'd encourage you to read both of them because
they are both extremely critical concepts in this class. There are some things that – it's
like, "Yeah, it's not so important." These two are really important so please make sure to
read the handout.
There is a couple of new concepts I want to show you briefly in the beginning, and then
we're gonna begin to put a whole bunch of things together. So so far in class, what we've
done is, when you saw Caroline we did a bunch of stuff with Java so far, we've shown
you a bunch of bits and pieces of things and put some larger pieces together. And today's
the day where it really all comes together, and a whole bunch of stuff that we've talked
about before, where I said, "Oh, later on this'll make more sense," today's the day when
hopefully that will all make sense.
So first thing to cover, just very briefly, is something called, "Strings." And if you've
been reading along in the book you've seen some references to Strings. We haven't talked
about them in class, and now it's time that we spent a little bit of time talking about
Strings. In about another week and a half we'll spend a whole bunch of time talking about
Strings, but you should at least get a little introduction to them.
So what is a String? A String is just a type, like we had ints and we had doubles, and
those boolean, stuff like that. String, except String starts with a capital "S," it's not lower
case, is actually a type, and so we can declare variables of this type; like, we can have
some variable called "str," which is a String.
And what does a String hold? What a String really sort of means is a string of characters,
it's a piece of text is all a String is. And the way we usually denote a String is its inside
double-quotes, or the " " that we're used to. So we might have some String, and initialize
it by setting it equal to, "Hello, space, there." So it's perfectly fine to have spaces and
other kinds of characters inside of a String. And, basically, it's just a piece of text that
says, "Hello there." It's just one variable that happens to have this value, "Hello there."
And so the important thing is it's text enclosed in double quotes.
And you can think of the text that you actually assign to the String. You can use things on
it like concatenation, just like you did in a print lin, when you actually want to print some
stuff on the screen, we use the plus operation to concatenate pieces of text together. Well,
we can do that with Strings.
So we could have some String, we'll call this String, "name." And we might set name =
"Bob." And we might have some integer age, which is just set to "20." And then we
might have some other String, so some other String s, which we want to set to be
something like "name:+," and then whatever the name actually is in this other String, so
name. What that'll do is concatenate the word Bob onto name:. And then maybe we want
to concatenate onto that something like age, and then concatenate onto that the value of
the variable age.
And so what we'll get here is we'll get something that in the end s is just some variable,
so it's just some box that contains something. And what it will contain is "name:" then
concatenated on with whatever name evaluated to. Well, name:, when we look it up in the
box is just Bob, so we'll say, "name: Bob." And then concatenate onto space "age:," and
then the value of whatever age was, which is "20." Just like this would work in a print lin,
if you were to say print line, and have parens around this whole thing, you would expect
it to print this out. You could actually just assign that to a String. So same sort of
concatenation works.
And then you might wonder, how do you assign these Strings or get these Strings? You
can assign what we refer to as "literal," which is an actual value to the String. You can
build up a String by concatenating a bunch of other stuff together.
Sometimes you want to read in some text from the user, and there's a little method called
"readLine," just like read int and read double, where you give it some piece of text but it's
gonna display a question mark because it's gonna ask for something. And what it gives
you back is basically the whole line that the user typed in. So it's not just looking for an
int or a double, but if they type in a whole line of text, including characters, it gives you
that back as a String, that's what it returns, and you can assign it somewhere. Like some
variable we might call "line," that's a type String.
So you should just, at this level, see what a String is, understand it, be able to get them
from users, in about a week and a half we'll do a whole bunch of funky things with
Strings and get into the nitty-gritty. But at this point, this is just to give you a little bit of
an idea as to what they are because we're gonna actually use them later on today. And
there are some references in the book that hopefully will make a little bit more sense, but
up until now, we didn't actually need them. So we just kind of deferred the discussion.
Is there any questions about String? All right.
So the next big thing, here is the real big topic that we're gonna cover today, is writing
our own "classes." So this whole time, when we've been writing classes, so we would
write something like, "My Program," and it would extend Console Program, and we'd
have all of our methods and stuff inside of that class for My Program. Wouldn't it be kind
of interesting to actually have multiple classes. Because in the days of yore, we talked
about a Java program as really just comprised of multiple classes. So today's the day
where you learn how to write other classes other than just the main program that you're
actually writing.
So what's the general form for actually writing a class. The idea is we say "public." For
right now all of our classes are gonna be public. The word class, and then we give the
name of the class. So this should be familiar to you, just like when we wrote programs
that extended Console Program, we had some name here, I'll put an underline to indicate
that that's not actually a literal name you just give it a name, and then potentially you
could also add onto this, "extends," and then some super class.
And so what you're saying is that you're creating a new class with this name that
potentially extends some other class. And then inside you have some open brace, you
have the body inside here, so this could be all your methods. It could also be variables
that are defined, as we talked about before, various instance variables or ivar's in the
class, and then the close of the class.
Now, this part over here, I'll put sort of in dashed parens because it's optional. You
actually don't have to extend something as part of a class. If you don't extend something
as part of a class, what Java sort of says is the default is everything in my universe, at
least for the time being, are classes. So if you don't tell me that you're extending some
existing class, there are some default class called "object," which by default you will
extend if you don't say extend some other existing class. So everything at the end of the
day in Java's world sort of moves up the chain, or the hierarchy that we talked about a
couple weeks ago, and ends up an object. So everything at the end of the day really is an
object, and if you're not extending something in particular then you're just directly an
object.
So if you want to write this class, what you end up doing is you want to create a new file.
And I'll show you how to do that in Eclipse in just a second. But, basically, that's why I
put it in the box here, the name of the file should be – we kind of define it to be whatever
the class name was and then a .java at the end. And the .java let's us know that this is a
Java program. So if this thing over here, this class, happens to be class Bob, we would
have some file Bob.java that would be where all the code that we write for the class Bob
actually lives.
And the stuff that's gonna be inside here is all the stuff like the methods, and variables,
and constants, which are just a particular form of variable, they're just a final variable so
they get no other value. And the stuff that you have inside here, the methods and the
variables, have certain visibility associated with them, as we talked about before. These
things can either be public or they can be private.
And the difference between those two, hopefully it'll make a little bit more sense, is
methods or variables that are public are what we refer to as being "exported out of the
class." So if you write a whole bunch of classes, let's say we write three classes. And so I
have my class Bob, and Bob might have in it some public method. Well, if I have some
other class Mary, Mary can actually call the public methods of Bob. So if it's public
anyone can access them.
If they're private, they can only be accessed by other methods inside of the class. So if I
have some method inside Bob, or some variable inside Bob that's private, the only things
that can refer to it are other methods inside Bob. If I have some other class Mary, the
methods in Mary cannot refer to the private members, or the private elements of Bob. So
that's the important thing to keep in mind.
There's a couple other notions. There's a notion known as "protected." And we'll talk
about that potentially toward the end of class. For right now, you don't need to worry
about it. The thing you do want to keep in mind is that most things in classes will actually
be private unless there's a good reason to make them public. In some sense think about
yourself, you want to have some notion of privacy.
You don't want to just broadcast to the world or let the world be able to come in, and say,
"Hey, you have some variable here which is, like, your age," and someone else is gonna
go and modify your age. That would kind of bother you, right? And so most things you
keep private unless there is a good reason to actually make them public. And so the run
method that we talked about so far, I actually sort of told you, it needs to be public. Most
things will be private. And I'll show you some examples of classes where we look at
things that are private versus public.
Is there any question about that, what public or private actually means or sort of the
general notion of how we would define the class? Hopefully, that's familiar to you
because you've seen it before.
Um hm.
Student: [Inaudible.]
Instructor (Mehran Sahami): Right. There is someone else, us, the 106a people, who
provided, actually the ACM people who provided the libraries for you to use, that call
your run method automatically when a program starts. So that's what's actually going on.
And when we get toward the end of the class I'll kind of lift the covers on what's going on
there.
Um hm.
Student: [Inaudible] any class, like, that somebody's using [inaudible]?
Instructor (Mehran Sahami): Yeah, exactly. And unless you want people in Timbuktu
touching stuff they shouldn't be touching it should be private. The way I like to think
about it is think about your private parts, you like to keep them private, you don’t want
them to be public. Most things that you have on your body are private. You are an object,
so most things in an object are private. All right. Just keep that in mind.
So, with that said, let's actually go and create a "new class." It's kind of fun to create
classes. So I want to create a new class. So what I'm gonna do is I'm gonna get my
Eclipse out and I'm gonna fire it up. And the way you can actually create a new class in
Eclipse is over here in this little window, that we haven't done all that much stuff with so
far, usually we give you some folder that's called Assignment 1 or Assignment 2, and you
work on it. If you right click on the name of that folder, and if you happen to be a Mac
person that's the same thing as "control clicking" on that, you'll get this big scary menu
that comes up, and you just want to pick New. And when you pick New you pick Class.
So that's all that's going on. A right click on the name of the folder, pick New and pick
Class.
And what happens now is this thing comes up, and it says, "Create a new job of class,"
and there's all these fields, and things like dogs and cats sleeping together, it's just totally
out of control, and the only thing you need to worry about here is what are you gonna
name your class.
And so maybe we want to write some class, I'll just call this "my counter.” Because I'm
gonna write a little class that creates counter. And notice when I started typing, as soon as
I started typing, let me get rid of this, I'll start typing again "my counter." Anyone see
what happened? Eclipse had a cow, and the cow that it had was up here up at the top. It
says, "The use of the default package is discouraged." And you sit there and you think,
1.) What is the default package? And 2.) Why is its use discouraged?
And the important thing to keep in mind is you don't care. This is one of those moments
in your life where you're gonna be rebellious, and you're like, "Bring it on, I'm in the
default package," that's life in the city. And what the package really is, is the package is
just a collection of classes; a set of classes that make sense together. Java provides a
facility to say we're gonna put them all in the same package.
Right now we're not gonna define a whole bunch of classes that all work together, so we
don't need to worry about packages. So all of our classes that we write are gonna be in the
default package, which means, kind of, the un-named package, it's just sort of the
package that everyone's a part of unless they're a part of some other package. So it says,
"Oh, it's discouraged," and you're like, "I don't care I'm in the default package, I'm living
for big gustos." And then you do something extremely complicated, which is you click
"Finish." And guess what? You just created a new class.
Now, a couple of things to keep in mind: Over here, in your little project that you have,
there is now an entry called, "my counter.java." Remember, we just named the class
"counter." Guess what Eclipse did for you, it did this, it created for you a file,
automatically ending with .java, that's name matched the name of the class.
What else did it do for you? It created what we refer to as a "stub." It said, "I'm gonna
create an empty class for you." You're gonna create a class called, "my counter." So what
I'm gonna do is create, for you, the empty shell. Right. It gives you basically two lines of
code and one of them is a brace. So it's not providing a whole lot to you, but it's, like,
ooh, ooh, you've gotta define a class, like, aren't we excited to write a class. And you're
like, all right, let's actually write the class.
Zgłoś jeśli naruszono regulamin