Question

In: Computer Science

PYTHON QUESTION Create a subclass of the Dog class named anything appropriate (e.g. Bulldog, Chihuahua, German...

PYTHON QUESTION

Create a subclass of the Dog class named anything appropriate (e.g. Bulldog, Chihuahua, German Shepherd, etc). This subclass should consist of the following:

1. It should inherit the __init__ method from the Dog class, but add one new attribute at instantiation.

2. It should add two new methods that were not in our Dog class

3. It should overwrite one of the methods that existed in the Dog class.

-------

I'm not entirely sure where I should add this information to my dog class, nor how I should go about overwriting one of the methods.

-------

class Dog:
    def __init__(self,name,age):
        self.name = name
        self.age = age

    def description(self):
        return self.name + ' is' + str(self.age) + ' years old.'

    def speak(self):
        return self.name + ' barks!'

    def run(self):
        return self.name + ' is running!'

    def fetch(self, toy):
        print(self.run())
        print(self.name + ' fetched the ' + toy)
        print(self.name + ' is bringing it back!')


    def wags_tail(self):
        return self.name + ' is so happy!'

    def isolder(self, dog2):
        if self.age > dog2.age:
            return True
        else:
            return False

dog1 = Dog('Hurc', 2)
dog2 = Dog('Sammy', 5)
print(dog1.description())
print(dog1.speak())
print(dog1.run())
dog1.fetch('ball')
print(dog1.wags_tail())
print(dog1.isolder(dog2))

Solutions

Expert Solution

/**********************main.py************************/

class Dog:
def __init__(self,name,age):
self.name = name
self.age = age

def description(self):
return self.name + ' is' + str(self.age) + ' years old.'

def speak(self):
return self.name + ' barks!'

def run(self):
return self.name + ' is running!'

def fetch(self, toy):
print(self.run())
print(self.name + ' fetched the ' + toy)
print(self.name + ' is bringing it back!')


def wags_tail(self):
return self.name + ' is so happy!'

def isolder(self, dog2):
if self.age > dog2.age:
return True
else:
return False
  
class BullDog(Dog):
  
def __init__(self,name,age,color):
super(BullDog,self).__init__(name,age)
self.color = color

def description(self):
return self.name + ' is Bulldog' + str(self.age) + ' years old. has ' + self.color+ ' color'
  
def feed(self):
return self.name + ' has been feeded'
  
def care(self):
  
return self.name + ' need more care'
  

dog1 = Dog('Hurc', 2)
dog2 = BullDog('Sammy', 5,'White')
print(dog1.description())
print(dog1.speak())
print(dog1.run())
dog1.fetch('ball')
print(dog1.wags_tail())
print(dog1.isolder(dog2))

print(dog2.description())
print(dog2.speak())
print(dog2.run())
dog2.fetch('ball')
print(dog2.wags_tail())
print(dog2.feed())
print(dog2.care())
print(dog2.isolder(dog1))

Please let me know if you have any doubt or modify the answer, Thanks :)


Related Solutions

Question: In a package named "oop" create a Scala class named "Team" and a Scala object...
Question: In a package named "oop" create a Scala class named "Team" and a Scala object named "Referee". Team will have: • State values of type Int representing the strength of the team's offense and defense with a constructor to set these values. The parameters for the constructor should be offense then defense • A third state variable named "score" of type Int that is not in the constructor, is declared as a var, and is initialized to 0 Referee...
Create a file named StudentArrayList.java,within the file create a class named StudentArrayList. This class is meant...
Create a file named StudentArrayList.java,within the file create a class named StudentArrayList. This class is meant to mimic the ArrayList data structure. It will hold an ordered list of items. This list should have a variable size, meaning an arbitrary number of items may be added to the list. Most importantly this class should implement the interface SimpleArrayList provided. Feel free to add as many other functions and methods as needed to your class to accomplish this task. In other...
Create a custom Exception named IllegalTriangleSideException. Create a class named Triangle. The Triangle class should contain...
Create a custom Exception named IllegalTriangleSideException. Create a class named Triangle. The Triangle class should contain 3 double variables containing the length of each of the triangles three sides. Create a constructor with three parameters to initialize the three sides of the triangle. Add an additional method named checkSides with method header - *boolean checkSides() throws IllegalTriangleSideException *. Write code so that checkSides makes sure that the three sides of the triangle meet the proper criteria for a triangle. It...
Create a custom Exception named IllegalTriangleSideException. Create a class named Triangle. The Triangle class should contain...
Create a custom Exception named IllegalTriangleSideException. Create a class named Triangle. The Triangle class should contain 3 double variables containing the length of each of the triangles three sides. Create a constructor with three parameters to initialize the three sides of the triangle. Add an additional method named checkSides with method header - *boolean checkSides() throws IllegalTriangleSideException *. Write code so that checkSides makes sure that the three sides of the triangle meet the proper criteria for a triangle. It...
Challenge: Documents Description: Create a class in Python 3 named Document that has specified attributes and...
Challenge: Documents Description: Create a class in Python 3 named Document that has specified attributes and methods for holding the information for a document and write a program to test the class. Purpose: The purpose of this challenge is to provide experience creating a class and working with OO concepts in Python 3. Requirements: Write a class in Python 3 named Document that has the following attributes and methods and is saved in the file Document.py. Attributes __title is a...
Description: Create a class in Python 3 named Animal that has specified attributes and methods. Purpose:...
Description: Create a class in Python 3 named Animal that has specified attributes and methods. Purpose: The purpose of this challenge is to provide experience creating a class and working with OO concepts in Python 3. Requirements: Write a class in Python 3 named Animal that has the following attributes and methods and is saved in the file Animal.py. Attributes __animal_type is a hidden attribute used to indicate the animal’s type. For example: gecko, walrus, tiger, etc. __name is a...
* Python * * Python Programming * Part 1: Product Class Write a class named Product...
* Python * * Python Programming * Part 1: Product Class Write a class named Product that holds data about an item in a retail store.   The class should store the following data in attributes: product id, item description, units in inventory, and price. Write the __init__ method to require all four attributes. Also write a __str__ method for debugging output.    Once you have written the class, write a main() function that creates three Product objects and stores the following...
Challenge: Dog Description: Create a Dog class that contains specified properties and methods. Create an instance...
Challenge: Dog Description: Create a Dog class that contains specified properties and methods. Create an instance of Dog and use its methods. Purpose: This application provides experience with creating classes and instances of objects in C#. Requirements: Project Name: Dog Target Platform: Console Programming Language: C# Documentation: Types and variables (Links to an external site.) (Microsoft) Classes and objects (Links to an external site.) (Microsoft) Enums (Links to an external site.) (Microsoft) Create a class called Dog. Dog is to...
Python Clean up the code Recall that once a subclass inherits from a parent class, it...
Python Clean up the code Recall that once a subclass inherits from a parent class, it automatically has access to all of the parents' methods. Sometimes, the subclass needs to do extra things within a method which the parent does not do. For example, both UserPlayer and BasicMonster have their specialized __init__ methods which override the one from Player. Discuss the following question with your partner: What are the other methods that are being overridden by a subclass in the...
In java: -Create a class named Animal
In java: -Create a class named Animal
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT