98 lines
2.6 KiB
Python
98 lines
2.6 KiB
Python
#!~/.virtualenvs/learning/bin/python
|
|
|
|
|
|
class Rectangle(object):
|
|
"""calculates the area and perimeter of a rectangle"""
|
|
|
|
def __init__(self, length, width, **kwargs):
|
|
super(Rectangle, self).__init__(**kwargs)
|
|
self.length = length
|
|
self.width = width
|
|
|
|
def area(self):
|
|
return(self.length * self.width)
|
|
|
|
def perimeter(self):
|
|
return(2 * self.length + 2 * self.width)
|
|
|
|
|
|
# Here we can declare the superclass for the subclass Square
|
|
|
|
class Square(Rectangle):
|
|
"""calculates the area and perimeter of a square"""
|
|
|
|
def __init__(self, length, **kwargs):
|
|
super(Square, self).__init__(length=length, width=length, **kwargs)
|
|
|
|
|
|
class Cube(Square):
|
|
"""calculates the surface area and volume of a cube"""
|
|
|
|
def __init__(self, length, **kwargs):
|
|
super(Cube, self).__init__(length=length, **kwargs)
|
|
self.length = length
|
|
|
|
def surface_area(self):
|
|
face_area = super(Cube, self).area()
|
|
return(6 * face_area)
|
|
|
|
def volume(self):
|
|
face_area = super(Cube, self).area()
|
|
return(face_area * self.length)
|
|
|
|
def area(self):
|
|
return(self.length * self.length + 1)
|
|
# return(super(Cube, self))
|
|
|
|
|
|
class Triangle(object):
|
|
"""calculates the area of a triangle"""
|
|
|
|
def __init__(self, base, height, **kwargs):
|
|
self.base = base
|
|
self.height = height
|
|
super(Triangle, self).__init__(**kwargs)
|
|
|
|
def tri_area(self):
|
|
return 0.5 * self.base * self.height
|
|
|
|
|
|
class RightPyramid(Square, Triangle):
|
|
"""calculates the surface area of a right pyramid"""
|
|
|
|
def __init__(self, base, slant_height, **kwargs):
|
|
kwargs['height'] = slant_height
|
|
kwargs['length'] = base
|
|
super(RightPyramid, self).__init__(base=base, **kwargs)
|
|
self.base = base
|
|
self.slant_height = slant_height
|
|
|
|
def area(self):
|
|
base_area = super(RightPyramid, self).area()
|
|
perimeter = super(RightPyramid, self).perimeter()
|
|
return(0.5 * perimeter * self.slant_height + base_area)
|
|
|
|
def area_2(self):
|
|
base_area = super(RightPyramid, self).area()
|
|
triangle_area = super(RightPyramid, self).tri_area()
|
|
return(triangle_area * 4 + base_area)
|
|
|
|
|
|
class new_cube(Cube):
|
|
"""docstring for new_cube"""
|
|
def __init__(self, new_length):
|
|
super(new_cube, self).__init__(length=new_length)
|
|
self.new_length = new_length
|
|
|
|
|
|
# print(RightPyramid.__mro__)
|
|
pyramid = RightPyramid(2, 4)
|
|
print(pyramid.area())
|
|
print(pyramid.area_2())
|
|
|
|
nc = new_cube(4)
|
|
print(nc.surface_area())
|
|
|
|
square = Square(3)
|
|
print(square.perimeter())
|