Skip to content

Commit 4951fb9

Browse files
Priyanshu1303dPriyanshu1303d
andauthored
[FEAT] Add Kinematics (SUVAT) equations (#7053)
[FEAT] Add SUVAT equation for motion Co-authored-by: Priyanshu1303d <priyanshu130d@gmail.com>
1 parent 5a68ba9 commit 4951fb9

File tree

2 files changed

+117
-0
lines changed

2 files changed

+117
-0
lines changed
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package com.thealgorithms.physics;
2+
/**
3+
* Implements the fundamental "SUVAT" equations for motion
4+
* under constant acceleration.
5+
*
6+
* @author [Priyanshu Kumar Singh](https://github.com/Priyanshu1303d)
7+
* @see <a href="https://en.wikipedia.org/wiki/Equations_of_motion#Uniform_acceleration">Wikipedia</a>
8+
*/
9+
public final class Kinematics {
10+
private Kinematics() {
11+
}
12+
13+
/**
14+
* Calculates the final velocity (v) of an object.
15+
* Formula: v = u + at
16+
*
17+
* @param u Initial velocity (m/s).
18+
* @param a Constant acceleration (m/s^2).
19+
* @param t Time elapsed (s).
20+
* @return The final velocity (m/s).
21+
*/
22+
23+
public static double calculateFinalVelocity(double u, double a, double t) {
24+
return u + a * t;
25+
}
26+
27+
/**
28+
* Calculates the displacement (s) of an object.
29+
* Formula: s = ut + 0.5 * a * t^2
30+
*
31+
* @param u Initial velocity (m/s).
32+
* @param a Constant acceleration (m/s^2).
33+
* @param t Time elapsed (s).
34+
* @return The displacement (m).
35+
*/
36+
37+
public static double calculateDisplacement(double u, double a, double t) {
38+
return u * t + 0.5 * a * t * t;
39+
}
40+
41+
/**
42+
* Calculates the displacement (s) of an object.
43+
* Formula: v^2 = u^2 + 2 * a * s
44+
*
45+
* @param u Initial velocity (m/s).
46+
* @param a Constant acceleration (m/s^2).
47+
* @param s Displacement (m).
48+
* @return The final velocity squared (m/s)^2.
49+
*/
50+
51+
public static double calculateFinalVelocitySquared(double u, double a, double s) {
52+
return u * u + 2 * a * s;
53+
}
54+
55+
/**
56+
* Calculates the displacement (s) using the average velocity.
57+
* Formula: s = (u + v) / 2 * t
58+
*
59+
* @param u Initial velocity (m/s).
60+
* @param v Final velocity (m/s).
61+
* @param t Time elapsed (s).
62+
* @return The displacement (m).
63+
*/
64+
65+
public static double calculateDisplacementFromVelocities(double u, double v, double t) {
66+
double velocitySum = u + v;
67+
return velocitySum / 2 * t;
68+
}
69+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package com.thealgorithms.physics;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
5+
import org.junit.jupiter.api.DisplayName;
6+
import org.junit.jupiter.api.Test;
7+
8+
/**
9+
* Unit tests for the Kinematics utility class.
10+
*/
11+
12+
public final class KinematicsTest {
13+
// A small tolerance for comparing floating-point numbers
14+
private static final double DELTA = 1e-9;
15+
@Test
16+
@DisplayName("Test final velocity: v = u + at")
17+
void testCalculateFinalVelocity() {
18+
assertEquals(20.0, Kinematics.calculateFinalVelocity(10.0, 2.0, 5.0), DELTA);
19+
}
20+
21+
@Test
22+
@DisplayName("Test displacement: s = ut + 0.5at^2")
23+
void testCalculateDisplacement() {
24+
assertEquals(75.0, Kinematics.calculateDisplacement(10.0, 2.0, 5.0), DELTA);
25+
}
26+
27+
@Test
28+
@DisplayName("Test final velocity squared: v^2 = u^2 + 2as")
29+
void testCalculateFinalVelocitySquared() {
30+
assertEquals(400.0, Kinematics.calculateFinalVelocitySquared(10.0, 2.0, 75.0), DELTA);
31+
}
32+
33+
@Test
34+
@DisplayName("Test displacement from average velocity: s = (u+v)/2 * t")
35+
void testCalculateDisplacementFromVelocities() {
36+
assertEquals(75.0, Kinematics.calculateDisplacementFromVelocities(10.0, 20.0, 5.0), DELTA);
37+
}
38+
39+
@Test
40+
@DisplayName("Test with negative acceleration (deceleration)")
41+
void testDeceleration() {
42+
assertEquals(10.0, Kinematics.calculateFinalVelocity(30.0, -4.0, 5.0), DELTA);
43+
assertEquals(100.0, Kinematics.calculateDisplacement(30.0, -4.0, 5.0), DELTA);
44+
45+
assertEquals(100.0, Kinematics.calculateFinalVelocitySquared(30.0, -4.0, 100.0), DELTA);
46+
assertEquals(100.0, Kinematics.calculateDisplacementFromVelocities(30.0, 10.0, 5.0), DELTA);
47+
}
48+
}

0 commit comments

Comments
 (0)