Java Programming: Understanding Strings, Classes, and Variables, Study notes of Programming Methodologies

An introduction to the concepts of strings, classes, and variables in java programming. The instructor explains the difference between instance and class variables, public and private access, and the concept of shadowing. The document also covers the creation of objects and the use of constructors. Students will learn how to write their own classes and understand the importance of instance variables in keeping track of information between method calls.

Typology: Study notes

2010/2011

Uploaded on 10/04/2011

hollyb
hollyb 🇺🇸

4.8

(44)

431 documents

1 / 19

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
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.
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13

Partial preview of the text

Download Java Programming: Understanding Strings, Classes, and Variables and more Study notes Programming Methodologies in PDF only on Docsity!

Programming Methodology-Lecture

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.

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.

for a new value of the counter I'm gonna give you a new number. That means I need to keep track between method calls of what the old number was.

So if I need to keep track of something between method calls what am I gonna need? Instance variable. There's a few people over here, one person lying on the floor down there. So what I'm gonna have is an instance variable. And to find the instance variable down at the bottom I'm going to make the instance variable private because I don't want someone being able to muck with my instance variable. So I'm going to have private int, and I'll just call this "counter." So this is my instance variable over here.

And so what I'm going to do is, in my constructor I'm going to say, "Hey, initialize that counter to be equal to whatever start value the user gave me." So I have my own little instance variable here. Every counter object is gonna have its own little counter variable, and I'm just gonna start it off by giving it whatever value the user told me to set it to when they created an object of this type. And I'll show you in just a second how we create an object of this type.

Now, the other thing we could do, that's kind of funky, is you can actually have more than one constructor. You can have multiple constructors that have the same form, they're all public, at least so far. They return no type. But the thing that differentiates them, and the way the computer knows which constructor to actually use is what the parameters are. So we just created a constructor that has one parameter.

I'm also going to create another constructor that has no parameters. So someone can actually create a counter without telling me what the starting value is. Well, what is the starting value if they don't give me one? I still need to assign one. So I'm gonna say, "Hey, if you didn't give me one, I'm gonna set the starting value to be one." Because most of the time when people want a counter they just want to count from one. But if you want to do something funky, like have ID numbers for a university, you can tell me the starting value.

And so I'll show you in just a second, how we actually create objects of this type, and when we create objects how it differentiates between whether to call this method up here with a parameter or this method over here without a parameter.

A couple of other things we need to do. We need to find and specify some way of saying, "Give me the next value." Right. How do I get new numbers, how do I request new numbers from this counter? Well, I need to have some method. This method's going to return for me an integer, because my counter is just integers, and I'll call this "next value." It takes in no parameters because I don't need to tell you anything. I don't need to tell the object anything to get the next value. I just say, "Hey, buddy, next value." And it gives me the next value.

So the way it's gonna give me the next value is it's going to give me whatever value the counter has right now. So I might be inclined to say, "return counter." What's the problem with doing that? What happens the next time I call next value? I return the same. But

that's not very exciting, right. You go into the bakery, and you're like, "Hey, I got No. 1." And you look around and there's 300 other people that have No. 1. The fights break out, it's bad times.

So what we need to do is we need to say, "Hey, I need to keep track of what value the counter is currently right now because that's what I'm gonna return to you." So I'm gonna have some "temporary." And this temporary is a local variable, it only needs to live inside this method for the lifetime of this method. And I will initialize that temporary to be whatever the current counter value is. Then I'll go ahead and add one to the counter value. So I'll increment the counter so the number next time this gets called is gonna be one higher. And what am I gonna do now when I return? Any ideas? Return the temporary.

The temporary is what the value of the counter started at when this method was first called. So I said, "Oh, store off a copy of it, add one to the counter," because next time it'll be one greater and return whatever the old value was in temporary. And after this function's gone, temporary gets blown away. And it's like, "Hey, I was a local variable, I gave my life for the method." And we're like, "Thank you very much, temporary, that was very courageous of you." But you're gone now but you managed to return this value that you stored. So good times, you actually did what we needed you to do.

