diff --git a/week_6.ipynb b/week_6.ipynb new file mode 100644 index 0000000..6499225 --- /dev/null +++ b/week_6.ipynb @@ -0,0 +1,461 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "id": "sharp-luxembourg", + "metadata": {}, + "source": [ + "* Create a class named Triangle and Rectangle.\n", + " * Create a subclass named Square inherited from Rectangle.\n", + " * Create a subclass named Cube inherited from Square.\n", + " * Create a subclass named Pyramid multiple inherited both from Triangle and Square.\n", + " \n", + "\n", + "* Two dimensional classes (Triangle, Rectangle and Square) should have:\n", + " * its dimensions as attributes.(can be inherited from a superclass)\n", + " * methods which calculate its area and perimeter separately. \n", + " \n", + "\n", + "* Three dimensional classes (Cube and Pyramid) should have:\n", + " * its dimensions as attributes which are inherited from a superclass\n", + " * its extra dimensions if there is. (hint: maybe for Pyramid)\n", + " * methods which calculate its volume and surface area separately. (surface area is optional, you may not do this)" + ] + }, + { + "cell_type": "code", + "execution_count": 116, + "id": "surprised-lemon", + "metadata": {}, + "outputs": [], + "source": [ + "class Triangle:\n", + " def __init__(self, three_sides):\n", + " self.three_sides=three_sides\n", + " \n", + " @property\n", + " def perimeter(self):\n", + " return sum(self.three_sides) \n", + " \n", + " @property \n", + " def area(self):\n", + " u = self.perimeter/2\n", + " a,b,c = [i for i in self.three_sides]\n", + " return (u*(u-a)*(u-b)*(u-c))**0.5\n", + " \n", + " def __str__(self):\n", + " return f\"\"\"Perimeter\\t:\\t{self.perimeter} cm\n", + "Area\\t\\t:\\t{round(self.area,1)} cm2\n", + "\"\"\"\n", + "\n", + " \n", + "class Rectangle: \n", + " def __init__(self, two_sides):\n", + " self.two_sides=two_sides\n", + " \n", + " @property\n", + " def perimeter(self):\n", + " return sum(self.two_sides)*2 \n", + " \n", + " @property \n", + " def area(self):\n", + " a,b = [i for i in self.two_sides]\n", + " return a*b\n", + " \n", + " def __str__(self):\n", + " return f\"\"\"Perimeter\\t:\\t{self.perimeter} cm\n", + "Area\\t\\t:\\t{self.area} cm2\n", + "\"\"\"\n", + " \n", + "class Square(Rectangle):\n", + " def __init__(self,two_sides):\n", + " super().__init__(two_sides)\n", + " \n", + " @property \n", + " def perimeter(self):\n", + " a = self.two_sides[0]\n", + " return 4 * a \n", + " \n", + " @property \n", + " def area(self):\n", + " a = self.two_sides[0]\n", + " return a**2 \n", + "\n", + "class Cube(Square):\n", + " def __init__(self,two_sides):\n", + " super().__init__(two_sides) \n", + " \n", + " @property \n", + " def area(self):\n", + " a = self.two_sides[0]\n", + " return 6 * a**2\n", + " \n", + " @property \n", + " def volume(self):\n", + " a = self.two_sides[0]\n", + " return a**3\n", + " \n", + " def __str__(self):\n", + " return f\"\"\"Area\\t:\\t{self.area} cm2\n", + "Volume\\t:\\t{self.volume} cm3\n", + "\"\"\"\n", + " \n", + "class Pyramid(Triangle, Square):\n", + " def __init__(self, two_sides, height, three_sides=None):\n", + " Triangle.__init__(self,three_sides)\n", + " Square.__init__(self,two_sides)\n", + " self.height = height\n", + " \n", + " @property \n", + " def area(self):\n", + " a = self.two_sides[0]\n", + " h = self.height\n", + " return a**2 + a*(a**2+4*h**2)**0.5\n", + " \n", + " @property \n", + " def volume(self):\n", + " a = self.two_sides[0]\n", + " h = self.height\n", + " return 1/3 * a**2 * h\n", + " \n", + " def __str__(self):\n", + " return f\"\"\"Area\\t:\\t{round(self.area,1)} cm2\n", + "Volume\\t:\\t{self.volume} cm3\n", + "\"\"\"" + ] + }, + { + "cell_type": "code", + "execution_count": 117, + "id": "instrumental-place", + "metadata": {}, + "outputs": [], + "source": [ + "#This Part is extra\n", + "from math import pi\n", + "\n", + "class Circle:\n", + " def __init__(self, radius):\n", + " self.radius = radius\n", + " \n", + " @property\n", + " def perimeter(self):\n", + " r = self.radius\n", + " return 2*pi*r \n", + " \n", + " @property\n", + " def area(self): \n", + " return (self.perimeter**2)/(4*pi)\n", + " \n", + " def __str__(self):\n", + " return f\"\"\"Perimeter\\t:\\t{round(self.perimeter,1)} cm\n", + "Area\\t\\t:\\t{round(self.area,1)} cm2\"\"\"\n", + " \n", + "\n", + "class Sphere(Circle):\n", + " def __init__(self,radius):\n", + " super().__init__(radius) \n", + " self.radius=radius\n", + " \n", + " @property\n", + " def area(self): \n", + " r = self.radius\n", + " return 4*pi*r**2\n", + " \n", + " @property \n", + " def volume(self):\n", + " r = self.radius\n", + " return 1/3 * pi * r**3\n", + " \n", + " def __str__(self):\n", + " return f\"\"\"Area\\t:\\t{round(self.area,1)} cm2\n", + "Volume\\t:\\t{round(self.volume,1)} cm3\n", + "\"\"\"\n", + " \n", + "class Cylinder(Circle):\n", + " def __init__(self,radius, height):\n", + " super().__init__(radius) \n", + " self.radius=radius\n", + " self.height=height\n", + " \n", + " @property\n", + " def area(self): \n", + " r = self.radius\n", + " h = self.height\n", + " return 2*pi*r*(h + r)\n", + " \n", + " @property \n", + " def volume(self):\n", + " r = self.radius\n", + " h = self.height\n", + " return pi*h*r**2\n", + " \n", + " def __str__(self):\n", + " return f\"\"\"Area\\t:\\t{round(self.area,1)} cm2\n", + "Volume\\t:\\t{round(self.volume,1)} cm3\n", + "\"\"\"\n", + " \n", + "class Cone(Circle):\n", + " def __init__(self,radius, height):\n", + " super().__init__(radius) \n", + " self.radius=radius\n", + " self.height=height\n", + " \n", + " @property\n", + " def area(self): \n", + " r = self.radius\n", + " h = self.height\n", + " return pi*r*(r + (r**2 + h**2)**0.5)\n", + " \n", + " @property \n", + " def volume(self):\n", + " r = self.radius\n", + " h = self.height\n", + " return 1/3*pi*h*r**2\n", + " \n", + " def __str__(self):\n", + " return f\"\"\"Area\\t:\\t{round(self.area,1)} cm2\n", + "Volume\\t:\\t{round(self.volume,1)} cm3\n", + "\"\"\"" + ] + }, + { + "cell_type": "code", + "execution_count": 118, + "id": "disturbed-buddy", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Perimeter\t:\t12 cm\n", + "Area\t\t:\t6.0 cm2\n", + "\n" + ] + } + ], + "source": [ + "triangle_sides = [3,4,5]\n", + "obj1 = Triangle(triangle_sides)\n", + "\n", + "print(obj1)" + ] + }, + { + "cell_type": "code", + "execution_count": 119, + "id": "aboriginal-leonard", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Perimeter\t:\t14 cm\n", + "Area\t\t:\t12 cm2\n", + "\n" + ] + } + ], + "source": [ + "rectangle_sides = [3,4]\n", + "obj2 = Rectangle(rectangle_sides)\n", + "\n", + "print(obj2)" + ] + }, + { + "cell_type": "code", + "execution_count": 120, + "id": "lovely-nigeria", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Perimeter\t:\t20 cm\n", + "Area\t\t:\t25 cm2\n", + "\n" + ] + } + ], + "source": [ + "square_side = [5]\n", + "obj3 = Square(square_side)\n", + "\n", + "print(obj3)" + ] + }, + { + "cell_type": "code", + "execution_count": 121, + "id": "therapeutic-placement", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Area\t:\t24 cm2\n", + "Volume\t:\t8 cm3\n", + "\n" + ] + } + ], + "source": [ + "cube_side = [2]\n", + "obj4 = Cube(cube_side)\n", + "\n", + "print(obj4)" + ] + }, + { + "cell_type": "code", + "execution_count": 122, + "id": "incident-storm", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Area\t:\t34.6 cm2\n", + "Volume\t:\t12.0 cm3\n", + "\n" + ] + } + ], + "source": [ + "pyramid_square_base_side = [3]\n", + "pyramid_height = 4\n", + "obj5 = Pyramid(pyramid_square_base_side, pyramid_height)\n", + "\n", + "print(obj5)" + ] + }, + { + "cell_type": "code", + "execution_count": 123, + "id": "juvenile-leeds", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Perimeter\t:\t12.6 cm\n", + "Area\t\t:\t12.6 cm2\n" + ] + } + ], + "source": [ + "circle_radius = 2\n", + "obj6 = Circle(circle_radius)\n", + "\n", + "print(obj6)" + ] + }, + { + "cell_type": "code", + "execution_count": 124, + "id": "moving-jewelry", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Area\t:\t50.3 cm2\n", + "Volume\t:\t8.4 cm3\n", + "\n" + ] + } + ], + "source": [ + "sphere_radius = 2\n", + "obj7 = Sphere(sphere_radius)\n", + "\n", + "print(obj7)" + ] + }, + { + "cell_type": "code", + "execution_count": 125, + "id": "balanced-adoption", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Area\t:\t75.4 cm2\n", + "Volume\t:\t50.3 cm3\n", + "\n" + ] + } + ], + "source": [ + "cylinder_radius = 2\n", + "cylinder_height = 4\n", + "obj8 = Cylinder(cylinder_radius, cylinder_height)\n", + "\n", + "print(obj8)" + ] + }, + { + "cell_type": "code", + "execution_count": 126, + "id": "average-credits", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Area\t:\t40.7 cm2\n", + "Volume\t:\t16.8 cm3\n", + "\n" + ] + } + ], + "source": [ + "cone_radius = 2\n", + "cone_height = 4\n", + "obj9 = Cone(cone_radius, cone_height)\n", + "\n", + "print(obj9)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "useful-cheese", + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.8.5" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +}