Cats and dogs have much in common - they\'re both furry mammals that like people
ID: 3796754 • Letter: C
Question
Cats and dogs have much in common - they're both furry mammals that like people and make great pets.
Sadly, they have shorter lifespans than people and we sometimes refer to 'dog years' for this accelerated aging process. Cats and dogs age differently as well, with cats living significantly longer. The general rule for this is:
Dogs
Cats
For this assignment, create a pets python module with a Pet superclass having common attributes (name, age, breed), where 'age' is in human years. Then create Cat and Dog subclasses that inherit from Pet and implement methods to calculate appropriate 'animal' years and print pet information.
Other programs should be able to import your pets.py module and perform operations like below:
Hints
You can use a python data collection to map human years to pet years, or you can use conditional logic. Consider which is easier and what kind of collection might work best,
See my example of class inheritance here - https://canvas.seattlecentral.edu/files/73429878/
Each of your sub-classes will need a method to calculate non-human age. Ideally, you would name this method the same in each sub-class,
For simplicity, you may want each subclass to have its own __str__ method
Explanation / Answer
pets.py
class Pet:
"""docstring for Pet"""
def __init__(self, name,age,breed):
self.name = name
self.age = age
self.breed = breed
def __str__(self):
return "Name: "+self.name+", Breed: "+self.breed+", Age: "+str(self.age)
class Dog(Pet):
"""docstring for Dog"""
def __init__(self, name, age, breed):
Pet.__init__(self,name,age,breed)
def getDogYears(self):
tempAge = self.age
humanAge = 0;
if(tempAge>0):
humanAge += 15
tempAge -= 1
if(tempAge>0):
humanAge += 9
tempAge -= 1
while(tempAge>0):
humanAge += 5
tempAge -= 1
return humanAge
def __str__(self):
return Pet.__str__(self) + " (" + str(self.getDogYears()) + " dog years)"
class Cat(Pet):
"""docstring for Cat"""
def __init__(self, name, age, breed):
Pet.__init__(self,name,age,breed)
def getCatYears(self):
tempAge = self.age
humanAge = 0;
if(tempAge>0):
humanAge += 15
tempAge -= 1
if(tempAge>0):
humanAge += 9
tempAge -= 1
while(tempAge>0):
humanAge += 4
tempAge -= 1
return humanAge
def __str__(self):
return Pet.__str__(self)+" ("+str(self.getCatYears())+" cat years)"
#############################################################
Main.py
from pets import*
def main():
print str(Dog('fido',6,'great dane'))
print str(Cat('sparky',6, 'siamese'))
if __name__ == '__main__':
main()
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.