Object Oriented Programming

Homework Assignment

Problem 1

Fill in the Line class methods to accept coordinate as a pair of tuples and return the slope and distance of the line.


In [19]:
import math
class Line(object):
    
    def __init__(self,coor1,coor2):
        self.coor1 = coor1
        self.coor2 = coor2
        return
    
    def distance(self):
        return math.sqrt((self.coor2[1]-self.coor1[1])**2+(self.coor2[0]-self.coor1[0])**2)
    
    def slope(self):
        return (self.coor2[1]-self.coor1[1])/(self.coor2[0]-self.coor1[0])

In [20]:
# EXAMPLE OUTPUT

coordinate1 = (3,2)
coordinate2 = (8,10)

li = Line(coordinate1,coordinate2)

In [21]:
li.distance()


Out[21]:
9.433981132056603

In [22]:
li.slope()


Out[22]:
1.6

Problem 2

Fill in the class


In [26]:
class Cylinder(object):
    pi = 3.14
    
    def __init__(self,height=1,radius=1):
        self.height = height
        self.radius = radius
        return
        
    def volume(self):
        return Cylinder.pi*self.radius**2*self.height
    
    def surface_area(self):
        return (Cylinder.pi*self.radius**2*2)+(2*Cylinder.pi*self.radius*self.height)

In [27]:
# EXAMPLE OUTPUT
c = Cylinder(2,3)

In [28]:
c.volume()


Out[28]:
56.52

In [29]:
c.surface_area()


Out[29]:
94.2

In [ ]: