1 DIG 3134 – Lecture 16 Objects and Classes and The Rest of the Course Michael Moshell University...

36
1 DIG 3134 – Lecture 16 Objects and Classes and The Rest of the Course Michael Moshell University of Central Florida (Parts of this lecture are courtesy of Jon Friskics) Media Software Design

Transcript of 1 DIG 3134 – Lecture 16 Objects and Classes and The Rest of the Course Michael Moshell University...

Page 1: 1 DIG 3134 – Lecture 16 Objects and Classes and The Rest of the Course Michael Moshell University of Central Florida (Parts of this lecture are courtesy.

1

DIG 3134 – Lecture 16

Objects and Classesand

The Rest of the Course

Michael MoshellUniversity of Central Florida

(Parts of this lecture are courtesy of Jon Friskics)

Media Software Design

Page 2: 1 DIG 3134 – Lecture 16 Objects and Classes and The Rest of the Course Michael Moshell University of Central Florida (Parts of this lecture are courtesy.

2

Our objectives today:1. Plan the rest of the course2. Basic object & class concepts

Topics for final exam: Databases, plus:

Objects: a different way to 'package' softwareXML: a different way to 'package' informationGraphics: creating images with the GD library

PDF: creating documents with the tcpdf libraryCURL: calling other programs and websitesExcel: reading and writing Excel files

Advanced topics: Recursion, Array sorting, SQL queries

Page 3: 1 DIG 3134 – Lecture 16 Objects and Classes and The Rest of the Course Michael Moshell University of Central Florida (Parts of this lecture are courtesy.

3

Strategy for Final Exam: Database, plus (6-choose-5).Each question will look approximately like this:

Level 1 (60%) read and hand-simulate small program

Level 2 (+20%) answer a conceptual question

Level 3 (+20%) write or repair a small program

Our objectives today:1. Plan the rest of the course2. Basic object & class concepts

Page 4: 1 DIG 3134 – Lecture 16 Objects and Classes and The Rest of the Course Michael Moshell University of Central Florida (Parts of this lecture are courtesy.

4

Some metaphors

23$x scalar variable:a box with something in it(number, string, resource)

$x

$list[1]

23

23 array variable:a cabinet with drawerseach drawer has a label(index)

$list[2]

$list[3]

$list[1]

Joe$list[2]

97.2$list[3]

Page 5: 1 DIG 3134 – Lecture 16 Objects and Classes and The Rest of the Course Michael Moshell University of Central Florida (Parts of this lecture are courtesy.

5

$age['Joe'] 23 associative array:a cabinet with drawershaving 'text string' labels

$age['Ann']]

$age['Moe']

$age['Joe']

64$age['Ann']

97$age['Moe']

text string:an array of characters

The rain in Spain

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16

T h e   r a i n   i n   S p a i n

Some metaphors

Page 6: 1 DIG 3134 – Lecture 16 Objects and Classes and The Rest of the Course Michael Moshell University of Central Florida (Parts of this lecture are courtesy.

6

function:a machine with inputs,internal storage andoutputs

function numnum($k,$n){var $j, $result;

for ($j=1; $j<=$k; $j++){ if ($n==1) $result.="One"; else $result.="Two";}return $result;

}# numnum

$j

$result . . .

. . .numnum

4 1

OneOneOneOne

Some metaphors

Page 7: 1 DIG 3134 – Lecture 16 Objects and Classes and The Rest of the Course Michael Moshell University of Central Florida (Parts of this lecture are courtesy.

7

nouns: represent THINGS (information, status)

scalarsarraysstrings

verbs: represent ACTIONS (work, algorithm, process)

functions

Some metaphors

Page 8: 1 DIG 3134 – Lecture 16 Objects and Classes and The Rest of the Course Michael Moshell University of Central Florida (Parts of this lecture are courtesy.

8

functions can contain local variables (not known to outer world)

$j

$result . . .

. . .numnum

4 1

OneOneOneOne

This kind of"encapsulation"provides someprotection andprivacy.

Otherwise, my $jis also your $j, andit's a mess!

Some metaphors

Page 9: 1 DIG 3134 – Lecture 16 Objects and Classes and The Rest of the Course Michael Moshell University of Central Florida (Parts of this lecture are courtesy.

9

Objects and Classes:Motivation

As programs got bigger in the 1970s, programming got MUCH harder.There were spectacular project failures.

example:

Therac_25 Canadian RadiationTherapy

Software design error causedmassive overdose,killing patients 50quidsoundboy.net

Page 10: 1 DIG 3134 – Lecture 16 Objects and Classes and The Rest of the Course Michael Moshell University of Central Florida (Parts of this lecture are courtesy.

10

Objects and Classes:Conferences

Object Oriented Programs, Systems, Languages and

Applications (OOPSLA) – 1986 (Portland, OR)\

- (I attended all the OOPSLAs through 2005) -

Page 11: 1 DIG 3134 – Lecture 16 Objects and Classes and The Rest of the Course Michael Moshell University of Central Florida (Parts of this lecture are courtesy.

11

Objects and Classes:Key Concepts

Inheritance and Encapsulation

Page 12: 1 DIG 3134 – Lecture 16 Objects and Classes and The Rest of the Course Michael Moshell University of Central Florida (Parts of this lecture are courtesy.

12

Objects and Classes:Key Concepts

Inheritance: A Class Hierarchy

Vehicle

Automobile Aircraft Boat

HelicopterFixedWing

Car Truck

Page 13: 1 DIG 3134 – Lecture 16 Objects and Classes and The Rest of the Course Michael Moshell University of Central Florida (Parts of this lecture are courtesy.

13

Objects and Classes:Key Concepts

Inheritance: A Class Hierarchy

Vehicle

Automobile

Car Truck

brand: stringdriver: string

tiresize:stringlicensestate:stringlicensenumber:string

nrofwheels:integerlength:number

nrdoors:integersunroof:boolean

Page 14: 1 DIG 3134 – Lecture 16 Objects and Classes and The Rest of the Course Michael Moshell University of Central Florida (Parts of this lecture are courtesy.

14

Objects and Classes:Key Concepts

Inheritance: A Class Hierarchy

Vehicle

Automobile

Car Truck

brand: stringdriver: string

tiresize:stringlicensestate:stringlicensenumber:string

nrofwheels:integerlength:number

nrdoors:integersunroof:boolean

Class

Page 15: 1 DIG 3134 – Lecture 16 Objects and Classes and The Rest of the Course Michael Moshell University of Central Florida (Parts of this lecture are courtesy.

15

Objects and Classes:Key Concepts

Inheritance: A Class Hierarchy

Vehicle

Automobile

Car Truck

brand: stringdriver: string

tiresize:stringlicensestate:stringlicensenumber:string

nrofwheels:integerlength:number

nrdoors:integersunroof:boolean

Subclass of'Vehicle'

Page 16: 1 DIG 3134 – Lecture 16 Objects and Classes and The Rest of the Course Michael Moshell University of Central Florida (Parts of this lecture are courtesy.

16

Objects and Classes:Key Concepts

Inheritance: A Class Hierarchy

Vehicle

Automobile

Car Truck

brand: stringdriver: string

tiresize:stringlicensestate:stringlicensenumber:string

nrofwheels:integerlength:number

nrdoors:integersunroof:boolean

Subclass of'Automobile'

Page 17: 1 DIG 3134 – Lecture 16 Objects and Classes and The Rest of the Course Michael Moshell University of Central Florida (Parts of this lecture are courtesy.

17

Inheritance: Advantages

The superclass can contain attributes (values) and methods(functions) that can be used by lots of subclasses.

This greatly enhances RE-USE and holds down costs.

The "dream" of software design is to be able to haveCOMPONENTS like the hardware folks.

hytek.in

images.yourdictionary.com

Page 18: 1 DIG 3134 – Lecture 16 Objects and Classes and The Rest of the Course Michael Moshell University of Central Florida (Parts of this lecture are courtesy.

18

Encapsulation: Methods

You can think of 'traditional' programming with data structuresand functions via these metaphors.

DATA:bullets FUNCTIONS:guns

gunpics.net

rt66.com

Page 19: 1 DIG 3134 – Lecture 16 Objects and Classes and The Rest of the Course Michael Moshell University of Central Florida (Parts of this lecture are courtesy.

19

Encapsulation: Methods

You can think of 'traditional' programming with data structuresand functions via these metaphors.

DATA:bullets FUNCTIONS:guns

DATA:ingredients FUNCTION:cookers

gunpics.net

rt66.com

ocdeals.com

05.com

Page 20: 1 DIG 3134 – Lecture 16 Objects and Classes and The Rest of the Course Michael Moshell University of Central Florida (Parts of this lecture are courtesy.

20

Encapsulation: Methods

You can think of Object Oriented software with objectsand their methods, with these metaphors

Self-operating weapons:

Self-heating MREs:

en.wikipedia.org

ejogjabelajar.com

amazon.com

Page 21: 1 DIG 3134 – Lecture 16 Objects and Classes and The Rest of the Course Michael Moshell University of Central Florida (Parts of this lecture are courtesy.

21

Encapsulation: Methods

Example: Traditional

$horse=array (0,3,2,4...);

function draw($thing){ //loops etc to draw $thing}

// MAIN:draw($horse);

Page 22: 1 DIG 3134 – Lecture 16 Objects and Classes and The Rest of the Course Michael Moshell University of Central Florida (Parts of this lecture are courtesy.

22

Encapsulation: Methods

Example: Traditional

$horse=array (0,3,2,4...);

function draw($thing){ //loops etc to draw $thing}

// MAIN:draw($horse);

Example: OO

class GraphicObject{ public function draw()

{ // details }}class Animal extends

GraphicObject{ // details of animals }

// MAIN:$horse=new Animal(0,3,2,4...);

$horse->draw();

Page 23: 1 DIG 3134 – Lecture 16 Objects and Classes and The Rest of the Course Michael Moshell University of Central Florida (Parts of this lecture are courtesy.

23

Encapsulation: MethodsExample: OO

class GraphicObject{ public function draw()

{ // details }}class Animal extends

GraphicObject{ // details of animals }

// MAIN:$horse=new Animal(0,3,2,4...);

$horse->draw();

horse

Draw

Page 24: 1 DIG 3134 – Lecture 16 Objects and Classes and The Rest of the Course Michael Moshell University of Central Florida (Parts of this lecture are courtesy.

24

Encapsulation: MethodsExample: OO

class GraphicObject{ public function draw()

{ // details }}class Animal extends

GraphicObject{ // details of animals }

// MAIN:$horse=new Animal(0,3,2,4...);

$horse->draw();

horse

Draw

horse is an instance of class Animal.Making one is called

instantiation.

Page 25: 1 DIG 3134 – Lecture 16 Objects and Classes and The Rest of the Course Michael Moshell University of Central Florida (Parts of this lecture are courtesy.

25

Encapsulation: Methods

The methods are reallyfunctions that arebuilt into the objects.

Like the handle on the hand grenade

or the trigger on the land mine

or the tear-strip on theMRE

horse

Draw

Page 26: 1 DIG 3134 – Lecture 16 Objects and Classes and The Rest of the Course Michael Moshell University of Central Florida (Parts of this lecture are courtesy.

26

Encapsulation: Advantages

The methods can neverget "lost" from the data

(in a complex project, orat some later time)

because the data and themethod are built together.

So they are guaranteedto work together.

horse

Draw

Page 27: 1 DIG 3134 – Lecture 16 Objects and Classes and The Rest of the Course Michael Moshell University of Central Florida (Parts of this lecture are courtesy.

27

– Variables inside objects are called properties

– Properties are very similar to variables, but they have special OOP-related controls ("safety covers")

– Visibility: 3 kinds of Properties

• Public – global property – can be accessed anywhere

• Private – property can only be accessed from within the enclosing class

• Protected – property can only be accessed from within either the

enclosing class or any subclasses.

Getting Data into Objects

Page 28: 1 DIG 3134 – Lecture 16 Objects and Classes and The Rest of the Course Michael Moshell University of Central Florida (Parts of this lecture are courtesy.

28

– Properties get defined in a class

– class Person {

public $firstname,

$lastname,

$phone,

$email;

}

– Every object instance we create will now contain those four properties

Getting Data into Objects

Page 29: 1 DIG 3134 – Lecture 16 Objects and Classes and The Rest of the Course Michael Moshell University of Central Florida (Parts of this lecture are courtesy.

29

– Once properties are defined, you can access them with PHP’s object notation

• $person1 = new Person();

print "who?".$person1->firstname. " ".

$person1->lastname;

Prints who?

because we haven't yet put anything into the properties.

Getting Data in and out of Objects

Page 30: 1 DIG 3134 – Lecture 16 Objects and Classes and The Rest of the Course Michael Moshell University of Central Florida (Parts of this lecture are courtesy.

30

$person1 = new Person();

Because the properties are declared as public, we can put data into the object the same way (no safety controls)

$person1->firstname = “Jonathon”;

$person1->lastname = “Seagull”;

print "who? ".$person1->first_name. " ".

$person1->last_name;

Prints who? Jonathan Seagull

Getting Data in and out of Objects

Page 31: 1 DIG 3134 – Lecture 16 Objects and Classes and The Rest of the Course Michael Moshell University of Central Florida (Parts of this lecture are courtesy.

31

– Here's how a class can protect itself.

class Person{

private $firstname;

private $lastname;

public function setnames($fn,$ln)

{

if (is_numeric($fn))

print "Class Person does not accept numeric first names

like $fn.<br />";

else

$this->firstname=$fn;

//etc for lastname

}

Taking Control of Inputs: private

Page 32: 1 DIG 3134 – Lecture 16 Objects and Classes and The Rest of the Course Michael Moshell University of Central Florida (Parts of this lecture are courtesy.

32

– Here's how a class can protect itself.

class Person{

private $firstname;

private $lastname;

public function setnames($fn,$ln)

{

if (is_numeric($fn))

print "Class Person does not accept numeric first names

like $fn.<br />";

else

$this->firstname=$fn;

//etc for lastname

}

#### Examine the example program: names.php

'this' refers to the object itself

Page 33: 1 DIG 3134 – Lecture 16 Objects and Classes and The Rest of the Course Michael Moshell University of Central Florida (Parts of this lecture are courtesy.

33

– You can automate part of the new-object-creation process:

class Person{

private $firstname;

private $lastname; // NOTE the two underscores __ at beginning

public function __construct($fn,$ln)

{

if (is_numeric($fn))

print "Class Person does not accept numeric first names

like $fn.<br />";

else

$this->firstname=$fn;

//etc for lastname

}

#### Examine names2.php

The __construct function

Page 34: 1 DIG 3134 – Lecture 16 Objects and Classes and The Rest of the Course Michael Moshell University of Central Florida (Parts of this lecture are courtesy.

34

Compare it to the starter kit for Battleship

Same kind of loops (without the Visible array)

but it uses a Class, with Properties and Methods

STUDY LEVELS:

Level 1: Hand simulate the private function 'goodisland'

Level 2: Explain a command like:

$color=$this->grid[$i][$j];

What means 'this'? What data does 'grid' refer to? Why not $grid?

What means "private function"?

What does 'print_r' do?

Level 3: Write a function recognize a 2x2 island in the ocean.

islands.php (for study)

Page 35: 1 DIG 3134 – Lecture 16 Objects and Classes and The Rest of the Course Michael Moshell University of Central Florida (Parts of this lecture are courtesy.

35

Level 3: Write a function to recognize a 2x2 island in the ocean.

good2islands.php (challenge problem)

                            

Specifically, good2island($x,$y) returns TRUE if ($x,$y) is pointing to

one of the four black squares that constitute a 2x2 black block

entirely surrounded by white squares.

I have provided a mini-essay on problem solving (on the course website)

that walks through a solution to this problem.

Level 4: Use good2island to COUNT the 2x2 islands in the ocean....

(It's easier than it may seem at first glance ...)

Page 36: 1 DIG 3134 – Lecture 16 Objects and Classes and The Rest of the Course Michael Moshell University of Central Florida (Parts of this lecture are courtesy.

36

FOR THURSDAY:The usual shoot-out model:GET your Group's BEST GAME ready to play!

If you need help with Project 3, come SEE ME.