Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

FreeBodyObjects.py 1 #Aecall in coding Problem 4.4.4 (and before that, in coding

ID: 3919762 • Letter: F

Question

FreeBodyObjects.py 1 #Aecall 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 71#3ystem into one that uses objects. 9#To start, create a class called Force. The constructor for 10 Force should have two required arguments: magnitude and 11#angle. These should be saved to two attributes called 12 #"magnitude' and 'angle'. You should assume angle is 13 #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 vertical component of 21 the force, according to the formula: 22 # vertical 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, it should return the angle in degrees; if it 27 # is false, it should return the angle in radians. 29 #HINT: Don't overcomplicate this. All we want here is 30 a class called Force with four methods init 31 #get horizontal, get vertical, and get angle. Note that 32 #these are not true "getters" even though they have "get" 33 Win their names: all three will have some reasoning #beyond just returning a single value 36 #HINT 2: angle will initially be passed into the 37 constructor in degrtnes. You may atore it in either 38 degrees or radians. Each approach has different benefits, 39 fbut make sure?o keep track of when it's dnnngles and 40 wwhen it's in degreea

Explanation / Answer

from math import sin, cos, atan2, radians, degrees, sqrt

class Force:

def __init__(self, magnitude, angle):

self.magnitude = magnitude

self.angle = angle

def get_horizontal(self):

return self.magnitude * cos(self.get_angle(use_degrees = False))

def get_vertical(self):

return self.magnitude * sin(self.get_angle(use_degrees = False))

def get_angle(self, use_degrees = True):

return self.angle if use_degrees else radians(self.angle)

a_force = Force(500, 60)

print("Magnitude:", a_force.magnitude)

print("Horizontal:", a_force.get_horizontal())

print("Vertical:", a_force.get_vertical())

print("Angle in Degrees:", a_force.get_angle())

print("Angle in Radians:", a_force.get_angle(use_degrees = False))

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote