csci 1101a
play

CSCI-1101A Object-Oriented Programming (OOP) Mohammad T . Irfan - PDF document

5/4/15 CSCI-1101A Object-Oriented Programming (OOP) Mohammad T . Irfan Reading: Handout (also on Blackboard) Advanced and Optional: Ch 8 (Guttag) Optional: Ch 16 (Guzdial) OOP u A different style of programming from functional


  1. 5/4/15 ¡ CSCI-1101A Object-Oriented Programming (OOP) Mohammad T . Irfan Reading: Handout (also on Blackboard) Advanced and Optional: Ch 8 (Guttag) Optional: Ch 16 (Guzdial) OOP u A different style of programming from functional decomposition u Focuses on “objects” 1 ¡

  2. 5/4/15 ¡ Some history u Smalltalk language (1973) u Designed by Alan Kay, Dan Ingalls, and Adele Goldberg at Xerox PARC u Alan Kay u Coined in OOP u “Cells” analogy u Complex and large-scale program u Robust Alan Kay u Reuse code ACM Turing Award (2003) Why OOP: building large projects 1. Functional decomposition What we’ve been doing so far u 2. Abstraction: OOP u For really large projects 2 ¡

  3. 5/4/15 ¡ Flappy Bird http://youtu.be/YHH2101OFfI 3 ¡

  4. 5/4/15 ¡ Objects vs. Classes u Objects are the “things” (nouns) that we see/perceive/model u Properties/attributes (nouns) – instance variables u Behavior/actions (verbs) – methods u Classes u Factory for creating objects u Template or blueprint for creating objects u Analogy u https://www.youtube.com/watch?v=__u2ei6-M- U&spfreload=10 Turtle Objects Demo 4 ¡

  5. 5/4/15 ¡ Codes def turtleDemo(): zoo = World() bob = Turtle(zoo) bob.setColor(blue) ann = Turtle(zoo) ann.setColor(red) bob.forward() ann.turnRight() ann.forward(200) bob.turnLeft() bob.backward(-150) bob.forward() Key points u ann and bob are objects of the Turtle class u Two objects of the same class have different properties/attributes u Two turtles with different colors, position, etc. u But they share the same behavior u forward(), turn(), etc. 5 ¡

  6. 5/4/15 ¡ Writing our own class Dog u Any dog has a name. It barks. 6 ¡

  7. 5/4/15 ¡ Instance variable: Each “object” may have a different value Method: Implements behavior Items in Hannaford’s inventory u Hannaford’s inventory consists of many items. Each item has a name, barcode, price, and quantity (in stock). Hannaford can change the price of an item anytime. The quantity is also changed when Hannaford sells an item or restocks it. 7 ¡

  8. 5/4/15 ¡ Item class Instance variable: Each object may have a different value Method: Implements behavior >= Item class (continued) u Alternative to printInfo(…): def __str__(self): return “Item Name: ” + self.name + “Price: ” + self.price + “Stock: ” + self.quantity u Enables print command for the object print objectName #The returned string is printed 8 ¡

  9. 5/4/15 ¡ main() function outside of the Item class Problems u Bank account u A bank account has a name and a balance, among other information. The balance gets changed when the account holder withdraws or deposits money. u Slide show (from Guzdial’s Ch 16) u Each slide in a slide show has a picture and some music. Each slide also has a show functionality, which shows the picture and plays the music. 9 ¡

  10. 5/4/15 ¡ Hannaford’s inventory u Create another class named Inventory that keeps a list of Item objects. The list is initially empty, but objects of the Item class can be added to it. The Inventory class will have certain functionalities like finding the lowest price item and finding the minimum stock item. Announcement u 2048 Lab u Deadline extended to Wednesday, May 6 (9pm) u Exam 5 on Wednesday, May 6 u Topics u Recursion (see the slides on recursion) u Watch the video lectures u OOP: up to today’s (Monday’s) class u Practice the codes from the slides and the handout (uploaded to Blackboard) u Additional practice (optional): Guttag’s book 10 ¡

  11. 5/4/15 ¡ Video lectures (recursion) u Counting the number of vowels in a string (5 min) u http://bit.ly/1qEIhsX u Series sum (4 min) u http://bit.ly/10Yz4Fc u Binary Search (15 min) u http://bit.ly/1xj43dg Office Hours u Everyday 3—5pm during the reading period u Check emails 11 ¡

  12. 5/4/15 ¡ All-or-nothing card game Suits 12 ¡

  13. 5/4/15 ¡ Ranks All-or-nothing card game u This is a human vs. computer game. The game starts with a deck of cards, which has 52 cards in it. Each card has one of four possible suits and one of 13 possible ranks. u This is how the game is played. The dealer shuffles the deck of cards. Neither 1. the computer nor the human can see any of the cards. The computer picks a card from the deck 2. randomly. The human can choose any of the 51 remaining 3. cards from the deck (without seeing, of course). Whoever has the better suit wins. If both suits 4. are the same, the player with the better rank wins. 13 ¡

  14. 5/4/15 ¡ Questions u What classes would you design? u What would be their instance variables and methods? u How would you simulate playing the game? 14 ¡

  15. 5/4/15 ¡ OOP concepts u Encapsulation u Inheritance u Polymorphism u Abstraction 15 ¡

  16. 5/4/15 ¡ Final exam u Monday, 5/11 at 9am in Searles 217 u Topics u The basics of Python (Guttag’s Ch 2, 3, Section 4.1) u Text and files (Chapter 10, class, video lecture, phonebook lab) u Recursion (Chapter 15.4, class, video lectures, Optional: Guttag’s Section 4.3– see pdf) u Object-oriented programming u [Nothing on picture/sound] For Data Structures u Head start u Learn the syntax (grammars) of C and C++ u Python to C++ transition guide: http://personal.denison.edu/~krone/cs173/files/ PythontoC++.pdf u C/C++ Tutorial: Practice codes from http://www.cprogramming.com/tutorial/c- tutorial.html u C++ compiler u Netbeans (among a host of other options) u https://netbeans.org/features/cpp/index.html 16 ¡

  17. 5/4/15 ¡ Outlook u NY Times: May 2, 2014 17 ¡

  18. 5/4/15 ¡ Thank You! 18 ¡

Recommend


More recommend