using python Write an Animal class (this is based on the example from Chapter 8,
ID: 3661807 • Letter: U
Question
using python
Write an Animal class (this is based on the example from Chapter 8, and you are welcome to start with what is included there, you will need to make modifications). Your object is to make this client code work:
>>> a = Animal()
>>> a
Animal('default','default',0)
>>> a.speak()
I am a 0 year-old default and I default.
>>> a.setSpecies('tiger')
>>> a.setLanguage('growl')
>>> a.setAge(8)
>>> a
Animal('tiger','growl',8)
>>> a.speak()
I am a 8 year-old tiger and I growl.
>>> b = Animal('jackalope','can imitate anything',33)
>>> b
Animal('jackalope','can imitate anything',33)
>>> b.speak()
I am a 33 year-old jackalope and I can imitate anything.
>>> [b.speak()] # make sure print not return
I am a 33 year-old jackalope and I can imitate anything.
[None]
TEST for __repr__
note that quotes should appear around
the species and language but not the age
>>> eval(str(a))
Animal('tiger','growl',8)
>>> eval(str(b))
Animal('jackalope','can imitate anything',33)
TEST == ie., __eq__
also requires __repr__ to be written correctly
>>> a==b
False
>>> eval(str(a))==a
True
>>> eval(str(Animal()))==Animal()
True
An Animal object has three variables:
species - str
language -str
age - int
The Animal class has the following methods:
__init__ - can create Animals either by specifying no arguments (see above) or by specifying the species, language and age.
__repr__ – returns str representation of Animal. See client code. Note for full credit, eval statements should work.
setSpecies – sets species
setLanguage – sets language
setAge - takes an integer as a parameter and sets the animal's age to the value of the parameter
speak – prints message (see sample client code)
__eq__ – determines whether two animals are the same, i.e., if all 3 variables have the same value
Explanation / Answer
Code:
class Animal:
def __init__(self,name="default",language="default",age=0):
self.name= name
self.language=language
self.age=age
def __repr__(self):
return 'Animal('%s','%s',%s)'% (self.name,self.language,self.age)
def setSpecies(self,name):
self.name = name
def setLanguage(self,language):
self.language = language
def setAge(self,age):
self.age = age
def speak(self):
print 'I am a '+str(self.age)+' year-old '+self.name+' and I '+self.language
def __eq__(self,other):
return ( self.name == other.name ) and (self.language == other.language) and (self.age == other.age)
output:
>>> class Animal: ... def __init__(self,name="default",language="default",age=0): ... self.name= name ... self.language=language ... self.age=age ... def __repr__(self): ... return 'Animal('%s','%s',%s)'% (self.name,self.language,self.age) ... def setSpecies(self,name): ... self.name = name ... def setLanguage(self,language): ... self.language = language ... def setAge(self,age): ... self.age = age ... def speak(self): ... print 'I am a '+str(self.age)+' year-old '+self.name+' and I '+self.language ... def __eq__(self,other): ... return ( self.name == other.name ) and (self.language == other.language) and (self.age == other.age)
>>> a = Animal()
>>> a
Animal(default,default,0)
>>> a.speak()
I am a 0 year-old default and I default
>>> a.setSpecies('tiger')
>>> a.setLanguage('growl')
>>> a.setAge(8)
>>> a
Animal(tiger,growl,8)
>>> a.speak()
I am a 8 year-old tiger and I growl
>>> b = Animal('jackalope','can imitate anything',33)
>>> b
Animal(jackalope,canimitateanything,33)
>>> b.speak()
I am a 33 year-old jackalope and I can imitate anything
>>> [b.speak()]
I am a 33 year-old jackalope and I can imitate anything
[None]
>>> eval(str(a))
Animal(tiger,growl,8)
>>> eval(str(b))
Animal(jackalope,canimitateanything,33)
>>> a==b
False
>>> eval(str(a))==a
True
>>> eval(str(Animal()))==Animal()
True
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.