diff options
Diffstat (limited to 'Programming_With_Java_A_Primer')
18 files changed, 3693 insertions, 0 deletions
diff --git a/Programming_With_Java_A_Primer/README.txt b/Programming_With_Java_A_Primer/README.txt new file mode 100644 index 00000000..215ec20a --- /dev/null +++ b/Programming_With_Java_A_Primer/README.txt @@ -0,0 +1,10 @@ +Contributed By: Vaibhav Vajani +Course: others +College/Institute/Organization: brahmanand institute of management & science +Department/Designation: Assistant Professor +Book Title: Programming With Java A Primer +Author: E. Balagurusamy +Publisher: Tata McGraw-Hill Publishing Company Limited +Year of publication: 2007 +Isbn: 0070617139 +Edition: 3rd
\ No newline at end of file diff --git a/Programming_With_Java_A_Primer/chapter10.ipynb b/Programming_With_Java_A_Primer/chapter10.ipynb new file mode 100644 index 00000000..47f27579 --- /dev/null +++ b/Programming_With_Java_A_Primer/chapter10.ipynb @@ -0,0 +1,133 @@ +{ + "metadata": { + "name": "", + "signature": "sha256:995bd55986cb75ede34cceda310ffd2c9f00a5c6e6a22d85c294456d1db3fd3f" + }, + "nbformat": 3, + "nbformat_minor": 0, + "worksheets": [ + { + "cells": [ + { + "cell_type": "heading", + "level": 1, + "metadata": {}, + "source": [ + "Chapter 10: Interfaces: Multiple Inheritance" + ] + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "example 10.1, page no. page no. 184" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\n", + "\"\"\"\n", + "There are no interfaces in Python. We will use normal classes instead\n", + "\"\"\"\n", + "\n", + "class Rectangle:\n", + " def compute(self, x, y):\n", + " return x*y\n", + "\n", + "class Circle:\n", + " pi = 3.14\n", + " def compute(self, x, y):\n", + " return self.pi*x*x\n", + "\n", + "rect = Rectangle()\n", + "cir = Circle()\n", + "print \"Area of rectangle: \", rect.compute(10, 20)\n", + "print \"Area of circle: \", cir.compute(10, 0)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Area of rectangle: 200\n", + "Area of circle: 314.0\n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "example 10.2, page no. 211" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\n", + "class Student(object):\n", + " rollNumber = 0\n", + " def getNumber(self, n):\n", + " self.rollNumber = n\n", + " def putNumber(self):\n", + " print \"Roll No.: \", self.rollNumber\n", + "\n", + "class Test(Student, object):\n", + " part1 = 0.0\n", + " part2 = 0.0\n", + " def getMarks(self, m1, m2):\n", + " self.part1 = m1\n", + " self.part2 = m2\n", + " def putMarks(self):\n", + " print \"Marks Obtained\"\n", + " print \"Part 1 = \", self.part1\n", + " print \"Part 2 = \", self.part2\n", + "\n", + "class Results(Test):\n", + " sportWt = 6.0\n", + " def putWt(self):\n", + " print \"Sports Wt: \", self.sportWt\n", + " def display(self):\n", + " total = self.part1 + self.part2 + self.sportWt\n", + " self.putNumber()\n", + " self.putMarks()\n", + " self.putWt()\n", + " print \"Total Score: \", total\n", + "\n", + "student1 = Results()\n", + "student1.getNumber(1234)\n", + "student1.getMarks(27.5, 33.0)\n", + "student1.display()" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Roll No.: 1234\n", + "Marks Obtained\n", + "Part 1 = 27.5\n", + "Part 2 = 33.0\n", + "Sports Wt: 6.0\n", + "Total Score: 66.5\n" + ] + } + ], + "prompt_number": 9 + } + ], + "metadata": {} + } + ] +}
\ No newline at end of file diff --git a/Programming_With_Java_A_Primer/chapter11.ipynb b/Programming_With_Java_A_Primer/chapter11.ipynb new file mode 100644 index 00000000..66fe04c8 --- /dev/null +++ b/Programming_With_Java_A_Primer/chapter11.ipynb @@ -0,0 +1,151 @@ +{ + "metadata": { + "name": "", + "signature": "sha256:661266ea5854f8cdb4c1381d519c16a00dd0491ecb04f34e084c46771d5dc464" + }, + "nbformat": 3, + "nbformat_minor": 0, + "worksheets": [ + { + "cells": [ + { + "cell_type": "heading", + "level": 1, + "metadata": {}, + "source": [ + "Chapter 11: Pckages: Putting Classes Together" + ] + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "example 11.1, page no. 198" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\n", + "\"\"\"\n", + "Note there are no packages in Python, a python program(file) can be used as a module in anoter Python program\n", + "\"\"\"\n", + "\n", + "from package1 import *\n", + "from package2 import *\n", + "\n", + "a = classA()\n", + "b = classB()\n", + "\n", + "a.display()\n", + "b.displayB()" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Class A\n", + "Class B\n", + "m = 10.0\n" + ] + } + ], + "prompt_number": 14 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "example 11.2, page no. 200" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\n", + "\"\"\"\n", + "Note there are no packages in Python, a python program(file) can be used as a module in anoter Python program\n", + "\"\"\"\n", + "\n", + "from package2 import *\n", + "\n", + "class classC(classB):\n", + " n = 20\n", + " def displayC(self):\n", + " print \"Class C\"\n", + " print \"m = \", self.m\n", + " print \"n = \", self.n\n", + "\n", + "c = classC()\n", + "c.displayB()\n", + "c.displayC()" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Class B\n", + "m = 10.0\n", + "Class C\n", + "m = 10.0\n", + "n = 20\n" + ] + } + ], + "prompt_number": 15 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "example 11.3, page no. 204" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\n", + "\"\"\"\n", + "there is no concept of static import. We will use normal import instead\n", + "\"\"\"\n", + "\n", + "import math\n", + "\n", + "class mathop:\n", + " def circle(self, r):\n", + " area = math.pi*r*r\n", + " print \"The Area of circle is: \", area\n", + "\n", + "obj = mathop()\n", + "obj.circle(2.3)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "The Area of circle is: 16.6190251375\n" + ] + } + ], + "prompt_number": 16 + } + ], + "metadata": {} + } + ] +}
\ No newline at end of file diff --git a/Programming_With_Java_A_Primer/chapter12.ipynb b/Programming_With_Java_A_Primer/chapter12.ipynb new file mode 100644 index 00000000..ea0f31a7 --- /dev/null +++ b/Programming_With_Java_A_Primer/chapter12.ipynb @@ -0,0 +1,231 @@ +{ + "metadata": { + "name": "", + "signature": "sha256:0815ba2045ff6b90ad464c5410843a3c00d29eb193c3c9a13b08fcf0be33a42e" + }, + "nbformat": 3, + "nbformat_minor": 0, + "worksheets": [ + { + "cells": [ + { + "cell_type": "heading", + "level": 1, + "metadata": {}, + "source": [ + "Chapter 12: Multithreaded Programming" + ] + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "example 12.1, page no. 211" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\n", + "\"\"\"\n", + "note: the output will differ from that given in textbook as Threading in Python differs from that in J\n", + "\"\"\"\n", + "\n", + "\n", + "def threadA():\n", + " for i in range(1, 6):\n", + " print \"From Thread A: i = \", i\n", + " print \"Exit from A\"\n", + "\n", + "def threadB():\n", + " for i in range(1, 6):\n", + " print \"From Thread B: i = \", i\n", + " print \"Exit from B\"\n", + "\n", + "def threadC():\n", + " for i in range(1, 6):\n", + " print \"From Thread C: i = \", i\n", + " print \"Exit from C\"\n", + "\n", + "thread1 = Thread(target=threadA, args=())\n", + "thread2 = Thread(target=threadB, args=())\n", + "thread3 = Thread(target=threadC, args=())\n", + "\n", + "thread1.start()\n", + "thread2.start()\n", + "thread3.start()" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "From Thread C: i = 1\n", + "From Thread C: i = 2\n", + "From Thread C: i = 3\n", + "From Thread C: i = 4\n", + "From Thread C: i = 5\n", + "Exit from C\n", + "From Thread A: i = 1\n", + "From Thread A: i = 2\n", + "From Thread A: i = 3\n", + "From Thread A: i = 4\n", + "From Thread A: i = 5\n", + "Exit from A\n", + "From Thread B: i = 1\n", + "From Thread B: i = 2\n", + "From Thread B: i = 3\n", + "From Thread B: i = 4\n", + "From Thread B: i = 5\n", + "Exit from B\n" + ] + } + ], + "prompt_number": 18 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "example 12.2, page no. 217" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\n", + "\"\"\"\n", + "there is no yield or stop function for threads in Python. Demonstrating just sleep() function\n", + "\"\"\"\n", + "\n", + "from threading import Thread\n", + "\n", + "import time\n", + "\n", + "def threadA():\n", + " for i in range(1, 6):\n", + " print \"From Thread A: i = \", i\n", + " print \"Exit from A\"\n", + "\n", + "def threadB():\n", + " for i in range(1, 6):\n", + " print \"From Thread B: i = \", i\n", + " print \"Exit from B\"\n", + "\n", + "def threadC():\n", + " for i in range(1, 6):\n", + " print \"From Thread C: i = \", i\n", + " time.sleep(0.5)\n", + " print \"Exit from C\"\n", + "\n", + "thread1 = Thread(target=threadA, args=())\n", + "thread2 = Thread(target=threadB, args=())\n", + "thread3 = Thread(target=threadC, args=())\n", + "\n", + "thread1.start()\n", + "time.sleep(0.5)\n", + "thread2.start()\n", + "time.sleep(0.5)\n", + "thread3.start()\n", + "time.sleep(0.5)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "From Thread A: i = 1\n", + "From Thread A: i = 2\n", + "From Thread A: i = 3\n", + "From Thread A: i = 4\n", + "From Thread A: i = 5\n", + "Exit from A\n", + "From Thread B: i = 1\n", + "From Thread B: i = 2\n", + "From Thread B: i = 3\n", + "From Thread B: i = 4\n", + "From Thread B: i = 5\n", + "Exit from B\n", + "From Thread C: i = 1\n" + ] + } + ], + "prompt_number": 2 + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#There is no way you can set priority for a thread in Python. Hence, example 12.3 is avoided" + ], + "language": "python", + "metadata": {}, + "outputs": [] + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "example 12.4, page no. 225" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\n", + "\"\"\"\n", + "There is no runnable interface in Python. Will use normal threading instead\n", + "\"\"\"\n", + "\n", + "import threading\n", + "class X(threading.Thread):\n", + " def run(self):\n", + " for i in range(1, 11):\n", + " print \"ThreadX: \", i\n", + " print \"End of ThreadX\"\n", + "\n", + "runnable = X()\n", + "threadx = Thread()\n", + "threadx.start()\n", + "print \"End of main thread\"\n", + "runnable.run()" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "End of main thread\n", + "ThreadX: 1\n", + "ThreadX: 2\n", + "ThreadX: 3\n", + "ThreadX: 4\n", + "ThreadX: 5\n", + "ThreadX: 6\n", + "ThreadX: 7\n", + "ThreadX: 8\n", + "ThreadX: 9\n", + "ThreadX: 10\n", + "End of ThreadX\n" + ] + } + ], + "prompt_number": 69 + } + ], + "metadata": {} + } + ] +}
\ No newline at end of file diff --git a/Programming_With_Java_A_Primer/chapter13.ipynb b/Programming_With_Java_A_Primer/chapter13.ipynb new file mode 100644 index 00000000..7c07d269 --- /dev/null +++ b/Programming_With_Java_A_Primer/chapter13.ipynb @@ -0,0 +1,267 @@ +{ + "metadata": { + "name": "", + "signature": "sha256:5b64344dd4b10e210167de1e2d127d9539461b4485363c51a2c3ddba9df7db38" + }, + "nbformat": 3, + "nbformat_minor": 0, + "worksheets": [ + { + "cells": [ + { + "cell_type": "heading", + "level": 1, + "metadata": {}, + "source": [ + "Chapter 13: Managing Errors & Exceptions" + ] + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "example 13.1, page no. 231" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\n", + "\"\"\"\n", + "there is no need of semicolon. We will show error for something else\n", + "\"\"\"\n", + "\n", + "print \"Hello Python..." + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "ename": "SyntaxError", + "evalue": "EOL while scanning string literal (<ipython-input-1-c13cce4dd116>, line 7)", + "output_type": "pyerr", + "traceback": [ + "\u001b[1;36m File \u001b[1;32m\"<ipython-input-1-c13cce4dd116>\"\u001b[1;36m, line \u001b[1;32m7\u001b[0m\n\u001b[1;33m print \"Hello Python...\u001b[0m\n\u001b[1;37m ^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m EOL while scanning string literal\n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "example 13.2, page no. 232" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\n", + "a = 10\n", + "b = 5\n", + "c = 5\n", + "x = a/(b-c)\n", + "print \"x = \", x\n", + "y = a/(b+c)\n", + "print \"y = \", y" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "ename": "ZeroDivisionError", + "evalue": "integer division or modulo by zero", + "output_type": "pyerr", + "traceback": [ + "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m\n\u001b[1;31mZeroDivisionError\u001b[0m Traceback (most recent call last)", + "\u001b[1;32m<ipython-input-2-d52b7922541b>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[0;32m 4\u001b[0m \u001b[0mb\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;36m5\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 5\u001b[0m \u001b[0mc\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;36m5\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 6\u001b[1;33m \u001b[0mx\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0ma\u001b[0m\u001b[1;33m/\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mb\u001b[0m\u001b[1;33m-\u001b[0m\u001b[0mc\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 7\u001b[0m \u001b[1;32mprint\u001b[0m \u001b[1;34m\"x = \"\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mx\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 8\u001b[0m \u001b[0my\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0ma\u001b[0m\u001b[1;33m/\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mb\u001b[0m\u001b[1;33m+\u001b[0m\u001b[0mc\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n", + "\u001b[1;31mZeroDivisionError\u001b[0m: integer division or modulo by zero" + ] + } + ], + "prompt_number": 2 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 13.3 page no : 235" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\n", + "a = 10\n", + "b = 5\n", + "c = 5\n", + "try:\n", + " x = a/(b-c)\n", + "except Exception,e :\n", + " print \"Division by zero\"\n", + "y = a/(b+c)\n", + "print \"y = \",y" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Division by zero\n", + "y = 1\n" + ] + } + ], + "prompt_number": 2 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 13.4 page no : 236" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\n", + "import sys\n", + "invalid = 0\n", + "count = 0\n", + "for i in sys.argv:\n", + " try:\n", + " a = int(i)\n", + " except:\n", + " invalid += 1\n", + " print \"Invalid number\" , i\n", + " continue\n", + " count += 1\n", + "\n", + "print \"Valid Numbers : \" ,count\n", + "print \"Invalid numbers \",invalid\n", + " " + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " Invalid number -c\n", + "Invalid number -f\n", + "Invalid number /tmp/tmp1tOqar/profile_default/security/kernel-082ebfe6-5650-4ba4-8bcf-cd17bc99af7a.json\n", + "Invalid number --IPKernelApp.parent_appname='ipython-notebook'\n", + "Invalid number --profile-dir\n", + "Invalid number /tmp/tmp1tOqar/profile_default\n", + "Invalid number --parent=1\n", + "Valid Numbers : 0\n", + "Invalid numbers 7\n" + ] + } + ], + "prompt_number": 4 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "example 13.5, page no. 239" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\n", + "a = [5,10]\n", + "b = 5\n", + "try:\n", + " x = a[2]/b - a[1]\n", + "except AssertionError:\n", + " print \"AssertionError:\"\n", + "except IndexError:\n", + " print \"Array Index Error\"\n", + "except Exception:\n", + " print \"Any Error\"\n", + "y = a[1]/a[0]\n", + "print \"y = \" , y " + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Array Index Error\n", + "y = 2\n" + ] + } + ], + "prompt_number": 6 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 13.6 page no : 239" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\n", + "x = 5\n", + "y = 1000\n", + "\n", + "class MyException(Exception):\n", + " def __init__(self, message):\n", + " Exception.__init__(self, message)\n", + " def error(self):\n", + " print self.message\n", + "\n", + "try:\n", + " z = x/y\n", + " if(z<0.01):\n", + " raise MyException(\"Number is too small\")\n", + "except MyException, error:\n", + " print \"Caught MyException\"\n", + " print error.message\n", + "finally:\n", + " print \"I am always here\"" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Caught MyException\n", + "Number is too small\n", + "I am always here\n" + ] + } + ], + "prompt_number": 8 + } + ], + "metadata": {} + } + ] +}
\ No newline at end of file diff --git a/Programming_With_Java_A_Primer/chapter16.ipynb b/Programming_With_Java_A_Primer/chapter16.ipynb new file mode 100644 index 00000000..8bd99a77 --- /dev/null +++ b/Programming_With_Java_A_Primer/chapter16.ipynb @@ -0,0 +1,426 @@ +{ + "metadata": { + "name": "", + "signature": "sha256:218fbe19da172953280529e0559355d55a34bf2741e9ecd127042579a2a52e47" + }, + "nbformat": 3, + "nbformat_minor": 0, + "worksheets": [ + { + "cells": [ + { + "cell_type": "heading", + "level": 1, + "metadata": {}, + "source": [ + "Chapter 16: Managing Input/Output file" + ] + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "example 16.1, page no. 300" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\n", + "fr = open(\"input.txt\", \"r\")\n", + "fw = open(\"output.txt\", \"w\")\n", + "my_str = fr.readline()\n", + "\n", + "for letter in my_str:\n", + " fw.write(letter) \n", + "#close a file\n", + "fr.close()\n", + "fw.close()" + ], + "language": "python", + "metadata": {}, + "outputs": [], + "prompt_number": 3 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 16.2, page no. 303" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\n", + "fw = open(\"cities.txt\", \"w\")\n", + "cities = ['Delhi\\n','Madras\\n','London\\n'];\n", + "count = len(cities)\n", + "for city in cities:\n", + " fw.write(city) \n", + "fw.close()\n", + "fr = open(\"cities.txt\", \"r\")\n", + "for line in fr.readlines():\n", + " print line," + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Delhi\n", + "Madras\n", + "London\n" + ] + } + ], + "prompt_number": 2 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 16.3 is done using CLI which is not possible in IPython Notebook" + ] + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 16.4, page no. 305" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\n", + "\"\"\"\n", + "there is no bytes stream in Python. We will use normal file operations to copy from one file to another\n", + "\"\"\"\n", + "\n", + "fr = open(\"in.txt\", \"r\")\n", + "fw = open(\"out.txt\", \"w\")\n", + "for line in fr.readlines():\n", + " fw.write(line) \n", + "fr.close()\n", + "fw.close()\n", + "\n", + "fw = open(\"out.txt\", \"r\")\n", + "for line in fw.readlines():\n", + " print line" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Java programming for internet.\n", + "\n", + "Javascript for webpage develpoment.\n", + "\n", + "Perl for server side scripting.\n", + "\n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 16.5, page no. 307" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\"\"\"\n", + "Note: In Python you can store data in file only as string.\n", + "\"\"\"\n", + "\n", + "fos = open(\"prim.dat\", \"w\")\n", + "fos.write(str(1999)+'\\n')\n", + "fos.write(str(375.85)+'\\n')\n", + "fos.write(str(False)+'\\n')\n", + "fos.write(\"x\"+'\\n')\n", + "fos.close()\n", + "\n", + "fis = open(\"prim.dat\", \"r\")\n", + "for line in fis.readlines():\n", + " print line\n", + "fis.close()" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "1999\n", + "\n", + "375.85\n", + "\n", + "False\n", + "\n", + "x\n", + "\n" + ] + } + ], + "prompt_number": 3 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 16.6, page no. 308" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\n", + "from random import *\n", + "\n", + "dos = open(\"rand.dat\", \"w\")\n", + "try:\n", + " for i in range(20):\n", + " dos.write(str(randint(0,100))+'\\n')\n", + "except IOError:\n", + " print IOError.message\n", + "finally:\n", + " dos.close()\n", + "\n", + "dis = open(\"rand.dat\", \"r\")\n", + "# Note: random numbers are generated so output will differ from that given in the textbook.\n", + "for line in dis.readlines():\n", + " print line," + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "92\n", + "49\n", + "68\n", + "67\n", + "71\n", + "25\n", + "85\n", + "53\n", + "10\n", + "87\n", + "15\n", + "5\n", + "33\n", + "30\n", + "72\n", + "52\n", + "80\n", + "85\n", + "36\n", + "84\n" + ] + } + ], + "prompt_number": 4 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 16.7, page no. 310" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\n", + "file1 = open(\"file1.txt\", \"r\")\n", + "file2 = open(\"file2.txt\", \"r\")\n", + "file3 = open(\"file3.txt\", \"a\")\n", + "\n", + "for line in file1.readlines():\n", + " file3.write(line)\n", + "for line in file2.readlines():\n", + " file3.write(line)\n", + " \n", + "file1.close()\n", + "file2.close()\n", + "file3.close()" + ], + "language": "python", + "metadata": {}, + "outputs": [], + "prompt_number": 5 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 16.8, page no. 312" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\n", + "fp = open(\"random.dat\", \"w+\")\n", + "fp.write(\"x\\n\")\n", + "fp.write(str(555)+'\\n')\n", + "fp.write(str(3.1412)+'\\n')\n", + "fp.seek(0)\n", + "print fp.readline()\n", + "print fp.readline()\n", + "print fp.readline()\n", + "fp.seek(2)\n", + "print fp.readline()\n", + "fp.seek(fp.tell()+len(fp.readline()))\n", + "fp.write(\"False\\n\")\n", + "fp.seek(13)\n", + "print fp.readline()\n", + "fp.close()" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "x\n", + "\n", + "555\n", + "\n", + "3.1412\n", + "\n", + "555\n", + "\n", + "False\n", + "\n" + ] + } + ], + "prompt_number": 6 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 16.9, page no. 314" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\n", + "fp = open(\"cities.txt\", \"a\")\n", + "fp.write(\"Mumbai\\n\")\n", + "fp.close()" + ], + "language": "python", + "metadata": {}, + "outputs": [], + "prompt_number": 85 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 16.10, page no. 315" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\n", + "dos = open(\"invent.dat\", \"w\")\n", + "dos.write(raw_input(\"Enter code number: \")+'\\n')\n", + "dos.write(raw_input(\"Enter number of items: \")+'\\n')\n", + "dos.write(raw_input(\"Enter cost: \")+'\\n')\n", + "dos.close()\n", + "dis = open(\"invent.dat\", \"r\")\n", + "codeNumber = int(dis.readline())\n", + "totalItems = int(dis.readline())\n", + "itemCost = int(dis.readline())\n", + "totalCost = totalItems*itemCost\n", + "dis.close()\n", + "\n", + "print \"Code Number: \", codeNumber\n", + "print \"Item Cost: \", itemCost\n", + "print \"Total Items: \", totalItems\n", + "print \"Total Cost: \", totalCost" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter code number: 1001\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter number of items: 193\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter cost: 452\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Code Number: 1001\n", + "Item Cost: 452\n", + "Total Items: 193\n", + "Total Cost: 87236\n" + ] + } + ], + "prompt_number": 91 + } + ], + "metadata": {} + } + ] +}
\ No newline at end of file diff --git a/Programming_With_Java_A_Primer/chapter17.ipynb b/Programming_With_Java_A_Primer/chapter17.ipynb new file mode 100644 index 00000000..c8344546 --- /dev/null +++ b/Programming_With_Java_A_Primer/chapter17.ipynb @@ -0,0 +1,61 @@ +{ + "metadata": { + "name": "", + "signature": "sha256:6e44d5a558f592081f494afbf193b979399d3f382d6ddf2dd199186d661ef909" + }, + "nbformat": 3, + "nbformat_minor": 0, + "worksheets": [ + { + "cells": [ + { + "cell_type": "heading", + "level": 1, + "metadata": {}, + "source": [ + "Chapter 17: Assertion & Design by Contract" + ] + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 17.1, page no. 336" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\n", + "class Division():\n", + " def assertcheck(self, a, b):\n", + " assert(b!=0), \"The value b cannot be zero\"\n", + " c = a/b\n", + " print \"The result is: \", c\n", + "div = Division()\n", + "div.assertcheck(5, 0)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "ename": "AssertionError", + "evalue": "The value b cannot be zero", + "output_type": "pyerr", + "traceback": [ + "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m\n\u001b[1;31mAssertionError\u001b[0m Traceback (most recent call last)", + "\u001b[1;32m<ipython-input-3-39ef7600cfd3>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[0;32m 9\u001b[0m \u001b[1;32mprint\u001b[0m \u001b[1;34m\"The result is: \"\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mc\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 10\u001b[0m \u001b[0mdiv\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mDivision\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 11\u001b[1;33m \u001b[0mdiv\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0massertcheck\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;36m5\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;36m0\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", + "\u001b[1;32m<ipython-input-3-39ef7600cfd3>\u001b[0m in \u001b[0;36massertcheck\u001b[1;34m(self, a, b)\u001b[0m\n\u001b[0;32m 5\u001b[0m \u001b[1;32mclass\u001b[0m \u001b[0mDivision\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 6\u001b[0m \u001b[1;32mdef\u001b[0m \u001b[0massertcheck\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0ma\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mb\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 7\u001b[1;33m \u001b[1;32massert\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mb\u001b[0m\u001b[1;33m!=\u001b[0m\u001b[1;36m0\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;34m\"The value b cannot be zero\"\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 8\u001b[0m \u001b[0mc\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0ma\u001b[0m\u001b[1;33m/\u001b[0m\u001b[0mb\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 9\u001b[0m \u001b[1;32mprint\u001b[0m \u001b[1;34m\"The result is: \"\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mc\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n", + "\u001b[1;31mAssertionError\u001b[0m: The value b cannot be zero" + ] + } + ], + "prompt_number": 3 + } + ], + "metadata": {} + } + ] +}
\ No newline at end of file diff --git a/Programming_With_Java_A_Primer/chapter18.ipynb b/Programming_With_Java_A_Primer/chapter18.ipynb new file mode 100644 index 00000000..3760aefe --- /dev/null +++ b/Programming_With_Java_A_Primer/chapter18.ipynb @@ -0,0 +1,372 @@ +{ + "metadata": { + "name": "", + "signature": "sha256:b65111374697402ea20166f271bb1488f1e63cc13c5da33961c35513244b0d0c" + }, + "nbformat": 3, + "nbformat_minor": 0, + "worksheets": [ + { + "cells": [ + { + "cell_type": "heading", + "level": 1, + "metadata": {}, + "source": [ + "Chapter 18: Java Collections" + ] + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 18.1, page no. 348" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\"\"\"\n", + "Note: there is no such class in Python. We will perform normal operations on array\n", + "\"\"\"\n", + "\n", + "arrayList = []\n", + "print \"Initial size of arrayList: \", len(arrayList)\n", + "arrayList.append(\"A\")\n", + "arrayList.append(\"B\")\n", + "arrayList.append(\"C\")\n", + "arrayList.append(\"D\")\n", + "arrayList.insert(2, \"E\")\n", + "print \"Changed contents of arraylist by adding element at the given index: \", arrayList\n", + "arrayList.pop(3)\n", + "arrayList.remove(\"A\")\n", + "print \"Changed contents of arraylist by removing element from the list: \", arrayList" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Initial size of arrayList: 0\n", + "Changed contents of arraylist by adding element at the given index: ['A', 'B', 'E', 'C', 'D']\n", + "Changed contents of arraylist by removing element from the list: ['B', 'E', 'D']\n" + ] + } + ], + "prompt_number": 9 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 18.2, page no. 349" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\"\"\"\n", + "Note: There is no such collection in Python. We will dqueue instead, which is very much similar\n", + "\"\"\"\n", + "\n", + "from collections import deque\n", + "\n", + "d = deque()\n", + "\n", + "class MyStack():\n", + " def push1(self, o):\n", + " d.appendleft(o)\n", + " def push2(self, o):\n", + " d.append(o)\n", + " def bottom(self):\n", + " return d.pop()\n", + " def pop(self):\n", + " return d.popleft()\n", + "\n", + "class Car():\n", + " car1 = \"Benz\"\n", + " car2 = \"Toyoto\"\n", + " car3 = \"Qualis\"\n", + " car4 = \"Santro\"\n", + "\n", + "class Bird():\n", + " bird1 = \"parrot\"\n", + " bird2 = \"duck\"\n", + " bird3 = \"raven\"\n", + "\n", + "myCar = Car()\n", + "myBird = Bird()\n", + "s = MyStack()\n", + "s.push1(myCar.car1)\n", + "s.push2(myBird.bird3)\n", + "myCar = s.pop()\n", + "print \"The first element in the list: \", myCar\n", + "print \"The last element in the list: \", myBird.bird3" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "The first element in the list: Benz\n", + "The last element in the list: raven\n" + ] + } + ], + "prompt_number": 15 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 18.3, page no. 351" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\"\"\"\n", + "Note: there is no collection hash set in Python. We will solve it in different manner\n", + "\"\"\"\n", + "\n", + "hs = set()\n", + "\n", + "hs.add(\"D\")\n", + "hs.add(\"A\")\n", + "hs.add(\"C\")\n", + "hs.add(\"B\")\n", + "hs.add(\"E\")\n", + "\n", + "#answer will differ due to difference in built-in techniques\n", + "print \"The elements available in the hash set are: \", hs" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "The elements available in the hash set are: set(['A', 'C', 'B', 'E', 'D'])\n" + ] + } + ], + "prompt_number": 23 + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#There is no TreeSet or similar collection in Python (can be done using normal list like 18.1) so skipping example 18.4" + ], + "language": "python", + "metadata": {}, + "outputs": [], + "prompt_number": 25 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 18.5, page no. 353" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\"\"\"\n", + "Note: there is no vector in Python. Doing using normal lists.\n", + "\"\"\"\n", + "\n", + "fruits = []\n", + "\n", + "fruits.append(\"Apple\")\n", + "fruits.append(\"Orange\")\n", + "fruits.append(\"Grapes\")\n", + "fruits.append(\"Pine\")\n", + "\n", + "for ele in fruits:\n", + " print ele" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Apple\n", + "Orange\n", + "Grapes\n", + "Pine\n" + ] + } + ], + "prompt_number": 26 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 18.6, page no. 354" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\"\"\"\n", + "In python we can use list itself as stack\n", + "\"\"\"\n", + "\n", + "st = []\n", + "\n", + "st.append(\"Java\")\n", + "st.append(\"latest\")\n", + "st.append(\"Edition\")\n", + "st.append(\"-fifth\")\n", + "\n", + "print \"The elements in the Stack: \", st\n", + "print \"The element at the top: \", st[-1:]\n", + "print \"The element poped out of the stack: \", st.pop()\n", + "print \"The element in a stack after pop out element: \", st\n", + "try:\n", + " \"The result of searching: \", st.index(\"r u\")\n", + "except ValueError:\n", + " print \"The result of searching: \", -1" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "The elements in the Stack: ['Java', 'latest', 'Edition', '-fifth']\n", + "The element at the top: ['-fifth']\n", + "The element poped out of the stack: -fifth\n", + "The element in a stack after pop out element: ['Java', 'latest', 'Edition']\n", + "The result of searching: -1\n" + ] + } + ], + "prompt_number": 29 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 18.7, page no. 355" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\"\"\"\n", + "Note: we will use dictionaries\n", + "\"\"\"\n", + "\n", + "ht ={}\n", + "\n", + "ht['item1'] = \"Apple\"\n", + "ht['item2'] = \"Orange\"\n", + "ht['item3'] = \"Grapes\"\n", + "ht['item4'] = \"Pine\"\n", + "\n", + "for key in ht.iterkeys():\n", + " print ht[key]\n", + " " + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Orange\n", + "Grapes\n", + "Apple\n", + "Pine\n" + ] + } + ], + "prompt_number": 38 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 18.8, page no. 357" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\n", + "#Answers will differ due to difference in built-in techniques of the classes\n", + "\n", + "from random import shuffle\n", + "\n", + "l = []\n", + "\n", + "l.append(\"java\")\n", + "l.append(\"is\")\n", + "l.append(\"platform\")\n", + "l.append(\"independent\")\n", + "\n", + "l_r = list(reversed(l))\n", + "l_r = sorted(l_r)\n", + "\n", + "print \"List sorted in reverse order is: \"\n", + "for ele in l_r:\n", + " print ele,\n", + "\n", + "print \"\\nList shuffled: \"\n", + "shuffle(l_r)\n", + "for ele in l_r:\n", + " print ele,\n", + "\n", + "print \"\\nMinimum: \", min(l_r)\n", + "print \"Maximum: \", max(l_r)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "List sorted in reverse order is: \n", + "independent is java platform \n", + "List shuffled: \n", + "java independent is platform \n", + "Minimum: independent\n", + "Maximum: platform\n" + ] + } + ], + "prompt_number": 2 + } + ], + "metadata": {} + } + ] +}
\ No newline at end of file diff --git a/Programming_With_Java_A_Primer/chapter3.ipynb b/Programming_With_Java_A_Primer/chapter3.ipynb new file mode 100644 index 00000000..15da94c7 --- /dev/null +++ b/Programming_With_Java_A_Primer/chapter3.ipynb @@ -0,0 +1,178 @@ +{ + "metadata": { + "name": "", + "signature": "sha256:900e8c40d4fed93f16924c238f3501224263d68b0f636bde52b89454eb250cc4" + }, + "nbformat": 3, + "nbformat_minor": 0, + "worksheets": [ + { + "cells": [ + { + "cell_type": "heading", + "level": 1, + "metadata": {}, + "source": [ + "Chapter 3 : Overview of Java Language" + ] + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "exmple 3.1, page no. 25" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "print \"Python is better than Java.\"" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Python is better than Java.\n" + ] + } + ], + "prompt_number": 2 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "example 3.2, page no. 27" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\n", + "import math\n", + "\n", + "x = 5\n", + "y = math.sqrt(x)\n", + "print \"y = \", round(y,5)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "y = 2.23607\n" + ] + } + ], + "prompt_number": 4 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "example 3.3, page no. 28" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\"\"\"\n", + "Note: there is no need of another class here, as in Python there is no need of main class.\n", + "\"\"\"\n", + "\n", + "class Room:\n", + " length = 0.0\n", + " breadth = 0.0\n", + " def getdata(self, a, b):\n", + " self.length = a\n", + " self.breadth = b\n", + "\n", + "room1 = Room()\n", + "room1.getdata(14.0, 10.0)\n", + "area = room1.length*room1.breadth\n", + "print \"Area = \", area" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Area = 140.0\n" + ] + } + ], + "prompt_number": 6 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "example 3.4, page no. 35" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\n", + "print \"Hellow!\"\n", + "print \"Welcome to the world of Python.\"\n", + "print \"Let us learn Python.\"" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Hellow!\n", + "Welcome to the world of Python.\n", + "Let us learn Python.\n" + ] + } + ], + "prompt_number": 7 + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\"\"\"\n", + "Command line arguments cannot be taken from IPython Notebook.\n", + "Hence, example 3.5 is ignored.\n", + "\"\"\"" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "metadata": {}, + "output_type": "pyout", + "prompt_number": 8, + "text": [ + "'\\nCommand line arguments cannot be taken from IPython Notebook.\\nHence, example 3.5 is ignored.\\n'" + ] + } + ], + "prompt_number": 8 + } + ], + "metadata": {} + } + ] +}
\ No newline at end of file diff --git a/Programming_With_Java_A_Primer/chapter4.ipynb b/Programming_With_Java_A_Primer/chapter4.ipynb new file mode 100644 index 00000000..fd3a6d08 --- /dev/null +++ b/Programming_With_Java_A_Primer/chapter4.ipynb @@ -0,0 +1,191 @@ +{ + "metadata": { + "name": "", + "signature": "sha256:d5e2b3b4e2eecb1f7195196f8d754ec6a4ed41953a9ae9d18560339b9d5ef6ea" + }, + "nbformat": 3, + "nbformat_minor": 0, + "worksheets": [ + { + "cells": [ + { + "cell_type": "heading", + "level": 1, + "metadata": {}, + "source": [ + "Chapter 4: Constants, Variables & Datatypes" + ] + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "example 4.1, page no. 50" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\n", + "intnum = int(raw_input(\"Enter an Integer: \"))\n", + "floatnum = float(raw_input(\"Enter a Float: \"))\n", + "\n", + "print \"intnum = \", intnum\n", + "print \"floatnum = \", floatnum" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter an Integer: 3\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter a Float: 6.9\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "intnum = 3\n", + "floatnum = 6.9\n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "example 4.2, page no. 54" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\n", + "print \"Variables Created\"\n", + "c = \"x\"\n", + "b = 50\n", + "s = 1996\n", + "i = 123456789\n", + "l = 1234567654321L\n", + "f1 = 3.142\n", + "f2 = 1.2e-5\n", + "d2 = 0.000000987\n", + "\n", + "print \"c = \", c\n", + "print \"b = \", b\n", + "print \"s = \", s\n", + "print \"i = \", i\n", + "print \"l = \", l\n", + "print \"f1 = \", f1\n", + "print \"f2 = \", f2\n", + "print \"d2 = \", d2\n", + "\n", + "print \"Types Converted\"\n", + "#there is no short type in python\n", + "s1 = b\n", + "s2 = i\n", + "n1 = float(l)\n", + "m1 = int(f1)\n", + "\n", + "print \"s1 = \", s1\n", + "print \"s2 = \", s2\n", + "print \"n1 = %.5e\"% n1\n", + "print \"m1 = \", m1" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Variables Created\n", + "c = x\n", + "b = 50\n", + "s = 1996\n", + "i = 123456789\n", + "l = 1234567654321\n", + "f1 = 3.142\n", + "f2 = 1.2e-05\n", + "d2 = 9.87e-07\n", + "Types Converted\n", + "s1 = 50\n", + "s2 = 123456789\n", + "n1 = 1.23457e+12\n", + "m1 = 3\n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "example 4.3, page no. 56" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\n", + "for i in range(1, 10):\n", + " for j in range(1, i+1):\n", + " print i,\n", + " print \"\"\n", + "print \"Screen Display Done\"" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "1 \n", + "2 2 \n", + "3 3 3 \n", + "4 4 4 4 \n", + "5 5 5 5 5 \n", + "6 6 6 6 6 6 \n", + "7 7 7 7 7 7 7 \n", + "8 8 8 8 8 8 8 8 \n", + "9 9 9 9 9 9 9 9 9 \n", + "Screen Display Done\n" + ] + } + ], + "prompt_number": 2 + }, + { + "cell_type": "code", + "collapsed": false, + "input": [], + "language": "python", + "metadata": {}, + "outputs": [] + } + ], + "metadata": {} + } + ] +}
\ No newline at end of file diff --git a/Programming_With_Java_A_Primer/chapter5.ipynb b/Programming_With_Java_A_Primer/chapter5.ipynb new file mode 100644 index 00000000..12e20bd5 --- /dev/null +++ b/Programming_With_Java_A_Primer/chapter5.ipynb @@ -0,0 +1,325 @@ +{ + "metadata": { + "name": "", + "signature": "sha256:c8fa15513a66bcc3c782e246851674786a1e5f0313759a5d553c588db3808162" + }, + "nbformat": 3, + "nbformat_minor": 0, + "worksheets": [ + { + "cells": [ + { + "cell_type": "heading", + "level": 1, + "metadata": {}, + "source": [ + "Chapter 5: Operators & Expressions" + ] + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "example 5.1, page no. 62" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\n", + "a = 20.5\n", + "b = 6.4\n", + "\n", + "print \"a = \", a\n", + "print \"b = \", b\n", + "print \"a+b = \", (a+b)\n", + "print \"a-b = \", (a-b)\n", + "print \"a*b = \", (a*b)\n", + "print \"a/b = \", (a/b)\n", + "print \"a%b = \", (a%b)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "a = 20.5\n", + "b = 6.4\n", + "a+b = 26.9\n", + "a-b = 14.1\n", + "a*b = 131.2\n", + "a/b = 3.203125\n", + "a%b = 1.3\n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "example 5.2, page no. 64" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\n", + "a = 15.0\n", + "b = 20.75\n", + "c = 15.0\n", + "print \"a = \", a\n", + "print \"b = \", b\n", + "print \"c = \", c\n", + "print \"a<b is \", (a<b)\n", + "print \"a>b is \", (a>b)\n", + "print \"a==c is \", (a==c)\n", + "print \"a<=c is \", (a<=c)\n", + "print \"a>=b is \", (a>=b)\n", + "print \"b!=c is \", (b!=c)\n", + "print \"b==(a+c) is \", (b==(a+c))" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "a = 15.0\n", + "b = 20.75\n", + "c = 15.0\n", + "a<b is True\n", + "a>b is False\n", + "a==c is True\n", + "a<=c is True\n", + "a>=b is False\n", + "b!=c is True\n", + "b==(a+c) is False\n" + ] + } + ], + "prompt_number": 2 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "example 5.3, page no. 67" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\n", + "#there is no inrement operator in Python, we will use += instead\n", + "\n", + "m = 10\n", + "n = 20\n", + "print \"m = \", m\n", + "print \"n = \", n\n", + "m+=1\n", + "print \"++m = \", m \n", + "print \"n++ = \", n\n", + "n+=1\n", + "print \"m = \", m\n", + "print \"n = \", n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "m = 10\n", + "n = 20\n", + "++m = 11\n", + "n++ = 20\n", + "m = 11\n", + "n = 21\n" + ] + } + ], + "prompt_number": 18 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "example 5.4, page no. 73" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\n", + "my_sum = 0\n", + "for i in range(1, 11):\n", + " my_sum = my_sum + (1/float(i))\n", + " print \"i = \", i,\n", + " print \"sum = \", my_sum" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "i = 1 sum = 1.0\n", + "i = 2 sum = 1.5\n", + "i = 3 sum = 1.83333333333\n", + "i = 4 sum = 2.08333333333\n", + "i = 5 sum = 2.28333333333\n", + "i = 6 sum = 2.45\n", + "i = 7 sum = 2.59285714286\n", + "i = 8 sum = 2.71785714286\n", + "i = 9 sum = 2.82896825397\n", + "i = 10 sum = 2.92896825397\n" + ] + } + ], + "prompt_number": 21 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "example 5.5, page no. 74" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\n", + "my_list = []\n", + "my_list.append(1)\n", + "my_list.append(2)\n", + "l = len(my_list)-1\n", + "total = 0\n", + "while l >= 0:\n", + " total = total + my_list[l]\n", + " l -= 1\n", + "print \"The total amount is \", total" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "The total amount is 3\n" + ] + } + ], + "prompt_number": 23 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "example 5.6, page no. 77" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\n", + "a = 10\n", + "b = 5\n", + "c = 8\n", + "d = 2\n", + "x = 6.4\n", + "y = 3.0\n", + "\n", + "answer1 = a * b + c / d\n", + "answer2 = a * (b+c) / d\n", + "\n", + "answer3 = a/c\n", + "answer4 = float(a)/c\n", + "answer5 = a / y\n", + "\n", + "answer6 = a % c\n", + "answer7 = float(x%y)\n", + "\n", + "bool1 = a > b and c > d\n", + "bool2 = a < b and c > d\n", + "bool3 = a < b or c > d\n", + "bool4 = not(a - b == c)\n", + "\n", + "print \"Order of Evaluation\"\n", + "print \"a * b + c / d = \", answer1\n", + "print \"a * (b+c) / d = \", answer2\n", + "\n", + "print \"Type Conversion\"\n", + "print \"a / c = \", answer3\n", + "print \"float(a)/c = \", answer4\n", + "print \"a / y = \", answer5\n", + "\n", + "print \"Modulo Operations\"\n", + "print \"a % c = \", answer6\n", + "print \"x % y = \", answer6\n", + "\n", + "print \"Logical Operations\"\n", + "print \"a > b and c > d = \", bool1\n", + "print \"a < b and c > d = \", bool2\n", + "print \"a < b or c > d = \", bool3\n", + "print \"not(a - b == c) \", bool4" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Order of Evaluation\n", + "a * b + c / d = 54\n", + "a * (b+c) / d = 65\n", + "Type Conversion\n", + "a / c = 1\n", + "float(a)/c = 1.25\n", + "a / y = 3.33333333333\n", + "Modulo Operations\n", + "a % c = 2\n", + "x % y = 2\n", + "Logical Operations\n", + "a > b and c > d = True\n", + "a < b and c > d = False\n", + "a < b or c > d = True\n", + "not(a - b == c) True\n" + ] + } + ], + "prompt_number": 2 + }, + { + "cell_type": "code", + "collapsed": false, + "input": [], + "language": "python", + "metadata": {}, + "outputs": [] + } + ], + "metadata": {} + } + ] +}
\ No newline at end of file diff --git a/Programming_With_Java_A_Primer/chapter6.ipynb b/Programming_With_Java_A_Primer/chapter6.ipynb new file mode 100644 index 00000000..3ffd656c --- /dev/null +++ b/Programming_With_Java_A_Primer/chapter6.ipynb @@ -0,0 +1,245 @@ +{ + "metadata": { + "name": "", + "signature": "sha256:1252446619e6e4563a32f0df79325967492fbf5a5dc688c7dbacba6bd7f62327" + }, + "nbformat": 3, + "nbformat_minor": 0, + "worksheets": [ + { + "cells": [ + { + "cell_type": "heading", + "level": 1, + "metadata": {}, + "source": [ + "Chapter 6: Decision Making & Branching" + ] + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "example 6.1, page no. 84" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\n", + "weight = [45.0, 55.0, 47.0, 51.0, 54.0]\n", + "height = [176.5, 174.2, 168.0, 170.7, 169.0]\n", + "count = 0\n", + "count1 = 0\n", + "for i in range(0, 5):\n", + " if(weight[i]<50.0 and height[i]>170.0):\n", + " count1 += 1\n", + " count += 1\n", + "count2 = count - count1\n", + "print \"Number of persons with...\"\n", + "print \"Weight<50 and height>170 = \", count1\n", + "print \"Others = \", count2" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Number of persons with...\n", + "Weight<50 and height>170 = 1\n", + "Others = 4\n" + ] + } + ], + "prompt_number": 2 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "example 6.2, page no. 86" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\n", + "number = [50, 65, 56, 71, 81]\n", + "even = 0\n", + "odd = 0\n", + "for i in range(0, len(number)):\n", + " if(number[i] % 2 == 0):\n", + " even += 1\n", + " else:\n", + " odd += 1\n", + "print \"Even numbers = \", even, \"Odd Numbers = \", odd" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Even numbers = 2 Odd Numbers = 3\n" + ] + } + ], + "prompt_number": 3 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "example 6.3, page no. 89" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\n", + "a = 325\n", + "b = 712\n", + "c = 478\n", + "\n", + "print \"Largets value is \",\n", + "if a > b:\n", + " if a > c:\n", + " print a\n", + " else:\n", + " print c\n", + "else:\n", + " if c > b:\n", + " print c\n", + " else:\n", + " print b" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Largets value is 712\n" + ] + } + ], + "prompt_number": 4 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "example 6.4, page no. 92" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\n", + "rollnumber = [111, 222, 333, 444]\n", + "marks = [81, 75, 43, 58]\n", + "for i in range(0, len(rollnumber)):\n", + " if marks[i] > 79:\n", + " print rollnumber[i], \"Honours\"\n", + " elif marks[i] > 59:\n", + " print rollnumber[i], \"I Division\"\n", + " elif marks[i] > 49:\n", + " print rollnumber[i], \"II Division\"\n", + " else:\n", + " print rollnumber[i], \"Fail\"" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "111 Honours\n", + "222 I Division\n", + "333 Fail\n", + "444 II Division\n" + ] + } + ], + "prompt_number": 5 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "example 6.5, page no. 96" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\n", + "#there is switch() statement in python, we will use if...else instead\n", + "\n", + "print \"Select your choice: \"\n", + "print \"M --> Madras\"\n", + "print \"B --> Bombay\"\n", + "print \"C --> Calcutta\"\n", + "print \"Choice --> \",\n", + "choice = raw_input()\n", + "if choice == \"M\" or choice == \"m\":\n", + " print \"Madras: Booklet 5\"\n", + "elif choice == \"B\" or choice == \"b\":\n", + " print \"Bombay: Booklet 9\"\n", + "elif choice == \"C\" or choice == \"c\":\n", + " print \"Calcutta: Booklet 15\"\n", + "else:\n", + " print \"Invalid Choice (IC)\"" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Select your choice: \n", + "M --> Madras\n", + "B --> Bombay\n", + "C --> Calcutta\n", + "Choice --> " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "m\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " Madras: Booklet 5\n" + ] + } + ], + "prompt_number": 6 + } + ], + "metadata": {} + } + ] +}
\ No newline at end of file diff --git a/Programming_With_Java_A_Primer/chapter7.ipynb b/Programming_With_Java_A_Primer/chapter7.ipynb new file mode 100644 index 00000000..66904b83 --- /dev/null +++ b/Programming_With_Java_A_Primer/chapter7.ipynb @@ -0,0 +1,309 @@ +{ + "metadata": { + "name": "", + "signature": "sha256:a924e86900c65c3fd8c20ee5aee9bc052eac287d056598356137191816cafe4b" + }, + "nbformat": 3, + "nbformat_minor": 0, + "worksheets": [ + { + "cells": [ + { + "cell_type": "heading", + "level": 1, + "metadata": {}, + "source": [ + "Chapter 7: Decision Making & Looping" + ] + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "example 7.1, page no. 108" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\n", + "\"\"\"\n", + "the method shown in textbook is not possible in python.\n", + "We will implement in a different way\n", + "\"\"\"\n", + "\n", + "print \"Enter a string. Enter quit to stop\"\n", + "\n", + "my_str = \"\"\n", + "while True:\n", + " c = raw_input()\n", + " if c == \"quit\":\n", + " break\n", + " my_str = my_str + c + \" \"\n", + "print \"String you entered is: \", my_str" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter a string. Enter quit to stop\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Java\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "is\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Good\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "quit\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "String you entered is: Java is Good \n" + ] + } + ], + "prompt_number": 3 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "example 7.2, page no. 109" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#Do.. While loop\n", + "\n", + "\n", + "row = 1\n", + "column = 1\n", + "\n", + "while row <= 3:\n", + " column = 1\n", + " while column <= 3:\n", + " y = row * column\n", + " print \" \", y,\n", + " column += 1\n", + " print \"\"\n", + " row += 1" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " 1 2 3 \n", + " 2 4 6 \n", + " 3 6 9 \n" + ] + } + ], + "prompt_number": 6 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "example 7.3, page no. 112" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\n", + "print \"2 to power-n n 2 to power n\"\n", + "p = 1\n", + "for n in range(0,10):\n", + " if n == 0:\n", + " p = 1\n", + " else:\n", + " p = p * 2\n", + " q = float(1.0/p)\n", + " print q,\" \",n,\" \",p" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "2 to power-n n 2 to power n\n", + "1.0 0 1\n", + "0.5 1 2\n", + "0.25 2 4\n", + "0.125 3 8\n", + "0.0625 4 16\n", + "0.03125 5 32\n", + "0.015625 6 64\n", + "0.0078125 7 128\n", + "0.00390625 8 256\n", + "0.001953125 9 512\n" + ] + } + ], + "prompt_number": 20 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "example 7.4, page no. 116" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\n", + "states = [\"TamilNadu\", \"AndhraPradesh\", \"UttarPradesh\", \"Rajasthan\"]\n", + "for i in range(0, len(states)):\n", + " print \"Standard for-loop: state name: \", states[i]\n", + "print \"\"\n", + "for state in states:\n", + " print \"Enhanced for-loop: state name: \", state\n", + "\n", + "print \"\"\n", + "cities = []\n", + "cities.append(\"Delhi\")\n", + "cities.append(\"Mumbai\")\n", + "cities.append(\"Calcutta\")\n", + "cities.append(\"Chennai\")\n", + "\n", + "for i in range(0, len(cities)):\n", + " print \"Standard for-loop: state name: \", cities[i]\n", + "print \"\"\n", + "for city in cities:\n", + " print \"Enhanced for-loop: enhanced name: \", city\n", + "\n", + "print \"\\nIn Collection\"\n", + "for i in cities:\n", + " print i\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Standard for-loop: state name: TamilNadu\n", + "Standard for-loop: state name: AndhraPradesh\n", + "Standard for-loop: state name: UttarPradesh\n", + "Standard for-loop: state name: Rajasthan\n", + "\n", + "Enhanced for-loop: state name: TamilNadu\n", + "Enhanced for-loop: state name: AndhraPradesh\n", + "Enhanced for-loop: state name: UttarPradesh\n", + "Enhanced for-loop: state name: Rajasthan\n", + "\n", + "Standard for-loop: state name: Delhi\n", + "Standard for-loop: state name: Mumbai\n", + "Standard for-loop: state name: Calcutta\n", + "Standard for-loop: state name: Chennai\n", + "\n", + "Enhanced for-loop: enhanced name: Delhi\n", + "Enhanced for-loop: enhanced name: Mumbai\n", + "Enhanced for-loop: enhanced name: Calcutta\n", + "Enhanced for-loop: enhanced name: Chennai\n", + "\n", + "In Collection\n", + "Delhi\n", + "Mumbai\n", + "Calcutta\n", + "Chennai\n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "example 7.5, page no. 120" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\n", + "for i in range(1,100):\n", + " print \" \"\n", + " if i >= 10:\n", + " break\n", + " for j in range(1, 100):\n", + " print \" * \",\n", + " if j == i:\n", + " break" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " \n", + " * \n", + " * * \n", + " * * * \n", + " * * * * \n", + " * * * * * \n", + " * * * * * * \n", + " * * * * * * * \n", + " * * * * * * * * \n", + " * * * * * * * * * \n" + ] + } + ], + "prompt_number": 28 + } + ], + "metadata": {} + } + ] +}
\ No newline at end of file diff --git a/Programming_With_Java_A_Primer/chapter8.ipynb b/Programming_With_Java_A_Primer/chapter8.ipynb new file mode 100644 index 00000000..85db9833 --- /dev/null +++ b/Programming_With_Java_A_Primer/chapter8.ipynb @@ -0,0 +1,319 @@ +{ + "metadata": { + "name": "", + "signature": "sha256:8fe09a5e04ffadf78c63c627faf8ee708ea9328cbf18aa701a8f4bd8d5b46292" + }, + "nbformat": 3, + "nbformat_minor": 0, + "worksheets": [ + { + "cells": [ + { + "cell_type": "heading", + "level": 1, + "metadata": {}, + "source": [ + "Chapter 8: Classes, Objects & Methods" + ] + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "example 8.1, page no. 132" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\n", + "class Rectangle:\n", + " length = 0\n", + " width = 0\n", + " def getData(self, x, y):\n", + " self.length = x\n", + " self.width = y\n", + " def rectArea(self):\n", + " area = self.length * self.width\n", + " return area\n", + "\n", + "rect1 = Rectangle()\n", + "rect1.length = 15\n", + "rect1.width = 10\n", + "rect2 = Rectangle()\n", + "area1 = rect1.length*rect1.width\n", + "rect2.getData(20,12)\n", + "area2 = rect2.rectArea()\n", + "print \"Area1 = \", area1\n", + "print \"Area2 = \", area2" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " Area1 = 150\n", + "Area2 = 240\n" + ] + } + ], + "prompt_number": 3 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "example 8.2, page no. 134" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\n", + "class Rectangle:\n", + " length = 0\n", + " width = 0\n", + " def __init__(self, x=0, y=0):\n", + " self.length = x\n", + " self.width = y\n", + " def rectArea(self):\n", + " return self.length * self.width\n", + "rect1 = Rectangle(15, 10)\n", + "area1 = rect1.rectArea()\n", + "print \"Area1 = \", area1" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Area1 = 150\n" + ] + } + ], + "prompt_number": 4 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "example 8.3, page no. 136" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\n", + "#there are no static methods in Python, we will use normal methods instead\n", + "\n", + "class MathOperations:\n", + " def mul(self, x, y):\n", + " return x*y\n", + " def divide(self, x, y):\n", + " return x/y\n", + "\n", + "obj = MathOperations()\n", + "a = obj.mul(4.0,5.0)\n", + "b = obj.divide(a,2.0)\n", + "print \"b = \", b" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "b = 10.0\n" + ] + } + ], + "prompt_number": 7 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "example 8.3, page no. 137" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\n", + "class Nesting:\n", + " m = 0\n", + " n = 0\n", + " def __init__(self, x, y):\n", + " self.m = x\n", + " self.n = y\n", + " def largest(self):\n", + " if(self.m>=self.n):\n", + " return self.m\n", + " else:\n", + " return self.n\n", + " def display(self):\n", + " large = self.largest()\n", + " print \"Largest Value: \", large\n", + "\n", + "nest = Nesting(50, 40)\n", + "nest.display()" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Largest Value: 50\n" + ] + } + ], + "prompt_number": 9 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "example 8.4, page no. 139" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\n", + "class Room(object):\n", + " def __init__(self, x, y):\n", + " self.length = x\n", + " self.breadth = y\n", + " def area(self):\n", + " return self.length*self.breadth\n", + "\n", + "class BedRoom(Room):\n", + " height = 0\n", + " def __init__(self, x, y, z):\n", + " Room.__init__(self, x, y)\n", + " self.height = z\n", + " def volume(self):\n", + " return self.length*self.breadth*self.height\n", + "\n", + "room1 = BedRoom(14, 12, 10)\n", + "area1 = room1.area()\n", + "volume1 = room1.volume()\n", + "print \"Area = \", area1\n", + "print \"Volume = \", volume1" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Area = 168\n", + "Volume = 1680\n" + ] + } + ], + "prompt_number": 63 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "example 8.6, page no. 142" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\n", + "class Super(object):\n", + " x = 0\n", + " def __init__(self, x):\n", + " self.x = x\n", + " def display(self):\n", + " print \"Super x = \", self.x\n", + "\n", + "class Sub(Super):\n", + " y = 0\n", + " def __init__(self, x, y):\n", + " Super.__init__(self, x)\n", + " self.y = y\n", + " def display(self):\n", + " print \"Super x = \", self.x\n", + " print \"Sub y = \", self.y\n", + "s1 = Sub(100, 200)\n", + "s1.display()" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Super x = 100\n", + "Sub y = 200\n" + ] + } + ], + "prompt_number": 65 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "example 8.7, page no. 145" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\n", + "class Exampleprg:\n", + " def __init__(self, *args):\n", + " for arg in args:\n", + " print \"Hello \", arg\n", + "\n", + "e = Exampleprg(\"John\", \"David\", \"Suhel\")" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Hello John\n", + "Hello David\n", + "Hello Suhel\n" + ] + } + ], + "prompt_number": 1 + } + ], + "metadata": {} + } + ] +}
\ No newline at end of file diff --git a/Programming_With_Java_A_Primer/chapter9.ipynb b/Programming_With_Java_A_Primer/chapter9.ipynb new file mode 100644 index 00000000..af0b5898 --- /dev/null +++ b/Programming_With_Java_A_Primer/chapter9.ipynb @@ -0,0 +1,475 @@ +{ + "metadata": { + "name": "", + "signature": "sha256:5c730697085cba47efcd5224cc57d70aca1160f9a5e192f67596eb53b4b5b4af" + }, + "nbformat": 3, + "nbformat_minor": 0, + "worksheets": [ + { + "cells": [ + { + "cell_type": "heading", + "level": 1, + "metadata": {}, + "source": [ + "Chapter 9: Arrays, Strings & Vectors" + ] + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "example 9.1, page no. 157" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\n", + "number = [55, 40, 80, 65, 71]\n", + "print \"Given List: \",\n", + "for num in number:\n", + " print num,\n", + "number = sorted(number)[: : -1]\n", + "print \"\\nSorted List: \",\n", + "for num in number:\n", + " print num," + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Given List: 55 40 80 65 71 \n", + "Sorted List: 80 71 65 55 40\n" + ] + } + ], + "prompt_number": 3 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "example 9.2, page no. 160" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\n", + "rows = 20\n", + "columns = 20\n", + "product = [[0 for x in range(rows)] for x in xrange(columns)]\n", + "print \"Multiplication Table\"\n", + "for i in range(10, rows):\n", + " for j in range(10, columns):\n", + " product[i][j] = i*j\n", + " print \" \", product[i][j],\n", + " print \" \"" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Multiplication Table\n", + " 100 110 120 130 140 150 160 170 180 190 \n", + " 110 121 132 143 154 165 176 187 198 209 \n", + " 120 132 144 156 168 180 192 204 216 228 \n", + " 130 143 156 169 182 195 208 221 234 247 \n", + " 140 154 168 182 196 210 224 238 252 266 \n", + " 150 165 180 195 210 225 240 255 270 285 \n", + " 160 176 192 208 224 240 256 272 288 304 \n", + " 170 187 204 221 238 255 272 289 306 323 \n", + " 180 198 216 234 252 270 288 306 324 342 \n", + " 190 209 228 247 266 285 304 323 342 361 \n" + ] + } + ], + "prompt_number": 17 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "example 9.3, page no. 163" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\n", + "name = [\"Madras\", \"Delhi\", \"Ahmedabad\", \"Calcutta\", \"Bombay\"]\n", + "name = sorted(name)\n", + "\n", + "for n in name:\n", + " print n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Ahmedabad\n", + "Bombay\n", + "Calcutta\n", + "Delhi\n", + "Madras\n" + ] + } + ], + "prompt_number": 18 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "example 9.4, page no. 164" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\n", + "aString = \"Object language\"\n", + "print \"Original String : \", aString\n", + "print \"Length of String: \", len(aString)\n", + "for i in range(len(aString)):\n", + " print \"Character at position: %d is %c\" %(i+1,aString[i])\n", + "\n", + "aString = aString.split(\" \")\n", + "aString.insert(1, \"-Oriented\") \n", + "aString = \" \".join(aString)\n", + "print \"Modified String: \", aString\n", + "aString = aString + \" improves security.\"\n", + "print \"Appended String: \", aString\n", + "\n", + "# Note : In python, string is immutable so we can not edit/update it." + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Original String : Object language\n", + "Length of String: 15\n", + "Character at position: 1 is O\n", + "Character at position: 2 is b\n", + "Character at position: 3 is j\n", + "Character at position: 4 is e\n", + "Character at position: 5 is c\n", + "Character at position: 6 is t\n", + "Character at position: 7 is \n", + "Character at position: 8 is l\n", + "Character at position: 9 is a\n", + "Character at position: 10 is n\n", + "Character at position: 11 is g\n", + "Character at position: 12 is u\n", + "Character at position: 13 is a\n", + "Character at position: 14 is g\n", + "Character at position: 15 is e\n", + "Modified String: Object -Oriented language\n", + "Appended String: Object -Oriented language improves security.\n" + ] + } + ], + "prompt_number": 4 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "example 9.5, page no. 166" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\n", + "#vector is an array only in Python & commandline arguments cannot be taken in IPython Notebook\n", + "\n", + "v1 = []\n", + "v1.append(\"Ada\")\n", + "v1.append(\"BASIC\")\n", + "v1.append(\"COBOL\")\n", + "v1.append(\"C++\")\n", + "v1.append(\"FORTRAN\")\n", + "v1.append(\"Python\")\n", + "\n", + "print \"List of Languages: \"\n", + "for ele in v1:\n", + " print ele" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "List of Languages: \n", + "Ada\n", + "BASIC\n", + "COBOL\n", + "C++\n", + "FORTRAN\n", + "Python\n" + ] + } + ], + "prompt_number": 2 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "example 9.6, page no. 169" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\n", + "\"\"\"\n", + "There is no wrapper class in Python.\n", + "\"\"\"\n", + "\n", + "principalAmount = float(raw_input(\"Enter Principal Amount: \"))\n", + "interestRate = float(raw_input(\"Enter Interest Rate: \"))\n", + "numYears = float(raw_input(\"Enter Number of Years: \"))\n", + "\n", + "def loan(p, r, n):\n", + " year = 1\n", + " my_sum = p\n", + " while year<=n:\n", + " my_sum = my_sum*(1+r)\n", + " year += 1\n", + " return my_sum\n", + "\n", + "def printline():\n", + " for i in range(1, 30):\n", + " print \"-\",\n", + " print \"\"\n", + "\n", + "value = loan(principalAmount, interestRate, numYears)\n", + "printline()\n", + "print \"Final Value: \", round(value,2)\n", + "printline()" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter Principal Amount: 5000\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter Interest Rate: 0.15\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter Number of Years: 4\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "- - - - - - - - - - - - - - - - - - - - - - - - - - - - - \n", + "Final Value: 8745.03\n", + "- - - - - - - - - - - - - - - - - - - - - - - - - - - - - \n" + ] + } + ], + "prompt_number": 4 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "example 9.7, page no. 195" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\n", + "\"\"\"\n", + "There is no inbuilt module to use stack as it is done in the book \"java.util.stack\". We will use normal lists as stack in Python\n", + "\"\"\"\n", + "\n", + "stack = []\n", + "stack.append(10)\n", + "stack.append(20)\n", + "stksum1 = stack.pop()\n", + "stksum2 = stack.pop()\n", + "stksum = stksum1 + stksum2\n", + "print \"The top most element from the stack is: \", stksum2\n", + "print \"The next top most element from the stack is: \", stksum1\n", + "print \"The sum of two elements from the stack is: \", stksum" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "The top most element from the stack is: 10\n", + "The next top most element from the stack is: 20\n", + "The sum of two elements from the stack is: 30\n" + ] + } + ], + "prompt_number": 10 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "example 9.8, page no. 196" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\n", + "\"\"\"\n", + "Enum is not supported by default in Python 2.X version. It has been backported to various versions of Python.\n", + "But, the package has to be installed seperately.\n", + "Using normal class here.\n", + "\"\"\"\n", + "\n", + "class WorkingDays:\n", + " Sunday = 0\n", + " Monday = 1\n", + " Tuesday = 2\n", + " Wednesday = 3\n", + " Thursday = 4\n", + " Friday = 5\n", + " Saturday = 6\n", + "\n", + "def weekend(d):\n", + " if d == WorkingDays.Sunday:\n", + " print \"Value = Sunday is a Holiday\"\n", + " elif d == WorkingDays.Monday:\n", + " print \"Value = Monday is a working day\"\n", + " elif d == WorkingDays.Tuesday:\n", + " print \"Value = Tuesday is a working day\"\n", + " elif d == WorkingDays.Wednesday:\n", + " print \"Value = Wednesday is a working day\"\n", + " elif d == WorkingDays.Thursday:\n", + " print \"Value = Thursday is a working day\" \n", + " elif d == WorkingDays.Friday:\n", + " print \"Value = Friday is a working day\"\n", + " else:\n", + " print \"Value = Saturday is a working day\"\n", + "\n", + "for i in range(WorkingDays.Sunday, WorkingDays.Saturday+1):\n", + " weekend(i)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Value = Sunday is a Holiday\n", + "Value = Monday is a working day\n", + "Value = Tuesday is a working day\n", + "Value = Wednesday is a working day\n", + "Value = Thursday is a working day\n", + "Value = Friday is a working day\n", + "Value = Saturday is a working day\n" + ] + } + ], + "prompt_number": 18 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "example 9.9, page no. 199" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\n", + "\"\"\"\n", + "There is no concept of annotations in Python\n", + "\"\"\"\n", + "\n", + "class MySingle:\n", + " value = 0\n", + " def __init__(self, x):\n", + " self.value = 100\n", + " def display(self):\n", + " print \"The value is: \", self.value\n", + "\n", + "ob = MySingle(100)\n", + "try:\n", + " ob.display()\n", + "except:\n", + " print \"No such method found...\"" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "The value is: 100\n" + ] + } + ], + "prompt_number": 50 + } + ], + "metadata": {} + } + ] +}
\ No newline at end of file diff --git a/Programming_With_Java_A_Primer/screenshots/chapter13.png b/Programming_With_Java_A_Primer/screenshots/chapter13.png Binary files differnew file mode 100644 index 00000000..3d211895 --- /dev/null +++ b/Programming_With_Java_A_Primer/screenshots/chapter13.png diff --git a/Programming_With_Java_A_Primer/screenshots/chapter18.png b/Programming_With_Java_A_Primer/screenshots/chapter18.png Binary files differnew file mode 100644 index 00000000..68c1c7e5 --- /dev/null +++ b/Programming_With_Java_A_Primer/screenshots/chapter18.png diff --git a/Programming_With_Java_A_Primer/screenshots/chapter9.png b/Programming_With_Java_A_Primer/screenshots/chapter9.png Binary files differnew file mode 100644 index 00000000..9e456528 --- /dev/null +++ b/Programming_With_Java_A_Primer/screenshots/chapter9.png |