Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Add polymorphism to your parent and child classes (subclasses) by adding __str__

ID: 3720016 • Letter: A

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 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

Explanation / Answer

The code given below has been edited as per the question. The __str__ function is just below the __init__ function of each class and makeNoise() function is at the end of each class. Hope you have got your answer.

#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

#__str__ method for Animal class

def __str__(self):

print('Animal Type = ', self.animal_type)

print('Animal Name = ', self.name)

print('Animal Type = ', self.age)

print('Animal Type = ', self.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

#__str__ method for Dog class

def __str__(self):

Animal.__str__(self)

print('Animal Gender = ', self.gender)

makeNoise(self)

def set_gender(self, G):

self.gender = G

def get_gender(self):

return self.gender

#makeNoise function overridden for dog class

def makeNoise(self):

print('Animal Sound = bark ')

#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

#__str__ method for monkey class

def __str__(self):

Animal.__str__(self)

print('Animal Gender = ', self.gender)

makeNoise(self)

def set_gender(self, G):

self.gender = G

def get_gender(self):

return self.gender

#makeNoise function overridden for monkey class

def makeNoise(self):

print('Animal Sound = Scream ')

#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

#__str__ method for gorilla class

def __str__(self):

Animal.__str__(self)

print('Animal Gender = ', self.gender)

makeNoise(self)

def set_gender(self, G):

self.gender = G

def get_gender(self):

return self.gender

#makeNoise function overridden for monkey class

def makeNoise(self):

print('Animal Sound = roar ')

#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())

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote