Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Using Inheritance to Create a Derived Class Summary: In this lab, you create a derived class from a base class, and then use the derived

Using Inheritance to Create a Derived Class

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 Motorcycles speed, accelerate the Motorcycle object, and check its sidecar status.

Instructions:

Open the file named Motorcycle.py.

Create the Motorcycle class by deriving it from the Vehicle class. Use a public derivation.

In the Motorcycle class, create an attribute named sidecar.

Write a public set method to set the value for sidecar.

Write a public get method to retrieve the value of sidecar.

Write a public acceleratemethod. This method overrides the acceleratemethod inherited from the Vehicle class. Change the message in the acceleratemethod so the following is displayed when the Motorcycle tries to accelerate beyond its maximum speed: "This motorcycle cannot go that fast".

Open the file named MyMotorcycleClassProgram.py.

In the MyMotorcycleClassProgram, there are two Motorcycleobjects named motorcycleOne and motorcycleTwo.

Set the sidecar value of motorcycleOne to true and the sidecar value of motorcycleTwo to false.

Set motorcycleOnes maximum speed to 90 and motorcycleTwos maximum speed to 85.

Set motorcycleOnes current speed to 65 and motorcycleTwos current speed to 60.

Accelerate motorcycleOne by 30 mph, and accelerate motorcycleTwo by 20 mph.

Print the current speed of motorcycleOne and motorcycleTwo.

Determine if motorcycleOneand 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".

Execute the program.

Assignment (Motorcycle.py):

First tab, "Motorcycle.py", is blank

Under second tab, "MyMotorcycleClassProgram.py", here's the code:

# 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.sidecar: print("This motorcycle has a sidecar") else: print("This motorcycle does not have a sidecar")

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

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions

Question

6. Have you used solid reasoning in your argument?

Answered: 1 week ago