|
| 1 | +# Problem 6-1 |
| 2 | +# 10.0/10.0 points (graded) |
| 3 | +# This question has 3 parts |
| 4 | + |
| 5 | +# Consider the following hierarchy of classes: |
| 6 | + |
| 7 | +class Person(object): |
| 8 | + def __init__(self, name): |
| 9 | + self.name = name |
| 10 | + def say(self, stuff): |
| 11 | + return self.name + ' says: ' + stuff |
| 12 | + def __str__(self): |
| 13 | + return self.name |
| 14 | + |
| 15 | +class Lecturer(Person): |
| 16 | + def lecture(self, stuff): |
| 17 | + return 'I believe that ' + Person.say(self, stuff) |
| 18 | + |
| 19 | +class Professor(Lecturer): |
| 20 | + def say(self, stuff): |
| 21 | + return self.name + ' says: ' + self.lecture(stuff) |
| 22 | + |
| 23 | +class ArrogantProfessor(Professor): |
| 24 | + def say(self, stuff): |
| 25 | + return 'It is obvious that ' + self.say(stuff) |
| 26 | + |
| 27 | +# As written, this code leads to an infinite loop when using the Arrogant Professor class. |
| 28 | + |
| 29 | +# Change the definition of ArrogantProfessor so that the following behavior is achieved: |
| 30 | + |
| 31 | +# e = Person('eric') |
| 32 | +# le = Lecturer('eric') |
| 33 | +# pe = Professor('eric') |
| 34 | +# ae = ArrogantProfessor('eric') |
| 35 | + |
| 36 | +# >>> e.say('the sky is blue') |
| 37 | +# eric says: the sky is blue |
| 38 | + |
| 39 | +# >>> le.say('the sky is blue') |
| 40 | +# eric says: the sky is blue |
| 41 | + |
| 42 | +# >>> le.lecture('the sky is blue') |
| 43 | +# I believe that eric says: the sky is blue |
| 44 | + |
| 45 | +# >>> pe.say('the sky is blue') |
| 46 | +# eric says: I believe that eric says: the sky is blue |
| 47 | + |
| 48 | +# >>> pe.lecture('the sky is blue') |
| 49 | +# I believe that eric says: the sky is blue |
| 50 | + |
| 51 | +# >>> ae.say('the sky is blue') |
| 52 | +# eric says: It is obvious that eric says: the sky is blue |
| 53 | + |
| 54 | +# >>> ae.lecture('the sky is blue') |
| 55 | +# It is obvious that eric says: the sky is blue |
| 56 | + |
| 57 | +# For this question, you will not be able to see the test cases we run. This problem will test your ability to come up with your own |
| 58 | +# test cases. |
| 59 | + |
| 60 | + |
| 61 | +# Paste your class here |
| 62 | +class ArrogantProfessor(Person): |
| 63 | + def say(self, stuff): |
| 64 | + return self.name + ' says: ' + 'It is obvious that ' + self.name + ' says: ' + stuff |
| 65 | + def lecture(self, stuff): |
| 66 | + return 'It is obvious that ' + self.name + ' says: ' + stuff |
| 67 | + |
| 68 | +# Correct |
0 commit comments