{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "b7b62c64",
   "metadata": {},
   "source": [
    "# 14-20 · Mini-projekt: kształty polimorficzne\n",
    "\n",
    "Praktyka do sekcji [„Mini-projekt: polimorficzne kształty”](/pl/chapters/rozdzial-14/14-20-mini-proekt-figury.html)."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "83867f58",
   "metadata": {},
   "source": [
    "## Cel\n",
    "\n",
    "Wspólny przodek definiuje interfejs, a każda figura implementuje go na swój sposób."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "22a9164e",
   "metadata": {},
   "source": [
    "## Sprawa robocza"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "b59b59c4",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-08-17T19:43:09.767018Z",
     "iopub.status.busy": "2026-08-17T19:43:09.766910Z",
     "iopub.status.idle": "2026-08-17T19:43:09.785225Z",
     "shell.execute_reply": "2026-08-17T19:43:09.784749Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "круг 78.54\n",
      "прямоугольник 24\n",
      "треугольник 12.0\n"
     ]
    }
   ],
   "source": [
    "class Figura:\n",
    "    def __init__(self, nazvanie):\n",
    "        self.nazvanie = nazvanie\n",
    "\n",
    "    def ploshchad(self):\n",
    "        raise NotImplementedError\n",
    "\n",
    "\n",
    "class Krug(Figura):\n",
    "    def __init__(self, radius):\n",
    "        super().__init__(\"круг\")\n",
    "        self.radius = radius\n",
    "\n",
    "    def ploshchad(self):\n",
    "        return 3.14159 * self.radius ** 2\n",
    "\n",
    "\n",
    "class Pryamougolnik(Figura):\n",
    "    def __init__(self, width, height):\n",
    "        super().__init__(\"прямоугольник\")\n",
    "        self.width = width\n",
    "        self.height = height\n",
    "\n",
    "    def ploshchad(self):\n",
    "        return self.width * self.height\n",
    "\n",
    "\n",
    "class Treugolnik(Figura):\n",
    "    def __init__(self, base, height):\n",
    "        super().__init__(\"треугольник\")\n",
    "        self.base = base\n",
    "        self.height = height\n",
    "\n",
    "    def ploshchad(self):\n",
    "        return 0.5 * self.base * self.height\n",
    "\n",
    "figury = [Krug(5), Pryamougolnik(4, 6), Treugolnik(8, 3)]\n",
    "for f in figury:\n",
    "    print(f.nazvanie, round(f.ploshchad(), 2))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1880bee9",
   "metadata": {},
   "source": [
    "## Zadanie niezależne od zadania ★★\n",
    "\n",
    "Dodaj `Figura` metoda-szablon `perimetr()` (`raise NotImplementedError`) i zaimplementuj go we wszystkich trzech figurach."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "55c0a5ec",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-08-17T19:43:09.786463Z",
     "iopub.status.busy": "2026-08-17T19:43:09.786219Z",
     "iopub.status.idle": "2026-08-17T19:43:09.793587Z",
     "shell.execute_reply": "2026-08-17T19:43:09.793144Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "31.42 20 18.0\n"
     ]
    }
   ],
   "source": [
    "class Figura:\n",
    "    def __init__(self, nazvanie):\n",
    "        self.nazvanie = nazvanie\n",
    "\n",
    "    def ploshchad(self):\n",
    "        raise NotImplementedError\n",
    "\n",
    "    def perimetr(self):\n",
    "        raise NotImplementedError\n",
    "\n",
    "\n",
    "class Krug(Figura):\n",
    "    def __init__(self, radius):\n",
    "        super().__init__(\"круг\")\n",
    "        self.radius = radius\n",
    "\n",
    "    def ploshchad(self):\n",
    "        return 3.14159 * self.radius ** 2\n",
    "\n",
    "    def perimetr(self):\n",
    "        return 2 * 3.14159 * self.radius\n",
    "\n",
    "\n",
    "class Pryamougolnik(Figura):\n",
    "    def __init__(self, width, height):\n",
    "        super().__init__(\"прямоугольник\")\n",
    "        self.width = width\n",
    "        self.height = height\n",
    "\n",
    "    def ploshchad(self):\n",
    "        return self.width * self.height\n",
    "\n",
    "    def perimetr(self):\n",
    "        return 2 * (self.width + self.height)\n",
    "\n",
    "\n",
    "class Treugolnik(Figura):\n",
    "    def __init__(self, base, height):\n",
    "        super().__init__(\"треугольник\")\n",
    "        self.base = base\n",
    "        self.height = height\n",
    "\n",
    "    def ploshchad(self):\n",
    "        return 0.5 * self.base * self.height\n",
    "\n",
    "    def perimetr(self):\n",
    "        # trójkąt równoramienny: bok — zgodnie z twierdzeniem Pitagoreja\n",
    "        # o połowie podstawy i wysokości\n",
    "        storona = (self.base / 2) ** 2 + self.height ** 2\n",
    "        return self.base + 2 * storona ** 0.5\n",
    "\n",
    "krug = Krug(5)\n",
    "pryamougolnik = Pryamougolnik(4, 6)\n",
    "treugolnik = Treugolnik(8, 3)\n",
    "print(round(krug.perimetr(), 2), pryamougolnik.perimetr(), treugolnik.perimetr())"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Cartesian Python 3.14",
   "language": "python",
   "name": "cartesian-python314"
  },
  "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.14.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
