Created
September 4, 2017 00:18
-
-
Save bhavishyagopesh/fb0753b598ab9a03c48045fa49d053da to your computer and use it in GitHub Desktop.
Hiererachy of quadrilaterals
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class trapezium(): | |
| """ | |
| Trapezium class that takes three parameters two of it's | |
| parallel sides(a & b) and height(h) | |
| >>> t1 = trapezium(2,2,4) | |
| >>> t1.area | |
| 8 | |
| >>>t2 = trapezium.(2,2,4,2,2) | |
| 8 | |
| """ | |
| def __init__(self, a, b, h, *args): | |
| self._a = a | |
| self._b = b | |
| self._h = h | |
| if len(args)==2: | |
| self._c = args[0] | |
| self._d = args[1] | |
| @property | |
| def area(self): | |
| try: | |
| return .5 * (self._a * self._b)*(self._h) | |
| except: | |
| print ("Invalid operation") | |
| @property | |
| def perimeter(self): | |
| try: | |
| return self._a + self._b + self._c + self._d | |
| except: | |
| print ("Invalid operation") | |
| class parallelogram(trapezium): | |
| """Parallelogram class takes a,b and h | |
| >>> t1 = parallelogram(2,2,4) | |
| >>> t1.area | |
| 8 | |
| >>>t1.perimeter | |
| 8 | |
| """ | |
| def ___init__(self, a, b, h): | |
| self._a = a | |
| self._h = h | |
| self._b = b | |
| @property | |
| def area(self): | |
| try: | |
| return self._a * self._h | |
| except: | |
| print ("Invalid operation") | |
| @property | |
| def perimeter(self): | |
| try: | |
| return 2*(self._a + self._b) | |
| except: | |
| print ("Invalid operation") | |
| class rectangle(parallelogram): | |
| """ | |
| Rectangle takes a,b | |
| >>> t1 = rectangle(2,4) | |
| >>> t1.area | |
| 8 | |
| >>>t1.perimeter | |
| 12 | |
| """ | |
| def __init__(self, a, b): | |
| self._a = a | |
| self._b = b | |
| @property | |
| def area(self): | |
| try: | |
| return self._a * self._b | |
| except: | |
| print ("Invalid operation") | |
| @property | |
| def perimeter(self): | |
| try: | |
| return 2*(self._a + self._b) | |
| except: | |
| print ("Invalid operation") | |
| class rhombus(trapezium): | |
| """ | |
| Rectangle takes a,b | |
| >>> t1 = rhombus(2) | |
| >>> t1.perimeter | |
| 8 | |
| >>>t2 = rhombus(5, 6, 8) | |
| 24 | |
| """ | |
| def __init__(self, a, *args): | |
| self._a = a | |
| if len(args)==2: | |
| self._d1 = args[0] | |
| self._d2 = args[1] | |
| @property | |
| def area(self): | |
| try: | |
| return .5 * self._d1 * self._d2 | |
| except: | |
| print ("Invalid operation") | |
| @property | |
| def perimeter(self): | |
| try: | |
| return 4*self._a | |
| except: | |
| print ("Invalid operation") | |
| class square(rectangle): | |
| """ | |
| Square takes a | |
| >>> t1 = square(2) | |
| >>> t1.perimeter | |
| 8 | |
| >>>t1.area | |
| 9 | |
| """ | |
| def __init__(self, a): | |
| self._a = a | |
| self._d = ((2)**.5)*a | |
| @property | |
| def area(self): | |
| try: | |
| return self._a **2 | |
| except: | |
| print ("Invalid operation") | |
| @property | |
| def perimeter(self): | |
| try: | |
| return 4*(self._a) | |
| except: | |
| print ("Invalid operation") | |
| if __name__=="main": | |
| import doctest | |
| doctest.testmod() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment