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.
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 classyPurpose of this chapterGain experience creating and manipulating objects from the standard Java typesWhyPrepares you for defining your own classes and creating and manipulating the objects of those classes3Values versus objectsNumbersHave values but they do not have behaviorsObjectsHave attributes and behaviors4Using objectsFirst, we create an object:Scanner stdin = new Scanner (System.in);Most object creation lines look like thisThen we use the objectstdin.nextInt();stdin.nextDouble();Note that we could have called the object foo, bar, or anythingstdin is just what we chose to call it5Using Rectangle objectsLet’s create some Rectangle objectsRectangle 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 objectsLet’s create some String objectsString 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 objectsObjects are “things” that have properties (attributes) and behaviors (methods)We first create one or more objectsWe then manipulate their properties and call their methods8So why bother with objects?Let’s say you want to do a lot of String manipulationOnce you create a String object, all the manipulation methods are contained thereinSun already wrote the methods for usSo we can use String objects instead of writing our own code to get the substring, indexOf, etc.9More on StringsStrings are used very oftenAs a shortcut, you can use:String s = “Hello world”;instead of:String s = new String (“Hello world”);It’s just a shortcut that Java allowsThe two lines are almost the sameThere is a minor difference between the twoWhich we’ll get to later10Visualizing objectsClass (type) nameAttributes (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 sayingssayingsI MISS MY EXI MISS MY EXPEAKED AT 17PEAKED AT 17MAIL ORDERMAIL ORDERTABLE FOR 1TABLE FOR 1I CRY ON QI CRY ON QU C MY BLOG?U C MY BLOG?REJECT PILEREJECT PILEPILLOW HUGGINPILLOW HUGGINASYLUM BOUNDASYLUM BOUNDDIGNITY FREEDIGNITY FREEPROG FANPROG FANSTATIC CLINGSTATIC CLINGWE HAD PLANSWE HAD PLANSXANADU 2NITEXANADU 2NITESETTLE 4LESSSETTLE 4LESSNOT AGAIN NOT AGAIN1414Bittersweets: Dysfunctional Bittersweets: Dysfunctional sayingssayingsRUMORS TRUERUMORS TRUEPRENUP OKAY?PRENUP OKAY?HE CAN LISTENHE CAN LISTENGAME ON TVGAME ON TVCALL A 900#CALL A 900#P.S. I LUV MEP.S. I LUV MEDO MY DISHESDO MY DISHESUWATCH CMT UWATCH CMT PAROLE IS UP!PAROLE IS UP!BE MY YOKOBE MY YOKOU+ME=GRIEFU+ME=GRIEFI WANT HALFI WANT HALFRETURN 2 PITRETURN 2 PITNOT MY MOMMYNOT MY MOMMYBE MY PRISONBE MY PRISONC THAT DOOR? C THAT DOOR?15ReviewVariables of primitive typesint, double, char, boolean, etc.Can assign a value to itCan read a value from itCan’t do much else!ObjectsString, Rectangle, etc.Have many partsRectangle has width, length, etc.Like a complex typeHave methodsString has length(), substring(), etc.16String methodslength(): 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 7Note that calling s.length() is different than calling t.length()!Both return the lengthBut of different Strings17More String methodsConsiderString 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 methodsConsiderString 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