is12 - introduction to programming lecture 10: classes and ...peterb/0012-161/l10.pdf · define...
TRANSCRIPT
![Page 1: IS12 - Introduction to Programming Lecture 10: Classes and ...peterb/0012-161/L10.pdf · Define class Segment where each object is defined by two end points (x1,y1,x2,y2) – Easy](https://reader033.vdocuments.mx/reader033/viewer/2022060315/5f0be1027e708231d432a9fc/html5/thumbnails/1.jpg)
IS12 - Introduction to Programming Lecture 10: Classes and Objects
Peter Brusilovsky http://www2.sis.pitt.edu/~peterb/0012-161/
![Page 2: IS12 - Introduction to Programming Lecture 10: Classes and ...peterb/0012-161/L10.pdf · Define class Segment where each object is defined by two end points (x1,y1,x2,y2) – Easy](https://reader033.vdocuments.mx/reader033/viewer/2022060315/5f0be1027e708231d432a9fc/html5/thumbnails/2.jpg)
Outline
■ Objects as heterogeneous data aggregates
■ Methods ■ Encapsulation ■ Data processing with classes and
objects
![Page 3: IS12 - Introduction to Programming Lecture 10: Classes and ...peterb/0012-161/L10.pdf · Define class Segment where each object is defined by two end points (x1,y1,x2,y2) – Easy](https://reader033.vdocuments.mx/reader033/viewer/2022060315/5f0be1027e708231d432a9fc/html5/thumbnails/3.jpg)
Why Objects and OOP?
■ Heterogeneous data aggregates ■ Encapsulation of actions on data
objects ■ Inheritance and more efficient
programming ■ More efficient GUI programming
![Page 4: IS12 - Introduction to Programming Lecture 10: Classes and ...peterb/0012-161/L10.pdf · Define class Segment where each object is defined by two end points (x1,y1,x2,y2) – Easy](https://reader033.vdocuments.mx/reader033/viewer/2022060315/5f0be1027e708231d432a9fc/html5/thumbnails/4.jpg)
From Program to Data Structures
machine level data storage
primitive data types
basic data aggregates
high-level data structures
0100110001101001010001
28! 3.1415 'A'
stack dictionary tree
array structure
list
![Page 5: IS12 - Introduction to Programming Lecture 10: Classes and ...peterb/0012-161/L10.pdf · Define class Segment where each object is defined by two end points (x1,y1,x2,y2) – Easy](https://reader033.vdocuments.mx/reader033/viewer/2022060315/5f0be1027e708231d432a9fc/html5/thumbnails/5.jpg)
Object and Classes
■ Objects are – data aggregates – that can hold heterogeneous data fields – referring to these elements by name
■ A Class is a blueprint for objects with the same structure (same collection of fields)
![Page 6: IS12 - Introduction to Programming Lecture 10: Classes and ...peterb/0012-161/L10.pdf · Define class Segment where each object is defined by two end points (x1,y1,x2,y2) – Easy](https://reader033.vdocuments.mx/reader033/viewer/2022060315/5f0be1027e708231d432a9fc/html5/thumbnails/6.jpg)
Objects: Keeping data together
class Student: # name – student name
# ybirth – year of birth # height – sts' height
john.name = "John Doe" john.ybirth = 1989
john.height = 172.3
name
ybirth
height
172.3
1989
name
ybirth
height
john
John Doe
![Page 7: IS12 - Introduction to Programming Lecture 10: Classes and ...peterb/0012-161/L10.pdf · Define class Segment where each object is defined by two end points (x1,y1,x2,y2) – Easy](https://reader033.vdocuments.mx/reader033/viewer/2022060315/5f0be1027e708231d432a9fc/html5/thumbnails/7.jpg)
Objects: keeping data together
Class Point:
def __init__(self, x, y): self.x = x
self.y = y
pt1 = Point(0,0) pt2 = Point(1,3)
x
y
0
0
x
y
pt1
1
3
x
y
pt2
![Page 8: IS12 - Introduction to Programming Lecture 10: Classes and ...peterb/0012-161/L10.pdf · Define class Segment where each object is defined by two end points (x1,y1,x2,y2) – Easy](https://reader033.vdocuments.mx/reader033/viewer/2022060315/5f0be1027e708231d432a9fc/html5/thumbnails/8.jpg)
Objects: What we can do?
■ Create object of a class – Constructors required p1 = Point(20, 0)
■ Assign field p1.y = 1
■ Use field print(p1.y) if p1.x < 30: p1.x = 30
■ Assign object – Another reference is created p2 = p1
x
y
p1 20 0
x
y
p1 20 1
p2
![Page 9: IS12 - Introduction to Programming Lecture 10: Classes and ...peterb/0012-161/L10.pdf · Define class Segment where each object is defined by two end points (x1,y1,x2,y2) – Easy](https://reader033.vdocuments.mx/reader033/viewer/2022060315/5f0be1027e708231d432a9fc/html5/thumbnails/9.jpg)
Structures: What else we can do?
■ Pass the structure as a parameter to a function – Reference is really passed myfunc(p1)
■ Return the whole structure from a function def mypoint(x1, x2, y):
pt3 = Point((x1 + x2)/ 2, y)
return pt3
![Page 10: IS12 - Introduction to Programming Lecture 10: Classes and ...peterb/0012-161/L10.pdf · Define class Segment where each object is defined by two end points (x1,y1,x2,y2) – Easy](https://reader033.vdocuments.mx/reader033/viewer/2022060315/5f0be1027e708231d432a9fc/html5/thumbnails/10.jpg)
Example: Above or Under
class Point:def __init__(self, x, y): self.x = x self.y = y
def readpoint():x = float(input("Enter x:"))y = float(input("Enter y:"))p = Point(x, y)return p
def above(pt1, pt2):if pt1.y > pt2.y: return Trueelse: return False
![Page 11: IS12 - Introduction to Programming Lecture 10: Classes and ...peterb/0012-161/L10.pdf · Define class Segment where each object is defined by two end points (x1,y1,x2,y2) – Easy](https://reader033.vdocuments.mx/reader033/viewer/2022060315/5f0be1027e708231d432a9fc/html5/thumbnails/11.jpg)
Example : Above or Under
def main():print("Enter base point")base = readpoint()for i in range(5): print("Enter point") p = readpoint() if above(p, base): print("This is above base point") else: print("This is not above base point")
![Page 12: IS12 - Introduction to Programming Lecture 10: Classes and ...peterb/0012-161/L10.pdf · Define class Segment where each object is defined by two end points (x1,y1,x2,y2) – Easy](https://reader033.vdocuments.mx/reader033/viewer/2022060315/5f0be1027e708231d432a9fc/html5/thumbnails/12.jpg)
Objects vs. Lists / Tuples
■ Objects – Good for
heterogeneous data – Each element has a
unique name within object: pt.x
– Elements are accessed by name: top.x, mypt.y
– Support OOP
■ Lists / Tuples – Good for
homogeneous data – Each element has a
position – Elements are
accessed by index: pt[7], line[23]
– Index can be computed: ar[i-1]
![Page 13: IS12 - Introduction to Programming Lecture 10: Classes and ...peterb/0012-161/L10.pdf · Define class Segment where each object is defined by two end points (x1,y1,x2,y2) – Easy](https://reader033.vdocuments.mx/reader033/viewer/2022060315/5f0be1027e708231d432a9fc/html5/thumbnails/13.jpg)
Classes and Methods
■ Once you have objects, you probably need various functions to work with them
■ Functions that a specifically designed to work with objects of a class are called methods
■ Technically, a class is a collection of methods that work on objects of this class
■ Methods are special kinds of functions – Defined within a class – Have a special treatment of its first parameter (self) – Have a special "object-oriented" way of calling
![Page 14: IS12 - Introduction to Programming Lecture 10: Classes and ...peterb/0012-161/L10.pdf · Define class Segment where each object is defined by two end points (x1,y1,x2,y2) – Easy](https://reader033.vdocuments.mx/reader033/viewer/2022060315/5f0be1027e708231d432a9fc/html5/thumbnails/14.jpg)
Calling Functions vs Methods
■ Functions – Not defined in a class – No special treatment
of any parameters – All parameters
passed as an ordered tupple
above(p1, p2)
distance(p1, p2)
■ Methods – Defined in a class – First parameter has
special meaning – First parameter
passed in a special way, others are usual parameters
– p1.above(p2) – p1.distance(p2)
![Page 15: IS12 - Introduction to Programming Lecture 10: Classes and ...peterb/0012-161/L10.pdf · Define class Segment where each object is defined by two end points (x1,y1,x2,y2) – Easy](https://reader033.vdocuments.mx/reader033/viewer/2022060315/5f0be1027e708231d432a9fc/html5/thumbnails/15.jpg)
Object Creation: __init__
class Student: def __init__(self, name, ybirth, height): self.name = name self.ybirth = y self.height = h john = Student("John Doe", 1989, 172.3)
Class Point: def __init__(self, x, y): self.x = x self.y = y p1 = Point (20, 0)
172.3
1989
name
ybirth
height
john
John Doe
x
y
p1 20 0
![Page 16: IS12 - Introduction to Programming Lecture 10: Classes and ...peterb/0012-161/L10.pdf · Define class Segment where each object is defined by two end points (x1,y1,x2,y2) – Easy](https://reader033.vdocuments.mx/reader033/viewer/2022060315/5f0be1027e708231d432a9fc/html5/thumbnails/16.jpg)
Example: Above or Under
class Point:def __init__(self, x, y): self.x = x self.y = y def above(self, pt): if self.y > pt.y: return True else: return False
def readpoint():x = float(input("Enter x:"))y = float(input("Enter y:"))p = Point(x, y)return p
![Page 17: IS12 - Introduction to Programming Lecture 10: Classes and ...peterb/0012-161/L10.pdf · Define class Segment where each object is defined by two end points (x1,y1,x2,y2) – Easy](https://reader033.vdocuments.mx/reader033/viewer/2022060315/5f0be1027e708231d432a9fc/html5/thumbnails/17.jpg)
Example : Above or Under
def main():print("Enter base point")base = readpoint()for i in range(5): print("Enter point") p = readpoint() if p.above(base): print("This is above base point") else: print("This is not above base point")
![Page 18: IS12 - Introduction to Programming Lecture 10: Classes and ...peterb/0012-161/L10.pdf · Define class Segment where each object is defined by two end points (x1,y1,x2,y2) – Easy](https://reader033.vdocuments.mx/reader033/viewer/2022060315/5f0be1027e708231d432a9fc/html5/thumbnails/18.jpg)
Classes and Modules
■ Classes could be aggregated in modules
■ Modules could be used by other programs with import command
■ There are many exiting modules that could be used to write powerful programs with little own code
import randomimport math
![Page 19: IS12 - Introduction to Programming Lecture 10: Classes and ...peterb/0012-161/L10.pdf · Define class Segment where each object is defined by two end points (x1,y1,x2,y2) – Easy](https://reader033.vdocuments.mx/reader033/viewer/2022060315/5f0be1027e708231d432a9fc/html5/thumbnails/19.jpg)
Example: Distance (Class)
import mathclass Point:
def __init__(self, x, y): self.x = x self.y = y def above(self, pt): if self.y > pt.y: return True else: return Falsedef distance(self, p): return math.sqrt((self.x - p.x)**2 + (self.y - p.y)**2) def midpoint(self, p): return Point((self.x + p.x)/2, (self.y + p.y)/2)
![Page 20: IS12 - Introduction to Programming Lecture 10: Classes and ...peterb/0012-161/L10.pdf · Define class Segment where each object is defined by two end points (x1,y1,x2,y2) – Easy](https://reader033.vdocuments.mx/reader033/viewer/2022060315/5f0be1027e708231d432a9fc/html5/thumbnails/20.jpg)
Example: Distance (Main)
def main():print("Enter point A")a = readpoint()print("Enter point B")b = readpoint()c = a.midpoint(b)print("Distance between A and B", a.distance(b))print("Distance between A and midpoint", a.distance(c))print("Distance between B and midpoint", b.distance(c))
![Page 21: IS12 - Introduction to Programming Lecture 10: Classes and ...peterb/0012-161/L10.pdf · Define class Segment where each object is defined by two end points (x1,y1,x2,y2) – Easy](https://reader033.vdocuments.mx/reader033/viewer/2022060315/5f0be1027e708231d432a9fc/html5/thumbnails/21.jpg)
Nested Objects
class Rectangle:
r.pt1 = point(1,1)
r.pt2 = point(4,4)
pt1
pt2
![Page 22: IS12 - Introduction to Programming Lecture 10: Classes and ...peterb/0012-161/L10.pdf · Define class Segment where each object is defined by two end points (x1,y1,x2,y2) – Easy](https://reader033.vdocuments.mx/reader033/viewer/2022060315/5f0be1027e708231d432a9fc/html5/thumbnails/22.jpg)
Exercise
■ Define class Segment where each object is defined by two end points (x1,y1,x2,y2) – Easy way: no nesting, use 4 numbers – Harder way: nesting, use 2 objects of class point
■ Create method length to return length ■ Create method above to check whether one
segment if fully above the other ■ Create function to input a segment ■ Create main program that asks for data on 2
segments, return length of each, and checks whether one is above another
![Page 23: IS12 - Introduction to Programming Lecture 10: Classes and ...peterb/0012-161/L10.pdf · Define class Segment where each object is defined by two end points (x1,y1,x2,y2) – Easy](https://reader033.vdocuments.mx/reader033/viewer/2022060315/5f0be1027e708231d432a9fc/html5/thumbnails/23.jpg)
Before Next Lecture:
■ Do reading assignment (links online) ■ Run and explore Classroom Examples ■ Exercises:
– Finish classroom exercise ■ Check yourself by working with MG
system ■ Do mandatory exercises ■ Homework programming assignment
due Friday next week