1 / 36

Objects and Classes: Introduction, Concepts, and Topics for Final Exam

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.

cwaddell
Télécharger la présentation

Objects and Classes: Introduction, Concepts, and Topics for Final Exam

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 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)

  2. 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

  3. 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

  4. 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]

  5. 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

  6. 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

  7. Some metaphors nouns: represent THINGS (information, status) scalars arrays strings verbs: represent ACTIONS (work, algorithm, process) functions

  8. 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

  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 Radiation Therapy Software design error caused massive 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 FixedWing Helicopter Car Truck

  13. 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

  14. 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

  15. 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

  16. 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

  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 have COMPONENTS like the hardware folks. hytek.in images.yourdictionary.com

  18. Encapsulation: Methods You can think of 'traditional' programming with data structures and functions via these metaphors. DATA:bullets FUNCTIONS:guns gunpics.net rt66.com

  19. 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

  20. 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

  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: 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);

  23. 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

  24. 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.

  25. 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

  26. 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

  27. 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.

  28. 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

  29. 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.

  30. 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

  31. 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 }

  32. '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

  33. 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

  34. 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.

  35. 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 ...)

  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. -36 -

More Related