diff options
Diffstat (limited to 'Programming_With_Java_A_Primer_by_E._Balagurusamy/chapter8.ipynb')
-rwxr-xr-x | Programming_With_Java_A_Primer_by_E._Balagurusamy/chapter8.ipynb | 319 |
1 files changed, 319 insertions, 0 deletions
diff --git a/Programming_With_Java_A_Primer_by_E._Balagurusamy/chapter8.ipynb b/Programming_With_Java_A_Primer_by_E._Balagurusamy/chapter8.ipynb new file mode 100755 index 00000000..85db9833 --- /dev/null +++ b/Programming_With_Java_A_Primer_by_E._Balagurusamy/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 |