DOC PREVIEW
UVA CS 101 - Classes

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.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 208 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 208 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 208 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 208 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 208 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 208 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 208 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 208 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 208 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 208 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 208 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 208 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 208 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 208 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 208 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 208 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 208 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 208 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 208 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 208 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 208 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 208 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 208 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 208 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 208 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 208 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 208 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 208 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 208 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 208 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 208 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 208 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 208 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 208 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 208 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 208 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 208 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 208 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 208 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 208 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 208 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 208 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 208 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 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 Bloomfield2PreparationScene so far has been background material and experienceComputing systems and problem solvingVariablesTypesInput and outputExpressions AssignmentsUsing objectsStandard classes and methodsDecisions (if, switch)Loops (while, for, do-while)Next: 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BigDecimalBut the first step is on creating methods…55MethodsMethods6Methods 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


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

186 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?