This preview shows page 1-2-3-4-5-6-7-8-9-10-11-12-13-14-97-98-99-100-101-102-103-104-105-106-107-108-109-110-195-196-197-198-199-200-201-202-203-204-205-206-207-208 out of 208 pages.
ClassesPreparationObject-oriented programmingKnown ClassesMethodsMethods we’ve seenOur first class with methodsOur first class with methods, continuedProgram DemoWhat’s happening thereNotes on these methodsToday’s demotivatorsHW J3Revamping HW J3HW J3 CodeHW J3 Code with methodsSlide 17What happened herePros of RefactoringCons of RefactoringReturn ValuesThe return keywordReturn valuesReturn values (aka return types)Slide 26Return typesHow well do you feel you understand return values?ParametersThe methodsA method with multiple parametersThe main() methodSlide 33The 2006 Ig Nobel PrizesReturning objectsSlide 36Modifying parametersSlide 38Pass by valueHow well do you feel you understand parameters?Variable scopingMethod notes summaryHow well do you feel you understand scoping?Slide 44The Car classMore on classes vs. objectsA new example: creating a Car classCar’s instance variablesInstance variables and attributesCar behaviors or methodsCreating a new carConstructorsOur Car class so farSlide 54Another constructorSlide 56Using our Car classSo what does private mean?So how do we get the fuel of a Car?So how do we set the fuel of a Car?Why use all this?Back to our specific constructorSlide 64So what’s left to add to our Car class?Using the Car’s fuelSetting both positions at onceThe 2005 Ig Nobel PrizesDrawing the CarSlide 70Slide 71What happens when the car runs out of fuel?Slide 73Our car in actionHow well do you feel you understand the Car class?Fan-supplied demotivators!Parameter passingSlide 78Slide 79Slide 80Slide 81Slide 82Slide 83Slide 84Slide 85Slide 86How well do you feel you understand parameter passing?Yale vs. HarvardMiscellaneous StuffWhat I’m not expecting you to know yet…TerminologySlide 93Running a class without a main() methodSlide 95Variable initializationHow well do you feel you understand creating classes so far?Slide 98The Circle classA Circle classCircle class propertiesOur Circle classAccessing our Circle objectWhat’s the output?What’s the output now?What’s going on?Circle class behaviorsCalling the Circle constructorSlide 110Slide 111What happens in memoryCircle class: our class so farHow well do you feel you understand the Constructors?What do these pictures mean?Slide 116Consider the following codeSlide 118Slide 119Slide 120The use of static for fieldsMore on static fieldsEven more on static fieldsEven even more on static fieldsEven even even more on static fieldsHow well do you feel you understand static-ness?New 2005 demotivatiors!Back to our Circle classAdding a methodUsing that methodWhat happens when that method is calledReview of our Circle classA note about methods/variable orderAdding another methodWhat happens now?One way to fix this…Back to our program…Where the “crash” occursMotivation for private fieldsSlide 140From DubaiSlide 142Slide 143The problem now…Our Circle class so farUsing the get/set methodsWait! Another problem!This problem is easily fixedReview of static for fieldsVisibilities in JavaA few notes on visibilitiesHow well do you feel you understand visibilities?Overriding methods (and constructors)Overriding methods (and constructors), take 2Using mutators in the constructorSlide 157Chapter 2: Computer bugsWhy we always use the mutatorsBack to the static discussionMake getPI() staticInvoking static methodsstatic methods and non-static fieldsWhat happening in memoryThe main static lessonstatic and non-static rulesSlide 167Back to our main() methodImplications of main() being staticAnother use of static methodsCircle ID numbersAlien SongMore on methodsCalling a methodSlide 176Slide 177More on returnsSlide 179Returning an object from a methodReturning a non-object from a methodEnd of slides gone over in lectureRational classWhat we’ve seen so farWhat properties should our Rational class have?What do we want our Rational class to do?Our first take at our Rational classOur next take at our Rational classThe default constructorThe specific constructorAccessor methodsMutator methodsRational additionThe this keywordSlide 195Slide 196Slide 197Slide 198Slide 199Rational subtractionRational multiplicationRational divisionPrinting it to the screenThe toString() methodSlide 205Slide 206Our full Rational classOur Rational class in use, part 1 of 4Our Rational class in use, part 2 of 4Our Rational class in use, part 3 of 4Our Rational class in use, part 4 of 4A demo of our Rational classOther things we might want to add to our Rational classHow well do you feel you understand the Rational class?1ClassesChapter 4Fall 2006CS 101Aaron Bloomfield2PreparationScene so far has been background material and experienceComputing systems and problem solvingVariablesTypesInput and outputExpressions AssignmentsUsing objectsStandard classes and methodsDecisions (if, switch)Loops (while, for, do-while)Next: Experience what Java is really aboutDesign and implement objects representing information and physical world objects3Object-oriented programmingBasisCreate and manipulate objects with attributes and behaviors that the programmer can specifyMechanismClassesBenefitsAn information type is design and implemented onceReused as neededNo need reanalysis and re-justification of the representation4Known ClassesClasses we’ve seenBigIntegerStringRectangleVectorScannerSystemClasses we’ll be seeing soonBigDecimalBut the first step is on creating methods…55MethodsMethods6Methods we’ve seenWe’ve seen methods (functions) beforeangleSin = Math.sin (90 * PI/180.0);System.out.println (“Hello world”);value = card.getBlackjackValue();We are going to start defining themNote that many of these “return” a valueMath.sin() and card.getBlackjack()The way to name methods is the same as variablesallTheWordsTogetherWith the first letter of each word capitalizedExcept the very first letter is lower case7Our first class with methodspublic class Methods1 { public static void main (String args[]) {Scanner stdin = new Scanner (System.in);System.out.println ("Enter a valid int value");int value = stdin.nextInt();if ( value == 1 ) validValue();else if ( value == 2 ) validValue();else if ( value == 3 ) invalidValue();else if ( value == 4 ) invalidValue();else validValue(); }}8Our first class with methods, continued public static void invalidValue() { System.out.println ("You have
View Full Document