Day 15 Object-Oriented Programming¶

Nov. 5, 2020¶

CMSE logo

Administrative¶

  • Homework 4 is due Fri Nov 13
  • Projects Complete the project survey by Friday! Only 18 responses so far.
  • Midterm is being graded. Done by early next week.
  • No final exam

Pre-Class¶

Questions/Issues¶

  • I'm confused about classes and instances and what the differences are
  • When are we creating/using attributes versus methods?
  • Why/when do we need to use Object-Oriented Programming?

We will be doing some more OOP on Tuesday and we will start using it in models after that.

In [15]:
class Point(object):
   def __init__(self,x=0,y=0,z=0):
        self.x_coord = x ## setting the attribute x_coord
        self.y_coord = y ## setting the attribute y_coord
        self.z_coord = z
In [16]:
my_point = Point(2,3) ## creating an instance of Point called 'my_point'
#print(my_point.x_coord)
#print(my_point.y_coord)
my_point.z_coord = 1
print(my_point.z_coord)

new_point = Point()
new_point.z_coord
1
Out[16]:
0
In [17]:
class Point(object):
   def __init__(self,x=0,y=0):
        self.x_coord = x
        self.y_coord = y
   def translate(self,x=0,y=0):
        self.x_coord += x
        self.y_coord += y
   def print_point(self):
        print("({:d},{:d})".format(self.x_coord, self.y_coord))
In [21]:
my_point = Point()
my_point.print_point()
my_point.translate(-1,2)
my_point.print_point()
my_point.translate(1,-2)
my_point.print_point()
(0,0)
(-1,2)
(0,0)

Questions, Comments, Concerns?¶