summaryrefslogtreecommitdiff
path: root/Programming_With_Java_A_Primer/chapter10.ipynb
blob: 47f275791ba28a1f4606abe5998af902d1fab81d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
{
 "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": {}
  }
 ]
}