Question: Problem 1 Create a Python project DriveCar. Add a Python file car.py to this project. Define a class Car in this file. In this class,

Problem 1

Create a Python project DriveCar. Add a Python file car.py to this project. Define a class Car in this file. In this class, create three publicly accessible instance variables to store the cars make, model, and speed. Also define the following methods:

A __init__ method that accepts cars make and model as arguments. Write statements in __init__ to store them in instance variables. Also create an instance variable for speed and initialize it to 0.

An accelerate method to increase the speed of the car by 5 mph. This method has no parameters and no return value.

A decelerate method to decrease the speed of the car by 5 mph. This method needs to ensure that the speed will never go below 0. It has no parameters and no return value.

The following class diagram shows the design of this class:

Car

+make: String

+model: String

+speed: Float

+create(car_name:String, car_model:String)

+accelerate()

+decelerate()

Add a file driveCar_main.py to this project. This is the main module. Write code to ask the user to enter model and make of a car. Create an instance of Car. Display the initial speed of the car. Write a loop to control the speed of the car. In the loop, ask the user to enter 1 for acceleration, 2 for deceleration or 3 for exit. Every time the speed of the car changes, display the speed.

The following shows a sample test run:

Enter car make: Toyota

Enter car model: Prius

Car speed: 0

Enter 1 for accelerate, 2 for decelerate, or 3 to exit: 2

Car speed: 0

Enter 1 for accelerate, 2 for decelerate, or 3 to exit: 1

Car speed: 5

Enter 1 for accelerate, 2 for decelerate, or 3 to exit: 1

Car speed: 10

Enter 1 for accelerate, 2 for decelerate, or 3 to exit: 1

Car speed: 15

Enter 1 for accelerate, 2 for decelerate, or 3 to exit: 2

Car speed: 10

Enter 1 for accelerate, 2 for decelerate, or 3 to exit: 2

Car speed: 5

Enter 1 for accelerate, 2 for decelerate, or 3 to exit: 3

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!