And this is my whole class. Is there any questions about this class?

Now, let me show you one other thing before we actually make use of this class. And it's a way that we could have actually done things slightly differently. So when we actually created the class here's our constructor. In this case, what I've done is I've just renamed the class from my counter to be incrementor, because incrementor is kind of the more funky computer science term for it, and incrementor is something that adds one to something and counts. So other than my counter, which is just all fuzzy and good, it's like, "Oh, it's My Counter," but people might think, "Oh, your counter, is that, like, the restaurant in Palo Alto that gives you burgers?" Anyone ever eaten there? It's a good time. And then they didn't even pay for the product placement, I've gotta say. We'll call it incrementor. But same kind of thing here, we're gonna have some constructor for incrementor. It's gonna take some start value. I'm just showing one of the constructors here.

And one thing you might say is, what are the properties, just to reiterate, of constructors? The name of the class is the same as the constructor name. The constructor does not specify return type, it's responsible for initializing whatever values we care about in the object. And this puppy gets called when an object is created. And I'll show you an example. Just like you created graphical objects in the past, we're gonna create an object an object of type incrementor in just a second, and I'll show the code for that. But that's when this implementor method actually gets invoked.

So one thing that people think about when they see this is, "Hey, you have this start value thing, and then over here you call it counter, why don't you just call it counter both

instance variables and there'll just be no confusion with this or that or whatever, you just keep the names distinct. And it's the preferred root.

You want to think about writing programs that require the human to think less when they're actually looking at the program. You're, like, "That's really weird, I thought computer science was all about thinking." Yeah, it's about thinking higher up the food chain, you don't want to worry about little details like this and get caught in these weird shadowing effects or aliasing effects, you just want to say, "Yeah, they're distinct," and then it's clear.

Is there are any questions about that? It's kind of a funky concept but it's just important for you to see it.

So with that said, let's actually look at not my counter anymore. We sort of wrote the bare bones code for my counter, but here are the full-fledged incrementor version. And so when we're now computer scientists, and we think about good programming style, we have some comment for the class. So just like the programs you wrote, you want to have some comment for the whole class, you want to have some comment per method you write. So this is all the same code we just wrote, now it's just commented. So good programming style.

Instructor (Mehran Sahami): Um hm.

Student: [Inaudible.]

Instructor (Mehran Sahami): Because if I declare it every time there is no way of me being able to keep track of what it's previous value was. So that's the critical concept of an instance variable, I need to keep track of some information between method calls. If I didn't, like temp, I didn't care what temp was the last time I called next value, it's just the local variable and it goes away. But counter I need to keep track of.

So how do I actually use counter? I wrote a program called "use counter." So use counter is basically just a Java program that is a program. So it extends Console Program, just like you're used to. I'm changing the font. You don’t need to worry about this line. All this line does is it just makes the font bigger so when I run the program you can actually see the text. Because there were people who were having trouble seeing the output text when I ran programs.

Now, how do I create objects of this class? This is just like you saw with graphical objects. What's going on is if I want to create some new counter, or some new incrementor, I specify the type of the variable incrementor. I give it a name, I'll call it "count one." And because I want to create an object, I go to the incrementor factory and say, "Give me a new incrementor." When I say new and the name of a class, what I get is a new object of that class. So at this point when I say new incrementor, a new incrementor object is created for me. That is when the constructor for that object gets invoked. So that constructor thing we just wrote, that initializes the object, the place it

gets called is when you ask for a new one of the class, when you get a new object the constructor's invoked.

Which version of the constructor does it invoke if you have multiple versions? It depends on the parameters you give. Right. We created two versions, one without a parameter, if you didn't specify the parameter it set the counter starting at one. One with a parameter, so if you specify parameter, it says, "Hey, there was a version of this that had a parameter," that's the version that's gonna get invoked, and so it's gonna set the beginning counter to 1,000.

Any questions about that?

