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 sideca

Microsoft Visual C#
7th Edition
ISBN:9781337102100
Author:Joyce, Farrell.
Publisher:Joyce, Farrell.
Chapter10: Introduction To Inheritance
Section: Chapter Questions
Problem 2CP
icon
Related questions
Question

Using Inheritance to Create a Derived Class in Python

Summary

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. Call the parent class __init()__ method inside the Motorcycle class's __init()__ method.
  3. In theMotorcycle 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.

First File: Motorcycle.py.

from Vehicle import Vehicle

# Write the Motorcycle class here
 

Second File: MyMotorcycleClassProgram.py.

# This program uses the programmer-defined Motorcycle class.

# Do NOT edit this file. Write your code in Motorcycle.py,
# then open this file and click "Run Code".

from Motorcycle import Motorcycle

motorcycleOne = Motorcycle(90.0, 65.0, True)
motorcycleTwo = Motorcycle(85.0, 60.0, False)

motorcycleOne.accelerate(30.0)
motorcycleTwo.accelerate(20.0)

print("The current speed of motorcycleOne is " + str(motorcycleOne.speed))
print("The current speed of motorcycleTwo is " + str(motorcycleTwo.speed))

if motorcycleOne.get_sidecar():
   print("This motorcycle has a sidecar")
else:
   print("This motorcycle does not have a sidecar")

if motorcycleTwo.get_sidecar():
   print("This motorcycle has a sidecar")
else:
   print("This motorcycle does not have a sidecar")

Third File: Vehicle.py

class Vehicle: 
    def __init__(self):
        self._fuel_capacity = 0
        self._max_speed = 0
        self._current_speed = 0 

    def set_speed(self, speed):
        self._current_speed = speed

    def get_speed(self):
        return self._current_speed

    def accelerate(self, mph):
        if self._current_speed + mph < max_speed:
            self._current_speed = self._current_speed + mph
        else: 
            print("This vehicle cannot go that fast.")

    def set_fuel_capacity(self, fuel):
        self._fuel_capacity = fuel

    def get_fuel_capacity(self):
        return self._fuel_capacity

    def set_max_speed(self, max):
        self._max_speed = max 

    def get_max_speed(self):
        return self._max_speed
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 2 steps with 2 images

Blurred answer
Knowledge Booster
Class
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Microsoft Visual C#
Microsoft Visual C#
Computer Science
ISBN:
9781337102100
Author:
Joyce, Farrell.
Publisher:
Cengage Learning,
C++ Programming: From Problem Analysis to Program…
C++ Programming: From Problem Analysis to Program…
Computer Science
ISBN:
9781337102087
Author:
D. S. Malik
Publisher:
Cengage Learning
EBK JAVA PROGRAMMING
EBK JAVA PROGRAMMING
Computer Science
ISBN:
9781337671385
Author:
FARRELL
Publisher:
CENGAGE LEARNING - CONSIGNMENT