In: Computer Science
Scenario
Suppose you work for a wildlife conservation organization. You are working on creating a system to educate the general public about different animals and get them more interested in conservation.
Aim
Create a Eagle class that inherits from the Bird class and has a clutch size (eggs laid in one nesting) attribute. Change the behavior of instances of the Eagle class to include this clutch size fact when they're printed.
Steps for Completion
Go to your main.py file.
Define the Eagle class that inherits from the Bird class. Override the __init__ initializer method and add a clutch_size attribute with the value of 3.
Override the __str__ method and modify it to include mention of the clutch size.
At the bottom of the script we have initialized an instance of the Eagle class and called print on the instance, which should display facts about the eagle.
Run the script by using python3 main.py in the terminal. The output should be as shown in Snippet 7.86:
The eagle has a wingspan up to 7.5ft, has a lifespan of 20 years and can fly at a maximum speed of 99mph. It also has a clutch size up to 3.
Program Code Screenshot:
Sample output:
The screenshots are attached below for reference.
Please follow them for proper indentation and output.
Program to copy:
class Bird:#Bird class definition
def __init__(self,h,l,s):#constructor of class
self.height=h
self.span=l
self.speed=s
def __str__(self):#string representation of Bird class
attributes
return "Height is: "+str(self.height)+" Life span
is:"+str(self.span)+" Speed is "+str(self.speed)
class Eagle(Bird):#Eagle class definition
def __init__(self,h,l,s,c):
super().__init__(h,l,s)
self.clutch_size=c
def __str__(self):#string representation of Eagle class
attributes
s=super().__str__()
return s+" clutcch size is "+str(self.clutch_size)
e=Eagle(7.5,20,99,3)#create object and print
print(e)