DOC PREVIEW
UVA CS 101 - Using Objects

This preview shows page 1-2-3-4-5-6-7-48-49-50-51-52-53-54-97-98-99-100-101-102-103 out of 103 pages.

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

Unformatted text preview:

Using ObjectsGetting classyValues versus objectsUsing objectsUsing Rectangle objectsUsing String objectsThe lowdown on objectsSo why bother with objects?More on StringsVisualizing objectsHow well do we understand using objects?Just in time for Valentine’s Day…Bittersweets: Dejected sayingsBittersweets: Dysfunctional sayingsReviewString methodsMore String methodsSlide 18These images are not animated…String program examplesProgram WordLength.javaProgram demoSlide 23Date translationDateTranslation.javaSlide 26Today’s demotivatorsClasses vs. ObjectsVariables vs. TypesHow well do we understand variables versus types?Slide 31More on classes vs. objectsHow well do we understand classes versus objects?Lots of piercings…ReferencesJava and variablesWhat is a referenceReferences 1References 2RepresentationSlide 42Shorthand represntationExamplesReferences 3Java’s garbage collectionSlide 47The null referenceUninitialized versus nullSlide 50Slide 51Slide 52Slide 53What happens in Windows…So what is a null reference good for?References and memoryThe 2004 Ig Nobel PrizesUsing object examplesAssignmentSlide 60String representationSlide 62Slide 63Final variablesAn optical illusionSlide 66RectangleSlide 68Slide 69String method usageSlide 71Rectangle method usageScanner reviewScanner usage examplesBeware!!!OverloadingSlide 77More on overloadingMore on more on overloadingMore on methodsAccessorsMutatorsConstructorsCalling the Circle constructorSlide 85Constructor varietiesMethod types reviewJava documentationSlide 90Java packagesExample: last semester’s HW J2Last semester’s HW J2PreliminariesStep 1Step 2Step 3Step 4Step 5Step 6Step 7Step 8Step 9Step 10Slide 1051Using ObjectsChapter 3Spring 2006CS 101Aaron Bloomfield2Getting classyPurpose of this chapterGain experience creating and manipulating objects from the standard Java typesWhyPrepares you for defining your own classes and creating and manipulating the objects of those classes3Values versus objectsNumbersHave values but they do not have behaviorsObjectsHave attributes and behaviors4Using objectsFirst, we create an object:Scanner stdin = new Scanner (System.in);Most object creation lines look like thisThen we use the objectstdin.nextInt();stdin.nextDouble();Note that we could have called the object foo, bar, or anythingstdin is just what we chose to call it5Using Rectangle objectsLet’s create some Rectangle objectsRectangle creation:Rectangle r = new Rectangle (10, 20);Objects have attributes (or properties):System.out.println (r.length);System.out.println (r.width);Objects have behaviors (or methods):r.grow (10, 20)r.isEmpty()r.setLocation (5,4)6Using String objectsLet’s create some String objectsString creation:String s = new String (“Hello world”);Objects have attributes (or properties):But we can’t access them…Objects have behaviors (or methods):s.substring(0,6)s.indexOf (“world”)s.toLowerCase()7The lowdown on objectsObjects are “things” that have properties (attributes) and behaviors (methods)We first create one or more objectsWe then manipulate their properties and call their methods8So why bother with objects?Let’s say you want to do a lot of String manipulationOnce you create a String object, all the manipulation methods are contained thereinSun already wrote the methods for usSo we can use String objects instead of writing our own code to get the substring, indexOf, etc.9More on StringsStrings are used very oftenAs a shortcut, you can use:String s = “Hello world”;instead of:String s = new String (“Hello world”);It’s just a shortcut that Java allowsThe two lines are almost the sameThere is a minor difference between the twoWhich we’ll get to later10Visualizing objectsClass (type) nameAttributes (properties)Methods (behaviors)+ grow (int, int) : void+ isEmpty ( ) : void+ setLocation ( int, int ) : void+ resize ( int, int ) : void+ ...Rectangle- width = 10- height = 20- ...11 How well do we understand How well do we understand using objects?using objects?1212Just in time for Valentine’s Just in time for Valentine’s Day…Day…1313Bittersweets: Dejected Bittersweets: Dejected sayingssayingsI MISS MY EXI MISS MY EXPEAKED AT 17PEAKED AT 17MAIL ORDERMAIL ORDERTABLE FOR 1TABLE FOR 1I CRY ON QI CRY ON QU C MY BLOG?U C MY BLOG?REJECT PILEREJECT PILEPILLOW HUGGINPILLOW HUGGINASYLUM BOUNDASYLUM BOUNDDIGNITY FREEDIGNITY FREEPROG FANPROG FANSTATIC CLINGSTATIC CLINGWE HAD PLANSWE HAD PLANSXANADU 2NITEXANADU 2NITESETTLE 4LESSSETTLE 4LESSNOT AGAIN NOT AGAIN1414Bittersweets: Dysfunctional Bittersweets: Dysfunctional sayingssayingsRUMORS TRUERUMORS TRUEPRENUP OKAY?PRENUP OKAY?HE CAN LISTENHE CAN LISTENGAME ON TVGAME ON TVCALL A 900#CALL A 900#P.S. I LUV MEP.S. I LUV MEDO MY DISHESDO MY DISHESUWATCH CMT UWATCH CMT PAROLE IS UP!PAROLE IS UP!BE MY YOKOBE MY YOKOU+ME=GRIEFU+ME=GRIEFI WANT HALFI WANT HALFRETURN 2 PITRETURN 2 PITNOT MY MOMMYNOT MY MOMMYBE MY PRISONBE MY PRISONC THAT DOOR? C THAT DOOR?15ReviewVariables of primitive typesint, double, char, boolean, etc.Can assign a value to itCan read a value from itCan’t do much else!ObjectsString, Rectangle, etc.Have many partsRectangle has width, length, etc.Like a complex typeHave methodsString has length(), substring(), etc.16String methodslength(): returns the String’s length (duh!)String s = “hello world”;String t = “goodbye”;System.out.println (s.length());System.out.println (t.length());Prints 11 and 7Note that calling s.length() is different than calling t.length()!Both return the lengthBut of different Strings17More String methodsConsiderString weddingDate = "August 21, 1976";String month = weddingDate.substring(0, 6);System.out.println("Month is " + month + ".");What is the output?Month is August.18More String methodsConsiderString fruit = "banana";String searchString = "an";int n1 = fruit.indexOf(searchString, 0);int n2 = fruit.indexOf(searchString, n1 + 1);int n3 = fruit.indexOf(searchString, n2 + 1);System.out.println("First search: " + n1);System.out.println("Second search: " + n2);System.out.println("Third search: " + n3);What is the output?First search: 1Second search: 3Third search:


View Full Document

UVA CS 101 - Using Objects

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

Classes

Classes

208 pages

Hardware

Hardware

21 pages

Arrays

Arrays

70 pages

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