Question

Python Define a class called Animal that abstracts animals and supports three methods: setSpecies(species): Sets the...

Python

Define a class called Animal that abstracts animals and supports three methods:

  • setSpecies(species): Sets the species of the animal object to species.
  • setLanguage(language): Sets the language of the animal object to language.
  • speak(): Prints a message from the animal as shown below.

The class must support supports a two, one, or no input argument constructor.

Then define Bird as a subclass of Animal and change the behavior of method speak() in class Bird.

>>> snoopy = Animal('dog', 'bark')

>>> snoopy.speak()

I am a dog and I bark.

>>> tweety = Animal('canary')

>>> tweety.speak()

I am a canary and I make sounds.

>>> animal = Animal()

>>> animal.speak()

I am a animal and I make sounds.

>>> daffy = Bird()

>>> daffy.speak()

quack! quack! quack!

Homework Answers

Answer #1

"""
  Python program to for class Animal
"""

class Animal:
  def __init__(self, sp = 'animal', lan = 'sounds'):
    self.species = sp
    self.language = lan

  def setSpecies(self, species):
    self.species = species

  def setLanguage(self, language):
    self.language = language
  
  def speak(self):
    if self.language != "sounds":
      print("I am a " + self.species + " and I " + self.language)
    else:
      print("I am a " + self.species + " and I make " + self.language)

class Bird(Animal):
  def speak(self):
    print("quak! "*3)

if __name__ == '__main__':
  
  snoopy = Animal('dog', 'bark')
  snoopy.speak()

  tweety = Animal('canary')
  tweety.speak()

  animal = Animal()
  animal.speak()

  daffy = Bird()
  daffy.speak()
Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions