Implement a class in Python called Pet. Include methods to set the pet\'s name,
ID: 3806342 • Letter: I
Question
Implement a class in Python called Pet. Include methods to set the pet's name, species, and age. Also include methods to get the pet's name, species, and age. Next, write Python code that demonstrates your new Pet class. At minimum, your code should create (instantiate) two Pet objects, and call each of the set/get methods for each of the two Pet objects. Be sure to output each of the items (name, species, age) as you call their get methods. examples in the text typically put the class code and the program code in two separate files it's fine if you put all of your class and program code into a single file.Explanation / Answer
# Pet program in Python
class Pet:
def get_name(self):#getter method
return self.__name
def set_name(self, name):#setter method
self.__name = name
def get_species(self):
return self.__species
def set_species(self, species):
self.__species = species
def get_age(self):
return self.__age
def set_age(self, age):
self.__age = age
pet1=Pet() # instantiate pet object
pet1.set_name("Bob") # setting the value
pet1.set_species("Dog")
pet1.set_age(1)
pet2=Pet()
pet2.set_name("Tom")
pet2.set_species("Cat")
pet2.set_age(2)
#printing the values
print "Pet 1 info : "
print "Name : "+pet1.get_name()
print "Species : "+pet1.get_species()
print "Age : "+`pet1.get_age()`
print "Pet 2 info : "
print "Name : "+pet2.get_name()
print "Species : "+pet2.get_species()
print "Age : "+`pet2.get_age()`
------------------------------------output---------------------------------
Pet 1 info :
Name : Bob
Species : Dog
Age : 1
Pet 2 info :
Name : Tom
Species : Cat
Age : 2
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.