This preview shows page 1-2-3-4-5-6-7-8-9-10-11-12-87-88-89-90-91-92-93-94-95-96-97-98-99-175-176-177-178-179-180-181-182-183-184-185-186 out of 186 pages.
ClassesPreparationObject-oriented programmingKnown ClassesMethodsMethods we’ve seenOur first class with methodsOur first class with methods, continuedProgram DemoWhat’s happening thereNotes on these methodsThe return keywordReturn valuesReturn values (aka return types)Slide 152004 IOCCC winnersParametersThe methodsA method with multiple parametersThe main() methodSlide 21Returning objectsSlide 23Modifying parametersSlide 25Pass by valueVariable scopingMethod notes summaryToday’s demotivatorsThe 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 39Another constructorSlide 42Using 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?The 2005 Ig Nobel PrizesBack to our specific constructorSlide 50So what’s left to add to our Car class?Using the Car’s fuelSetting both positions at onceDrawing the CarSlide 55Slide 56What happens when the car runs out of fuel?Slide 58Our car in actionSlide 60Miscellaneous StuffWhat I’m not expecting you to know yet…TerminologySlide 64Running a class without a main() methodSlide 66Variable initializationParameter passingSlide 70Slide 71Slide 72Slide 73Slide 74Slide 75Slide 76Slide 77Slide 78Slide 79Rational 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 92Slide 93Slide 94Slide 95Slide 96DeCSS: The recordingsRational subtractionRational multiplicationRational divisionPrinting it to the screenThe toString() methodSlide 103Slide 104Our 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 classFan-supplied demotivators!More on methodsCalling a methodSlide 116Slide 117More on returnsSlide 119Returning an object from a methodReturning a non-object from a methodFractalsThe 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 133Slide 134What happens in memoryNew 2005 demotivatiors!Circle class: review from last timeSlide 139Consider the following codeSlide 141Slide 142Slide 143The use of static for fieldsMore on static fieldsEven more on static fieldsEven even more on static fieldsEven even even more on static fieldsBack to our Circle classAdding a methodUsing that methodWhat happens when that method is calledSlide 153Review 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 162Star Wars Episode 3 TrailerSlide 164Slide 165Slide 166The problem now…Our Circle class so farUsing the get/set methodsWait! Another problem!This problem is easily fixedVisibilities in JavaA few notes on visibilitiesOverriding methods (and constructors)Overriding methods (and constructors), take 2Using mutators in the constructorSlide 177Why 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 rulesBack to our main() methodImplications of main() being staticAnother use of static methodsCircle ID numbersA two legged dog….1ClassesChapter 4Spring 2006CS 101Aaron Bloomfield2PreparationScene so far has been background material and experienceComputing systems and problem solvingVariablesTypesInput and outputExpressions AssignmentsObjectsStandard classes and methodsDecisions (if, switch)Loops (while, for, do-while)Now: 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 soonBigDecimal55MethodsMethods6Methods 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 entered an invalid value."); System.out.println ("The program will now exit."); System.exit (0); } public static void validValue() { System.out.println ("You have entered an valid value."); System.out.println ("Congratulations!"); System.out.println ("The program will now exit."); System.exit (0); }99Program DemoProgram DemoMethods1.javaMethods1.java10public static void validValue() { System.out.println ("You have entered an valid value."); System.out.println ("Congratulations!"); System.out.println ("The program will now exit."); System.exit (0); }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
View Full Document