IAT 800 Lecture 5 Objects Classes IAT 800
IAT 800 Lecture 5 Objects, Classes IAT 800 1
Today g Primitive types – byte, short, int, long g Object-oriented programming – objects – classes • sets (mutators) and gets (accessors) • object methods Sep 29, Fall 2009 IAT 800 2
Primitive types g Primitive types are determined by machine architecture byte: short: int: long: float: double: Sep 29, Fall 2009 8 bits 16 bits 32 bits 64 bits reference: (JVM Dependent) IAT 800 3
Reference g Like a remote control g a reference is a primitive thing that points at objects g the new keyword causes the reference to point at a new instance of the object Sep 29, Fall 2009 IAT 800 4
Sep 29, Fall 2009 IAT 800 5
Arrays g int[] nums = new int[7] ; Sep 29, Fall 2009 IAT 800 6
Array of objects g Dog[] pets = new Dog[7]; g It starts as an array of null references Sep 29, Fall 2009 IAT 800 7
Array of objects Dog[] pets = new Dog[7] ; pets[0] = new Dog(); pets[1] = new Dog(); Sep 29, Fall 2009 IAT 800 8
Objects Sep 29, Fall 2009 IAT 800 9
Real Objects g Real-world objects have – State – Behavior g Bicycle – State • selected gear, current pedal cadence, speed – Behavior • Change Gear, Set Cadence, Apply Brakes Sep 29, Fall 2009 IAT 800 10
Software Object g State int gear ; float speed ; float cadence ; g Behavior Change. Gears(int g); Brake( float level ); Change. Cadence( float c ); int Get. Gear(); float Get. Speed(); … Sep 29, Fall 2009 IAT 800 11
Java directly supports Objects g 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 change. Cadence(int new. Value) { cadence = new. Value; } void change. Gear(int new. Value) { gear = new. Value; } } Sep 29, Fall 2009 IAT 800 12
Java directly supports Objects g Java has direct syntactic and semantic support for Objects Semantics: class Bicycle { Only these methods private int cadence = 0; can read or write Bicycle private data private int speed = 0; private int gear = 1; void change. Cadence(int new. Value) { cadence = new. Value; } void change. Gear(int new. Value) { gear = new. Value; } } Sep 29, Fall 2009 IAT 800 13
Java Semantic support g Programming objects: usually takes place with Clock. Class clock = new Clock. Class(); clock. set. Second( 12 ); clock. set. Minute( 18 ); clock. set. Hour( 3 ); Sep 29, Fall 2009 IAT 800 14
Even Arrays are objects int[] bob = new int[10] ; bob[4] = 123 ; println( bob. size() ); Bicycle[] bikes = new Bicycle[10] ; bikes[0] = new Bicycle(); Sep 29, Fall 2009 IAT 800 15
Sets and Gets g what can you do with private data? – to set it: – to get it: set. Var. Name( var. Type new. Value) var. Type get. Var. Name() g Why? Sep 29, Fall 2009 IAT 800 16
temperature object class temp { private float kelvin ; void set. Celsius( float C ) { if( C < -273. 15 ) return ; // perhaps an error message would be in order else kelvin = C + 273. 15 ; } float get. Celsius() { return( kelvin - 273. 15 ); } void set. Kelvin( float k ) { if( k < 0 ) return ; else kelvin = k ; } } Sep 29, Fall 2009 IAT 800 17
temperature object g Controls access g Ensures correctness – can only run a set. XYZ() to change temp – can only do get. XYZ() to get the value in the desired scale g Who cares? Sep 29, Fall 2009 IAT 800 18
Who cares? g When you want to: – Solve the problem once and forget it – Reuse the solution elsewhere – Establish rules for use and change of data g The principle: – Information hiding – By interacting only with an object's methods, the details of its internal implementation remain hidden from the outside world. Sep 29, Fall 2009 IAT 800 19
Principle: Code re-use g If an object already exists, you can use that object in your program. g Specialists build, you use Sep 29, Fall 2009 IAT 800 20
Principle: Define the Interface g Define the interface: – The list of methods with Defined Operation g The interface is the thing that other people use g If you have the same interface with the same meaning – You can plug in a better implementation! Sep 29, Fall 2009 IAT 800 21
Define the Interface g 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! Sep 29, Fall 2009 IAT 800 22
Summary of principles g Hide unnecessary details g Clearly define the interface g Allow and support code re-use g Build Sep 29, Fall 2009 on the work of others IAT 800 23
How do we build on other work? g Divide and conquer – Cut the problem into smaller pieces – Solve those smaller problems – Aggregate the smaller solutions g Two approaches: – Top-down – Bottom-up Sep 29, Fall 2009 IAT 800 24
Sep 29, Fall 2009 IAT 800 25
Top Down g 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 g then… – Cut each part into sub-parts Sep 29, Fall 2009 IAT 800 26
Bottom-up g Cut the problem into parts, then sub-sub parts… – build a solution to each sub-part • aggregate sub-sub solutions into a sub-solution Sep 29, Fall 2009 IAT 800 27
How do we build on other work? g Recognize the problem as another problem in disguise – It’s Sep 29, Fall 2009 a sorting problem! a search problem! a translation problem! an optimization problem! IAT 800 28
The challenge g Software design is typically done top- down g Software implementation is typically done bottom-up Sep 29, Fall 2009 IAT 800 29
- Slides: 29