6 kyu

Projectile Motion

Description:

Task

You must create a class, Projectile, which takes in 3 arguments when initialized:

  • starting height (0 ≤ h0 < 200)
  • starting velocity (0 < v0 < 200)
  • angle of the projectile when it is released (0° < a < 90°, measured in degrees).

All units for distance are feet, and units for time are seconds.

Note: Some solutions were invalidated because I added tests for situations where the starting height is 0, in which case the equation for height would be in the form h(t) = -16.0t^2 + vt where v represents the initial vertical velocity.

h=16t2+vt+h0\large h = -16t^2 + vt + h_0

In the above equation, h represents the height of the projectile after t seconds; v represents the initial vertical velocity; and h0 represents the starting height.

You need to write the following methods for the Projectile class. In Crystal, the arguments passed when the object is initialized will always be of the type Float64, and in Java/Scala/Kotlin/Dart/C#, they will be int/Ints.

  1. A height_eq, heightEq, or HeightEq method, which returns the equation for height of the projectile as a function of time. [takes in nothing, returns a String]
  2. A horiz_eq, horizEq, or HeightEq method, which returns the equation for the horizontal position of the projectile as a function of time. [takes in nothing, returns a String]
  3. A height or Height method, which takes in an argument t and calculates the height of the projectile in feet. [takes in a double, returns a double]
  4. A horiz or Horiz method, which also takes in an argument t and calculates the horizontal distance that the projectile has traveled. [takes in a double, returns a double]
  5. A landing or Landing method which returns the point at which the projectile lands on the ground, in the form [x, y, t]. (y should always be 0). [takes in nothing, returns an array of doubles]

EVERYTHING, including values in the equations appearing as coefficients, must be rounded to THREE decimal places. However, if the value is whole, only show one decimal place (for example => -16 becomes -16.0, not -16.000). But ensure that you DO NOT use the three-decimal-place rounded values for calculations. Otherwise, you will find yourself getting answers CLOSE to the correct one but slightly off.

You also need to define instance variables as needed. These will not be tested.

Examples

Projectile Motion Example This example shows the initial vertical and horizontal velocity when a projectile is fired at 2 ft/s.

p = Projectile.new(5, 2, 45) #=> a projectile starting at 5 ft above the ground, traveling initially at 2 ft/s, and at an angle of 45 degrees with the horizontal (shown in the triangle above)
p.height_eq #=> "h(t) = -16.0t^2 + 1.414t + 5.0"
  # 1.414 = 2sin(45°)
p.horiz_eq #=> "x(t) = 1.414t"
  # 1.414 = 2cos(45°)
p.height(0.2) #=> 4.643 (Calculation: -16(0.2)^2 + (2sin(45°))(0.2) + 5)
p.horiz(0.2) #=> 0.283 (Calculation: 2cos(45°) * 0.2)
p.landing() #=> [0.856, 0, 0.605] (After 0.605 seconds (t = 0.605), the particle has landed on the ground (y = 0), and is 0.856 ft horizontally (x = 0.856) away from the release point.)
p = Projectile.new(5.0, 2.0, 45.0) #=> a projectile starting at 5 ft above the ground, traveling initially at 2 ft/s, and at an angle of 45 degrees with the horizontal (shown in the triangle above)
p.height_eq #=> "h(t) = -16.0t^2 + 1.414t + 5.0"
  # 1.414 = 2sin(45°)
p.horiz_eq #=> "x(t) = 1.414t"
  # 1.414 = 2cos(45°)
p.height(0.2) #=> 4.643 (Calculation: -16(0.2)^2 + (2sin(45°))(0.2) + 5)
p.horiz(0.2) #=> 0.283 (Calculation: 2cos(45°) * 0.2)
p.landing() #=> [0.856, 0, 0.605] (After 0.605 seconds (t = 0.605), the particle has landed on the ground (y = 0), and is 0.856 ft horizontally (x = 0.856) away from the release point.)
p = Projectile(5, 2, 45) #=> a projectile starting at 5 ft above the ground, traveling initially at 2 ft/s, and at an angle of 45 degrees with the horizontal (shown in the triangle above)
p.height_eq() #=> "h(t) = -16.0t^2 + 1.414t + 5.0"
  # 1.414 = 2sin(45°)
