FreeBodyObjects.py 1·Recall in Coding Problem 4.4.4 (and before that, in coding
ID: 3920499 • Letter: F
Question
FreeBodyObjects.py 1·Recall in Coding Problem 4.4.4 (and before that, in coding 2 Problem 4.3.9) you built a program for finding the net 3 force (magnitude and angle) on an object from several 4'#individual forces. 6|#In the next two exercises , we're going to convert that 7: #system into one that uses objects. 9|#70 start, create a class called Force. The constructor for 10 Force should have two required arguments: magnitude and 11 fangle. These should be saved to two attributes called 12| #'magnitude' and 'angle'. You should assume angle is 131 #initially in degrees , from-180 to 180. 15 #Then, add three methods to Force: 17 #-get-horizontal should return the horizontal component 18 of the force, according to the formula: 19 # horizontal-magnitude * cos (angle). 20#-get vertical should return the ve rtical component of 21 the force, according to the formula 22vertical magnitude sin(angle). 23 #-get-angle should return the angle of the force, but 24 should have a keyword parameter called use-degrees. 25 # use-degrees should default to True. If use, degrees 26- is true, itshould return the angle in degrees, if it 27 is false, it should return the angle in radians. 29 |NT: Don't overcomplicate this. All we want here is 30 a class called Force with four methodsnit 31 #get horizontal, get-vertical, and get, angle. Note that 32 these are not true.getters", even though they have "get- 33 in their names: all three will have some reasonin 34 #beyond just returning a single value. 36 1NT 2: angle will initially be passed into the 37 #constructor in degrees. You may store it in ather 38 #degrees or radians. Each approach has different benefits, 39 #but, make sure to keep t ac 40 when it's in degrees: when it's in angles andExplanation / Answer
ScreenShot
------------------------------------------------------------------------------------
Program
#Math package for operation
from math import sin,cos,radians,atan2,sqrt,degrees,pi
#Create a class force
class Force:
#Constructor
def __init__(self, magnitude,angle):
self.magnitude = magnitude
self.angle=angle
#Horizontal value return function
def get_Horizontal(self):
return (self.magnitude*cos(radians(self.angle)))
#Vertical value return function
def get_Vertical(self):
return (self.magnitude*sin(radians(self.angle)))
#Angle in degrees/Radiance return function
def get_Angle(self,**use_Degrees):
if use_Degrees:
return radians(self.angle)
else:
return self.angle
#Main method
def main():
#Class object creation
a_force=Force(500,60)
#call each method and display values
print("Magnitude: ",a_force.magnitude)
print("Angle: ",a_force.angle)
print("Horizontal: ",a_force.get_Horizontal())
print("Vertical: ",a_force.get_Vertical())
print("Angle in Degree: ",a_force.get_Angle())
print("Angle in Radiance: ",a_force.get_Angle(use_Degrees=False))
if __name__== "__main__":
main()
-------------------------------------------------------------------
Output
Magnitude: 500
Angle: 60
Horizontal: 250.00000000000006
Vertical: 433.0127018922193
Angle in Degree: 60
Angle in Radiance: 1.0471975511965976
Press any key to continue . . .
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.