-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathlec12prob1.py
94 lines (68 loc) · 2.33 KB
/
lec12prob1.py
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
# lec12prob1.py
#
# Lecture 12 - Object Oriented Programming
# Problem 1
#
# edX MITx 6.00.1x
# Introduction to Computer Science and Programming Using Python
# Consider the following code:
class Spell(object):
def __init__(self, incantation, name):
self.name = name
self.incantation = incantation
def __str__(self):
return self.name + ' ' + self.incantation + '\n' + self.getDescription()
def getDescription(self):
return 'No description'
def execute(self):
print self.incantation
class Accio(Spell):
def __init__(self):
Spell.__init__(self, 'Accio', 'Summoning Charm')
class Confundo(Spell):
def __init__(self):
Spell.__init__(self, 'Confundo', 'Confundus Charm')
def getDescription(self)
return 'Causes the victim to become confused and befuddled.'
def studySpell(spell):
print spell
spell = Accio()
spell.execute()
studySpell(spell)
studySpell(Confundo())
'''
1. What are the parent class(es)? Note that the term "parent class" is
interchangable with the term "superclass".
Spell <
Accio
Confundo
2. Whare are the child class(es)? Note that the term "child class" is
interchangeable with the term "subclass".
Spell
Accio <
Confundo <
3. What does the code print out? Try figuring it out in your head before you
try running it in Python?
Hint: This code prints out 5 lines. Enter each line that is printed out in its
own box, in sequential order.
Accio
Sumoning Charm Accio
No description
Confundus Charm Confundo
Causes the victim to become confused and befuddled.
4. Which getDescription method is called when studySpell(Confundo())
is executed?
The getDescription method defined within the Spell class
The getDescription method defined within the Accio class
The getDescription method defined within the Confundo class
5. How do we need to modify Accio so that 'print Accio()' will print the
following description?
Summoning Charm Accio
This charm summons an object to the caster, potentially over a significant distance.
class Accio(Spell):
def __init__(self):
Spell.__init__(self, 'Accio', 'Summoning Charm')
# Add following line to class Accio
def getDescription(self):
return 'This charm summons an object to the caster, potentially over a significant distance.'
'''