p.horiz_eq() #=> "x(t) = 1.414t"
  # 1.414 = 2cos(45°)
p.height(0.2) #=> 4.643 (Calculation: -16(0.2)^2 + (2sin(45°))(0.2) + 5)
p.horiz(0.2) #=> 0.283 (Calculation: 2cos(45°) * 0.2)
p.landing() #=> [0.856, 0, 0.605] (After 0.605 seconds (t = 0.605), the particle has landed on the ground (y = 0), and is 0.856 ft horizontally (x = 0.856) away from the release point.)
p = new Projectile(5, 2, 45) //=> a projectile starting at 5 ft above the ground, traveling initially at 2 ft/s, and at an angle of 45 degrees with the horizontal (shown in the triangle above)
p.heightEq() //=> "h(t) = -16.0t^2 + 1.414t + 5.0"
  # 1.414 = 2sin(45°)
p.horizEq() //=> "x(t) = 1.414t"
  # 1.414 = 2cos(45°)
p.height(0.2) //=> 4.643 (Calculation: -16(0.2)^2 + (2sin(45°))(0.2) + 5)
p.horiz(0.2) //=> 0.283 (Calculation: 2cos(45°) * 0.2)
p.landing() //=> [0.856, 0, 0.605] (After 0.605 seconds (t = 0.605), the particle has landed on the ground (y = 0), and is 0.856 ft horizontally (x = 0.856) away from the release point.)
p = new Projectile(5, 2, 45) #=> a projectile starting at 5 ft above the ground, traveling initially at 2 ft/s, and at an angle of 45 degrees with the horizontal (shown in the triangle above)
p.heightEq() #=> "h(t) = -16.0t^2 + 1.414t + 5.0"
  # 1.414 = 2sin(45°)
p.horizEq() #=> "x(t) = 1.414t"
  # 1.414 = 2cos(45°)
p.height(0.2) #=> 4.643 (Calculation: -16(0.2)^2 + (2sin(45°))(0.2) + 5)
p.horiz(0.2) #=> 0.283 (Calculation: 2cos(45°) * 0.2)
p.landing() #=> [0.856, 0, 0.605] (After 0.605 seconds (t = 0.605), the particle has landed on the ground (y = 0), and is 0.856 ft horizontally (x = 0.856) away from the release point.)
Projectile p = new Projectile(5, 2, 45); //=> a projectile starting at 5 ft above the ground, traveling initially at 2 ft/s, and at an angle of 45 degrees with the horizontal (shown in the triangle above)
p.heightEq(); //=> "h(t) = -16.0t^2 + 1.414t + 5.0"
  # 1.414 = 2sin(45°)
p.horizEq(); //=> "x(t) = 1.414t"
  # 1.414 = 2cos(45°)
p.height(0.2); //=> 4.643 (Calculation: -16(0.2)^2 + (2sin(45°))(0.2) + 5)
p.horiz(0.2); //=> 0.283 (Calculation: 2cos(45°) * 0.2)
p.landing(); //=> double[]{ 0.856, 0, 0.605 } (After 0.605 seconds (t = 0.605), the particle has landed on the ground (y = 0), and is 0.856 ft horizontally (x = 0.856) away from the release point.)
var p = new Projectile(5, 2, 45); //=> a projectile starting at 5 ft above the ground, traveling initially at 2 ft/s, and at an angle of 45 degrees with the horizontal (shown in the triangle above)
p.heightEq(); //=> "h(t) = -16.0t^2 + 1.414t + 5.0"
  # 1.414 = 2sin(45°)
p.horizEq(); //=> "x(t) = 1.414t"
  # 1.414 = 2cos(45°)
