DOC PREVIEW
Stanford CS 106A - Programming Methodology

This preview shows page 1-2-3-4-5 out of 16 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 16 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 16 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 16 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 16 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 16 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 16 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Programming Methodology-Lecture12 Instructor (Mehran Sahami):So welcome back to yet another fun filled, exciting day of CS106A. A couple quick announcements before we start. There’s actually no handouts for today, and you’re like if there’s no handouts for today, why are there two handouts in the back? If you already picked up the two handouts from last class, you might want to double check to make sure you don’t already have them, but if you don’t have them, feel free to pick them up. We just don’t want to cut down any more trees than we need to, so if you accidentally picked them up, you can put them back at the end of the class, pass them to a friend, whatever you’d like to do. As per sort of the general consensus last time, everyone wanted to have class slides on the web, so now all the class slides are posted on the web and after every class when we have some slides in class, they’ll be posted on the web as well. There’s a little link called, strangely enough, class slides that is now on sort of the left hand navigation bar on the 106A website, and so you can get all the class slides form there. In some cases, what I actually did was if we covered some slides over multiple days but they were all about the same topic, I might have aggregated them all into one set of slides or one deck, so you’ll see them in there as just one topical kind of thing. Just wondering – how many people have started break out? Good. How many people are done with break out? You folks get to see good times. You’re well ahead of the curve. I won’t ask how many people have not started. I assume it’s just the compliment of that. It is a fairly beefy assignment. It would be in your best interest to start soon. It’s due on Wednesday. With that said, it’s time to launch into an entirely new topic, and the entirely new topic is something that we refer to as enumeration. Enumeration is a pretty basic idea, and it comes from the word enumerate, as you can kind of imagine. When you enumerate something, you basically just have some way of referring to something through numbers. So if we wanted to enumerate, for example, the year that someone is in college, we might have freshman and sophomores and juniors and seniors – that’s the enumeration of the year you might be. And so the basic idea is we just want to have some set of things that we enumerate or give a set of numbers to, essentially, and you don’t necessarily want to think of it as having to be a set of numbers, but it’s basically just some set of items that go together. We generally give them some numbers or some listing as a way of keeping them all – a way we might keep track of them. One way we might do this in java, for example, is just have a series of constants that are integers and so just to save myself a little bit of time in writing, constants. Yeah, a beautiful thing. So we might have some constant public static final [inaudible], and so if we’re going to do enumeration, oftentimes we just use integers to refer to each of the individual items and we just count them up. So frosh would be one, sophomores two, juniors three, seniors four, and grad is five. That's just what year you might be in school. Oftentimes, computer scientists actually start counting from zero, but sometimes it actually makes sense to have these things benumbers that start at one. For example, if you want to know which year someone is in school or if you’re doing months of the year, January is generally number one as opposed to number zero, so just to keep with common [inaudible], we might actually number it this way. Now, there’s something that was introduced in one of the later versions of java, java 5.0, which is kind of the second to latest version, which is something called enumerated types, and the book talks about them briefly. I’m not going to talk about them here sort of for the same reasons the book doesn’t talk about them. The book actually talks about them and then says the advantages versus disadvantages of doing enumerations using this thing called enumerated type versus just listing them out as integers. This way, at least for the time being in sort of the development of java’s world, seems to win out. We’re just going to teach you this way. As a matter of fact, in the old school, anything before java 5.0 had to do it this way, so it’s just best that you see it this way, because most code these days is written this way and it probably will continue to be until at some point this new enumerated type thing takes off. As you see in the book, it talks about enum type. Don’t worry about it. We’re just gonna do it this way. The only problem that comes up with doing something like this, though, is that you want to figure out, well, how do I read in these things and display them? Well, these are all just integers, so if I actually want to ask someone their year in school, I would have to keep track of that with some ints that I might call year, and so I would read in an int, and I might ask the person for their year, for example. And when I read that in, that’s all good and well. The only problem is this thing is just an integer. The user gives me some number, hopefully between one and five. I might want to actually have some logic in here that checks to make sure they gave me a number between one and five, ‘cause if they gave me a six, I don’t know what that corresponds to. Maybe that’s the dreaded other student category. I need to do something to guarantee that it’s in this list one through five. The other problem is if I actually want to print out someone’s year, there’s no way for the computer to know that year one should print out the word frosh, so if I do something like print lin here and I just write out year, that’s gonna write out an integer. It’s gonna write out a value, whatever the value the user gave me, presumably between one and five. So if I actually want to have some nicety in there and say, oh, if the year is one, I actually want to write out frosh as opposed to writing out a one, I need to do that manually. So somewhere in my program, I need to have some function that I do a switch statement or I can do cascaded ifs and I switch, for example, on year. I might say, well, in the case where year happens to be frosh, then what I’m going to do is actually print out print lin …


View Full Document

Stanford CS 106A - Programming Methodology

Download Programming Methodology
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Programming Methodology and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Programming Methodology 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?