From a196f71fb03f91ee2cd53ee4c8c33fd0c5e768a8 Mon Sep 17 00:00:00 2001 From: Mbasha Seth <48843795+Mbashas@users.noreply.github.com> Date: Sat, 11 Nov 2023 11:21:20 -0800 Subject: [PATCH] demonstrating polymorphism --- Seth_A99501/day40.ipynb | 128 ++++++++++++++++++++++++++++++++++++ Seth_A99501/day41.ipynb | 142 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 270 insertions(+) create mode 100644 Seth_A99501/day40.ipynb create mode 100644 Seth_A99501/day41.ipynb diff --git a/Seth_A99501/day40.ipynb b/Seth_A99501/day40.ipynb new file mode 100644 index 00000000..519cc21e --- /dev/null +++ b/Seth_A99501/day40.ipynb @@ -0,0 +1,128 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "72\n", + "254.46900494077323\n", + "10.0\n", + "Demonstrate polymorphism by creating a list of different shapes\n", + "72\n", + "254.46900494077323\n", + "10.0\n" + ] + } + ], + "source": [ + "import math\n", + "\n", + "class Shape:\n", + " def __init__(self, length, width):\n", + " self.__length = length\n", + " self.__width = width\n", + "\n", + " @property\n", + " def length(self):\n", + " return self.__length\n", + "\n", + " @length.setter\n", + " def length(self, value):\n", + " if value >= 0:\n", + " self.__length = value\n", + " else:\n", + " print(\"Error: Length must be non-negative.\")\n", + "\n", + " @property\n", + " def width(self):\n", + " return self.__width\n", + "\n", + " @width.setter\n", + " def width(self, value):\n", + " if value >= 0:\n", + " self.__width = value\n", + " else:\n", + " print(\"Error: Width must be non-negative.\")\n", + "\n", + " def calculate_area(self):\n", + " pass # To be overridden by subclasses\n", + "\n", + "class Rectangle(Shape):\n", + " def calculate_area(self):\n", + " return self.length * self.width\n", + "\n", + "class Circle(Shape):\n", + " def __init__(self, radius):\n", + " super().__init__(radius, None) # Pass None as width for Circle\n", + " self.__radius = radius\n", + "\n", + " @property\n", + " def radius(self):\n", + " return self.__radius\n", + "\n", + " @radius.setter\n", + " def radius(self, value):\n", + " if value >= 0:\n", + " self.__radius = value\n", + " else:\n", + " print(\"Error: Radius must be non-negative.\")\n", + "\n", + " def calculate_area(self):\n", + " return math.pi * (self.radius ** 2)\n", + "class Triangle(Shape):\n", + " def calculate_area(self):\n", + " # Area of a triangle: (base * height) / 2\n", + " return (self.length * self.width) / 2\n", + "\n", + "# Create instances of Rectangle and Circle\n", + "rectangle = Rectangle(9, 8)\n", + "circle = Circle(9)\n", + "triangle = Triangle(5, 4)\n", + "# Calculate and print areas\n", + "print(rectangle.calculate_area())\n", + "print(circle.calculate_area())\n", + "print(triangle.calculate_area())\n", + "\n", + "print(\"Demonstrate polymorphism by creating a list of different shapes\")\n", + "shapes = [rectangle, circle, triangle]\n", + "\n", + "# Calculate and print areas using a loop\n", + "for shape in shapes:\n", + " print(shape.calculate_area())\n", + "\n", + "# # Use the setters for Shape's length and width\n", + "# rectangle.length = 12\n", + "# rectangle.width = 6\n", + "\n", + "# # Recalculate and print the area\n", + "# print(rectangle.calculate_area())\n" + ] + } + ], + "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.11.6" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/Seth_A99501/day41.ipynb b/Seth_A99501/day41.ipynb new file mode 100644 index 00000000..3f19d2f6 --- /dev/null +++ b/Seth_A99501/day41.ipynb @@ -0,0 +1,142 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "72\n", + "254.46900494077323\n", + "10.0\n", + "81\n", + "36\n", + "Demonstrate polymorphism by creating a list of different shapes\n", + "72\n", + "254.46900494077323\n", + "10.0\n", + "81\n" + ] + } + ], + "source": [ + "import math\n", + "\n", + "class Shape:\n", + " def __init__(self, length, width):\n", + " self.__length = length\n", + " self.__width = width\n", + "\n", + " @property\n", + " def length(self):\n", + " return self.__length\n", + "\n", + " @length.setter\n", + " def length(self, value):\n", + " if value >= 0:\n", + " self.__length = value\n", + " else:\n", + " print(\"Error: Length must be non-negative.\")\n", + "\n", + " @property\n", + " def width(self):\n", + " return self.__width\n", + "\n", + " @width.setter\n", + " def width(self, value):\n", + " if value >= 0:\n", + " self.__width = value\n", + " else:\n", + " print(\"Error: Width must be non-negative.\")\n", + "\n", + " def calculate_area(self):\n", + " pass # To be overridden by subclasses\n", + "\n", + "class Rectangle(Shape):\n", + " def calculate_area(self):\n", + " return self.length * self.width\n", + "\n", + "class Circle(Shape):\n", + " def __init__(self, radius):\n", + " super().__init__(radius, None) # Pass None as width for Circle\n", + " self.__radius = radius\n", + "\n", + " @property\n", + " def radius(self):\n", + " return self.__radius\n", + "\n", + " @radius.setter\n", + " def radius(self, value):\n", + " if value >= 0:\n", + " self.__radius = value\n", + " else:\n", + " print(\"Error: Radius must be non-negative.\")\n", + "\n", + " def calculate_area(self):\n", + " return math.pi * (self.radius ** 2)\n", + "class Triangle(Shape):\n", + " def calculate_area(self):\n", + " # Area of a triangle: (base * height) / 2\n", + " return (self.length * self.width) / 2\n", + " \n", + "class Square(Rectangle):\n", + " def __init__(self,length):\n", + " super().__init__(length,None) # Pass None as width for Circle\n", + " def calculate_perimimeter(self):\n", + " return self.length*4\n", + " def calculate_area(self):\n", + " return self.length * self.length\n", + " \n", + "\n", + "# Create instances of Rectangle and Circle\n", + "rectangle = Rectangle(9, 8)\n", + "circle = Circle(9)\n", + "triangle = Triangle(5, 4)\n", + "square = Square(9)\n", + "# Calculate and print areas\n", + "print(rectangle.calculate_area())\n", + "print(circle.calculate_area())\n", + "print(triangle.calculate_area())\n", + "print(square.calculate_area())\n", + "print(square.calculate_perimimeter())\n", + "print(\"Demonstrate polymorphism by creating a list of different shapes\")\n", + "shapes = [rectangle, circle, triangle,square]\n", + "\n", + "# Calculate and print areas using a loop\n", + "for shape in shapes:\n", + " print(shape.calculate_area())\n", + "\n", + "# # Use the setters for Shape's length and width\n", + "# rectangle.length = 12\n", + "# rectangle.width = 6\n", + "\n", + "# # Recalculate and print the area\n", + "# print(rectangle.calculate_area())\n" + ] + } + ], + "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.11.6" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +}