intro to oo object lessons on objects. objects 2 in java, everything has a type primitive types...

15
INTRO TO OO Object lessons on Objects

Upload: oscar-green

Post on 29-Dec-2015

222 views

Category:

Documents


1 download

TRANSCRIPT

INTRO TO OO

Object lessons on Objects

2

Objects

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

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

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 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

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

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 class

The top “slot” contains the class name

The middle “slot” contains attributes

The bottom “slot” contains operations

8

Class DiagramsClass Name

Instance Variables

Methods

PortableCDPlayer

boolean containsDiskint totalNumberOfTracksint trackPlayingint secondsPlayed

void insertCD()void removeCD()void play()void stop()void pause()void skipToNextTrack()

EXERCISE: Draw a class diagram for a Vehicle class

EXERCISE: Draw a class diagram for a Vehicle class

9

Our first program….

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

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 objectmethodName is the name of any void operation supported by the object

Assume “myPlayer” refers to a PortableCDPlayer ObjectAre the following valid method calls?

myPlayer.insertCD();myPlayer.draw()myPlayer.stop();myPlayer.accelerate();…

11

Example 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();

DrawingTool

«constructor» DrawingTool()

«update» void moveForward() void turnClockwise() void dontDraw() void draw()

12

Declaration and Instantiation

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 objectClassName is the name of the class of object that is being constructed

Example: how to create a DrawingTool named pen?

13

Declaration and Instantiation

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 protectedClassName is the name of the class of the object to be createdobjectName is the name of the object to be constructed

Example: how to declare, create, and use a DrawingTool named pen?

Our first ‘real’ Class

InstanceVarDecls

StatementSequence

ImportDecls

Method

public class ClassName {

public ClassName ( ) {

}

}

14

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