This program will be ran through a Python 3 IDE, please use correct formatting a
ID: 3690575 • Letter: T
Question
This program will be ran through a Python 3 IDE, please use correct formatting as such.
Write a class named Marsupial that can be used as shown below:
>>> m = Marsupial()
>>> m.put_in_pouch('doll')
>>> m.put_in_pouch('firetruck')
>>> m.put_in_pouch('kitten')
>>> m.pouch_contents()
['doll', 'firetruck', 'kitten']
Now write a class named Kangaroo as a subclass of Marsupial that inherits all the attributes of Marsupial and also:
extends the Marsupial __init__ constructor to take, as input, the coordinates x and y of the Kangaroo object,
supports method jump that takes number values dx and dy as input and moves the kangaroo by dx units along the x-axis and by dy units along the y-axis, and
overloads the __str__ operator so it behaves as shown below.
>>> k = Kangaroo(0,0)
>>> print(k)
I am a Kangaroo located at coordinates (0,0)
>>> k.put_in_pouch('doll')
>>> k.put_in_pouch('firetruck')
>>> k.put_in_pouch('kitten')
>>> k.pouch_contents()
['doll', 'firetruck', 'kitten']
>>> k.jump(1,0)
>>> k.jump(1,0)
>>> k.jump(1,0)
>>> print(k)
I am a Kangaroo located at coordinates (3,0)
Explanation / Answer
# Problem 1
class Marsupial(object):
def __init__(self):
self.lst = []
def put_in_pouch(self, item):
self.lst.append(item)
def pouch_contents(self):
return self.lst
# Problem 2
class Kangaroo(Marsupial):
def __init__(self,x,y):
Marsupial.__init__(self)
self.x = x
self.y = y
def jump(self,dx,dy):
self.x += dx
self.y += dy
def __str__(self):
return 'I am a Kangaroo located at coordinates ({},{})'.format(self.x,self.y)
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.