46
Object-Oriented Programming Lesson 16 McManus COP1006 1

Object-Oriented Programming Lesson 16 McManusCOP10061

Embed Size (px)

Citation preview

Page 1: Object-Oriented Programming Lesson 16 McManusCOP10061

COP1006 1

Object-Oriented Programming

Lesson 16

McManus

Page 2: Object-Oriented Programming Lesson 16 McManusCOP10061

Overview

• OOP– Classes– Objects– Inheritance– Functions

• GUI

• Event-driven Object-Oriented Programming– Events– Interactivity

McManus COP1006 2

Page 3: Object-Oriented Programming Lesson 16 McManusCOP10061

COP1006 3

Basic Concepts

• Procedural– Looks at the

actions or the data but not both at the same time.

– Associated with menu-driven systems

• Object-Oriented– Looks at the data

parts and the actions associated with those data parts as one unit.

– Associated with GUI systems

McManus

Page 4: Object-Oriented Programming Lesson 16 McManusCOP10061

COP1006 4

Object-Oriented – A Definition

• A programming methodology in which the programmer can define not only data types, but also procedures that are automatically associated with them.

McManus

Page 5: Object-Oriented Programming Lesson 16 McManusCOP10061

COP1006 5

Classes

• User-defined types created by programmers

• Contains data as well as the set of methods which manipulate that data.– Data components of a class are called instance

variables.– Nouns are used to create initial set of classes.– Verbs determine what methods to associate

with each class.– Can only contain code--no GUI elements.

• Ex. Books

McManus

Page 6: Object-Oriented Programming Lesson 16 McManusCOP10061

COP1006 6

Objects

• An individual (member) instantiation of a class

• Ex.– Harry Potter and the Sorcerer’s Stone

by J. K. Rowling– It

by Stephen King• More about Classes & Objects later

McManus

Page 7: Object-Oriented Programming Lesson 16 McManusCOP10061

COP1006 7

OO Uses

• Graphical objects (drawing tools)• Mathematical objects

– Vectors, matrices • Input-output devices

– The procedure to draw a line is different to different outputs

• Simulations • Reusable software components

McManus

Page 8: Object-Oriented Programming Lesson 16 McManusCOP10061

COP1006 8

Software Engineering Advantage

• Enables implementation decisions to be made in stages: – top level - focus on how to use a data

object and on what operators (procedures and functions) are needed.

– lower level - concentrate on implementation details.

McManus

Page 9: Object-Oriented Programming Lesson 16 McManusCOP10061

COP1006 9

Software Reusability

• Programmers – create new classes– reuse existing classes

• Software is created by new classes with existing, well-defined, carefully tested, well-documented, widely available components.

• Promotes Rapid Applications Development (RAD)

McManus

Page 10: Object-Oriented Programming Lesson 16 McManusCOP10061

COP1006 10

Data Abstraction & Classes

• Being able to describe an object’s behaviors without being concerned with the implementation details of those behaviors.

• Classes can be replaced without affecting the remainder of the code.

McManus

Page 11: Object-Oriented Programming Lesson 16 McManusCOP10061

COP1006 11

Procedural Example

• Library Checkout System– We would look at:

• Menu-driven program• Data input• Checking out a book• Checking in a book• Printouts of all outstanding books• Printouts of all books• Add, delete, and update book info

McManus

Page 12: Object-Oriented Programming Lesson 16 McManusCOP10061

COP1006 12

Object-Oriented Example

• Library Checkout System– We would look at:

• Books– Recognize Book ID– Recognize the Book’s author– Check out– Check in– Location of book– List all books– Add, delete, and change data on a book

McManus

Page 13: Object-Oriented Programming Lesson 16 McManusCOP10061

13

Looks Yummy!

I’m sure gladI don’t have to eat

this stuff!

Page 14: Object-Oriented Programming Lesson 16 McManusCOP10061

The Restaurant

14

Customer

Waiter (object)

Data: name: Joetables: 1,2tickets: 2

Methods: takeOrder putOrderonTurnstile pickup Order serverOrder

Turnstile (object)

Data: tickets: 1

Methods: isTicketReady add Ticket remove Ticket

Cook (object)

Data: name: Arnold specialties: HamandEggs Pancakes FrenchToast

Private Methods:makeHamandEggsmakePancakesmakeFrenchToast

Public Methods: takeTicketFromTurnstile putOrderOnCounter

Counter (object)

Data:ordersAvailable

Methods:isOrderReadyaddOrderremoveOrder

Messages invoke methods &methods send messages.

Page 15: Object-Oriented Programming Lesson 16 McManusCOP10061

15

Example of Classes of Objects

