a. Add a Dodo subclass of the superclass Bird. Define a method flap_wings() that
ID: 3917859 • Letter: A
Question
a. Add a Dodo subclass of the superclass Bird. Define a method flap_wings() that prints the message. Flapping wings is useless! SPLAT!!
Give dodos a method fly() that overrides the method in the superclass, and prints the message: Dodos can't fly!
They will flap their wings, but it is useless. Their fly method will raise a NotImplementedError to reflect this.
Construct a dodo object and try to make it fly using the polymorphic method lift_off(). Use a try/except block explicitly catch a NotImplementedError and print out the error message using repr().
b. Adjust the method spin_propellers() in the airplane class so it prints the message: Propellers: spin, spin, spin, spin, spin, spin, ... Print each "spin" after a brief delay. Use time.sleep(delay)
The fly() method for airplanes should print the message: "Airplane flying" and call the spin_propellers() method. v. Give whales a method called flex_body_wave_fins() that prints the message:
Flexing body and tail back and forth. Steering with fins.
Flexing: Back, forth, back, forth, back, forth, ...
Use a for loop and a delay so the whale flexes back and forth after a brief delay. The swim method should be updated to also call this method.
SAMPLE OUTPUT
Bird flying Flap, flap, flap, flap, flap, flap, ... Duck flying Flap, flap, flap, flap, flap, flap, ... Airplane flying Propellers: spin, spin, spin, spin, spin, spin, ... Dodos can't fly! Flapping wings is useless! SPLAT!! Poor Dodo: NotImplementedError() Ack! Whales cannot fly! Whale instance has no attribute 'fly' Whale swimming Flexing body and tail back and forth. Steering with fins. Flexing: Back, forth, back, forth, back, forth, ...
code:
Explanation / Answer
The code is below.
import time
# Focus on Duck Typing
class Bird(object):
def flap_wings(self):
print " Flap, ",
for n in range(5):
print "flap,",
time.sleep(0.25)
print "..."
def fly(self):
print("Bird flying")
self.flap_wings()
class Duck(Bird):
def fly(self):
print("Duck flying")
self.flap_wings()
class Dodo(Bird):
def flap_wings(self):
print("Flapping wings is useless! SPLAT!!")
def fly(self):
print("Dodos can't fly!")
self.flap_wings()
raise NotImplementedError("NotImplementedError()")
class Airplane(object):
def spin_propellers(self):
print " Propellers: Spin, ",
for n in range(5):
print "spin,",
time.sleep(0.25)
print "..."
def fly(self):
print("Airplane flying")
self.spin_propellers()
class Whale(object):
def flex_body_wave_fins(self):
print " Flexing body and tail back and forth. Steering with fins."
print " Flexing: Back, forth, ",
for n in range(5):
print "back, forth,",
time.sleep(0.25)
print "..."
def swim(self):
print("Whale swimming")
self.flex_body_wave_fins()
# polymorphic method
def lift_off(entity):
entity.fly()
# construct some objects from these classes
bird = Bird()
duck = Duck()
airplane = Airplane()
whale = Whale()
dodo = Dodo()
lift_off(bird) # prints Bird flying
lift_off(duck) # prints Duck flying
lift_off(airplane) # prints Airplane flying
try:
lift_off(dodo)
except Exception as e1:
print "Poor Dodo:", e1
try:
lift_off(whale) # Throws error: Whale object has no attribute fly
except Exception as e2:
print "Ack! Whales cannot fly!", e2
whale.swim()
Explanation:
As the other functions were implemented, the code for Dodo was written. Writing a fly method inside class Dodo automatically overrides the superclass method. The NotImplementedError can be raised by the command raise NotImplementedError("Message")
As for the other time loops, the loops for airplanes and whales has been written
Sample Output:
Bird flying
Flap, flap, flap, flap, flap, flap, ...
Duck flying
Flap, flap, flap, flap, flap, flap, ...
Airplane flying
Propellers: Spin, spin, spin, spin, spin, spin, ...
Dodos can't fly!
Flapping wings is useless! SPLAT!!
Poor Dodo: NotImplementedError()
Ack! Whales cannot fly! 'Whale' object has no attribute 'fly'
Whale swimming
Flexing body and tail back and forth. Steering with fins.
Flexing: Back, forth, back, forth, back, forth, back, forth, back, forth, back, forth, ...
Hope this helps!
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.