So what are we gonna do? Now what we're gonna do is write out some values of the counter and count up. So what I'm gonna do is write a method. This method is going to take an object as a parameter. There's no reason why it can't, parameters just have some type and some name. So this count five times method is not gonna return anything, but it's going to take as a parameter something of type incrementor, that I'll just call "counter." And what it's gonna do is very simple, it's going to loop through five times and just print out the next five values of that counter. So that's all it does, it just pulls the next five tickets from the counter and writes them out to the screen.

So I'm gonna say, what are the first five values for count one, and call this function. What are the first five values for count two, and call the function passing and count two, and what are another five values for count one.

So let me go ahead and run this program and you can see what's going on. So if I run this, there's what I get. So I say the first five values for count one. Remember count one started with a counter of one. It gives me one, two, three, four, five. Then I asked for the first five value of count two. Count two started at 1,000, so I get a 1,000 up to a 1,004. Then I ask for the next five values of count one, and I get six, seven, eight, nine, ten.

Now, this all seems fairly straightforward, except for one fact that should slightly disturb you. Is there anything that slightly disturbs you? The thing that should slightly disturb you is the conversation we had last time, where we talked about when you pass a parameter to something you get a copy of that parameter, or you get a copy of that thing that you're passing.

So the interesting thing that's going on is you would think, well, you're passing count one here. When you pass count one there shouldn't you have gotten a copy of the count one that started with its counter of one? So when it counts it up here shouldn't it have only been counting in that copy, and so when it was done here that copy should have gone away, and when you returned over here your counter should have still been one? If we were doing integers that's sort of what would have happened.

But the things that's different between integers and doubles and objects, and this is the important thing to think about objects, when you pass an object as a parameter you are

which I never told you about before, well, actually, I did tell you about it, I just didn't give you its actual name before, but now you're not old enough so I'll tell you what the real deal is. The basic idea is so far you've seen things like local variables, and you've seen instance variables. There is a new kind of variable, and it is called a "class variable" or a class var.

What a class variable is, it is a variable that is shared by all objects of that class. There is one variable that all objects of that class share, that's the funky concept. In an instance variable each object of that class has a different version of that variable.

So if we have some counter over here, and if we have some count one, and some count two, and they each have an instance variable that we just defined, each of them has its own box. If I were to create a class variable, there is one variable counter that all objects, count one, count two, all objects of the type, or of the class, that this class variable is in, are all referring to the same one single box.

Now, there's sometimes you want to do that. I'll just show you a very simple example of how that works, and how you actually specify it. So the way you specify it, is in instance variable you just specified if it was public or private, and you specified its type and you gave it a name. In class variable you add this word called "static." And sometimes class variables as a result are referred to as static variables.

And you've seen static before. When did you actually see static used in conjunction with a variable? Constants, right. Why did we make them class variables by adding static? Because it doesn't make sense for two different objects that are circles to have their own copy of the constant value pi. Pi is in some sense some universal for all circles. So it would be shared as a class variable among all circles. So even though it was a constant, if we didn't say static, we would have gotten one of those constant values for every circle we created, for example. We want to just have one that's shared, so we call it static. But you can have static variables that are not necessarily constants, they don't have to be final.

So let me show you an example on the computer. We come over here. Here's our incrementor that we just wrote. And we say private static int counter. So what I've now done is, instead of creating an instance variable I have created a class variable. How does this change the program? Let me save this and run this. What this will now do is, all of the things that I create that are incrementors are all referring to the same counter. You're, like, whoa, that's kind of funky. Yeah, let me show you what actually happens.

So when I run use counter, I get a 1,000 through a 1,004, then I get 1,005 through a 1,009, then I get 1,010 through a 1,014. And you're, like, whoa, what happened there? Let me explain to you what happened here and hopefully it'll be clear. Like what did it start at a 1,000, why didn't it start at one, what was actually going on?

What's going on, if I actually look at use counter, is remember there is only one counter variable for all objects of the class. This first count one comes along and say, "Hey, give