Menu

Breakfast Menu Dinner MenuLunch Menu

Child’s Menu Adult Menu

Senior MenuSt. Patrick’s

Day Menu

What characteristics should be defined at what level and what would be inherited by each object and to what level?

Page 16: Object-Oriented Programming Lesson 16 McManusCOP10061

COP1006 16

Some OO Terms

McManus

Page 17: Object-Oriented Programming Lesson 16 McManusCOP10061

COP1006 17

Data Abstraction

• Program design technique – focuses on

• the data objects needed by a program and • the operations on those objects.

• It specifies what data objects are needed for a problem without being concerned how the data objects will be represented.

McManus

Page 18: Object-Oriented Programming Lesson 16 McManusCOP10061

COP1006 18

Inheritance

• New data types can be defined based on previously defined objects– sharing code and behavior; and– involves classification of objects

according to shared properties

McManus

Page 19: Object-Oriented Programming Lesson 16 McManusCOP10061

COP1006 19

Animal Kingdom Scientific Classification (very incomplete)

McManus

AnimaliaAnimals

ProtozoaOne-Celled Animals

Parazoa Metazoa

PoriferaSponges

MolluscaMolluscs

ChordataChordates

AnnelidaSegmented Worms

ArthropodaJoint-Footed Animals

UrochordataTunicates

ThaliaceaSalps

AscidiaceaSea Squirts

VertebrataVertebrates

CephalocordataLancelets

ReptiliaReptiles

AmphibiaAmphibians

AgnathaLampreys &

Hagfishes

AvesBirds

MammaliaMammals

TestudinataTortoises

SerpentesSnakes

LacertiliaLizards

ChamaeleonidaeChameleons

IguanidaeNew World Lizards

TubulidendataAardvarks

CetaceaWhales

PrimatesPrimates

ChiropteraBats

HominidaeTupaiidae

ShrewsLemuridae

Lemurs

CrustaceaLobsters

ArachnidaSpiders

InsectaInsects

ChilopodaCentipedes

IsopteraTermites

PsocopteraBooklice

HemipteraBugs

CorixidaeWater-Boatmen

PentatomidaeStink-Bugs

TingidaeLace-Bugs

Page 20: Object-Oriented Programming Lesson 16 McManusCOP10061

COP1006 20

Object Instantiation

• a variable with the same type as the data object– Class - Singly-linked List– Object – A specific Singly-linked List

McManus

Page 21: Object-Oriented Programming Lesson 16 McManusCOP10061

COP1006 21

Information Hiding

• Concealing the details of a low-level module’s implementation from a higher-level module.

• Looks forward to future maintenance by building details into the design phase.

McManus

Page 22: Object-Oriented Programming Lesson 16 McManusCOP10061

COP1006 22

Polymorphism

• The use of different procedures, each with the same name, which are associated with different object types.– Allows each object to recognize the

properties pertinent to itself and to ignore those that are not.

McManus

Page 23: Object-Oriented Programming Lesson 16 McManusCOP10061

COP1006 23

Polymorphism

• Example:– Procedures named draw associated with

types • Point• Circle• Square

– Calling draw for any particular object gets the right drawing procedure for that type.

McManus

Page 24: Object-Oriented Programming Lesson 16 McManusCOP10061

COP1006 24

Polymorphism Example

McManus

Base Class

Generic recordset

Derived Classes

Dynaset-type recordset

Snapshot-type recordset

Table-type recordset

Uses the same name as the abstract data type recordset

Page 25: Object-Oriented Programming Lesson 16 McManusCOP10061

COP1006 25

Classes

• A collection of like items or objects.• Examples

– Books– Clients– Waiter– Cook

McManus

Page 26: Object-Oriented Programming Lesson 16 McManusCOP10061

COP1006 26

Objects

• An instance (member) of a class.• Instances of Books

– Stephen King’s It– Mark Twain’s Tom Sawyer– J.K. Rowling’s Harry Potter

and the Sorcerer’s Stone– Tom Clancy’s Hunt for Red October

McManus

Page 27: Object-Oriented Programming Lesson 16 McManusCOP10061

COP1006 27

Classes of Objects

• Visual Basic’s Toolbox contains controls, each a class of objects.

• Once selected, each instantiation is an object of a particular class of objects.

• Used in GUI’s.

McManus

Page 28: Object-Oriented Programming Lesson 16 McManusCOP10061

COP1006 28

Graphical User Interface (GUI’s)

• Computer screen designed for easy interaction between the user and the computer.

• Response to the demand by users for more user-friendliness in their interactivity with computers.

• Takes up a HUGE amount of space.• Languages that create GUI’s

