COSC 235: Programming and Problem Solving Ch. 4 or… Everything is an Object Instructor: Dr. X.
-
Upload
jordan-francis-parsons -
Category
Documents
-
view
215 -
download
2
Transcript of COSC 235: Programming and Problem Solving Ch. 4 or… Everything is an Object Instructor: Dr. X.
![Page 1: COSC 235: Programming and Problem Solving Ch. 4 or… Everything is an Object Instructor: Dr. X.](https://reader035.fdocuments.us/reader035/viewer/2022070400/56649f125503460f94c25f85/html5/thumbnails/1.jpg)
COSC 235: Programming and Problem Solving
Ch. 4 or… Everything is an ObjectInstructor: Dr. X
![Page 2: COSC 235: Programming and Problem Solving Ch. 4 or… Everything is an Object Instructor: Dr. X.](https://reader035.fdocuments.us/reader035/viewer/2022070400/56649f125503460f94c25f85/html5/thumbnails/2.jpg)
Goals
• Understand about objects as computational entities
• Understand and use methods and instance variables
• Use the dot notation• Use the graphics library
![Page 3: COSC 235: Programming and Problem Solving Ch. 4 or… Everything is an Object Instructor: Dr. X.](https://reader035.fdocuments.us/reader035/viewer/2022070400/56649f125503460f94c25f85/html5/thumbnails/3.jpg)
Everything is an Object
https://necessaryagitation.files.wordpress.com/2010/04/objects.gif
![Page 4: COSC 235: Programming and Problem Solving Ch. 4 or… Everything is an Object Instructor: Dr. X.](https://reader035.fdocuments.us/reader035/viewer/2022070400/56649f125503460f94c25f85/html5/thumbnails/4.jpg)
Everything is an Object
• Objects know stuff• Objects can do stuff• They are not exactly the same as the objects
that surround us• They are… computational entities!• Object Oriented (OO) Programming
![Page 5: COSC 235: Programming and Problem Solving Ch. 4 or… Everything is an Object Instructor: Dr. X.](https://reader035.fdocuments.us/reader035/viewer/2022070400/56649f125503460f94c25f85/html5/thumbnails/5.jpg)
Let’s code some objects
• Graphics: create some basic objects and get familiar with the graphics.py library
• We will use the “type” function to figure out what is going on
![Page 6: COSC 235: Programming and Problem Solving Ch. 4 or… Everything is an Object Instructor: Dr. X.](https://reader035.fdocuments.us/reader035/viewer/2022070400/56649f125503460f94c25f85/html5/thumbnails/6.jpg)
What did we learn?
• What did the type function reveal?• Where did we see a class before?• What is the difference between a method and
a function?• What is the difference between an instance
variable and a variable?
![Page 7: COSC 235: Programming and Problem Solving Ch. 4 or… Everything is an Object Instructor: Dr. X.](https://reader035.fdocuments.us/reader035/viewer/2022070400/56649f125503460f94c25f85/html5/thumbnails/7.jpg)
Everything is an Object
• Objects know stuff – instance variables• Objects can do stuff - methods
![Page 8: COSC 235: Programming and Problem Solving Ch. 4 or… Everything is an Object Instructor: Dr. X.](https://reader035.fdocuments.us/reader035/viewer/2022070400/56649f125503460f94c25f85/html5/thumbnails/8.jpg)
Class
• Determines the methods an object will have• An object is an instance of a class
![Page 9: COSC 235: Programming and Problem Solving Ch. 4 or… Everything is an Object Instructor: Dr. X.](https://reader035.fdocuments.us/reader035/viewer/2022070400/56649f125503460f94c25f85/html5/thumbnails/9.jpg)
Class vs Object
http://www.dgp.toronto.edu/~meng/courses/108/teaching99/lecture/3week/instance_variable.jpg
![Page 10: COSC 235: Programming and Problem Solving Ch. 4 or… Everything is an Object Instructor: Dr. X.](https://reader035.fdocuments.us/reader035/viewer/2022070400/56649f125503460f94c25f85/html5/thumbnails/10.jpg)
Instance Variable
http://www.dgp.toronto.edu/~meng/courses/108/teaching99/lecture/3week/instance_variable.jpg
![Page 11: COSC 235: Programming and Problem Solving Ch. 4 or… Everything is an Object Instructor: Dr. X.](https://reader035.fdocuments.us/reader035/viewer/2022070400/56649f125503460f94c25f85/html5/thumbnails/11.jpg)
Method
getName() getName()setName()setName()
![Page 12: COSC 235: Programming and Problem Solving Ch. 4 or… Everything is an Object Instructor: Dr. X.](https://reader035.fdocuments.us/reader035/viewer/2022070400/56649f125503460f94c25f85/html5/thumbnails/12.jpg)
Constructor
• A “special” method<class-name>(<param1>, <param2>, …)
• It builds objects
http://www.mymoneysocks.com/wp-content/uploads/2015/01/under_construction.jpeg
![Page 13: COSC 235: Programming and Problem Solving Ch. 4 or… Everything is an Object Instructor: Dr. X.](https://reader035.fdocuments.us/reader035/viewer/2022070400/56649f125503460f94c25f85/html5/thumbnails/13.jpg)
Let’s create some clones
![Page 14: COSC 235: Programming and Problem Solving Ch. 4 or… Everything is an Object Instructor: Dr. X.](https://reader035.fdocuments.us/reader035/viewer/2022070400/56649f125503460f94c25f85/html5/thumbnails/14.jpg)
Aliasing vs Cloning
• Alias: two variables refer to the same object• Clone: method that makes a copy of an object
![Page 15: COSC 235: Programming and Problem Solving Ch. 4 or… Everything is an Object Instructor: Dr. X.](https://reader035.fdocuments.us/reader035/viewer/2022070400/56649f125503460f94c25f85/html5/thumbnails/15.jpg)
Graphing Populations
• We will create a similar program as in the book but for populations
• NEED TO WORK ON EXAMPLE!!!
![Page 16: COSC 235: Programming and Problem Solving Ch. 4 or… Everything is an Object Instructor: Dr. X.](https://reader035.fdocuments.us/reader035/viewer/2022070400/56649f125503460f94c25f85/html5/thumbnails/16.jpg)
Event driven programming
• Event ~ Trigger• Event: Object that encapsulates data about
what just happened• Can you thing of example apps that are event
driven?
![Page 17: COSC 235: Programming and Problem Solving Ch. 4 or… Everything is an Object Instructor: Dr. X.](https://reader035.fdocuments.us/reader035/viewer/2022070400/56649f125503460f94c25f85/html5/thumbnails/17.jpg)
Event driven program
• Modify the convert.py to convert_gui.py• Add the option to convert meters to feet!