-
Notifications
You must be signed in to change notification settings - Fork 0
/
9-rectangle.py
36 lines (29 loc) · 1.17 KB
/
9-rectangle.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#!/usr/bin/python3
"""
Contains the class BaseGeometry and subclass Rectangle
"""
class BaseGeometry:
"""A class with public instance methods area and integer_validator"""
def area(self):
"""raises an exception when called"""
raise Exception("area() is not implemented")
def integer_validator(self, name, value):
"""validates that value is an integer greater than 0"""
if type(value) is not int:
raise TypeError("{:s} must be an integer".format(name))
if value <= 0:
raise ValueError("{:s} must be greater than 0".format(name))
class Rectangle(BaseGeometry):
"""A representation of a rectangle"""
def __init__(self, width, height):
"""instantiation of the rectangle"""
self.integer_validator("width", width)
self.__width = width
self.integer_validator("height", height)
self.__height = height
def area(self):
"""returns the area of the rectangle"""
return self.__width * self.__height
def __str__(self):
"""informal string representation of the rectangle"""
return "[Rectangle] {:d}/{:d}".format(self.__width, self.__height)