diff options
Diffstat (limited to 'Engineering_Thermodynamics:_A_Computer_Approach_(SI_Units_Version)/ch10.ipynb')
-rw-r--r-- | Engineering_Thermodynamics:_A_Computer_Approach_(SI_Units_Version)/ch10.ipynb | 286 |
1 files changed, 286 insertions, 0 deletions
diff --git a/Engineering_Thermodynamics:_A_Computer_Approach_(SI_Units_Version)/ch10.ipynb b/Engineering_Thermodynamics:_A_Computer_Approach_(SI_Units_Version)/ch10.ipynb new file mode 100644 index 00000000..4b6ac8a8 --- /dev/null +++ b/Engineering_Thermodynamics:_A_Computer_Approach_(SI_Units_Version)/ch10.ipynb @@ -0,0 +1,286 @@ +{ + "metadata": { + "name": "ch10" + }, + "nbformat": 3, + "nbformat_minor": 0, + "worksheets": [ + { + "cells": [ + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 10.1 Implementing the Ratio Class\n'''\nclass Ratio:\n def __init__(self):\n self.num = 0\n self.den = 0\n def assign(self,n,d):\n self.num = n\n self.den = d\n def convert(self):\n return float(self.num)/self.den\n def invert(self):\n self.num,self.den = self.den,self.num\n def print_(self):\n print self.num , '/' , self.den ,\n \nx = Ratio()\nx.assign(22,7)\nprint \"x = \",\nx.print_()\nprint \" = \" , x.convert() \nx.invert()\nprint \"1/x = \",\nx.print_()\n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "x = 22 / 7 = 3.14285714286\n1/x = 7 / 22\n" + } + ], + "prompt_number": 1 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 10.2 Class example\n'''\nclass Ratio:\n def __init__(self):\n self.num = 0\n self.den = 0\n def assign(self,n,d):\n self.num = n\n self.den = d\n def convert(self):\n return float(self.num)/self.den\n def invert(self):\n self.num,self.den = self.den,self.num\n def print_(self):\n print self.num , '/' , self.den ,\n", + "language": "python", + "metadata": {}, + "outputs": [], + "prompt_number": 2 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 10.3\nA Constructor Function for the Ratio Class\n'''\nclass Ratio:\n def __init__(self,n,d):\n self.num = n\n self.den = d\n def print_(self):\n print self.num , '/' , self.den ,\n\nx = Ratio(-1,3)\ny = Ratio(22,7)\nprint \"x = \",\nx.print_()\nprint \" and y = \",\ny.print_()\n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "x = -1 / 3 and y = 22 / 7\n" + } + ], + "prompt_number": 3 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 10.4 Constructors to the Ratio Class\n'''\nclass Ratio:\n def __init__(self,n=None,d=None):\n if n==None:\n self.num = 0\n self.den = 1\n elif d==None:\n self.num = n\n self.den = 1\n else:\n self.num = n\n self.den = d\n def print_(self):\n print self.num , '/' , self.den ,\n\nx = Ratio()\ny = Ratio(4)\nz = Ratio(22,7)\nprint \"x = \",\nx.print_()\nprint \"\\ny = \",\ny.print_()\nprint \"\\nz = \",\nz.print_()", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "x = 0 / 1 \ny = 4 / 1 \nz = 22 / 7\n" + } + ], + "prompt_number": 4 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 10.5\n'''\nclass Ratio:\n def __init__(self,n=None,d=None):\n if n==None:\n self.num = 0\n self.den = 1\n elif d==None:\n self.num = n\n self.den = 1\n else:\n self.num = n\n self.den = d\n def print_(self):\n print self.num , '/' , self.den ,\n\n", + "language": "python", + "metadata": {}, + "outputs": [], + "prompt_number": 5 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 10.6 Using Default Parameter Values in the Ratio Class Constructor\n'''\n\nclass Ratio:\n def __init__(self,n=0,d=1):\n self.num = n\n self.den = d\n def print_(self):\n print self.num , '/' , self.den ,\n\n\nx = Ratio()\ny = Ratio(4)\nz = Ratio(22,7)", + "language": "python", + "metadata": {}, + "outputs": [], + "prompt_number": 6 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 10.7 Access Functions in the Ratio Class\n'''\n\nclass Ratio:\n def __init__(self,n=0,d=1):\n self.num = n\n self.den = d\n def numerator(self):\n return self.num\n def denominator(self):\n return self.den\n def print_(self):\n print self.num , '/' , self.den ,\n\n\nx = Ratio(22,7)\nprint x.numerator() , '/' , x.denominator() ", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "22 / 7\n" + } + ], + "prompt_number": 7 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 10.8\n'''\ndef gcd(m,n):\n # returns the greatest common divisor of m and n:\n if (m<n):\n m,n = n,m\n while (n>0):\n r = m % n\n m = n\n n = r\n return m\n\nclass Ratio:\n def __init__(self,n=0,d=1):\n self.num = n\n self.den = d\n self.reduce()\n def numerator(self):\n return self.num\n def denominator(self):\n return self.den\n def print_(self):\n print self.num , '/' , self.den ,\n\n def reduce(self):\n # enforce invariant(den > 0):\n if (self.num == 0 or self.den == 0):\n self.num = 0\n self.den = 1\n return\n if (self.den < 0):\n self.den *= -1\n self.num *= -1\n # enforce invariant(gcd(num,den) == 1):\n if (self.den == 1):\n return\n # it's already reduced\n sgn = 0\n if self.num < 0:\n sgn = -1\n else:\n sgn = 1\n g = gcd(sgn*self.num,self.den)\n self.num /= g\n self.den /= g\n\nx = Ratio(100,-360)\nx.print_()\n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "-5 / 18\n" + } + ], + "prompt_number": 8 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 10.9 Adding a Copy Constructor to the Ratio Class\n'''\ndef gcd(m,n):\n # returns the greatest common divisor of m and n:\n if (m<n):\n m,n = n,m\n while (n>0):\n r = m % n\n m = n\n n = r\n return m\n\nclass Ratio:\n def __init__(self,n=0,d=None):\n if d == None:\n self.num = n.num\n self.den = n.den\n else: \n self.num = n\n self.den = d\n self.reduce()\n def numerator(self):\n return self.num\n def denominator(self):\n return self.den\n def print_(self):\n print self.num , '/' , self.den ,\n\n def reduce(self):\n # enforce invariant(den > 0):\n if (self.num == 0 or self.den == 0):\n self.num = 0\n self.den = 1\n return\n if (self.den < 0):\n self.den *= -1\n self.num *= -1\n # enforce invariant(gcd(num,den) == 1):\n if (self.den == 1):\n return\n # it's already reduced\n sgn = 0\n if self.num < 0:\n sgn = -1\n else:\n sgn = 1\n g = gcd(sgn*self.num,self.den)\n self.num /= g\n self.den /= g\n\nx = Ratio(100,360)\ny = Ratio(x)\nprint \"x = \",\nx.print_()\nprint \"y = \",\ny.print_()\n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "x = 5 / 18 y = 5 / 18\n" + } + ], + "prompt_number": 9 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 10.10 Tracing Calls to the Copy Constructor\n'''\n'''\nEXAMPLE 10.9 Adding a Copy Constructor to the Ratio Class\n'''\n'''\nEXAMPLE 10.8\n'''\ndef gcd(m,n):\n # returns the greatest common divisor of m and n:\n if (m<n):\n m,n = n,m\n while (n>0):\n r = m % n\n m = n\n n = r\n return m\n\nclass Ratio:\n def __init__(self,n=0,d=None):\n if d == None:\n print \"COPY CONSTRUCTOR CALLED\"\n self.num = n.num\n self.den = n.den\n else: \n self.num = n\n self.den = d\n self.reduce()\n def numerator(self):\n return self.num\n def denominator(self):\n return self.den\n def print_(self):\n print self.num , '/' , self.den ,\n\n def reduce(self):\n # enforce invariant(den > 0):\n if (self.num == 0 or self.den == 0):\n self.num = 0\n self.den = 1\n return\n if (self.den < 0):\n self.den *= -1\n self.num *= -1\n # enforce invariant(gcd(num,den) == 1):\n if (self.den == 1):\n return\n # it's already reduced\n sgn = 0\n if self.num < 0:\n sgn = -1\n else:\n sgn = 1\n g = gcd(sgn*self.num,self.den)\n self.num /= g\n self.den /= g\n\ndef f(r):\n s = Ratio(r)\n\nx = Ratio(22,7)\ny = Ratio(x) #calls the copy constructor, copying x to y\nf(y)\n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "COPY CONSTRUCTOR CALLED\nCOPY CONSTRUCTOR CALLED\n" + } + ], + "prompt_number": 12 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 10.11 Including a Destructor in the Ratio Class\nNote : Python objects die when program gets exit.\n'''\n\nclass Ratio:\n def __init__(self):\n print \"OBJECT IS BORN.\"\n def __del__(self):\n print \"OBJECT DIES.\"\n\nx = Ratio()\nprint \"Now x is alive.\"\nprint \"Now between blocks.\"\ny = Ratio()\nprint \"Now y is alive.\"\n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "OBJECT IS BORN.\nNow x is alive.\nNow between blocks.\nOBJECT IS BORN.\nNow y is alive.\n" + } + ], + "prompt_number": 13 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 10.12\n'''\nclass X:\n def __init(self):\n data = 0\n\np = X()\np.data = 22\nprint \"p.data = \" , p.data , \" = \" , p.data\np.data = 44\nprint \" p.data = \" , p.data , \" = \" , p.data ", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "p.data = 22 = 22\n p.data = 44 = 44\n" + } + ], + "prompt_number": 14 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 10.13 A Node Class for Linked Lists\n'''\nclass Node:\n def __init__(self,d,q=None):\n self.data = d\n self.next = q\n\nn = int(raw_input())\nq = Node(n)\nwhile True:\n n = int(raw_input())\n if n<=0:\n break\n p = Node(n, q)\n q = p\nk = p\nwhile k != None:\n print k.data , '->' , \n k = k.next\nprint '*'\n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "22\n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "33\n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "44\n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "55\n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "66\n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "77\n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "0\n" + }, + { + "output_type": "stream", + "stream": "stdout", + "text": "77 -> 66 -> 55 -> 44 -> 33 -> 22 -> *\n" + } + ], + "prompt_number": 15 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 10.14 \nPython does not support static data type.\nPython automatically handles local variable so we need not to delete it.\n'''\ncount = 0\nclass Widget:\n def __init__(self):\n global count\n count += 1\n \nw = Widget()\nx = Widget()\nprint \"Now there are \" , count , 'widgets'\nif True:\n w = Widget()\n x = Widget()\n y = Widget()\n z = Widget()\n print \"Now there are\" , count , 'widgets' \nprint \"Now there are \" , count , 'widgets'\ny = Widget()\nprint \"Now there are \" , count , 'widgets'\n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "OBJECT DIES.\nNow there are 2 widgets\nOBJECT DIES.\nNow there are 6 widgets\nNow there are 6 widgets\nNow there are 7 widgets\n" + } + ], + "prompt_number": 16 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 10.15 \n'''\ncount = 0\nclass Widget:\n def __init__(self):\n global count\n count += 1\n def numWidgets(self):\n global count\n return count\n \nw = Widget()\nx = Widget()\nprint \"Now there are \" , w.numWidgets() , 'widgets'\nif True:\n w = Widget()\n x = Widget()\n y = Widget()\n z = Widget()\n print \"Now there are\" , w.numWidgets() , 'widgets' \nprint \"Now there are \" , w.numWidgets() , 'widgets'\ny = Widget()\nprint \"Now there are \" , w.numWidgets() , 'widgets'\n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "Now there are 2 widgets\nNow there are 6 widgets\nNow there are 6 widgets\nNow there are 7 widgets\n" + } + ], + "prompt_number": 17 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 10.16\n'''\ncount = 0\nclass Widget:\n def __init__(self):\n global count\n count += 1\n def numWidgets(self):\n global count\n return count\n \nw = Widget()\nx = Widget()\nprint \"Now there are \" , w.numWidgets() , 'widgets'\nif True:\n w = Widget()\n x = Widget()\n y = Widget()\n z = Widget()\n print \"Now there are\" , w.numWidgets() , 'widgets' \nprint \"Now there are \" , w.numWidgets() , 'widgets'\ny = Widget()\nprint \"Now there are \" , w.numWidgets() , 'widgets'", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "Now there are 2 widgets\nNow there are 6 widgets\nNow there are 6 widgets\nNow there are 7 widgets\n" + } + ], + "prompt_number": 18 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "", + "language": "python", + "metadata": {}, + "outputs": [] + } + ], + "metadata": {} + } + ] +}
\ No newline at end of file |