p.height(0.2); //=> 4.643 (Calculation: -16(0.2)^2 + (2sin(45°))(0.2) + 5)
p.horiz(0.2); //=> 0.283 (Calculation: 2cos(45°) * 0.2)
p.landing(); //=> [0.856, 0, 0.605] (After 0.605 seconds (t = 0.605), the particle has landed on the ground (y = 0), and is 0.856 ft horizontally (x = 0.856) away from the release point.)
var p = new Projectile(5, 2, 45); #=> a projectile starting at 5 ft above the ground, traveling initially at 2 ft/s, and at an angle of 45 degrees with the horizontal (shown in the triangle above)
p.heightEq(); //=> "h(t) = -16.0t^2 + 1.414t + 5.0"
  # 1.414 = 2sin(45°)
p.horizEq(); //=> "x(t) = 1.414t"
  # 1.414 = 2cos(45°)
p.height(0.2); //=> 4.643 (Calculation: -16(0.2)^2 + (2sin(45°))(0.2) + 5)
p.horiz(0.2); //=> 0.283 (Calculation: 2cos(45°) * 0.2)
p.landing(); //=> Array(0.856, 0, 0.605) (After 0.605 seconds (t = 0.605), the particle has landed on the ground (y = 0), and is 0.856 ft horizontally (x = 0.856) away from the release point.)
var p = Projectile(5, 2, 45); //=> a projectile starting at 5 ft above the ground, traveling initially at 2 ft/s, and at an angle of 45 degrees with the horizontal (shown in the triangle above)
p.heightEq(); //=> "h(t) = -16.0t^2 + 1.414t + 5.0"
  # 1.414 = 2sin(45°)
p.horizEq(); //=> "x(t) = 1.414t"
  # 1.414 = 2cos(45°)
p.height(0.2); //=> 4.643 (Calculation: -16(0.2)^2 + (2sin(45°))(0.2) + 5)
p.horiz(0.2); //=> 0.283 (Calculation: 2cos(45°) * 0.2)
p.landing(); //=> doubleArrayOf(0.856, 0, 0.605) (After 0.605 seconds (t = 0.605), the particle has landed on the ground (y = 0), and is 0.856 ft horizontally (x = 0.856) away from the release point.)
Projectile p = new Projectile(5, 2, 45); //=> a projectile starting at 5 ft above the ground, traveling initially at 2 ft/s, and at an angle of 45 degrees with the horizontal (shown in the triangle above)
p.heightEq(); //=> "h(t) = -16.0t^2 + 1.414t + 5.0"
  # 1.414 = 2sin(45°)
p.horizEq(); //=> "x(t) = 1.414t"
  # 1.414 = 2cos(45°)
p.height(0.2); //=> 4.643 (Calculation: -16(0.2)^2 + (2sin(45°))(0.2) + 5)
p.horiz(0.2); //=> 0.283 (Calculation: 2cos(45°) * 0.2)
p.landing(); //=> [0.856, 0, 0.605] (After 0.605 seconds (t = 0.605), the particle has landed on the ground (y = 0), and is 0.856 ft horizontally (x = 0.856) away from the release point.)
Projectile p = Projectile(h: 5, v0: 2, a: 45) //=> a projectile starting at 5 ft above the ground, traveling initially at 2 ft/s, and at an angle of 45 degrees with the horizontal (shown in the triangle above)
p.heightEq() //=> "h(t) = -16.0t^2 + 1.414t + 5.0"
  # 1.414 = 2sin(45°)
p.horizEq() //=> "x(t) = 1.414t"
  # 1.414 = 2cos(45°)
p.height(time: 0.2) //=> 4.643 (Calculation: -16(0.2)^2 + (2sin(45°))(0.2) + 5)
p.horiz(time: 0.2) //=> 0.283 (Calculation: 2cos(45°) * 0.2)
p.landing() //=> [0.856, 0, 0.605] (After 0.605 seconds (t = 0.605), the particle has landed on the ground (y = 0), and is 0.856 ft horizontally (x = 0.856) away from the release point.)
Projectile p = new Projectile(5, 2, 45) //=> a projectile starting at 5 ft above the ground, traveling initially at 2 ft/s, and at an angle of 45 degrees with the horizontal (shown in the triangle above)
p.heightEq() //=> "h(t) = -16.0t^2 + 1.414t + 5.0"
  # 1.414 = 2sin(45°)
