CS 100, Summer 2001 Tuesday, 7/10 Lecture 8 ----------------------------------------------------------------------- Announcements: + Exercise 5 due 7/11 + PRELIM 1 results (avg 53, good!) + Assignment 2 results (avg 84/100!) + drop deadline is Friday + will post grades soon; please check them over + reading: Savitch 5.5 ----------------------------------------------------------------------- Topics: + methods + scope + intro to OOP (object oriented programming) ----------------------------------------------------------------------- ----------------------------------------------------------------------- + Methods - philosophy: eliminate redundancy and unnecessary repetition, division of labor, code reuse later - Example: song lyrics (when chorus is repeated, abbreviate by just writing "Chorus".) - "Floating" by the Moody Blues - two kinds: perform action, return value (or both) - examples: - "open the door" method (no input; action: opening door; no output) - "read this book" method (input: book, action: reading; no output) - "take this money and buy my groceries for me" method: (input: money, grocery list; action: purchasing, transport back; output: groceries (and change!)) ---------------------------------------------------------------------- + syntax: public static returntype name(parameters) { statements; return value; // unless the method is void } + Java is strongly typed, so 'parameters' is composed of series of inputs with this form: type var1, type var2, ..., type varn EXAMPLE: Methods.java EXAMPLE: NoMethods.java (same thing without methods) + return keyword/statement (ends processing of method) EXAMPLE: TensComplement.java - note method output is NOT the same as printing something out - method is a little box: you give it something, something else comes back out ---------------------------------------------------------------------- + Scope: - what portion of the code is visible to another portion ("within scope") - think visibility -- can a portion of code see a particular variable? - look for braces { } (these are like "walls") - variables declared inside the { } can only be seen by other statements inside the same set of braces - for classes, you will see that classes can be used from other classes when the classes are located in the same project - actually, you have already seen this (SavitchIn.java) EXAMPLE: scope.java --------------------------------------------------------------------- OOP + grapes: want to do stuff with them - wash the bunch of grapes - eat grapes - make wine! (no method for this. Some of us are under 21.) EXAMPLE: BunchOfGrapesNoObjects.java - main calls different BunchOfGrapes methods to test them. - There are two ways to write general methods for the bunches of grapes: 1) add a parameter indicating which bunch, and use switch 2) write separate methods for each bunch of grapes - Big drawbacks! 1) Lots of repetition 2) Have to change every method each time I want another bunch of grapes 3) Wastes programmer time 4) Error-prone - Imagine adding checks to eatGrape() to make sure that there are > 0 grapes available. Now, see BunchOfGrapes.java (and GrapesTester.java) - All of the information needed for a single bunch of grapes is included in BunchOfGrapes.java, and it is much shorter. - Can easily create new bunches of grapes - no need to change BunchOfGrapes.java! + OOP philosophy: group related information together + doesn't add new functionality, but makes a LOT of things easier! + combine data (variables) and functions (methods) together --------------------------------------------------------------------- Bring your questions tomorrow! OOP is fun, but it's a big concept to wrap your head around. We'll talk about this as much as you like. --------------------------------------------------------------------- Pick up Exercise 5 handout.