USE PYHTON CODING ####################### ## ## Definition for the class Pet ##
ID: 3678643 • Letter: U
Question
USE PYHTON CODING
#######################
##
## Definition for the class Pet
##
########################
class Pet:
owner=""
name=""
alive=False
def __init__(self,new_name):
self.name=new_name
self.alive=True
Using the code shown above as a base write a code to do the following:
- Add to the Pet class a method setName(...) which assigns a name to the pet
- Add to the Pet class a method setOwner(...) which takes the owners name as a parameter and applies it to the pet
- Add to the Pet class a method called speak() and a variable called sound which is an empty string by default
- Create and demonstrate sub-classes of the Pet called Dog and Cat
- Override the function speak() so that Dog.speak() returns “Woof” and Cat.speak() returns “Meow”
- Override the __str__ function to display the Pet’s name and owner.
Explanation / Answer
class Pet(object):
def __init__(self):
self._owner = ""
self._name = ""
self._alive = False
def init(self, new_name):
self._name = new_name
self._alive = True
def setName(self, name):
self._name = name
def setOwner(self, owner_name):
self._owner = owner_name
def speak(self):
sound = ""
def str(self):
pass
class Dog(object):
Pet = property()
def speak(self):
sound = "Woof"
Console.WriteLine("" + sound)
def str(self):
Console.WriteLine("Owner : " + owner + " Pet : " + owner)
class Cat(object):
Pet = property()
def speak(self):
sound = "Meow"
Console.WriteLine("" + sound)
def str(self):
Console.WriteLine("Owner : " + owner + " Pet : " + owner)
class PetTest(object):
def main(args):
pass
main = staticmethod(main)
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.