Create a class named \"Animal\". The attributes of this class include COLOR, FOO
ID: 3813814 • Letter: C
Question
Create a class named "Animal". The attributes of this class include COLOR, FOOD and methods RUN, EAT. a. RUN: print "An animal is running!" b. EAT: print "An animal is eating + FOOD!" (FOOD is given when you initialize on Animal instant) Dog is a subclass of Animal. It has one more attribute AGE and one more method SAY_HELLO. a. RUN: print "A dog is running!" (which is different from the Animal class when you call RUN) b. SAY_HELLO: print "I am a AGE years old dog! How are you?" (AGE is from user's input) Poodle is a subclass of class Dog. One more methods for Poodle class comparing with Dog class named SAY_BYE. a. RUN: print "A poodle is running!" b. SAY_BYE: print "BYE-BYE, my homework is done!"Explanation / Answer
# pastebin link for code as indenttaion is not preserved here: https://pastebin.com/LwsiP46h
class Animal(object):
def __init__(self, color, food):
self.COLOR = color
self.FOOD = food
def RUN(self):
print("An animal is running!")
def EAT(self):
print("An animal is eating " + self.FOOD + "!")
class Dog(Animal):
def __init__(self, color, food, age):
super(Dog, self).__init__(color, food)
self.AGE = age
def RUN(self):
print("A dog is running!")
def SAY_HELLO(self):
print("I am a " + str(self.AGE) + " years old dog! How are you?")
class Poodle(Dog):
def __init__(self, color, food, age):
super(Poodle, self).__init__(color, food, age)
def RUN(self):
print("A poodle is running!")
def SAY_BYE(self):
print("BYE-BYE, my homework is done!")
# driver code to test above classes
# Animal class
animal = Animal("RED", "milk")
animal.RUN()
animal.EAT()
# Dog class
dog = Dog("RED", "milk", 5)
dog.RUN()
dog.EAT()
dog.SAY_HELLO()
# Poodle class
poodle = Poodle("Red", "milk", 5)
poodle.RUN()
poodle.EAT()
poodle.SAY_HELLO()
poodle.SAY_BYE()
Sample output
Please rate positively if this solved your question. Comment if something is not clear.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.