p.horizEq() //=> "x(t) = 1.414t"
  # 1.414 = 2cos(45°)
p.height(0.2) //=> 4.643 (Calculation: -16(0.2)^2 + (2sin(45°))(0.2) + 5)
p.horiz(0.2) //=> 0.283 (Calculation: 2cos(45°) * 0.2)
p.landing() //=> [ 0.856, 0, 0.605 ] (After 0.605 seconds (t = 0.605), the particle has landed on the ground (y = 0), and is 0.856 ft horizontally (x = 0.856) away from the release point.)
Projectile p = new Projectile(5, 2, 45); //=> a projectile starting at 5 ft above the ground, traveling initially at 2 ft/s, and at an angle of 45 degrees with the horizontal (shown in the triangle above)
p.HeightEq(); //=> "h(t) = -16.0t^2 + 1.414t + 5.0"
  # 1.414 = 2sin(45°)
p.HorizEq(); //=> "x(t) = 1.414t"
  # 1.414 = 2cos(45°)
p.Height(0.2); //=> 4.643 (Calculation: -16(0.2)^2 + (2sin(45°))(0.2) + 5)
p.Horiz(0.2); //=> 0.283 (Calculation: 2cos(45°) * 0.2)
p.Landing(); //=> [0.856, 0, 0.605] (After 0.605 seconds (t = 0.605), the particle has landed on the ground (y = 0), and is 0.856 ft horizontally (x = 0.856) away from the release point.)
let p = Projectile::new(5, 2, 45) //=> a projectile starting at 5 ft above the ground, traveling initially at 2 ft/s, and at an angle of 45 degrees with the horizontal (shown in the triangle above)
p.height_eq() //=> "h(t) = -16.0t^2 + 1.414t + 5.0"
  # 1.414 = 2sin(45°)
p.horiz_eq() //=> "x(t) = 1.414t"
  # 1.414 = 2cos(45°)
p.height(0.2) //=> 4.643 (Calculation: -16(0.2)^2 + (2sin(45°))(0.2) + 5)
p.horiz(0.2) //=> 0.283 (Calculation: 2cos(45°) * 0.2)
p.landing() //=> [0.856, 0.0, 0.605] (After 0.605 seconds (t = 0.605), the particle has landed on the ground (y = 0.0), and is 0.856 ft horizontally (x = 0.856) away from the release point.)

Additionally, note that all coefficients are to be expressed as floats in the string equations, regardless of whether or not they are whole. This means that whole numbers should always be formatted with a ".0" appended.

Fundamentals

Similar Kata:

Stats:

CreatedMay 14, 2018
PublishedMay 15, 2018
Warriors Trained2290
Total Skips446
Total Code Submissions3905
Total Times Completed358
Ruby Completions23
Crystal Completions3
Python Completions132
JavaScript Completions67
CoffeeScript Completions7
Java Completions36
Scala Completions18
TypeScript Completions15
Kotlin Completions20
Dart Completions41
Swift Completions22
Groovy Completions6
C# Completions18
Rust Completions21
Total Stars52
% of votes with a positive feedback rating75% of 99
Total "Very Satisfied" Votes61
Total "Somewhat Satisfied" Votes27
Total "Not Satisfied" Votes11
Total Rank Assessments4
Average Assessed Rank
6 kyu
Highest Assessed Rank
5 kyu
Lowest Assessed Rank
7 kyu
Ad
Contributors
  • narayanswa30663 Avatar
  • raulbc777 Avatar
  • imjasonmiller Avatar
  • kazk Avatar
  • Blind4Basics Avatar
  • user9644768 Avatar
  • hobovsky Avatar
Ad