– C++ and Visual Basic

McManus

Page 29: Object-Oriented Programming Lesson 16 McManusCOP10061

COP1006 29

Events

• Each language has own set of events• Examples of events

– Mousedown– Mouseup– Keydown– Keyup– Double click– Single click

McManus

Page 30: Object-Oriented Programming Lesson 16 McManusCOP10061

COP1006 30

Event Driven Programming

• Use actions or events to trigger the execution of a set of instructions – Sometimes called scripts or modules

• The user dictates the order of program execution--not the programmer.

• The program is instructed as to what actions to perform when events happen to those objects.

McManus

Page 31: Object-Oriented Programming Lesson 16 McManusCOP10061

COP1006 31

Event Procedures

• Events are triggered by messages– When an event occurs, a message is generated

that describes what action occurred. – When the control receives the message, it

generates an appropriate message response.• Event Monitoring

– The state that the system is in after an event has completed execution but before a new event is detected.

McManus

Page 32: Object-Oriented Programming Lesson 16 McManusCOP10061

COP1006 32

Interactivity

• Exists when using event-driven languages

• Exists between screens and/or between the user and the computer.

• Produces a non-linear application.• Uses navigational tools

McManus

Page 33: Object-Oriented Programming Lesson 16 McManusCOP10061

COP1006 33

Navigational Tools

• Include – Command buttons that can

• Move forward or backward• Quit the program• Menu buttons

– Hot spots• Planning MUST be used to make the

navigation user-friendly.

McManus

Page 34: Object-Oriented Programming Lesson 16 McManusCOP10061

COP1006 34

OO Program Design

Lesson17

McManus

Page 35: Object-Oriented Programming Lesson 16 McManusCOP10061

COP1006 35

Interface Design

• Success is measured by the amount of planning and inclusion of the user in the development process.

• Include the user to give them warm fuzzies– This leads to buy-in by the client and

user.

McManus

Page 36: Object-Oriented Programming Lesson 16 McManusCOP10061

COP1006 36

Interface Design Rules

• Never lose sight of the purpose or the message.

• Keep it simple!

• Be consistent.

• Design the navigation with the user in mind.

• Understand when you can break the rules.

McManus

Page 37: Object-Oriented Programming Lesson 16 McManusCOP10061

COP1006 37

OO Design Issues

• Determine the– Requirements of the program

• Input and output– Classes of data

• Including properties and their attributes and the functions (operations) of each class

– Interactivity• Between classes and between the functions within

classes– Instructions to accomplish each function.

McManus

Page 38: Object-Oriented Programming Lesson 16 McManusCOP10061

COP1006 38

The Well-designed Interface

• Is– Pleasant to look at– Functional– Provides easy access to its functions– Provides easy to understand instructions on the

functionality of the application– Allows the user to move through the program

non-linearly– Provides the ability to quit the program

McManus

Page 39: Object-Oriented Programming Lesson 16 McManusCOP10061

COP1006 39

Designing an Event-Driven Object-Oriented Application

McManus

Page 40: Object-Oriented Programming Lesson 16 McManusCOP10061

COP1006 40

The Steps…

• Define the audience & the environment.– Know your audience– Know the environment in which the

program will be set

McManus

Page 41: Object-Oriented Programming Lesson 16 McManusCOP10061

COP1006 41

The Steps…

• Create the storyboards– Useful in designing the way your screen will

appear to the user.– It’s easier to take an eraser to paper than

redoing the screen – Even loosely defined, this is basically the same

as creating a structure chart for the project– Example of storyboard

http://saulcarliner.home.att.net/id/storyboard.htm#screen

McManus

Page 42: Object-Oriented Programming Lesson 16 McManusCOP10061

COP1006 42

Storyboard Example

McManus

Page 43: Object-Oriented Programming Lesson 16 McManusCOP10061

COP1006 43

The Steps…

• Define all objects– Screens are made up of objects– Developed

• Within the software application• In through an outside source

– Create an object dictionary• Similar to a data dictionary

McManus

Page 44: Object-Oriented Programming Lesson 16 McManusCOP10061

COP1006 44

The Steps…

• Define the interactivity between screens.– Chart the interaction between the

different screens. • Some may loop back on themselves• Some may only go forward through a

process

McManus

Page 45: Object-Oriented Programming Lesson 16 McManusCOP10061

COP1006 45

The Steps…

• Define the “scripts” (or modules)– Short sets of instructions attached to

objects (or to the entire application)– Includes the private functions that give

the object its functionality

McManus

Page 46: Object-Oriented Programming Lesson 16 McManusCOP10061

COP1006 46McManus