Intro to OO - PowerPoint PPT Presentation

1 / 15
About This Presentation
Title:

Intro to OO

Description:

Spiderman belongs to the class of ... Write a sequence of statements to draw a square using a DrawingTool object referred to as 'pen' ... – PowerPoint PPT presentation

Number of Views:59
Avg rating:3.0/5.0
Slides: 16
Provided by: Kenny77
Category:
Tags: draw | how | intro | spiderman | to

less

Transcript and Presenter's Notes

Title: Intro to OO


1
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 objects 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 indicator
  • a station indicator
  • a volume indicator
  • 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

8
Class Diagrams
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.
  • Consider a DrawingTool object
  • The class diagram is given below

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.methodNam
e() 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() my
Player.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.move
Forward() pen.turnClockwise() pen.moveForward()
12
Declaration and Instantiation
  • Objects dont 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
  • Objects cant 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
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()
Write a Comment
User Comments (0)
About PowerShow.com