me a new object that's an incrementor." And we say, "Okay." And we set it's counter to be one, right, that's what the constructor did. Then I say, "Hey, create for me another counter." And it says, "Okay, here's another incrementor over here, I'm now setting that counter variable to be a 1,000." And you're, like, uh-oh, that's the same counter variable that count one was using, too. Because now they're both sharing the same variable, and that variable is now a 1,000. The old value of one just got clobbered by this guy because it's referring to that same class variable.

There's only one counter variable among all objects of the class. So now when I count the first five values of count one, it's got a 1,000, it counts up to a 1,004. That 1,004 is shared by all instances of the class. So when I do the next five for count two, it's starting at a 1,005, and it counts from a 1,005 through 1,009. And then when I do count one again, count one's sharing that 1,009, and that's why we get a 1,010 through a 1,014.

Any questions about that?

Instructor (Mehran Sahami): Um hm.

Student: [Inaudible.]

Instructor (Mehran Sahami): Oh, yeah, a microphone would be good. And let me guess what your question is. If you switch the order, yes, you would actually start at zero – or you'd start at one. The first constructor would initialize the value at a 1,000; the second constructor would overwrite the 1,000 with one and then you'd count from one. Every once in a while I have this fleeting ten-second ability to read minds, and it goes away very quickly.

So with that said, it's time for something completely different. So any questions about instance variables versus class variables?

Instructor (Mehran Sahami): Um hm.

Student: [Inaudible.]

Instructor (Mehran Sahami): Maybe we should talk about that offline. Because it'd be good to know what particular thing you're referring to. But most of the time, for a lot of intents and purposes in this class, the main time you'll actually see static variables are when they're actually constants. There would be very few times when you actually have a static variable that's not a final variable, it's not a constant. But if you're interested, we could certainly talk about that offline.

So with that said, there's a funky concept called "Javadoc." And now you're old enough to learn about Javadoc. And all Javadoc really is, is it's basically Java's documentation system. This is just a little side point. And the reason why I'm giving this aside to you is you're gonna see Javadoc in just a second. We're gonna write another class using Javadoc.

java util random. And it tells us that by saying that actually this public class random generator extends random. So it's in fact a subclass.

And there's a whole bunch of stuff there that explains how this class actually works, and how you get random generator.get instance to get a version of it. But also what's generated for you, very nicely in these nice html tables, are things like, "Hey, here's the constructor." Constructor has no arguments, that's how you might potentially create a new random generator if you wanted to. But, in fact, most of the time what you want to do is get instance. So for all of the methods it has the name of the method, it has, basically, the form of the method, which is the method and then whatever parameters it takes, and then whatever comments you have about that method.

The other interesting thing about this is, it tells you which methods are kind of built in and which methods actually got inherited from your super class. So we mentioned that a random generator is an object that extends random. Well, some of the methods are actually defined in random generator specifically; some of the methods are inherited from this thing called random. It doesn't actually make a difference which ones are inherited or not, but you get kind of all this text.

You get even more text down here. So if you want to look at the excruciating details of next boolean, that takes in some double p, it tells us what the usage is. How do you use next boolean and what are its parameters. This is where those @ param little tag that was in there, there's an @ param tag that says p, and then has this comment, and this automatically gets turned into nice html.

So anytime you have questions about what something in the ACM library does or what methods are actually available to you for a particular object, you can actually go look at the Javadoc. You don't need to scrounge through code, it's actually just html. You can browse in a web browser by going to that URL I gave you and search around.

But the reason for showing you all of this, one, is to let you know that it actually exists, the second is we can now put this all together. We can create a class that has a bunch of stuff in it that we comment using Javadoc, and that also makes use of Strings, and gives you one nice big picture at the end.

So what I'm gonna do is I'm gonna create a class. This class basically is involving a student, something that hopefully you're all extremely familiar with at this point. So what a student class is going to provide, it's gonna have Javadoc comment. So up at the top I have the star, the slash star star, that wasn't a typo, that's actually a Javadoc comments, and explains what this whole class does.

