Question

In: Computer Science

Python: Using Inheritance to Create a Derived Class in Python In this lab, you create a...

Python: Using Inheritance to Create a Derived Class in Python

In this lab, you create a derived class from a base class, and then use the derived class in a Python program. The program should create two Motorcycle objects, and then set the Motorcycle’s speed, accelerate the Motorcycle object, and check its sidecar status.

Instructions

  1. Open the file named Motorcycle.py.
  2. Create the Motorcycle class by deriving it from the Vehicle class. Use a public derivation.
  3. In the Motorcycle class, create an attribute named sidecar.
  4. Write a public set method to set the value for sidecar.
  5. Write a public get method to retrieve the value of sidecar.
  6. Write a public accelerate method. This method overrides the accelerate method inherited from the Vehicle class. Change the message in the accelerate method so the following is displayed when the Motorcycle tries to accelerate beyond its maximum speed: "This motorcycle cannot go that fast".
  7. Open the file named MyMotorcycleClassProgram.py.
  8. In the MyMotorcycleClassProgram, there are two Motorcycle objects named motorcycleOne and motorcycleTwo.
  9. Set the sidecar value of motorcycleOne to true and the sidecar value of motorcycleTwo to false.
  10. Set motorcycleOne’s maximum speed to 90 and motorcycleTwo’s maximum speed to 85.
  11. Set motorcycleOne’s current speed to 65 and motorcycleTwo’s current speed to 60.
  12. Accelerate motorcycleOne by 30 mph, and accelerate motorcycleTwo by 20 mph.
  13. Print the current speed of motorcycleOne and motorcycleTwo.
  14. Determine if motorcycleOne and motorcycleTwo have sidecars. If yes, display the following: "This motorcycle has a sidecar". If not, display the following: "This motorcycle does not have a sidecar".
  15. Execute the program.

Solutions

Expert Solution

motorcycle.py:

#Vehicle is a parent class
class Vehicle:
def accelarate(self,a):
print(a)
#Motorcycle is child class inherits vehicle class
class Motorcycle(Vehicle):
def __init__(self):
self.maxSpeed=0
self.currentSpeed=0
self.sidecar=True
#setMethod is used to set sidecar value
def setMethod(self,x):
self.sidecar=x
#getMethod is used to retrieve sidecar value
def getMethod(self):
return self.sidecar
#accelerate method is used to set accceleration
def accelerate(self,a):
if (self.currentSpeed + a ) > self.maxSpeed:
print("This motorcycle cannot go that fast")
else:
self.currentSpeed += a
#This is the get method to find the current speed
def findSpeed(self):
return self.currentSpeed
#This method is used to check sidecar status
def checkSideCar(self):
if self.sidecar:
print("This motorcycle has a sidecar")
else:
print("This motorcycle does not have a sidecar")

MyMotorcycleClassProgram.py:

#Motorcycle is imported from motorcycle.py
from motorcycle import Motorcycle

#motorcycle objects are created
motorcycleOne=Motorcycle()
motorcycleTwo=Motorcycle()

#to set the sidecar value
motorcycleOne.setMethod(True)
motorcycleTwo.setMethod(False)

#to set the maximum speed of motorcycle
motorcycleOne.maxSpeed=90
motorcycleTwo.maxSpeed=85

#to set the speed of motorcycle
motorcycleOne.currentSpeed=65
motorcycleTwo.currentSpeed=60

#to set the acceleration of motorcycle
motorcycleOne.accelerate(30)
motorcycleTwo.accelerate(20)

print("The speed of motorcycleOne : ",motorcycleOne.findSpeed())
print("The speed of motorcycleTwo : ",motorcycleTwo.findSpeed())

motorcycleOne.checkSideCar()
motorcycleTwo.checkSideCar()

Output:

This motorcycle cannot go that fast
The speed of motorcycleOne : 65
The speed of motorcycleTwo : 80
This motorcycle has a sidecar
This motorcycle does not have a sidecar


Related Solutions

