{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "6127787b",
   "metadata": {},
   "source": [
    "# In-Class Exercises, Session 6"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8604d2b7",
   "metadata": {},
   "source": [
    "<U>Notes if you are using Jupyter Notebook</U>:  to call <B>exit()</B> from a notebook, please use <B>sys.exit()</B> (requires <B>import sys</B>).\n",
    "\n",
    "If a strange error occurs, it may be because Jupyter retains variables from all executed cells.  To reset the notebook, click 'Restart Kernel' (the circular arrow) -- this will not undo any changes made."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ffbf743b",
   "metadata": {},
   "source": [
    " "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3af54ad1",
   "metadata": {},
   "source": [
    "CLASSES - INSTANCES AND METHODS"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d7894a6b",
   "metadata": {},
   "source": [
    "<B>6.1:  <U>Define a class</U>.  </B>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7d285ef9",
   "metadata": {},
   "source": [
    "Use the <B>class</B> statement with name <B>MyClass</B> (or you may substitute a name of your own choice).  To fill the otherwise empty block, use the <B>pass</B> statement.\n",
    "\n",
    "Initialize an instance of the class and print its type to show that it is an instance of the class.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "732d90d2",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "4a98be82",
   "metadata": {},
   "source": [
    "<U>Expected Output</U>:  "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4857fdaa",
   "metadata": {},
   "source": [
    "<pre style=\"margin: 0; padding=0\"><DIV style=\"font-family: Courier; border: 1px solid #dbdbdb; padding: 7px\">&lt;class '__main__.MyClass'&gt;</DIV></pre>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "66486384",
   "metadata": {},
   "source": [
    " "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7c7b6430",
   "metadata": {},
   "source": [
    "<B>6.2:  <U>Create an 'instance' method</U>.</B>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bf5271ea",
   "metadata": {},
   "source": [
    "Add to the class <B>Hola</B> below a new method, <B>saludar()</B>, that prints \"bienvenidos a todos\" (or whatever greeting you'd prefer) when it is called.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "95555060",
   "metadata": {},
   "outputs": [],
   "source": [
    "class Hola:\n",
    "    \"\"\" una clase que is amigable \"\"\"\n",
    "\n",
    "    # your code here\n",
    "\n",
    "\n",
    "yo = Hola()\n",
    "\n",
    "yo.saludar()     # should print the greeting"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "db18c1b5",
   "metadata": {},
   "source": [
    "<U>Expected Output</U>:  "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "23af79f1",
   "metadata": {},
   "source": [
    "<pre style=\"margin: 0; padding=0\"><DIV style=\"font-family: Courier; border: 1px solid #dbdbdb; padding: 7px\">bienvenidos a todos</DIV></pre>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d6a88cce",
   "metadata": {},
   "source": [
    " "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b763cbfb",
   "metadata": {},
   "source": [
    "<B>6.3:  <U>Add argument and return value to method</U>.  </B>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "12fa32c0",
   "metadata": {},
   "source": [
    "Add the method <B>doubleit()</B> below, that doubles the value passed to it.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c17a269f",
   "metadata": {},
   "outputs": [],
   "source": [
    "Numbers:\n",
    "    \"\"\" various number methods \"\"\"\n",
    "\n",
    "\n",
    "num = Numbers()\n",
    "val = num.doubleit(5)\n",
    "print(val)               # 10\n",
    "\n",
    "val2 = num.doubleit(100)\n",
    "print(val2)              # 200"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0e356f37",
   "metadata": {},
   "source": [
    "<U>Expected Output</U>:  "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2293dd4e",
   "metadata": {},
   "source": [
    "<pre style=\"margin: 0; padding=0\"><DIV style=\"font-family: Courier; border: 1px solid #dbdbdb; padding: 7px\">10\n",
    "200</DIV></pre>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6a4dc69b",
   "metadata": {},
   "source": [
    " "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "92bfe6bc",
   "metadata": {},
   "source": [
    "LAB 1"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4cbdbae1",
   "metadata": {},
   "source": [
    "<div style=\"background-color: #ccffcc; padding: 10px; border: 2px solid green\"><B>6.4:  Create a class <B>Time</B> that has a method <B>get\\_time()</B> that returns the current time.  Call as shown.  </B></div>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9707db85",
   "metadata": {},
   "source": [
    "(A string showing the current time can be obtained from the <B>time</B> module using <B>time.ctime()</B>.)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "74940c8c",
   "metadata": {},
   "outputs": [],
   "source": [
    "import time\n",
    "\n",
    "# your code here\n",
    "\n",
    "\n",
    "obj = Time()\n",
    "print(obj.get_time())  # should show the current time"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "38ea87aa",
   "metadata": {},
   "source": [
    "<U>Expected Output</U>:  "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3060475a",
   "metadata": {},
   "source": [
    "<pre style=\"margin: 0; padding=0\"><DIV style=\"font-family: Courier; border: 1px solid #dbdbdb; padding: 7px\">Sat Oct 17 18:41:34 2020    (as your specific date and time)</DIV></pre>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9cbb15f6",
   "metadata": {},
   "source": [
    " "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f3a99e9c",
   "metadata": {},
   "source": [
    "<div style=\"background-color: #ccffcc; padding: 10px; border: 2px solid green\"><B>6.5:  Create a class <B>Random</B> that has a method <B>get\\_rand(val)</B> that returns a random number from 1 to the specified value.  Call as shown.  </B></div>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "59f53b08",
   "metadata": {},
   "source": [
    "(A random number can be generated with <B>random.randint(x, y)</b> where <B>x</B> and <B>y</B> are the minimum and maximum values.)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "39f48247",
   "metadata": {},
   "outputs": [],
   "source": [
    "import random\n",
    "\n",
    "# your code here\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "obj = Random()\n",
    "\n",
    "val = obj.get_rand(5)     # random number from 1-5\n",
    "print(val)\n",
    "\n",
    "val2 = obj.get_rand(18)   # random number from 1-18\n",
    "print(val2)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "869811a2",
   "metadata": {},
   "source": [
    "<U>Expected Output</U>:  "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e33a6c85",
   "metadata": {},
   "source": [
    "<pre style=\"margin: 0; padding=0\"><DIV style=\"font-family: Courier; border: 1px solid #dbdbdb; padding: 7px\">4\n",
    "3</DIV></pre>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0e51da3d",
   "metadata": {},
   "source": [
    " "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "882fbcbd",
   "metadata": {},
   "source": [
    "<div style=\"background-color: #ccffcc; padding: 10px; border: 2px solid green\"><B>6.6:  Create a class <B>Math</B> with method <B>add()</B> that takes two integer arguments and returns the values summed.  </B></div>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1bcea9e9",
   "metadata": {},
   "outputs": [],
   "source": [
    "# your code here\n",
    "\n",
    "\n",
    "\n",
    "obj = Math()\n",
    "\n",
    "mysum = obj.add(5, 10)        # 15\n",
    "print(mysum)\n",
    "\n",
    "mysum2 = obj.add(100, 150)    # 250\n",
    "print(mysum2)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "155630f1",
   "metadata": {},
   "source": [
    "<U>Expected Output</U>:  "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9cd2e7e5",
   "metadata": {},
   "source": [
    "<pre style=\"margin: 0; padding=0\"><DIV style=\"font-family: Courier; border: 1px solid #dbdbdb; padding: 7px\">15\n",
    "250</DIV></pre>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ef0adcee",
   "metadata": {},
   "source": [
    " "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ce9405da",
   "metadata": {},
   "source": [
    "CLASSES - CONSTRUCTOR, CLASS ATTRIBUTES AND METHODS"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b9c027c3",
   "metadata": {},
   "source": [
    "<B>6.7:  <U>Demonstration:  note the unique identifier of <B>self</B> and an instance from a class</U>.  </B>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "279da245",
   "metadata": {},
   "source": [
    "At the bottom of the code below, print <B>obj</B>, then call <B>obj.something()</B>, noting that this method prints <B>self</B>.  Compare the hex codes that identify the instance.\n",
    "\n",
    "Use a <B>print()</B> statement to create a blank line.\n",
    "\n",
    "Next, print <B>obj2</B> and call <B>obj2.something()</B>, and note the output, particularly the hex codes.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4c2e97f4",
   "metadata": {},
   "outputs": [],
   "source": [
    "class Do:\n",
    "    def something(self):\n",
    "        print(self)\n",
    "\n",
    "\n",
    "obj = Do()\n",
    "obj2 = Do()\n",
    "\n",
    "# your code here"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8f2b35b2",
   "metadata": {},
   "source": [
    "<U>Expected Output</U>:  "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "49daa058",
   "metadata": {},
   "source": [
    "<pre style=\"margin: 0; padding=0\"><DIV style=\"font-family: Courier; border: 1px solid #dbdbdb; padding: 7px\"><__main__.Do object at 0x10d648350>\n",
    "<__main__.Do object at 0x10d648350>\n",
    "\n",
    "<__main__.Do object at 0x10d648390>\n",
    "<__main__.Do object at 0x10d648390></DIV></pre>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ada2b7a8",
   "metadata": {},
   "source": [
    " "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2967e55b",
   "metadata": {},
   "source": [
    "<B>6.8:  <U>Create an \\_\\_init\\_\\_() method</U>.  </B>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b0fccc82",
   "metadata": {},
   "source": [
    "Add a method to the below class, <B>\\_\\_init\\_\\_(self)</B> that inside the function announces and prints the argument <B>self</B>, i.e. <B>print(f'self:    {self}')</B>.\n",
    "\n",
    "Construct 2 new instances, and then print each instance.  Put a blank line between each instance.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "987c876b",
   "metadata": {},
   "outputs": [],
   "source": [
    "class Be:\n",
    "    \"\"\" this class is something! \"\"\"\n",
    "\n",
    "\n",
    "obj1 = Be()\n",
    "print(f'object:  {obj1}')\n",
    "\n",
    "print()\n",
    "\n",
    "obj2 = Be()\n",
    "print(f'object:  {obj2}')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b566bd83",
   "metadata": {},
   "source": [
    "<U>Expected Output</U>:  "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9cf53a05",
   "metadata": {},
   "source": [
    "<pre style=\"margin: 0; padding=0\"><DIV style=\"font-family: Courier; border: 1px solid #dbdbdb; padding: 7px\">self:    <__main__.Be object at 0x10d648250>\n",
    "object:  <__main__.Be object at 0x10d648250>\n",
    "\n",
    "object:  <__main__.Be object at 0x10d648a50>\n",
    "self:    <__main__.Be object at 0x10d648a50></DIV></pre>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "cff3e25c",
   "metadata": {},
   "source": [
    "(Note that the above <B>0x</B> hex codes will not be the same as mine, but the values in each pair should match.)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e6c9a14d",
   "metadata": {},
   "source": [
    " "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fd77161e",
   "metadata": {},
   "source": [
    "<B>6.9:  <U>Set an instance attribute in \\_\\_init\\_\\_()</U>.  </B>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c6e19043",
   "metadata": {},
   "source": [
    "Create a method <B>\\_\\_init\\_\\_(self, num)</B> that sets <B>num</B>in <B>self</B> as a <B>.value</B> attribute.  At bottom, print <B>obj.value</B> to see the value.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b9bf91ef",
   "metadata": {},
   "outputs": [],
   "source": [
    "class Live:\n",
    "    \"\"\" a class that just wants to live \"\"\"\n",
    "    # your __init__() code here\n",
    "\n",
    "\n",
    "obj = Live(5)\n",
    "\n",
    "# print obj.value here"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9ce5a45e",
   "metadata": {},
   "source": [
    "<U>Expected Output</U>:  "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "23366889",
   "metadata": {},
   "source": [
    "<pre style=\"margin: 0; padding=0\"><DIV style=\"font-family: Courier; border: 1px solid #dbdbdb; padding: 7px\">5</DIV></pre>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1efe0b8f",
   "metadata": {},
   "source": [
    " "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "dc2f35d6",
   "metadata": {},
   "source": [
    "<B>6.10:  <U>Create a \"getter\" method</U>.  </B>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d7f3a799",
   "metadata": {},
   "source": [
    "Create a method <B>get\\_value()</B> that returns the <B>.value</B> attribute from the instance.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "58299ea6",
   "metadata": {},
   "outputs": [],
   "source": [
    "class Say:\n",
    "    def __init__(self, val):\n",
    "        self.thisval = val\n",
    "\n",
    "    # your code here\n",
    "\n",
    "\n",
    "obj = Say(100)\n",
    "\n",
    "vl = obj.get_value()\n",
    "print(vl)                # 100"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "863b503f",
   "metadata": {},
   "source": [
    "<U>Expected Output</U>:  "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ce96e637",
   "metadata": {},
   "source": [
    "<pre style=\"margin: 0; padding=0\"><DIV style=\"font-family: Courier; border: 1px solid #dbdbdb; padding: 7px\">100</DIV></pre>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e5e2c62f",
   "metadata": {},
   "source": [
    " "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a807b63a",
   "metadata": {},
   "source": [
    "<B>6.11:  <U>Demonstrate class attributes</U>.  </B>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2951dcb8",
   "metadata": {},
   "source": [
    "In the class below, set a class variable <B>cvar</B> to value <B>1000</B>.\n",
    "\n",
    "Print the value of <B>cvar</B> in three places:  1) instance <B>a</B> (<B>a.cvar</B>); 2) instance <B>b</B> (<B>b.cvar</B>); 3) the class itself (<B>Something.cvar</B>)\n",
    "\n",
    "Also print the <B>.attr</B> attribute from each of the two instances.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ce78a022",
   "metadata": {},
   "outputs": [],
   "source": [
    "class Something:\n",
    "    # your class variable here\n",
    "\n",
    "    def __init__(self, xx):\n",
    "        self.attr = xx\n",
    "\n",
    "a = Something('hi')\n",
    "b = Something('there')\n",
    "\n",
    "\n",
    "# print cvar in 3 places here\n",
    "\n",
    "# print .attr from each of the two instances here"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b48fe501",
   "metadata": {},
   "source": [
    "<U>Expected Output</U>:  "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "cdbe0f1c",
   "metadata": {},
   "source": [
    "<pre style=\"margin: 0; padding=0\"><DIV style=\"font-family: Courier; border: 1px solid #dbdbdb; padding: 7px\">1000\n",
    "1000\n",
    "1000\n",
    "hi\n",
    "there</DIV></pre>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a4e953ce",
   "metadata": {},
   "source": [
    " "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5f7c0a50",
   "metadata": {},
   "source": [
    "<B>6.12:  <U>Create a class method</U>.</B>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c4b4135c",
   "metadata": {},
   "source": [
    "Add a class method <B>classincrement(cls)</B> that uses its <B>cls</B> argument to increment the <B>cattr</B> class variable (<B>cattr</B> will be found to be an attribute of <B>cls</B>.\n",
    "\n",
    "Call <B>classincrement()</B> through the instance <B>obj</B> as well as through the class <B>MyClass</B>.\n",
    "\n",
    "The values printed below should both be <B>1</B>.\n",
    "\n",
    "Before this can work as shown, however, you must decorate <B>classincrement()</B> with <B>@classmethod</B>.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3c4b31b1",
   "metadata": {},
   "outputs": [],
   "source": [
    "class MyClass:\n",
    "    cattr = 0\n",
    "\n",
    "    # your classincrement(cls) method here\n",
    "\n",
    "\n",
    "obj = MyClass()\n",
    "\n",
    "\n",
    "obj.classincrement()\n",
    "\n",
    "print(obj.cattr)          # should be 1\n",
    "print(MyClass.cattr)      # should be 1"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "684a0068",
   "metadata": {},
   "source": [
    "<U>Expected Output</U>:  "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d94e6bd3",
   "metadata": {},
   "source": [
    "<pre style=\"margin: 0; padding=0\"><DIV style=\"font-family: Courier; border: 1px solid #dbdbdb; padding: 7px\">1\n",
    "1</DIV></pre>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "467ac26c",
   "metadata": {},
   "source": [
    " "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8256e632",
   "metadata": {},
   "source": [
    "<B>6.13:  <U>Create a static method</U>.</B>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e562810d",
   "metadata": {},
   "source": [
    "To the below class add the static method <B>ftoc(temp)</B> which converts a temperature in Fahrenheit to Celsius.\n",
    "\n",
    "The formula is <B>(temp - 32) * 5 / 9</B>\n",
    "\n",
    "To be a static method, the method must not take <B>self</B> as an argument, and must be decorated with <B>@staticmethod</B>.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ae933753",
   "metadata": {},
   "outputs": [],
   "source": [
    "class Forecast:\n",
    "\n",
    "    def __init__(self, forecast, high=0, low=0):\n",
    "        self.text = forecast\n",
    "        self.hightemp = high\n",
    "        self.lowtemp = low\n",
    "\n",
    "    # your ftoc(temp) static methood here\n",
    "\n",
    "\n",
    "\n",
    "t = Forecast('Light rain', high=62, low=48)\n",
    "\n",
    "\n",
    "print(t.ftoc(32))       # 0.0\n",
    "print(t.ftoc(212))      # 100.0"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b8492e42",
   "metadata": {},
   "source": [
    "<U>Expected Output</U>:  "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "284cf1bf",
   "metadata": {},
   "source": [
    "<pre style=\"margin: 0; padding=0\"><DIV style=\"font-family: Courier; border: 1px solid #dbdbdb; padding: 7px\">0.0\n",
    "100.0</DIV></pre>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "660e9295",
   "metadata": {},
   "source": [
    " "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "92595d29",
   "metadata": {},
   "source": [
    "LAB 2"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ec7f1adc",
   "metadata": {},
   "source": [
    "<div style=\"background-color: #ccffcc; padding: 10px; border: 2px solid green\"><B>6.14:  Create a class <B>Name</B> that allows you to store a person's first and last name:</B></div>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6c90d763",
   "metadata": {},
   "outputs": [],
   "source": [
    "n = Name('David', 'Blaikie')\n",
    "\n",
    "print(n.first)      # 'David'\n",
    "print(n.last)       # 'Blaikie'"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fa158ceb",
   "metadata": {},
   "source": [
    "The class only needs to define an <B>\\_\\_init\\_\\_()</B> method, which besides 'self' must accept the 2 additional arguments (first name and last name).\n",
    "\n",
    "In <B>\\_\\_init\\_\\_()</B>, store the two values in attributes <B>.first</B> and <B>.last</B>.\n",
    "\n",
    "Use the above code as a test to see that the attributes have been successfully stored."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c5af1b90",
   "metadata": {},
   "source": [
    " "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "15aec31c",
   "metadata": {},
   "source": [
    "<div style=\"background-color: #ccffcc; padding: 10px; border: 2px solid green\"><B>6.15:  Continuing the <B>Name</B> class, add a method <B>.get\\_name()</B> that returns the full name as 'first name last name'.  </B></div>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1761bdb9",
   "metadata": {},
   "source": [
    "Finally, add another method <B>.get\\_rname()</B> that returns the name in reverse order (last, first).\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "15990225",
   "metadata": {},
   "outputs": [],
   "source": [
    "n = Name('David', 'Blaikie')\n",
    "\n",
    "print(n.get_name())             # David Blaikie\n",
    "\n",
    "print(n.get_rname())            # Blaikie, David"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ba677ec8",
   "metadata": {},
   "source": [
    " "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2dbe090c",
   "metadata": {},
   "source": [
    "<div style=\"background-color: #ccffcc; padding: 10px; border: 2px solid green\"><B>6.16:  Continuing the <B>Name</B> class, set a class variable 'label' that refers to a string 'Name: ' -- this will serve as the label to be printed along with a person's name</B></div>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b57fcb37",
   "metadata": {},
   "outputs": [],
   "source": [
    "n = Name('David', 'Blaikie')\n",
    "\n",
    "print(n.get_name())             # Name:  David Blaikie\n",
    "print(n.get_rname())            # Name:  Blaikie, David\n",
    "\n",
    "\n",
    "n2 = Name('Marie', 'Sanchez')\n",
    "\n",
    "print(n2.get_name())            # Name:  Marie Sanchez\n",
    "print(n2.get_rname())           # Name:  Sanchez, Marie"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2e3b90eb",
   "metadata": {},
   "source": [
    " "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c5306006",
   "metadata": {},
   "source": [
    "MODULES"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "80ace29b",
   "metadata": {},
   "source": [
    "<B>6.17:  <U>Create a module that holds a function</U>.  </B>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6f46a8a1",
   "metadata": {},
   "source": [
    "Create a new file, <B>temputils.py</B>, that has the below functions <B>def ctof()</B> and <B>def ftoc()</B>.  Place this file in the same folder as this exercise file (<B>inclass\\_exercises</B> if this is a <B>.py</B> file; <B>notebooks\\_inclass\\_challege</B> if this is a Jupyter notebook).\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "08d67eee",
   "metadata": {},
   "source": [
    "Contents of <B>temputils.py</B>:"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2f9c0725",
   "metadata": {},
   "source": [
    "<pre style=\"margin: 0; padding=0\"><DIV style=\"font-family: Courier; border: 1px solid #dbdbdb; padding: 7px\">def ctof(temp):\n",
    "    \"\"\" function to convert celsius to fahrenheit \"\"\"\n",
    "    if not isinstance(temp, (int,float)):\n",
    "        raise TypeError('must be an int or float')\n",
    "    return (temp * 9 / 5) + 32\n",
    "\n",
    "\n",
    "def ftoc(temp):\n",
    "    \"\"\" function to convert fahrenheit to celsius \"\"\"\n",
    "    if not isinstance(temp, (int,float)):\n",
    "        raise TypeError('must be an int or float')\n",
    "    return (temp - 32) * 5 / 9</DIV></pre>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a7b4779a",
   "metadata": {},
   "outputs": [],
   "source": [
    "import temputils as tu\n",
    "\n",
    "val = tu.ftoc(212)\n",
    "print(val)             # 100.0\n",
    "\n",
    "val2 = tu.ctof(0)\n",
    "print(val2)            # 32.0\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d7cf0888",
   "metadata": {},
   "source": [
    "<U>Expected Output</U>:  "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f547b1de",
   "metadata": {},
   "source": [
    "<pre style=\"margin: 0; padding=0\"><DIV style=\"font-family: Courier; border: 1px solid #dbdbdb; padding: 7px\">100.0\n",
    "32.0</DIV></pre>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "10a1c274",
   "metadata": {},
   "source": [
    " "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e243b1f0",
   "metadata": {},
   "source": [
    "<B>6.18:  <U>Set PYTHONPATH environment variable to make <B>utils.py</B> importable from any directory.  </B>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fb279afe",
   "metadata": {},
   "source": [
    "Move (don't copy) <B>temputils.py</B> to your Desktop.  This can be done through the PyCharm file view or your Finder or Windows Explorer windows.\n",
    "\n",
    "If you are using Jupyter Notebook, for this exercise you must first restart the kernel.  Click the circular arrow in the menu bar, or <B>Kernel -> Restart</B> (no outputs will be cleared from your notebook).\n",
    "\n",
    "Run the below exercise and note the <B>ModuleNotFoundError</B>, which indicates that the module can no longer be located.  This is because it is not in the same directory as this exercise file.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e0a16882",
   "metadata": {},
   "outputs": [],
   "source": [
    "import temputils as tu\n",
    "\n",
    "val = tu.ftoc(212)\n",
    "print(val)             # 100.0\n",
    "\n",
    "val2 = tu.ctof(0)\n",
    "print(val2)            # 32.0\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d3e8b657",
   "metadata": {},
   "source": [
    "Now follow the directions under the slide <B>Setting the PYTHONPATH Environment Variable</B> to set or add your Desktop path there:\n",
    "\n",
    "The most common locations for <B>Desktop</B> are:\n",
    "\n",
    "<U>Windows</U>:  <B>C:\\Users\\<I>username</I>\\Desktop</B>\n",
    "<U>Mac</U>:  <B>/Users/<I>username</I>/Desktop</B>\n",
    "\n",
    "Where <I>username</I> is the name of your home directory.\n",
    "\n",
    "Finally, run the code again and see that the module was found in its new location.\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f5c7b542",
   "metadata": {},
   "source": [
    "<U>Expected Output</U>:  "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "972fac92",
   "metadata": {},
   "source": [
    "<pre style=\"margin: 0; padding=0\"><DIV style=\"font-family: Courier; border: 1px solid #dbdbdb; padding: 7px\">100.0\n",
    "32.0</DIV></pre>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4eeccf15",
   "metadata": {},
   "source": [
    " "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4be81c35",
   "metadata": {},
   "source": [
    "<B>6.19:  <U>Create a module that holds a class</U>.</B>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "aa96512b",
   "metadata": {},
   "source": [
    "Add the below class to <B>temputils.py</B>.\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c0b5cd7c",
   "metadata": {},
   "source": [
    "<pre style=\"margin: 0; padding=0\"><DIV style=\"font-family: Courier; border: 1px solid #dbdbdb; padding: 7px\">class TempConvert:\n",
    "\n",
    "    def __init__(self, temp, scale='F'):\n",
    "        self.itemp = temp\n",
    "        self.scale = scale\n",
    "\n",
    "    def as_fahrenheit(self):\n",
    "        \"\"\" function to convert celsius to fahrenheit \"\"\"\n",
    "        if self.scale == 'F':\n",
    "            return self.itemp\n",
    "        return (self.itemp * 9 / 5) + 32\n",
    "\n",
    "    def as_celsius(self):\n",
    "        \"\"\" function to convert fahrenheit to celsius \"\"\"\n",
    "        if self.scale == 'C':\n",
    "            return self.itemp\n",
    "        return (self.itemp - 32) * 5 / 9\n",
    "</DIV></pre>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "18bc44fc",
   "metadata": {},
   "source": [
    "Then import the class and use it as indicated below.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "491a53b8",
   "metadata": {},
   "outputs": [],
   "source": [
    "import temputils as tu\n",
    "\n",
    "\n",
    "# construct a TempConvert object as 32 degrees Fahrenheit\n",
    "\n",
    "# call .as_celsius() to see the value 0\n",
    "\n",
    "\n",
    "# construct a Tempconvert object as 100 degrees Celsius\n",
    "\n",
    "# call .as_fahrenheit() to see the value 212\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0f47073a",
   "metadata": {},
   "source": [
    "<U>Expected Output</U>:  "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e7c4ed7e",
   "metadata": {},
   "source": [
    "<pre style=\"margin: 0; padding=0\"><DIV style=\"font-family: Courier; border: 1px solid #dbdbdb; padding: 7px\">0.0\n",
    "212.0</DIV></pre>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b580107f",
   "metadata": {},
   "source": [
    " "
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 5
}
