Download presentation
Presentation is loading. Please wait.
1
Nov 5, Fall 2006IAT 8001 Lecture 5 Objects, Classes
2
Nov 5, Fall 2006IAT 8002 Today Primitive types –byte, short, int, long Object-oriented programming –objects –classes sets (mutators) and gets (accessors) object methods
3
Nov 5, Fall 2006IAT 8003 Primitive types Primitive types are determined by machine architecture byte: 8bitsreference: (JVM Dependent) short:16bits int: 32bits long:64bits float:32bits double:64bits
4
Nov 5, Fall 2006IAT 8004 Reference Like a remote control a reference is a primitive thing that points at objects the new keyword causes the reference to point at a new instance of the object
5
Nov 5, Fall 2006IAT 8005
6
Nov 5, Fall 2006IAT 8006 Arrays int[] nums = new int[7] ;
7
Nov 5, Fall 2006IAT 8007 Array of objects Dog[] pets = new Dog[7]; It starts as an array of null references
8
Nov 5, Fall 2006IAT 8008 Array of objects Dog[] pets = new Dog[7] ; pets[0] = new Dog(); pets[1] = new Dog();
9
Nov 5, Fall 2006IAT 8009 Objects
10
Nov 5, Fall 2006IAT 80010 Real Objects Real-world objects have –State –Behavior Bicycle –State selected gear, current pedal cadence, speed –Behavior Change Gear, Set Cadence, Apply Brakes
11
Nov 5, Fall 2006IAT 80011 Software Object State int gear ; float speed ; float cadence ; Behavior ChangeGears(int g); Brake( float level ); ChangeCadence( float c ); int GetGear(); float GetSpeed(); …
12
Nov 5, Fall 2006IAT 80012 Java directly supports Objects Java has direct syntactic and semantic support for Objects Syntax: class Bicycle { private int cadence = 0; private int speed = 0; private int gear = 1; void changeCadence(int newValue) { cadence = newValue; } void changeGear(int newValue) { gear = newValue; }
13
Nov 5, Fall 2006IAT 80013 Java directly supports Objects Java has direct syntactic and semantic support for Objects Semantics: class Bicycle { private int cadence = 0; private int speed = 0; private int gear = 1; void changeCadence(int newValue) { cadence = newValue; } void changeGear(int newValue) { gear = newValue; } Only these methods can read or write Bicycle private data
14
Nov 5, Fall 2006IAT 80014 Java Semantic support Programming usually takes place with objects: ClockClass clock = new ClockClass(); clock.setSecond( 12 ); clock.setMinute( 18 ); clock.setHour( 3 );
15
Nov 5, Fall 2006IAT 80015 Even Arrays are objects int[] bob = new int[10] ; bob[4] = 123 ; println( bob.size() ); Bicycle[]bikes = new Bicycle[10] ; bikes[0] = new Bicycle();
16
Nov 5, Fall 2006IAT 80016 Sets and Gets what can you do with private data? –to set it: setVarName( varType newValue) –to get it: varType getVarName() Why?
17
Nov 5, Fall 2006IAT 80017 temperature object class temp { private floatkelvin ; setCelsius( float C ); { if( C < -273.15 ) return ;// perhaps an error message would be in order else kelvin = C + 273.15 ; } float getCelsius() { return( kelvin - 273.15 ); } float setKelvin( float k ) { if( k < 0 ) return ; else kelvin = k ; }
18
Nov 5, Fall 2006IAT 80018 temperature object Controls access Ensures correctness –can only run a setXYZ() to change temp –can only do getXYZ() to get the value in the desired scale Who cares?
19
Nov 5, Fall 2006IAT 80019 Who cares? When you want to: –Solve the problem once and forget it –Reuse the solution elsewhere –Establish rules for use and change of data The principle: –Information hiding –By interacting only with an object's methods, the details of its internal implementation remain hidden from the outside world.
20
Nov 5, Fall 2006IAT 80020 Principle: Code re-use If an object already exists, you can use that object in your program. Specialists build, you use
21
Nov 5, Fall 2006IAT 80021 Principle: Define the Interface Define the interface: –The list of methods with Defined Operation The interface is the thing that other people use If you have the same interface with the same meaning –You can plug in a better implementation!
22
Nov 5, Fall 2006IAT 80022 Define the Interface If you have the same interface with the same meaning –You can plug in a better implementation! –You can plug in a More Interesting implementation!
23
Nov 5, Fall 2006IAT 80023 Summary of principles Hide unnecessary details Clearly define the interface Allow and support code re-use Build on the work of others
24
Nov 5, Fall 2006IAT 80024 How do we build on other work? Divide and conquer –Cut the problem into smaller pieces –Solve those smaller problems –Aggregate the smaller solutions Two approaches: –Top-down –Bottom-up
25
Nov 5, Fall 2006IAT 80025
26
Nov 5, Fall 2006IAT 80026 Top Down Take the big problem –Cut it into parts Analyze each part –Design a top-level solution that presumes you have a solution to each part then… –Cut each part into sub-parts
27
Nov 5, Fall 2006IAT 80027 Bottom-up Cut the problem into parts, then sub- parts, then sub-sub parts… –build a solution to each sub-sub-part aggregate sub-sub solutions into a sub-solution
28
Nov 5, Fall 2006IAT 80028 How do we build on other work? Recognize the problem as another problem in disguise –It’s a sorting problem! –It’s a search problem! –It’s a translation problem! –It’s an optimization problem!
29
Nov 5, Fall 2006IAT 80029 The challenge Software design is typically done top- down Software implementation is typically done bottom-up
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.