Create a PyCharm Python project named fly_drone . Add a Python file called drone
ID: 3917241 • Letter: C
Question
Create a PyCharm Python project named fly_drone. Add a Python file called drone.py to this project. Define a class called Drone in this file. In this class, create two publicly accessible instance variables to store the drone’s speed and height. Also define the following five methods:
__init__: constructor method that creates two instance variables that store the speed and height of the drone. Initialize them to 0.0. This method has no parameters other than self.
accelerate: method to increase the speed of the drone by 10. This method has no parameters other than self and no return value.
decelerate: method to decrease the speed of the drone by 10. The new speed cannot be negative. It has no parameters other than self and no return value.
ascend: method to increase the height of the drone by 10. This method has no parameters other than self and no return value.
descend: method to increase the height of the drone by 10. The new height cannot be negative. This method has no parameters other than self and no return value.
The following class diagram shows the design of this class:
Drone
+speed: Float
+height: Float
+create ()
+accelerate()
+decelerate()
+ascend()
+descend()
Add a file named fly_drone_main.py to this project. This is the main module. In this main module, create an instance of Drone. Write a loop to control the speed and height of the drone. In the loop, ask the user to enter 1 for acceleration, 2 for deceleration, 3 for ascending, 4 for descending, or 0 for exit. Call the appropriate method of the Drone object to change the speed or height of the drone. Every time the speed or height of the drone changes, display the speed and height.
The following shows a sample test run:
Enter 1 for accelerate, 2 for decelerate, 3 for ascend, 4 for descend, 0 for exit: 1
Speed: 10 Height: 0
Enter 1 for accelerate, 2 for decelerate, 3 for ascend, 4 for descend, 0 for exit: 1
Speed: 20 Height: 0
Enter 1 for accelerate, 2 for decelerate, 3 for ascend, 4 for descend, 0 for exit: 3
Speed: 20 Height: 10
Enter 1 for accelerate, 2 for decelerate, 3 for ascend, 4 for descend, 0 for exit: 3
Speed: 20 Height: 20
Enter 1 for accelerate, 2 for decelerate, 3 for ascend, 4 for descend, 0 for exit: 3
Speed: 20 Height: 30
Enter 1 for accelerate, 2 for decelerate, 3 for ascend, 4 for descend, 0 for exit: 2
Speed: 10 Height: 30
Enter 1 for accelerate, 2 for decelerate, 3 for ascend, 4 for descend, 0 for exit: 0
Speed: 10 Height: 30
Drone
+speed: Float
+height: Float
+create ()
+accelerate()
+decelerate()
+ascend()
+descend()
Explanation / Answer
Solution:
#######drone.py######
######fly_drone_main.py######
Sample Run:
Enter 1 for accelerate, 2 for decelerate, 3 for ascend, 4 for descend, 0 for exit:1
Speed: 10.0 Height: 0.0
Enter 1 for accelerate, 2 for decelerate, 3 for ascend, 4 for descend, 0 for exit:1
Speed: 20.0 Height: 0.0
Enter 1 for accelerate, 2 for decelerate, 3 for ascend, 4 for descend, 0 for exit:3
Speed: 20.0 Height: 10.0
Enter 1 for accelerate, 2 for decelerate, 3 for ascend, 4 for descend, 0 for exit:3
Speed: 20.0 Height: 20.0
Enter 1 for accelerate, 2 for decelerate, 3 for ascend, 4 for descend, 0 for exit:3
Speed: 20.0 Height: 30.0
Enter 1 for accelerate, 2 for decelerate, 3 for ascend, 4 for descend, 0 for exit:2
Speed: 10.0 Height: 30.0
Enter 1 for accelerate, 2 for decelerate, 3 for ascend, 4 for descend, 0 for exit:0
Speed: 10.0 Height: 30.0
Note: If you liked the solution then don't forget to give a big thumbs up. If you have any doubt then do comment below.