So it says, what I'm gonna do is create a class that keeps track of the student. What is information I care about for a student? Well, this is a very simplified form. I care about a name of a student, their ID No., and how many units they've earned. That's all I care about for right now. The book has an example that's a little bit more involved, but I'm gonna give you a real simple example here.

So what are my constructors? How do I create something as a student? Notice the class student does not extend anything. So all students are just objects, I'm not extending anything else, they're just objects. One of my constructors for a student says specify the name and ID for the student. As a matter of fact, that's gonna be my only constructor, I'm not gonna allow someone to do other kinds of things. Give me a name, which is a String, right, it's just a piece of text, and an integer, which is an ID No. So we'll assume all ID No.'s are integers. And what it's gonna do is keep track of some information about students that are going to be stored as instance variables.

Because every student is going to have their own information for their name, which is just student name that's a String, student ID, which is just an integer, and the number of units they've earned so far, which I'll keep as a double because there are some schools that actually allow half units for whatever classes. They don't here, but other places they actually do. So we're just making it a double for generality as opposed to having it be an int. So these are all private instance variables. No one else outside of a student can get in there and muck with these things. The only way we can potentially change them is through the methods that are involved in this class.

We're also gonna have the number of units that are required to graduate (at Stanford that's 180), and we're going to specify that as a constant that is static and final, which means all students share the same number of units to graduate. And, unfortunately for you, you can't say, "Hey, you know what, I'm just gonna set units to graduate to be like ten." Wouldn't that be fun? Like, you take 106a, you take IHUM and you're done. Final, yeah, you're not changing this, it's 180 until the cows come home. So that's why it's a constant as opposed to just being a class variable that's actually modifiable.

So what are all the things we allow someone to do in here? The things we allow someone to do are, for example, to get the name of a student. Once I've created a student the student name variable is private. I can't actually refer to that variable outside of this class. So how do I ask you for your name after you've been created? So you were just created as a new student at Stanford. And, I'm like, "What's your name?" And I want to kind of come over and get a little close and be like, "Hey, can I touch your variables?" And you're, like, "No, man, this is wrong." So I'm, like, "Oh, what do you have that's public? Oh, get your name." What's your name?

Student: Aki.

Instructor (Mehran Sahami): Aki. So I get back a String Aki from your object. I can't actually touch any of the private parts. ID number, same kind of deal. I can't go in and see what your ID – like, I'm not gonna go into your wallet and pull out your ID card and be, like, "Oh, ID number, let me get out my magic marker and change it." The only way I can get it is by asking you for it, and if you've made that public then I can get that information from you by making the method public.

A few other things. Now, here are some funky things. Set units I actually made to be public, which means I can allow someone, even though the number of units earned is a

stud.get name, that returns to me a String, which is the name that's Ben Newman. I append to that has, I append to that the number of units Ben has, and then add the word units at the end of it and write that out to the screen.

Then I want to see if Ben can graduate. So, again, I print out Ben's name, if he can graduate, his status by calling "has enough units." That will return true or false, that will write true or false out onto the screen as part of the String. Then Ben takes CS106a. So Ben takes CS106a, is what's gonna get written out, and then I increment Ben's units by five because he's now taking CS106a, a five unit class. And then I ask again can he graduate. And if he has enough units to graduate, I'll say, rock on, and write out the String version of the object that I have by calling two String.

So when I actually run this all here's what I get. I'm running. I'm running. I want to run Stanford. It's just that easy to run Stanford. So Ben Newman has 179.0 units, right, because it's a double so it puts that .0 at the end of it. Ben Newman can graduate. We get false back because he doesn't have enough units to graduate. But that 179 is stored inside the object, that stud object has the 179. Ben takes CS106a, so we increment that object's number of units by five, by calling the appropriate method, and then Ben Newman can graduate is now true because he's got 184 units.

And, finally, when we say, "Rock On," and we call two String, what two String gives us back is the name plus the ID number inside paren. So it says, "Rock On Ben Newman paren number 1001."

Any questions about that? Alrighty. Then have a good weekend and I will see you on Monday.

[End of Audio]

Duration: 52 minutes