From 51055bca86795822d62f34b17a561f8b2fc94b5e Mon Sep 17 00:00:00 2001 From: Priyanshu1303d Date: Thu, 6 Nov 2025 20:08:43 +0530 Subject: [PATCH] [FEAT] Add SUVAT equation for motion --- .../com/thealgorithms/physics/Kinematics.java | 69 +++++++++++++++++++ .../thealgorithms/physics/KinematicsTest.java | 48 +++++++++++++ 2 files changed, 117 insertions(+) create mode 100644 src/main/java/com/thealgorithms/physics/Kinematics.java create mode 100644 src/test/java/com/thealgorithms/physics/KinematicsTest.java diff --git a/src/main/java/com/thealgorithms/physics/Kinematics.java b/src/main/java/com/thealgorithms/physics/Kinematics.java new file mode 100644 index 000000000000..d017fe787afd --- /dev/null +++ b/src/main/java/com/thealgorithms/physics/Kinematics.java @@ -0,0 +1,69 @@ +package com.thealgorithms.physics; +/** + * Implements the fundamental "SUVAT" equations for motion + * under constant acceleration. + * + * @author [Priyanshu Kumar Singh](https://github.com/Priyanshu1303d) + * @see Wikipedia + */ +public final class Kinematics { + private Kinematics() { + } + + /** + * Calculates the final velocity (v) of an object. + * Formula: v = u + at + * + * @param u Initial velocity (m/s). + * @param a Constant acceleration (m/s^2). + * @param t Time elapsed (s). + * @return The final velocity (m/s). + */ + + public static double calculateFinalVelocity(double u, double a, double t) { + return u + a * t; + } + + /** + * Calculates the displacement (s) of an object. + * Formula: s = ut + 0.5 * a * t^2 + * + * @param u Initial velocity (m/s). + * @param a Constant acceleration (m/s^2). + * @param t Time elapsed (s). + * @return The displacement (m). + */ + + public static double calculateDisplacement(double u, double a, double t) { + return u * t + 0.5 * a * t * t; + } + + /** + * Calculates the displacement (s) of an object. + * Formula: v^2 = u^2 + 2 * a * s + * + * @param u Initial velocity (m/s). + * @param a Constant acceleration (m/s^2). + * @param s Displacement (m). + * @return The final velocity squared (m/s)^2. + */ + + public static double calculateFinalVelocitySquared(double u, double a, double s) { + return u * u + 2 * a * s; + } + + /** + * Calculates the displacement (s) using the average velocity. + * Formula: s = (u + v) / 2 * t + * + * @param u Initial velocity (m/s). + * @param v Final velocity (m/s). + * @param t Time elapsed (s). + * @return The displacement (m). + */ + + public static double calculateDisplacementFromVelocities(double u, double v, double t) { + double velocitySum = u + v; + return velocitySum / 2 * t; + } +} diff --git a/src/test/java/com/thealgorithms/physics/KinematicsTest.java b/src/test/java/com/thealgorithms/physics/KinematicsTest.java new file mode 100644 index 000000000000..c5274b0814a7 --- /dev/null +++ b/src/test/java/com/thealgorithms/physics/KinematicsTest.java @@ -0,0 +1,48 @@ +package com.thealgorithms.physics; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; + +/** + * Unit tests for the Kinematics utility class. + */ + +public final class KinematicsTest { + // A small tolerance for comparing floating-point numbers + private static final double DELTA = 1e-9; + @Test + @DisplayName("Test final velocity: v = u + at") + void testCalculateFinalVelocity() { + assertEquals(20.0, Kinematics.calculateFinalVelocity(10.0, 2.0, 5.0), DELTA); + } + + @Test + @DisplayName("Test displacement: s = ut + 0.5at^2") + void testCalculateDisplacement() { + assertEquals(75.0, Kinematics.calculateDisplacement(10.0, 2.0, 5.0), DELTA); + } + + @Test + @DisplayName("Test final velocity squared: v^2 = u^2 + 2as") + void testCalculateFinalVelocitySquared() { + assertEquals(400.0, Kinematics.calculateFinalVelocitySquared(10.0, 2.0, 75.0), DELTA); + } + + @Test + @DisplayName("Test displacement from average velocity: s = (u+v)/2 * t") + void testCalculateDisplacementFromVelocities() { + assertEquals(75.0, Kinematics.calculateDisplacementFromVelocities(10.0, 20.0, 5.0), DELTA); + } + + @Test + @DisplayName("Test with negative acceleration (deceleration)") + void testDeceleration() { + assertEquals(10.0, Kinematics.calculateFinalVelocity(30.0, -4.0, 5.0), DELTA); + assertEquals(100.0, Kinematics.calculateDisplacement(30.0, -4.0, 5.0), DELTA); + + assertEquals(100.0, Kinematics.calculateFinalVelocitySquared(30.0, -4.0, 100.0), DELTA); + assertEquals(100.0, Kinematics.calculateDisplacementFromVelocities(30.0, 10.0, 5.0), DELTA); + } +}