diff options
Diffstat (limited to 'Engineering_Thermodynamics:_A_Computer_Approach_(SI_Units_Version)/ch12.ipynb')
-rw-r--r-- | Engineering_Thermodynamics:_A_Computer_Approach_(SI_Units_Version)/ch12.ipynb | 228 |
1 files changed, 228 insertions, 0 deletions
diff --git a/Engineering_Thermodynamics:_A_Computer_Approach_(SI_Units_Version)/ch12.ipynb b/Engineering_Thermodynamics:_A_Computer_Approach_(SI_Units_Version)/ch12.ipynb new file mode 100644 index 00000000..304a3ccb --- /dev/null +++ b/Engineering_Thermodynamics:_A_Computer_Approach_(SI_Units_Version)/ch12.ipynb @@ -0,0 +1,228 @@ +{ + "metadata": { + "name": "ch12" + }, + "nbformat": 3, + "nbformat_minor": 0, + "worksheets": [ + { + "cells": [ + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 12.1 A Person Class\nHere is a simple definition for a class to represent people.\n'''\nclass Person:\n def __init__(self,n=\"\",nat=\"U.S.A.\",s=1):\n self.name = n\n self.nationality = nat\n self.sex = s\n\n def printName(self):\n print self.name,\n \n def printNationality(self):\n print self.nationality,\n\ncreator = Person(\"Bjarne Stroustrup\", \"Denmark\")\nprint \"The creator of C++ was \" ,\ncreator.printName() \nprint \", who was born in \" ,\ncreator.printNationality() \n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "The creator of C++ was Bjarne Stroustrup , who was born in Denmark\n" + } + ], + "prompt_number": 1 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 12.2 A Date Class\n'''\n\nclass Date:\n def __init__(self,m=0,d=0,y=0):\n self.month = m\n self.day = d\n self.year = y\n \n def setDate(self,m,d,y):\n self.month = m\n self.day = d\n self.year = y\n # Python doesn't have >> operator for input so we are just using input function\n def input(self):\n self.month = int(raw_input()) \n self.day = int(raw_input())\n self.year = int(raw_input()) \n \n # Python doesn't have << operator for output so we are just using print function\n def print_(self):\n monthName = [\"\", \"January\",\"February\",\"March\", \"April\", \"May\", \"June\",\\\n \"July\", \"August\",\"September\", \"October\", \"November\",\\\n \"December\"]\n print monthName[self.month] , self.day , \",\" , self.year\n\npeace = Date(11,11,1918)\nprint \"World War I ended on \" ,\npeace.print_()\npeace.setDate(8,14,1945)\nprint \"World War II ended on \" ,\npeace.print_()\nprint \"Enter month, day, and year: \"\ndate = Date()\ndate.input()\nprint \"The date is \" , \ndate.print_()\n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "World War I ended on November 11 , 1918\nWorld War II ended on August 14 , 1945\nEnter month, day, and year: \n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "7\n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "4\n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "1976\n" + }, + { + "output_type": "stream", + "stream": "stdout", + "text": "The date is July 4 , 1976\n" + } + ], + "prompt_number": 2 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 12.3 Composing the Date Class with the Person Class\n'''\nclass Date:\n def __init__(self,m=0,d=0,y=0):\n self.month = m\n self.day = d\n self.year = y\n \n def setDate(self,m,d,y):\n self.month = m\n self.day = d\n self.year = y\n # Python doesn't have >> operator for input so we are just using input function\n def input(self):\n self.month = int(raw_input()) \n self.day = int(raw_input())\n self.year = int(raw_input()) \n \n # Python doesn't have << operator for output so we are just using print function\n def print_(self):\n monthName = [\"\", \"January\",\"February\",\"March\", \"April\", \"May\", \"June\",\\\n \"July\", \"August\",\"September\", \"October\", \"November\",\\\n \"December\"]\n print monthName[self.month] , self.day , \",\" , self.year\n\nclass Person:\n def __init__(self,n=\"\",s=0,nat=\"U.S.A.\"):\n self.name = n\n self.nationality = nat\n self.sex = s\n self.dob = Date()\n self.dod = Date()\n def setDOB(self,m,d,y):\n self.dob.setDate(m,d,y)\n def setDOD(self,m,d,y):\n self.dod.setDate(m,d,y)\n def printName(self):\n print self.name,\n def printNationality(self):\n print self.nationality,\n def printDOB(self):\n self.dob.print_()\n def printDOD(self):\n self.dod.print_()\n\nauthor = Person(\"Thomas Jefferson\", 1)\nauthor.setDOB(4,13,1743)\nauthor.setDOD(7,4,1826)\nprint \"The author of the Declaration of Independence was \",\nauthor.printName()\nprint \".\\nHe was born on \",\nauthor.printDOB()\nprint \" and died on \",\nauthor.printDOD()\n\n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "The author of the Declaration of Independence was Thomas Jefferson .\nHe was born on April 13 , 1743\n and died on July 4 , 1826\n" + } + ], + "prompt_number": 3 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 12.4 Deriving a Student Class from the Person Class\nStudents are people. So it is natural to use the Person class to derive a Student class\n'''\nclass Date:\n def __init__(self,m=0,d=0,y=0):\n self.month = m\n self.day = d\n self.year = y\n \n def setDate(self,m,d,y):\n self.month = m\n self.day = d\n self.year = y\n # Python doesn't have >> operator for input so we are just using input function\n def input(self):\n self.month = int(raw_input()) \n self.day = int(raw_input())\n self.year = int(raw_input()) \n \n # Python doesn't have << operator for output so we are just using print function\n def print_(self):\n monthName = [\"\", \"January\",\"February\",\"March\", \"April\", \"May\", \"June\",\\\n \"July\", \"August\",\"September\", \"October\", \"November\",\\\n \"December\"]\n print monthName[self.month] , self.day , \",\" , self.year\n\nclass Person:\n def __init__(self,n=\"\",s=0,nat=\"U.S.A.\"):\n self.name = n\n self.nationality = nat\n self.sex = s\n self.dob = Date()\n self.dod = Date()\n def setDOB(self,m,d,y):\n self.dob.setDate(m,d,y)\n def setDOD(self,m,d,y):\n self.dod.setDate(m,d,y)\n def printName(self):\n print self.name,\n def printNationality(self):\n print self.nationality,\n def printDOB(self):\n self.dob.print_()\n def printDOD(self):\n self.dod.print_()\n\nclass Student(Person):\n def __init__(self,n,s=0,i=\"\"):\n Person.__init__(self,n,s)\n self.id = i\n self.credits = 0\n self.gpa = 0\n self.dom = Date()\n\n def setDOM(self,m,d,y):\n self.dom.setDate(m, d, y)\n def printDOM(self):\n self.dom.print_()\n\nx = Student(\"Ann Jones\", 0, \"219360061\")\nx.setDOB(5, 13, 1977)\nx.setDOM(8, 29, 1995)\nx.printName()\nprint \"\\n\\t Born: \" ,\nx.printDOB()\nprint \"\\n\\tMatriculated: \",\nx.printDOM()\n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "Ann Jones \n\t Born: May 13 , 1977\n\n\tMatriculated: August 29 , 1995\n" + } + ], + "prompt_number": 4 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 12.5 The Person Class\n'''\nclass Date:\n def __init__(self,m=0,d=0,y=0):\n self.month = m\n self.day = d\n self.year = y\n \n def setDate(self,m,d,y):\n self.month = m\n self.day = d\n self.year = y\n # Python doesn't have >> operator for input so we are just using input function\n def input(self):\n self.month = int(raw_input()) \n self.day = int(raw_input())\n self.year = int(raw_input()) \n \n # Python doesn't have << operator for output so we are just using print function\n def print_(self):\n monthName = [\"\", \"January\",\"February\",\"March\", \"April\", \"May\", \"June\",\\\n \"July\", \"August\",\"September\", \"October\", \"November\",\\\n \"December\"]\n print monthName[self.month] , self.day , \",\" , self.year\n\nclass Person:\n def __init__(self,n=\"\",s=0,nat=\"U.S.A.\"):\n self.name = n\n self.nationality = nat\n self.sex = s\n self.dob = Date()\n self.dod = Date()\n def setDOB(self,m,d,y):\n self.dob.setDate(m,d,y)\n def setDOD(self,m,d,y):\n self.dod.setDate(m,d,y)\n def printName(self):\n print self.name,\n def printNationality(self):\n print self.nationality,\n def printDOB(self):\n self.dob.print_()\n def printDOD(self):\n self.dod.print_()\n\nclass Student(Person):\n def __init__(self,n,s=0,i=\"\"):\n Person.__init__(self,n,s)\n self.id = i\n self.credits = 0\n self.gpa = 0\n self.dom = Date()\n\n def setDOM(self,m,d,y):\n self.dom.setDate(m, d, y)\n def printDOM(self):\n self.dom.print_()\n def printSex(self):\n if self.sex == 1:\n print \"male\"\n else:\n print 'female'\n\nx = Student(\"Ann Jones\", 0, \"219360061\")\nx.setDOB(5, 13, 1977)\nx.setDOM(8, 29, 1995)\nx.setDOD(7,4,1826)\nx.printName()\nprint \"\\n\\t Born: \" , \nx.printDOB()\nprint \"\\n\\t Sex: \" ,\nx.printSex()\nprint \"\\n\\tMatriculated: \",\nx.printDOM()\n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "Ann Jones \n\t Born: May 13 , 1977\n\n\t Sex: female\n\n\tMatriculated: August 29 , 1995\n" + } + ], + "prompt_number": 5 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 12.6 Dominating a Data Member and Overriding a Member Function\nHere are two classes, X and Y, with Y inheriting from X.\n'''\nclass X:\n def __init__(self):\n self.a = 0\n def f(self):\n print \"X::f() executing\"\nclass Y(X):\n def __init__(self):\n self.a = 0\n def f(self):\n print \"Y::f() executing\"\nx = X()\nx.a = 22\nx.f()\nprint \"x.a = \" , x.a\ny = Y()\ny.a = 44\n# assigns 44 to the a defined in Y\ny._X__a = 66\n# assigns 66 to the a defined in X\ny.f()\n# invokes the f() defined in Y\nX.f(x)\n# invokes the f() defined in X\nprint \"y.a = \" , y.a \nprint \"y._X__a = \" , y._X__a \nz = y\nprint \"z.a = \" , z._X__a \n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "X::f() executing\nx.a = 22\nY::f() executing\nX::f() executing\ny.a = 44\ny._X__a = 66\nz.a = 66\n" + } + ], + "prompt_number": 6 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 12.7 Parent Constructors and Destructors\nNote : Python destuctor is called when program goes exit. So output may be differ than c.\n'''\nclass X:\n def __init__(self):\n print \"X::X() constructor executing \"\n def __del__(self):\n print \"X::X() destructor executing \"\n\nclass Y(X):\n def __init__(self):\n X.__init__(self)\n print \"Y::Y() constructor executing \"\n def __del__(self):\n print \"Y::Y() destructor executing \"\n\nclass Z(Y):\n def __init__(self,i):\n Y.__init__(self)\n print \"Z::Z(\" , i , \") constructor executing \"\n def __del__(self):\n print \"Z::Z() destructor executing \"\n \n\nZ = Z(44)\n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "X::X() constructor executing \nY::Y() constructor executing \nZ::Z( 44 ) constructor executing \n" + } + ], + "prompt_number": 7 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 12.8 Parent Constructors and Destructors\nHere is a demo program that uses a base class Person and a derived class Student:\nNote : Python destuctor is called when program goes exit. So output may be differ than c.\n'''\nclass Person:\n def __init__(self,s):\n self.name = s\n def __del__(self):\n pass\n\nclass Student(Person):\n def __init__(self,s,m):\n Person.__init__(self,s)\n self.major = m\n def __del__(self):\n pass\nx = Person(\"Bob\")\ny = Student(\"Sarah\", \"Biology\")\n", + "language": "python", + "metadata": {}, + "outputs": [], + "prompt_number": 8 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 12.9\n'''\nclass Person:\n def __init__(self,n=\"\",s=0,nat=\"U.S.A.\"):\n self.name = n\n self.nationality = nat\n self.sex = s\n self.dob = Date()\n self.dod = Date()\n def setDOB(self,m,d,y):\n self.dob.setDate(m,d,y)\n def setDOD(self,m,d,y):\n self.dod.setDate(m,d,y)\n def printName(self):\n print self.name,\n def printNationality(self):\n print self.nationality,\n def printDOB(self):\n self.dob.print_()\n def printDOD(self):\n self.dod.print_()\n def setHSgraduate(self,g):\n self.hs = g\n def isHSgraduate(self):\n return hs\n", + "language": "python", + "metadata": {}, + "outputs": [], + "prompt_number": 9 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 12.10 Using virtual Functions\nThis demo program declares p to be a pointer to objects of the base class X. First it assigns p to\npoint to an instance x of class X. Then it assigns p to point to an instance y of the derived class Y.\nNote : By default all methods in python are virtual. so output would be differ than c.\n'''\nclass X:\n def f(self):\n print \"X::f() executing\"\nclass Y(X):\n def f(self):\n print \"Y::f() executing\"\nx = X()\ny = Y()\np = x\np.f()\np = y\np.f()\n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "X::f() executing\nY::f() executing\n" + } + ], + "prompt_number": 10 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 12.11 Polymorphism through virtual Functions\nHere is a Person class with a Student subclass and a Professor subclass:\n'''\nclass Person:\n def __init__(self,n):\n self.name = n\n def print_(self):\n print 'My name is' , self.name\n\nclass Student(Person):\n def __init__(self,s,g):\n Person.__init__(self,s)\n self.gpa = g\n def print_(self):\n print 'My name is ',self.name, ' and my G.P.A. is', self.gpa\n\nclass Professor(Person):\n def __init__(self,s,n):\n Person.__init__(self,s)\n self.publs = n\n def print_(self):\n print 'My name is ', self.name,' and i have ' , self.publs,' publications.'\n\nx = Person(\"Bob\")\np = x\np.print_()\ny = Student(\"Tom\", 3.47)\np = y\np.print_()\nz = Professor(\"Ann\", 7)\np = z\np.print_()", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "My name is Bob\nMy name is Tom and my G.P.A. is 3.47\nMy name is Ann and i have 7 publications.\n" + } + ], + "prompt_number": 11 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 12.12 Memory Leaks\nThis program is similar to Example 12.6:\n'''\nclass X:\n def __init__(self):\n self.p = [0,0]\n print 'X().',\n def __del__(self):\n print '~X().'\n\nclass Y(X):\n def __init__(self):\n X.__init__(self)\n self.q = []\n for i in range(1023):\n self.q.append(0)\n print 'Y() : Y::q = ', hex(id(self.q)) ,'.',\n def __del__(self):\n print '~Y().'\n\nfor i in range(8):\n r = Y()", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "X(). Y() : Y::q = 0x90d91cc . ~Y().\nX(). Y() : Y::q = 0x90c944c . ~Y().\nX(). Y() : Y::q = 0x90d91cc . ~Y().\nX(). Y() : Y::q = 0x90c944c . ~Y().\nX(). Y() : Y::q = 0x90d91cc . ~Y().\nX(). Y() : Y::q = 0x90c944c . ~Y().\nX(). Y() : Y::q = 0x90d91cc . ~Y().\nX(). Y() : Y::q = 0x90c944c . ~Y().\n" + } + ], + "prompt_number": 13 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 12.13 A Hierarchy of Media Classes\nHere is a hierarchy of classes to represent various media objects:\nMedia Audio Book Periodical CD Tape Record Magazine Newspaper Journal Newsletter\nThe primary ABC is the Media class:\n'''\nclass Media:\n def __init__(self):\n self.title = ''\n def print_(self):\n pass\n def id(self):\n pass\n\nclass Book(Media):\n def __init__(self,a='',t=\"\",p=\"\",i=\"\"):\n self.author = a\n self.publisher = p\n self.isbn = i\n self.title = t\n def print_(self):\n print self.title , \" by \" , self.author\n def id(self):\n return self.isbn\n\nclass CD(Media):\n def __init__(self,t=\"\",c=\"\",m=\"\",n=\"\"):\n self.composer = c\n self.make = m\n self.number = n\n self.title = t\n def print_(self):\n print self.title , \", \" , self.composer\n def id(self):\n s = str(self.make) + ' ' + str(self.number)\n return s\n\nclass Magazine(Media):\n def __init__(self,t=\"\",i=\"\",v=0, n=0):\n self.issn = i\n self.volume = v\n self.number = n\n self.title = t\n def print_(self):\n print self.title , \" Magazine, Vol. \", self.volume , \", No.\" , self.number\n def id(self):\n return self.issn\n\nbook = Book(\"Bjarne Stroustrup\", \"The C++ Programming Language\",\"Addison-Wesley\", \"0-201-53992-6\")\nmagazine = Magazine(\"TIME\", \"0040-781X\", 145, 23)\ncd = CD(\"BACH CANTATAS\", \"Johann Sebastian Bach\",\"ARCHIV\", \"D120541\")\nbook.print_()\nprint \"\\tid: \" , book.id() \nmagazine.print_()\nprint \"\\tid: \" , magazine.id() \ncd.print_()\nprint \"\\tid: \" , cd.id()", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "The C++ Programming Language by Bjarne Stroustrup\n\tid: 0-201-53992-6\nTIME Magazine, Vol. 145 , No. 23\n\tid: 0040-781X\nBACH CANTATAS , Johann Sebastian Bach\n\tid: ARCHIV D120541\n" + } + ], + "prompt_number": 14 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "", + "language": "python", + "metadata": {}, + "outputs": [] + } + ], + "metadata": {} + } + ] +}
\ No newline at end of file |