Tell us what’s happening:
What am i missing here? The results of the test are looking good:
The method should return the correct value. For Projectile(12, 12, 12), given the 4 $x$ coordinates of 0, 1, 2 and 3, the y coordinate should be approximately 12, 12.18, 12.28, 12.32.
Your code so far
import math
GRAVITATIONAL_ACCELERATION = 9.81
PROJECTILE = "∙"
x_axis_tick = "T"
y_axis_tick = "⊣"
class Projectile:
__slots__ = ('__speed', '__height', '__angle')
def __init__(self, speed, height, angle):
self.__speed = speed
self.__height = height
self.__angle = math.radians(angle)
def __str__(self):
return f'''
Projectile details:
speed: {self.__speed} m/s
height: {self.__height} m
angle: {round(math.degrees(self.__angle))}°
displacement: {round(self.__calculate_displacement(), 1)} m
'''
def __calculate_displacement(self):
horizontal_component = self.__speed * math.cos(self.__angle)
vertical_component = self.__speed * math.sin(self.__angle)
squared_component = vertical_component**2
gh_component = 2 * GRAVITATIONAL_ACCELERATION * self.__height
sqrt_component = math.sqrt(squared_component + gh_component)
return horizontal_component * (vertical_component + sqrt_component) / GRAVITATIONAL_ACCELERATION
# User Editable Region
def __calculate_y_coordinate(self, x):
y = self.__height + x * math.tan(self.__angle) - ((GRAVITATIONAL_ACCELERATION * x**2) / (2 * self.__speed**2 * math.cos(self.__angle)))
return y
# User Editable Region
ball = Projectile(12, 12, 12)
print(ball)
print(ball._Projectile__calculate_y_coordinate(2))
Your browser information:
User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.6 Safari/605.1.15
Challenge Information:
Learn Encapsulation by Building a Projectile Trajectory Calculator - Step 6