summaryrefslogtreecommitdiff
path: root/Beginning_C++_through_Game_Programming/ch11.ipynb
blob: 3f21ca72eea8ac6e9bd0166678b5fe7c90e70c7f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
{
 "metadata": {
  "name": "ch11"
 },
 "nbformat": 3,
 "nbformat_minor": 0,
 "worksheets": [
  {
   "cells": [
    {
     "cell_type": "heading",
     "level": 1,
     "metadata": {},
     "source": "CHAPTER 11 : MEMBER FUNCTIONS"
    },
    {
     "cell_type": "heading",
     "level": 3,
     "metadata": {},
     "source": "example 11.1 page no :112\n"
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": "'''\nexample 11.1 page no :112\n'''\nclass Time:\n    def __init__(self,h,m,s):\n        self.hour = h\n        self.minute = m\n        self.second = s\n\n    def printTime(self):\n        print self.hour , \":\" , self.minute , \":\" , self.second\n        print \"Time is \" , self.hour , \" hour \" , self.minute , \" minutes \" , self.second \n\n\nc = Time( 9, 14, 30.0 )\nc.printTime ()",
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": "9 : 14 : 30.0\nTime is  9  hour  14  minutes  30.0\n"
      }
     ],
     "prompt_number": 1
    },
    {
     "cell_type": "heading",
     "level": 3,
     "metadata": {},
     "source": "example 11.2 page  no :114\n"
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": "'''\nexample 11.2 page  no :114\n'''\nclass Time:\n    def __init__(self,h,m,s):\n        self.hour = h\n        self.minute = m\n        self.second = s\n\n    def printTime(self):\n        print self.hour , \":\" , self.minute , \":\" , self.second\n        print \"Time is \" , self.hour , \" hour \" , self.minute , \" minutes \" , self.second \n\n    def increment (self,secs):\n        self.second += secs\n        while (self.second >= 60.0):\n            self.second -= 60.0;\n            self.minute += 1;\n\n        while (self.minute >= 60):\n            self.minute -= 60.0;\n            self.hour += 1;\n\nc = Time (9, 14, 30.0)\nc.increment (500.0);\nc.printTime ();",
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": "9 : 22 : 50.0\nTime is  9  hour  22  minutes  50.0\n"
      }
     ],
     "prompt_number": 2
    },
    {
     "cell_type": "heading",
     "level": 3,
     "metadata": {},
     "source": "example 11.3 page no :115\n"
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": "'''\nexample 11.3 page no :115\n'''\nclass Time:\n    def __init__(self,h,m,s):\n        self.hour = h\n        self.minute = m\n        self.second = s\n\n    def printTime(self):\n        print self.hour , \":\" , self.minute , \":\" , self.second\n        print \"Time is \" , self.hour , \" hour \" , self.minute , \" minutes \" , self.second \n\n    def convertToSeconds(self):\n        minutes = self.hour * 60 + self.minute;\n        seconds = minutes * 60 + self.second;\n        return seconds;\n\n\nc = Time (9, 14, 30.0)\nprint c.convertToSeconds()",
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": "33270.0\n"
      }
     ],
     "prompt_number": 3
    },
    {
     "cell_type": "heading",
     "level": 3,
     "metadata": {},
     "source": "example 11.4 page no :115\n"
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": "'''\nexample 11.4 page no :115\n'''\n\n\nclass Time:\n    def __init__(self,h,m,s):\n        self.hour = h\n        self.minute = m\n        self.second = s\n\n    def printTime(self):\n        print self.hour , \":\" , self.minute , \":\" , self.second\n        print \"Time is \" , self.hour , \" hour \" , self.minute , \" minutes \" , self.second \n\n    def convertToSeconds(self):\n        minutes = self.hour * 60 + self.minute;\n        seconds = minutes * 60 + self.second;\n        return seconds;\n\n    def after (self,time2):\n        if (self.hour > time2.hour):\n            return True;\n        if (self.hour < time2.hour): \n            return False;\n        if (self.minute > time2.minute): \n            return True;\n        if (self.minute < time2.minute):\n            return False;\n        if (self.second > time2.second): \n            return True;\n        return False;\n\nt1 = Time( 11, 59, 3.14159 )\nt2 = Time( 12, 42, 3.234 )\n\nif (t2.after(t1)):\n    print \"The bread will be done after it starts.\" ",
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": "The bread will be done after it starts.\n"
      }
     ],
     "prompt_number": 4
    },
    {
     "cell_type": "heading",
     "level": 3,
     "metadata": {},
     "source": "example 11.5 page no :116\n"
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": "'''\nexample 11.5 page no :116\n'''\n\nclass Time:\n    def __init__(self,h,m=None,s=None):\n        if m==None:\n            self.hour = int (secs / 3600.0);\n            secs -= self.hour * 3600.0;\n            self.minute = int (secs / 60.0);  \n            secs -= self.minute * 60.0;\n            self.second = secs;\n        else:\n            self.hour = h\n            self.minute = m\n            self.second = s\n\n    def printTime(self):\n        print self.hour , \":\" , self.minute , \":\" , self.second\n        print \"Time is \" , self.hour , \" hour \" , self.minute , \" minutes \" , self.second \n\nc = Time (9, 14, 30.0)\nc.printTime()",
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": "9 : 14 : 30.0\nTime is  9  hour  14  minutes  30.0\n"
      }
     ],
     "prompt_number": 5
    },
    {
     "cell_type": "heading",
     "level": 3,
     "metadata": {},
     "source": "example 11.6 page no : 117\n"
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": "'''\nexample 11.6 page no : 117\n'''\nclass Time:\n    def __init__(self,h,m,s):\n        self.hour = h\n        self.minute = m\n        self.second = s\n\n    def printTime(self):\n        print self.hour , \":\" , self.minute , \":\" , self.second\n        print \"Time is \" , self.hour , \" hour \" , self.minute , \" minutes \" , self.second \n\nc = Time (9, 14, 30.0)\nc.printTime()",
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": "9 : 14 : 30.0\nTime is  9  hour  14  minutes  30.0\n"
      }
     ],
     "prompt_number": 6
    },
    {
     "cell_type": "heading",
     "level": 3,
     "metadata": {},
     "source": "example 11.7 page no :119\n"
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": "'''\nexample 11.7 page no :119\n'''\nclass Time:\n    def __init__(self,h,m=None,s=None):\n        if m==None:\n            self.hour = int (secs / 3600.0);\n            secs -= self.hour * 3600.0;\n            self.minute = int (secs / 60.0);  \n            secs -= self.minute * 60.0;\n            self.second = secs;\n        else:\n            self.hour = h\n            self.minute = m\n            self.second = s\n\n    def printTime(self):\n        print self.hour , \":\" , self.minute , \":\" , self.second\n        print \"Time is \" , self.hour , \" hour \" , self.minute , \" minutes \" , self.second \n\n    def after (self,time2):\n        if (self.hour > time2.hour):\n            return True;\n        if (self.hour < time2.hour): \n            return False;\n        if (self.minute > time2.minute): \n            return True;\n        if (self.minute < time2.minute):\n            return False;\n        if (self.second > time2.second): \n            return True;\n        return False;\n\n    def convertToSeconds(self):\n        minutes = self.hour * 60 + self.minute;\n        seconds = minutes * 60 + self.second;\n        return seconds;\n\n    def increment (self,secs):\n        self.second += secs\n        while (self.second >= 60.0):\n            self.second -= 60.0;\n            self.minute += 1;\n\n        while (self.minute >= 60):\n            self.minute -= 60.0;\n            self.hour += 1;\n\n    def add(self,t2):\n        a = t2.convertToSeconds()\n        self.increment(a)\n\nc = Time(9, 14, 30.0)\nc.increment (500.0);\nc.printTime();\nb = Time(3, 35, 0.0);\ncurent = c\nc.add(b)\nd = c\nd.printTime()\nif(d.after(curent)):\n    print \"The bread will be done after it starts.\" ",
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": "9 : 22 : 50.0\nTime is  9  hour  22  minutes  50.0\n12 : 57.0 : 50.0\nTime is  12  hour  57.0  minutes  50.0\n"
      }
     ],
     "prompt_number": 7
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": "",
     "language": "python",
     "metadata": {},
     "outputs": []
    }
   ],
   "metadata": {}
  }
 ]
}