Presentation is loading. Please wait.

Presentation is loading. Please wait.

INTRO TO OO Object lessons on Objects. Objects 2  In Java, everything has a type  Primitive types (numbers / booleans / chars)  Complex types (classes)

Similar presentations


Presentation on theme: "INTRO TO OO Object lessons on Objects. Objects 2  In Java, everything has a type  Primitive types (numbers / booleans / chars)  Complex types (classes)"— Presentation transcript:

1 INTRO TO OO Object lessons on Objects

2 Objects 2  In Java, everything has a type  Primitive types (numbers / booleans / chars)  Complex types (classes)  An Object is a value of complex type  has attributes (what the object is - values)  has behavior (what the object can do - operations)

3 Consider an AntiqueRadio Object 3  What attributes of such a radio are important?  Is it on or off?  What station is it tuned to?  What is the volume setting?  What operations can be performed on such a radio?  Change the station setting  Turn it on/off  Turn it up/down

4 Classes and Objects 4  Every object belongs to a group of objects of the same type. This is called the object’s class.  Similar usage to the phrase “class-action lawsuit”  Examples  The 2008 superbowl is a value of type …  Wisconsin is a value of type …  Spiderman belongs to the class of …  A class is a definition of the attributes and behavior for some group of objects  Attributes are defined via variables (called instance variable) in Java  Operations are defined via methods in Java sporting event state super hero

5 A Radio Class 5  A Radio class would specify that a radio has  an on/off setting  a station setting  a volume setting  an on/off control  a station select control  a volume setting control.  This is a definition of a radio.

6 A Radio Object 6  A radio object of the Radio class  what do we know about such an object?  how many radio objects are there?  how many radio classes are there?  More precisely we say:  An instance of the Radio class  We instantiate Radio….

7 Class Diagrams 7  A class is a definition of a data type  Class diagrams are “simple” ways of representing the definition without programming  UML is a formal way of drawing class diagrams (and hence defining a class)  Classes can also be defined in Java Class Name Instance Variables Methods A box represents a classThe top “slot” contains the class nameThe middle “slot” contains attributesThe bottom “slot” contains operations

8 Class Diagrams 8 Class Name Instance Variables Methods PortableCDPlayer boolean containsDisk int totalNumberOfTracks int trackPlaying int secondsPlayed void insertCD() void removeCD() void play() void stop() void pause() void skipToNextTrack() EXERCISE: Draw a class diagram for a Vehicle class

9 Our first program…. 9  Consider a DrawingTool object  The class diagram is given below: DrawingTool «constructor» DrawingTool() «update» void moveForward() void turnClockwise() void dontDraw() void draw() If you were handed a DrawingTool object, what kinds of things could you do with it?

10 Method Calls 10  The method call is the basic programming instruction in Java  A method call is a command to an object to “do something” Syntax of method calls: objectReference.methodName(); objectReference refers to an object methodName is the name of any void operation supported by the object Assume “myPlayer” refers to a PortableCDPlayer Object Are the following valid method calls? myPlayer.insertCD(); myPlayer.draw() myPlayer.stop(); myPlayer.accelerate(); …

11 Example 11  Write a sequence of statements to draw a square using a DrawingTool object referred to as “pen”: pen.draw(); pen.moveForward(); pen.turnClockwise(); pen.moveForward(); pen.turnClockwise(); pen.moveForward(); pen.turnClockwise(); pen.moveForward(); pen.draw(); pen.moveForward(); pen.turnClockwise(); pen.moveForward(); pen.turnClockwise(); pen.moveForward(); pen.turnClockwise(); pen.moveForward(); DrawingTool «constructor» DrawingTool() «update» void moveForward() void turnClockwise() void dontDraw() void draw()

12 Declaration and Instantiation 12  Objects don’t exist until they are created  This is known as “instantiation”  Attempting to call a method on an un-created object is an error! Syntax of object creation: objectName = new ClassName(); objectName is the name of an object ClassName is the name of the class of object that is being constructed Example: how to create a DrawingTool named pen?

13 Declaration and Instantiation 13  Objects can’t be created until they are declared  THE GOLDEN RULE OF PROGRAMMING  DECLARE before creating  CREATE before using  USE Syntax of instance variable declaration: modifier ClassName objectName; modifier is either private, public, or protected ClassName is the name of the class of the object to be created objectName is the name of the object to be constructed Example: how to declare, create, and use a DrawingTool named pen?

14 Our first ‘real’ Class InstanceVarDecls StatementSequence ImportDecls Method publicclassClassName{ publicClassName(){ } } 14

15 Our first ‘real’ class public class Driver { private DrawingTool pen; public Driver() { pen = new DrawingTool(); pen.draw(); pen.moveForward(); pen.turnClockwise(); pen.moveForward(); pen.turnClockwise(); pen.moveForward(); pen.turnClockWise(); pen.moveForward(); } 15


Download ppt "INTRO TO OO Object lessons on Objects. Objects 2  In Java, everything has a type  Primitive types (numbers / booleans / chars)  Complex types (classes)"

Similar presentations


Ads by Google