In c++, when dealing with inheritance in a class hierarchy, a derived class often has the...
In c++, when dealing with inheritance in a class hierarchy, a derived class often has the opportunity to overload or override an inherited member function. What is the difference? and which one is the better?
Create a derived class that allows you to reset the number of sides on a die....
Create a derived class that allows you to reset the number of sides on a die. (Hint: You will need to make one change in diceType.) -- Completed in Part 1 Overload the << and >> operators. -- Completed in Part 1 Overload the +, -, and ==, <, > operators for your derived class. Overload the = operator. I need help with 3 and 4 of the question. I've already completed step 1 and 2 Note: You should test...
This is using Python, it is utilizing code from a Fraction class to create a Binary...
This is using Python, it is utilizing code from a Fraction class to create a Binary Class Please leave comments so I may be able to learn from this. Instruction for Binary Class: Exercise 6.18: Design an immutable class BinaryNumber, in the style of our Fraction class. Internally, your only instance variable should be a text string that represents the binary value, of the form '1110100'. Implement a constructor that takes a string parameter that specifies the original binary value....
................................................ ................................................ This programming lab assignment requires that you create a class and use an equals...
................................................ ................................................ This programming lab assignment requires that you create a class and use an equals method to compare two or more objects. Your should use your QC5 as a reference. …………………………...…….. …………………………...……. Instructions LAB5 Instructions Using QC5 as a model, create a Rectangle class and a CompareUsingequalsMethod class that uses an   equals Method to determine if two rectangles are equal if and only if their areas are equal. The Rectangle class should have two instance variables length and width....
In python please Problem Create two server objects using the class create a function, biggest_server_object_sum, outside...
In python please Problem Create two server objects using the class create a function, biggest_server_object_sum, outside of the ServerClass class, that: 1. Takes the IP 2. Sums the octets of the IP together (example 127.0.0.1 = 127+0+0+1 = 128) 3. Returns the Server object with the larger sum Example: server_one = ServerClass("127.0.0.1") server_two = ServerClass("192.168.0.1") result = biggest_ip_sum(server_one, server_two) print(result) Hint Modify get_server_ip in the previous problem. -------------------- Please use this code to start class ServerClass: """ Server class for...
i just need the Polygon class Shapes In this lab you will create an interface, and...
i just need the Polygon class Shapes In this lab you will create an interface, and then implement that interface in three derived classes. Your interface will be called Shape, and will define the following functions: Return Type Name Parameters Description double area none computes the area of the shape double perimeter none computes the perimeter of the shape Point2d center none computes the center of the shape You will then create 3 implementations of this interface: Rectangle, Circle, and...
Write a python program using the following requirements: Create a class called Sentence which has a...
Write a python program using the following requirements: Create a class called Sentence which has a constructor that takes a sentence string as input. The default value for the constructor should be an empty string The sentence must be a private attribute in the class contains the following class methods: get_all_words — Returns all the words in the sentence as a list get_word — Returns only the word at a particular index in the sentence Arguments: index set_word — Changes...
Please answer using python 3 and def functions! Lab 2 Drill 3: (function practice) create and...
Please answer using python 3 and def functions! Lab 2 Drill 3: (function practice) create and use a function named highest() that takes three inputs and returns the highest number. After you have got it working, try calling the function with inputs ‘hat’, ‘cat’, ‘rat’.
In C++ Demonstrate inheritance. Create an Airplane class with the following attributes: • manufacturer : string...
In C++ Demonstrate inheritance. Create an Airplane class with the following attributes: • manufacturer : string • speed : float Create a FigherPlane class that inherits from the Airplane class and adds the following attributes: • numberOfMissiles : short
Objectives: Use class inheritance to create new classes. Separate class definition and implementation in different files....
Objectives: Use class inheritance to create new classes. Separate class definition and implementation in different files. Use include guard in class header files to avoid multiple inclusion of a header. Tasks: In our lecture, we wrote a program that defines and implements a class Rectangle. The source code can be found on Blackboard > Course Content > Classes and Objects > Demo Program 2: class Rectangle in three files. In this lab, we will use class inheritance to write a...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT