1 DIG 3134 – Lecture 16 Objects and Classes and The Rest of the Course Michael Moshell University...
-
Upload
emma-stevens -
Category
Documents
-
view
215 -
download
1
Transcript of 1 DIG 3134 – Lecture 16 Objects and Classes and The Rest of the Course Michael Moshell University...
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
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
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
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]
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
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
7
nouns: represent THINGS (information, status)
scalarsarraysstrings
verbs: represent ACTIONS (work, algorithm, process)
functions
Some metaphors
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
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
10
Objects and Classes:Conferences
Object Oriented Programs, Systems, Languages and
Applications (OOPSLA) – 1986 (Portland, OR)\
- (I attended all the OOPSLAs through 2005) -
11
Objects and Classes:Key Concepts
Inheritance and Encapsulation
12
Objects and Classes:Key Concepts
Inheritance: A Class Hierarchy
Vehicle
Automobile Aircraft Boat
HelicopterFixedWing
Car Truck
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
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
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'
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'
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
18
Encapsulation: Methods
You can think of 'traditional' programming with data structuresand functions via these metaphors.
DATA:bullets FUNCTIONS:guns
gunpics.net
rt66.com
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
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
21
Encapsulation: Methods
Example: Traditional
$horse=array (0,3,2,4...);
function draw($thing){ //loops etc to draw $thing}
// MAIN:draw($horse);
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();
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
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.
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
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
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
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
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
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
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
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
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
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)
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 ...)
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.