Question: Add polymorphism to your parent and child classes (subclasses) by adding __str__ functions. The __str__ function should print the parent attributes as well as the
Add polymorphism to your parent and child classes (subclasses) by adding __str__ functions. The __str__ function should print the parent attributes as well as the unique child attributes.
The makeNoise() function of the Animal class will need to be overridden in each subclass. The makeNoise function will need to print the sound the animal makes to the screen.
All instantiated Animal objects should be stored in the Zoo list. Traverse the Zoo list using a loop and have each animal polymorphically print its attributes and call the makeNoise function.
PLEASE KEEP THE CODE AT THE SAME LEVEL OF CODING
#Animal Parent Class class Animal: def __init__(self, ani_type, ani_name, ani_age, ani_color): self.animal_type = ani_type self.name = ani_name self.age = ani_age self.color = ani_color #set animal type method def set_animal_type(self, ani_type): print('in set animal type ', ani_type ) self.animal_type = ani_type #set the name of the animal def set_age(self, ani_age): print('in get age ', ani_age) self.age = ani_age #set color of animal method def set_color(self, ani_color): print('in get color ', ani_color) self.color = ani_color #set the name of the animal method def set_name(self, ani_name): print('in set name ', ani_name) self.name = ani_name #get animal type method def get_animal_type(self): return self.animal_type #get animal age method def get_age(self): return self.age #get animal color method def get_color(self): return self.color #get animal name method def get_name(self): return self.name #A stub function called makeNoise() def makeNoise(self): pass #subclass called dog inheriting all of animals attributes class dog(Animal): def __init__(self, ani_type, ani_name, ani_age, ani_color): Animal.__init__(self, ani_type, ani_name, ani_age, ani_color) self.gender = G def set_gender(self, G): self.gender = G def get_gender(self): return self.gender #subclass called monkey inheriting all of animals attributes class monkey(Animal): def __init__(self, ani_type, ani_name, ani_age, ani_color): Animal.__init__(self, ani_type, ani_name, ani_age, ani_color) self.gender = G def set_gender(self, G): self.gender = G def get_gender(self): return self.gender #subclass called gorilla inheriting all of animals attributes class gorilla(Animal): def __init__(self, ani_type, ani_name, ani_age, ani_color): Animal.__init__(self, ani_type, ani_name, ani_age, ani_color) self.gender = G def set_gender(self, G): self.gender = G def get_gender(self): return self.gender #set the values to animal 1, 2, and 3 animal1 = Animal('Dog', 'Roxy', '10', 'brown') animal2 = Animal('Monkey', 'Lucian', '13', 'black') animal3 = Animal('Gorilla', 'Kayle', '14', 'Green', ) #Print the information of all of the animals to the user print('First animal name = ', animal1.get_name(), ' First animal type = ', animal1.get_animal_type(), ' First animal age = ', animal1.get_age(), ' First animal color = ', animal1.get_color()) print('Second animal name = ', animal2.get_name(), ' Second animal type = ', animal2.get_animal_type(), ' Second animal age = ', animal2.get_age(), ' Second animal2 color = ', animal1.get_color()) print('Third animal name = ', animal3.get_name(), ' Third animal type = ', animal3.get_animal_type(), ' Third animal age = ', animal3.get_age(), ' Third animal color = ', animal3.get_color())
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
