iat 800
DESCRIPTION
IAT 800. Lecture 5 Objects, Classes. Today. Primitive types byte, short, int, long Object-oriented programming objects classes sets (mutators) and gets (accessors) object methods. Primitive types. Primitive types are determined by machine architecture - PowerPoint PPT PresentationTRANSCRIPT
![Page 1: IAT 800](https://reader034.vdocument.in/reader034/viewer/2022042822/56813324550346895d9a044e/html5/thumbnails/1.jpg)
IAT 800 1
IAT 800
Lecture 5Objects, Classes
![Page 2: IAT 800](https://reader034.vdocument.in/reader034/viewer/2022042822/56813324550346895d9a044e/html5/thumbnails/2.jpg)
Sep 29, Fall 2009 IAT 800 2
Today
Primitive types– byte, short, int, long
Object-oriented programming– objects– classes
• sets (mutators) and gets (accessors)• object methods
![Page 3: IAT 800](https://reader034.vdocument.in/reader034/viewer/2022042822/56813324550346895d9a044e/html5/thumbnails/3.jpg)
Sep 29, Fall 2009 IAT 800 3
Primitive types
Primitive types are determined by machine architecturebyte: 8bits reference: (JVM Dependent)short: 16bitsint: 32bitslong: 64bitsfloat: 32bitsdouble: 64bits
![Page 4: IAT 800](https://reader034.vdocument.in/reader034/viewer/2022042822/56813324550346895d9a044e/html5/thumbnails/4.jpg)
Sep 29, Fall 2009 IAT 800 4
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
![Page 5: IAT 800](https://reader034.vdocument.in/reader034/viewer/2022042822/56813324550346895d9a044e/html5/thumbnails/5.jpg)
Sep 29, Fall 2009 IAT 800 5
![Page 6: IAT 800](https://reader034.vdocument.in/reader034/viewer/2022042822/56813324550346895d9a044e/html5/thumbnails/6.jpg)
Sep 29, Fall 2009 IAT 800 6
Arrays
int[] nums = new int[7] ;
![Page 7: IAT 800](https://reader034.vdocument.in/reader034/viewer/2022042822/56813324550346895d9a044e/html5/thumbnails/7.jpg)
Sep 29, Fall 2009 IAT 800 7
Array of objects
Dog[] pets = new Dog[7]; It starts as an array of null
references
![Page 8: IAT 800](https://reader034.vdocument.in/reader034/viewer/2022042822/56813324550346895d9a044e/html5/thumbnails/8.jpg)
Sep 29, Fall 2009 IAT 800 8
Array of objects
Dog[] pets = new Dog[7] ;pets[0] = new Dog();pets[1] = new Dog();
![Page 9: IAT 800](https://reader034.vdocument.in/reader034/viewer/2022042822/56813324550346895d9a044e/html5/thumbnails/9.jpg)
Sep 29, Fall 2009 IAT 800 9
Objects
![Page 10: IAT 800](https://reader034.vdocument.in/reader034/viewer/2022042822/56813324550346895d9a044e/html5/thumbnails/10.jpg)
Sep 29, Fall 2009 IAT 800 10
Real Objects
Real-world objects have– State– Behavior
Bicycle– State
• selected gear, current pedal cadence, speed
– Behavior• Change Gear, Set Cadence, Apply Brakes
![Page 11: IAT 800](https://reader034.vdocument.in/reader034/viewer/2022042822/56813324550346895d9a044e/html5/thumbnails/11.jpg)
Sep 29, Fall 2009 IAT 800 11
Software Object
Stateint gear ;float speed ;float cadence ;
BehaviorChangeGears(int g);Brake( float level );ChangeCadence( float c );int GetGear();float GetSpeed(); …
![Page 12: IAT 800](https://reader034.vdocument.in/reader034/viewer/2022042822/56813324550346895d9a044e/html5/thumbnails/12.jpg)
Sep 29, Fall 2009 IAT 800 12
Java directly supports Objects Java has direct syntactic and semantic support for
ObjectsSyntax:
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; }}
![Page 13: IAT 800](https://reader034.vdocument.in/reader034/viewer/2022042822/56813324550346895d9a044e/html5/thumbnails/13.jpg)
Sep 29, Fall 2009 IAT 800 13
Java directly supports Objects Java has direct syntactic and semantic support for
ObjectsSemantics:
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
![Page 14: IAT 800](https://reader034.vdocument.in/reader034/viewer/2022042822/56813324550346895d9a044e/html5/thumbnails/14.jpg)
Sep 29, Fall 2009 IAT 800 14
Java Semantic support
Programming usually takes place with objects:ClockClass clock = new ClockClass();
clock.setSecond( 12 );clock.setMinute( 18 );clock.setHour( 3 );
![Page 15: IAT 800](https://reader034.vdocument.in/reader034/viewer/2022042822/56813324550346895d9a044e/html5/thumbnails/15.jpg)
Sep 29, Fall 2009 IAT 800 15
Even Arrays are objects
int[] bob = new int[10] ;bob[4] = 123 ;println( bob.size() );
Bicycle[] bikes = new Bicycle[10] ;bikes[0] = new Bicycle();
![Page 16: IAT 800](https://reader034.vdocument.in/reader034/viewer/2022042822/56813324550346895d9a044e/html5/thumbnails/16.jpg)
Sep 29, Fall 2009 IAT 800 16
Sets and Gets
what can you do with private data?– to set it: setVarName( varType
newValue)– to get it: varType getVarName()
Why?
![Page 17: IAT 800](https://reader034.vdocument.in/reader034/viewer/2022042822/56813324550346895d9a044e/html5/thumbnails/17.jpg)
Sep 29, Fall 2009 IAT 800 17
temperature objectclass temp { private float kelvin ; void 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 ); } void setKelvin( float k ) { if( k < 0 )
return ; else kelvin = k ;
}}
![Page 18: IAT 800](https://reader034.vdocument.in/reader034/viewer/2022042822/56813324550346895d9a044e/html5/thumbnails/18.jpg)
Sep 29, Fall 2009 IAT 800 18
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?
![Page 19: IAT 800](https://reader034.vdocument.in/reader034/viewer/2022042822/56813324550346895d9a044e/html5/thumbnails/19.jpg)
Sep 29, Fall 2009 IAT 800 19
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.
![Page 20: IAT 800](https://reader034.vdocument.in/reader034/viewer/2022042822/56813324550346895d9a044e/html5/thumbnails/20.jpg)
Sep 29, Fall 2009 IAT 800 20
Principle: Code re-use
If an object already exists, you can use that object in your program.
Specialists build, you use
![Page 21: IAT 800](https://reader034.vdocument.in/reader034/viewer/2022042822/56813324550346895d9a044e/html5/thumbnails/21.jpg)
Sep 29, Fall 2009 IAT 800 21
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!
![Page 22: IAT 800](https://reader034.vdocument.in/reader034/viewer/2022042822/56813324550346895d9a044e/html5/thumbnails/22.jpg)
Sep 29, Fall 2009 IAT 800 22
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!
![Page 23: IAT 800](https://reader034.vdocument.in/reader034/viewer/2022042822/56813324550346895d9a044e/html5/thumbnails/23.jpg)
Sep 29, Fall 2009 IAT 800 23
Summary of principles
Hide unnecessary details Clearly define the interface Allow and support code re-use
Build on the work of others
![Page 24: IAT 800](https://reader034.vdocument.in/reader034/viewer/2022042822/56813324550346895d9a044e/html5/thumbnails/24.jpg)
Sep 29, Fall 2009 IAT 800 24
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
![Page 25: IAT 800](https://reader034.vdocument.in/reader034/viewer/2022042822/56813324550346895d9a044e/html5/thumbnails/25.jpg)
Sep 29, Fall 2009 IAT 800 25
![Page 26: IAT 800](https://reader034.vdocument.in/reader034/viewer/2022042822/56813324550346895d9a044e/html5/thumbnails/26.jpg)
Sep 29, Fall 2009 IAT 800 26
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
![Page 27: IAT 800](https://reader034.vdocument.in/reader034/viewer/2022042822/56813324550346895d9a044e/html5/thumbnails/27.jpg)
Sep 29, Fall 2009 IAT 800 27
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
![Page 28: IAT 800](https://reader034.vdocument.in/reader034/viewer/2022042822/56813324550346895d9a044e/html5/thumbnails/28.jpg)
Sep 29, Fall 2009 IAT 800 28
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!
![Page 29: IAT 800](https://reader034.vdocument.in/reader034/viewer/2022042822/56813324550346895d9a044e/html5/thumbnails/29.jpg)
Sep 29, Fall 2009 IAT 800 29
The challenge
Software design is typically done top-down
Software implementation is typically done bottom-up