Please write in Python!!! Define a member method print_all() for class PetData.
ID: 3763981 • Letter: P
Question
Please write in Python!!!
Define a member method print_all() for class PetData. Make use of the base class' print_all() method. Sample output for the given program:
class AnimalData:
def __init__(self):
self.full_name = ''
self.age_years = 0
def set_name(self, given_name):
self.full_name = given_name
def set_age(self, num_years):
self.age_years = num_years
# Other parts omitted
def print_all(self):
print('Name:', self.full_name)
print('Age:', self.age_years)
class PetData(AnimalData):
def __init__(self):
AnimalData.__init__(self)
self.id_num = 0
def set_id(self, pet_id):
self.id_num = pet_id
# FIXME: Add print_all() member method
'''Your solution goes here'''
user_pet = PetData()
user_pet.set_name('Fluffy')
user_pet.set_age(5)
user_pet.set_id(4444)
user_pet.print_all()
Explanation / Answer
class AnimalData:
def __init__(self, animal, name, bark):
self.animal = animal
self.name = name
self.bark = bark
def say(self):
print('A {} goes {}'.format(self.animal, self.bark))
class Dog(Animal):
def __init__(self):
Animal.__init__(self, "dog", "", "Woof!")
class Cat(Animal):
def __init__(self):
Animal.__init__(self, "cat", "", "Miao!")
d1 = Dog()
d1.say()
c1 = Cat()
c1.say()
class PetData(AnimalData):
def __init__(self, name, animal_type, age):
self.__name = name
self.__animal_type = animal_type
self.__age = age
def set_name(self, name):
self.__name = name
def set_type(self, animal_type):
self.__animal_type = animal_type
def set_age(self, age):
self.__age = age
def get_name(self):
return self.__name
def get_animal_type(self):
return self.__animal_type
def get_age(self):
return self.__age
# The main function
def main():
name = input('What is the name of the pet: ')
animal_type = input('What type of pet is it: ')
age = int(input('How old is your pet: '))
pets = Pet(name, animal_type, age)
print('This will be added to the records. ')
print('Here is the data you entered:')
print('Pet Name: ', pets.get_name)
print('Animal Type: ', pets.get_animal_type)
print('Age: ', pets.get_age)
main()