{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### <p style=\"text-align: right;\"> &#9989; **Put your name here** </p>\n",
    "#### <p style=\"text-align: right;\"> &#9989; Put your group member names here</p>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Day 16 In-Class Assignment: More OOP\n",
    "    "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Agenda for today's class\n",
    "\n",
    "</p>\n",
    "\n",
    "1. More about standard methods\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 1 More standard methods\n",
    "\n",
    "Still working with our `Point` class below."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Standard operators\n",
    "Python has two classes of standard operators:\n",
    "\n",
    "* math operations (+, -, *, /, etc.)\n",
    "* collection operations ([], iteration, etc.)\n",
    "\n",
    "It maps the symbols used (such as +) to a set of pre-defined methods. There are well over 60 such methods, but we can get the basic ideas with just a few\n",
    "\n",
    "How does Python map + to a method?\n",
    "\n",
    "<img src=\"https://i.ibb.co/bHmdkbC/add-method.png\" alt=\"add-method\" border=\"0\">"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "There are two important points in the above diagram:\n",
    "\n",
    "* every operator has an associated method as provided by Python. For example:\n",
    "    * `+:__add__`\n",
    "    * `-:__sub__`\n",
    "    * etc. All begin and end with two underlines.\n",
    "    * you can only connect an operator to one of the existing methods (you can't make them up)\n",
    "* in a binary operator calling sequence (as the figure shows), the left-hand-side (*lhs*) of the operator is used to call the method and the right-hand-side (*rhs*) is passed as a parameter.\n",
    "    * Python does this translation automatically\n",
    "    * both forms are **exactly** equivalent, but the binary operator form is more convenient."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Init called\n",
      "Add called\n",
      "Init called\n",
      "Result type is:  <class '__main__.Point'>\n",
      "String called\n",
      "(01:01) String called\n",
      "(02:02)\n"
     ]
    }
   ],
   "source": [
    "class Point(object):\n",
    "   def __init__(self,x=0,y=0):   \n",
    "        self.x_coord = x\n",
    "        self.y_coord = y\n",
    "        print(\"Init called\")\n",
    "   def __str__(self):\n",
    "        print(\"String called\")\n",
    "        return \"({:02d}:{:02d})\".format(self.x_coord, self.y_coord)\n",
    "   def translate(self,x=0,y=0): \n",
    "        self.x_coord += x\n",
    "        self.y_coord += y\n",
    "   def __add__ (self, rhs):\n",
    "        x = self.x_coord + rhs.x_coord\n",
    "        y = self.y_coord + rhs.y_coord\n",
    "        print(\"Add called\")\n",
    "        return Point(x,y)\n",
    "\n",
    "my_point = Point(1,1)\n",
    "result = my_point + my_point\n",
    "# result = my_point.__add__(my_point) # same thing!!!\n",
    "print(\"Result type is: \", type(result))\n",
    "print(my_point, result)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "In the `Point` class, we now include an `__add__` method. When we call the `__add__` method a new `Point` instance is made. Note the second call of `__init__`, that is from inside the `__add__` method when the new `Point` is returned. Also notice that the original `my_point` is not changed as a result of the call."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Copy the `Point` code above in the cell below but modify the `__add__` method to be a single line (which is all it needs to be)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# your code here"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 2 Updating our Clock class\n",
    "\n",
    "OK, let's update our `Clock` class. New elements are marked with a *\n",
    "- each `Clock` instance will represent the present time in hours, minutes and seconds.\n",
    "    - thus each instance will have three data attributes: `hours`, `minutes` and `seconds`\n",
    "- the default instance will have all attributes set to 0\n",
    "- provide a `__str__` method\n",
    "- provide an `__add__` such that we can add two clock instances, yielding a new clock instance:\n",
    "    - no hour will be greater than 23, no minute or second greater than 59\n",
    "    - we will use clock arithmetic: when a sum is larger than the limit, a carry is added\n",
    "    - as our `Clock` instance has no days represented, there is no carry recorded for hours.\n",
    " \n",
    " Do it in stages. Copy from your previous `Clock` class"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# your code here"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "01:00\n",
      "01:01\n",
      "02:01\n"
     ]
    }
   ],
   "source": [
    "# some test code\n",
    "c1 = Clock(24,60)  \n",
    "print(c1)          # should be 01:00\n",
    "c2 = Clock(1,1)\n",
    "print(c2)          # should be 01:01\n",
    "c3 = c1 + c2\n",
    "print(c3)          # should be 02:01"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "-----\n",
    "### Congratulations, we're done!\n",
    "\n",
    "Now, you just need to submit this assignment by uploading it to the course <a href=\"https://d2l.msu.edu/\">Desire2Learn</a> web page for today's submission folder (Don't forget to add your names in the first cell).\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "&#169; Copyright Michigan State University Board of Trustees"
   ]
  }
 ],
 "metadata": {
  "anaconda-cloud": {},
  "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.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
