Question
**PYTHON** In this question, we will create a class called Dog that is a subclass of the Animal class. In addition, the Dog class will
**PYTHON**
In this question, we will create a class called Dog that is a subclass of the Animal class. In addition, the Dog class will have a member variable called breed(string) that designates the dog's breed.
Note that we are inheriting from the parent class (or _super_class) Animal but we are also redefining the __init__ function. The new __init__ function of Dogtakes two parameters breed and name. The breed parameter is new, but the name parameter is the same parameter defined in the superclass Animal. Thus, we need to call the __init__ function of Animal. The modern Pythonic way to call a method of the superclass (Animal) of a subclass (Dog) from inside the subclass is to use super().
For example, let's say we have defined a class:
class Parent: def __init__(self, parent_a): self.parent_a = parent_a
and we have another class named Child that inherits from Parent. If the Child class takes an additional parameter as well as the original parent_aparameter, we would use super() to call the __init__ function of Parent inside Child:
class Child(Parent): def __init__(self, parent_a, child_b): super().__init__(parent_a) self.child_b = child_b
>>> child = Child('a', 'b') >>> print(child.parent_a) a >>> print(child.child_b) b
Make sure to use the Animal class's __init__ function to initialize the Dog object for the name and species member variables. In the Dog class, pass in the string "Dog" as the species parameter for the Animal's init function.
The Dog class will support a similar function: get_breed that simply returns the breed member variable of the Dog class. In addition, it will support one additional function: get_speed that returns an integer based on the breed of the Dog. If the breed contains retriever, then the function should return 6, if the breed contains labrador then return 4. In all other cases, the function should return 2.
class Dog(Animal): ''' Represents a Dog Attributes ---------- name: A string representing the Dog object's name breed: A string designating the Dog object's breed Methods ------- __init__: Initializes the Dog object. get_breed: returns breed attribute of Dog object get_speed: returns an integer based if the breed is a 'retriever', 'labrador' or any other breed ''' def __init__(self, breed, name): ''' Initializes the Dog class. Parameters ---------------- breed: A string representing the dog object's breed name: A string representing the dog object's name Attributes ---------- Dog.breed (str): set to `breed`. Dog.name (str): set to `name`. ''' # YOUR CODE HERE def get_breed(self): ''' Retrieves the breed attribute of the Dog object.
Returns ------- String. breed attribute. ''' # YOUR CODE HERE def get_speed(self): ''' Returns an integer representing speed based on follow conditions: 1) breed contains 'retriever' -> 6 2) breed contains 'labrador' -> 4 3) all other breeds -> 2
Returns ------- Integer ''' # YOUR CODE HERE
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started