DOC PREVIEW
UVA CS 101 - Classes

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.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 186 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 186 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 186 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 186 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 186 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 186 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 186 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 186 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 186 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 186 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 186 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 186 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 186 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 186 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 186 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 186 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 186 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 186 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 186 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 186 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 186 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 186 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 186 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 186 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 186 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 186 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 186 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 186 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 186 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 186 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 186 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 186 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 186 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 186 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 186 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 186 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 186 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 186 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

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 Bloomfield2PreparationScene so far has been background material and experienceComputing systems and problem solvingVariablesTypesInput and outputExpressions AssignmentsObjectsStandard classes and methodsDecisions (if, switch)Loops (while, for, do-while)Now: Experience what Java is really aboutDesign and implement objects representing information and physical world objects3Object-oriented programmingBasisCreate and manipulate objects with attributes and behaviors that the programmer can specifyMechanismClassesBenefitsAn information type is design and implemented onceReused as neededNo need reanalysis and re-justification of the representation4Known ClassesClasses we’ve seenBigIntegerStringRectangleVectorScannerSystemClasses we’ll be seeing soonBigDecimal55MethodsMethods6Methods we’ve seenWe’ve seen methods (functions) beforeangleSin = Math.sin (90 * PI/180.0);System.out.println (“Hello world”);value = card.getBlackjackValue();We are going to start defining themNote that many of these “return” a valueMath.sin() and card.getBlackjack()The way to name methods is the same as variablesallTheWordsTogetherWith the first letter of each word capitalizedExcept 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 DemoMethods1.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

UVA CS 101 - Classes

Documents in this Course
Classes

Classes

53 pages

Recursion

Recursion

15 pages

Iteration

Iteration

88 pages

PLEDGED

PLEDGED

6 pages

Objects

Objects

33 pages

PLEDGED

PLEDGED

11 pages

CS 101

CS 101

42 pages

Classes

Classes

83 pages

Iteration

Iteration

92 pages

Classes

Classes

208 pages

Hardware

Hardware

21 pages

Arrays

Arrays

70 pages

Load more
Download Classes
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 Classes 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 Classes 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?