360 likes | 370 Views
This lecture covers the basic concepts of objects and classes, as well as the topics that will be covered in the final exam, including databases, XML, graphics, PDF, CURL, and Excel. It also discusses advanced topics such as recursion, array sorting, and SQL queries.
E N D
Media Software Design 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)
Our objectives today: 1. Plan the rest of the course 2. Basic object & class concepts Topics for final exam: Databases, plus: Objects: a different way to 'package' software XML: a different way to 'package' information Graphics: creating images with the GD library PDF: creating documents with the tcpdf library CURL: calling other programs and websites Excel: reading and writing Excel files Advanced topics: Recursion, Array sorting, SQL queries
Our objectives today: 1. Plan the rest of the course 2. Basic object & class concepts 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
Some metaphors 23 scalar variable: a box with something in it (number, string, resource) $x 23 $x $list[1] $list[1] 23 array variable: a cabinet with drawers each drawer has a label (index) $list[2] Joe $list[2] $list[3] 97.2 $list[3]
Some metaphors $age['Joe'] 23 associative array: a cabinet with drawers having 'text string' labels $age['Ann'] 64 $age['Joe'] $age['Ann']] $age['Moe'] 97 $age['Moe'] text string: an array of characters The rain in Spain
Some metaphors function: a machine with inputs, internal storage and outputs 4 1 numnum $j $result . . . function numnum($k,$n) {var $j, $result; for ($j=1; $j<=$k; $j++) { if ($n==1) $result.="One"; else $result.="Two"; } return $result; }# numnum . . . OneOneOneOne
Some metaphors nouns: represent THINGS (information, status) scalars arrays strings verbs: represent ACTIONS (work, algorithm, process) functions
Some metaphors functions can contain local variables (not known to outer world) 4 1 This kind of "encapsulation" provides some protection and privacy. Otherwise, my $j is also your $j, and it's a mess! numnum $j $result . . . . . . OneOneOneOne
Objects and Classes: Motivation As programs got bigger in the 1970s, programming got MUCH harder. There were spectacular project failures. example: Therac_25 Canadian Radiation Therapy Software design error caused massive overdose, killing patients 50quidsoundboy.net
Objects and Classes: Conferences Object Oriented Programs, Systems, Languages and Applications (OOPSLA) – 1986 (Portland, OR) \ - (I attended all the OOPSLAs through 2005) -
Objects and Classes: Key Concepts Inheritance and Encapsulation
Objects and Classes: Key Concepts Inheritance: A Class Hierarchy Vehicle Automobile Aircraft Boat FixedWing Helicopter Car Truck
Objects and Classes: Key Concepts Inheritance: A Class Hierarchy brand: string driver: string Vehicle Automobile tiresize:string licensestate:string licensenumber:string Truck Car nrofwheels:integer length:number nrdoors:integer sunroof:boolean
Objects and Classes: Key Concepts Inheritance: A Class Hierarchy brand: string driver: string Vehicle Automobile tiresize:string licensestate:string licensenumber:string Class Truck Car nrofwheels:integer length:number nrdoors:integer sunroof:boolean
Objects and Classes: Key Concepts Inheritance: A Class Hierarchy brand: string driver: string Vehicle Automobile tiresize:string licensestate:string licensenumber:string Subclass of 'Vehicle' Truck Car nrofwheels:integer length:number nrdoors:integer sunroof:boolean
Objects and Classes: Key Concepts Inheritance: A Class Hierarchy brand: string driver: string Vehicle Automobile tiresize:string licensestate:string licensenumber:string Subclass of 'Automobile' Truck Car nrofwheels:integer length:number nrdoors:integer sunroof:boolean
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 have COMPONENTS like the hardware folks. hytek.in images.yourdictionary.com
Encapsulation: Methods You can think of 'traditional' programming with data structures and functions via these metaphors. DATA:bullets FUNCTIONS:guns gunpics.net rt66.com
Encapsulation: Methods You can think of 'traditional' programming with data structures and functions via these metaphors. DATA:bullets FUNCTIONS:guns DATA:ingredients FUNCTION:cookers gunpics.net rt66.com 05.com ocdeals.com
Encapsulation: Methods You can think of Object Oriented software with objects and their methods, with these metaphors Self-operating weapons: Self-heating MREs: ejogjabelajar.com en.wikipedia.org amazon.com
Encapsulation: Methods Example: Traditional $horse=array (0,3,2,4...); function draw($thing) { //loops etc to draw $thing } // MAIN: draw($horse);
Encapsulation: Methods 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(); Example: Traditional $horse=array (0,3,2,4...); function draw($thing) { //loops etc to draw $thing } // MAIN: draw($horse);
Encapsulation: Methods 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(); horse Draw
Encapsulation: Methods 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(); horse Draw horse is an instance of class Animal. Making one is called instantiation.
Encapsulation: Methods The methods are really functions that are built into the objects. Like the handle on the hand grenade or the trigger on the land mine or the tear-strip on the MRE horse Draw
Encapsulation: Advantages The methods can never get "lost" from the data (in a complex project, or at some later time) because the data and the method are built together. So they are guaranteed to work together. horse Draw
Getting Data into Objects • 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 • 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 in and out of Objects • 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 • $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
Taking Control of Inputs: private • 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 }
'this' refers to the object itself • 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
The __construct function • 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
islands.php (for study) 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.
good2islands.php (challenge problem) Level 3: Write a function to recognize a 2x2 island in the ocean. 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 ...)
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. -36 -