From cc0a7009b7a051a04cc4338b1aa7e4683c94bc15 Mon Sep 17 00:00:00 2001 From: kvadrik <41710943+kvadrik@users.noreply.github.com> Date: Wed, 18 Mar 2026 16:11:59 +0200 Subject: [PATCH 01/13] Create Relativity.java Created the file that implements relativity formulae. --- .../com/thealgorithms/physics/Relativity.java | 78 +++++++++++++++++++ 1 file changed, 78 insertions(+) create mode 100644 src/main/java/com/thealgorithms/physics/Relativity.java diff --git a/src/main/java/com/thealgorithms/physics/Relativity.java b/src/main/java/com/thealgorithms/physics/Relativity.java new file mode 100644 index 000000000000..10f231c43c7d --- /dev/null +++ b/src/main/java/com/thealgorithms/physics/Relativity.java @@ -0,0 +1,78 @@ +package com.thealgorithms.physics; + +/** + * Implements relativity theory formulae. + * Provides simple static methods to calculate length contraction and time dilation + * in the laboratory frame with respect to the object's own frame, and velocity + * with respect to the moving frame. + * + * @see + * Wikipedia + */ +public final class Relativity { + + /* Speed of light in m s^-1 */ + public static final double SPEED_OF_LIGHT = 299792458.0; + + /** + * Private constructor to prevent instantiation of this utility class. + */ + private Relativity() { + } + + /** + * Calculates the gamma parameter that is of paramount importance in relativity + * theory. It is a dimensionless parameter that is equal to 1 for zero velocity + * but tends to infinity when velocity approaches the speed of light. + * + * @param v The velocity (m/s). + * @return The value of gamma parameter. + */ + public static double gamma(double v) { + if (Math.abs(v) >= SPEED_OF_LIGHT) + throw new IllegalArgumentException("Speed must be lower than the speed of light"); + return 1.0/Math.sqrt(1-v*v/(SPEED_OF_LIGHT*SPEED_OF_LIGHT)); + } + + /** + * Calculates the length of an object in the moving frame. + * + * @param length The length of an object in its own frame (m). + * @param v The velocity of the object (m/s). + * @return The length of an object in the laboratory frame (m). + */ + public static double lengthContraction (double length, double v) { + if (length < 0) + throw new IllegalArgumentException("Length must be non-negative"); + return length/gamma(v); + } + + /** + * Calculates the time that has passed in the moving frame. + * + * @param length The time that has passed in the object's own frame (s). + * @param v The velocity of the object (m/s). + * @return The time that has passed in the laboratory frame (s). + */ + public static double timeDilation (double time, double v) { + if (time < 0) + throw new IllegalArgumentException("Time must be non-negative"); + return time*gamma(v); + } + + /** + * Calculates the velocity with respect to the moving frame. + * + * @param v1 The velocity of the object with respect to laboratory frame (m/s). + * @param v The velocity of the moving frame (m/s). + * @return The velocity with respect to the moving frame (m/s). + */ + public static double velocityAddition (double v1, double v) { + if (Math.abs(v1) > SPEED_OF_LIGHT) + throw new IllegalArgumentException("Speed must not exceed the speed of light"); + if (Math.abs(v) >= SPEED_OF_LIGHT) + throw new IllegalArgumentException("Frame speed must be lower than the speed of light"); + return (v1 - v)/(1 - v1*v/(SPEED_OF_LIGHT*SPEED_OF_LIGHT)); + } +} + From c23e5be661bf4351cf6a52987fe1ff8cbdb62707 Mon Sep 17 00:00:00 2001 From: kvadrik <41710943+kvadrik@users.noreply.github.com> Date: Wed, 18 Mar 2026 16:14:32 +0200 Subject: [PATCH 02/13] Create RelativityTest.java Test file created for Relativity.java. --- .../thealgorithms/physics/RelativityTest.java | 74 +++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100644 src/test/java/com/thealgorithms/physics/RelativityTest.java diff --git a/src/test/java/com/thealgorithms/physics/RelativityTest.java b/src/test/java/com/thealgorithms/physics/RelativityTest.java new file mode 100644 index 000000000000..74b1ee1423a5 --- /dev/null +++ b/src/test/java/com/thealgorithms/physics/RelativityTest.java @@ -0,0 +1,74 @@ +package com.thealgorithms.physics; + +import static org.junit.jupiter.api.Assertions.assertArrayEquals; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; + +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; + +/** + * Unit tests for the Relativity utility class. + */ +final class RelativityTest { + + // A small tolerance (delta) for comparing floating-point numbers + private static final double DELTA = 1e-9; + private static final double C = Relativity.SPEED_OF_LIGHT; + + @Test + @DisplayName("Test the gamma parameter") + void testGamma() { + double myGamma = Relativity.gamma(0.6*C); + assertEquals(1.25, myGamma, DELTA); + } + + @Test + @DisplayName("Test the length contraction") + void testLengthContraction() { + double myLength = Relativity.lengthContraction(5.0, 0.8*C); + assertEquals(3.0, myLength, DELTA); + } + + @Test + @DisplayName("Test the time dilation") + void testTimeDilation() { + double myTime = Relativity.timeDilation(4.0, 0.6*C); + assertEquals(5.0, myTime, DELTA); + } + + @Test + @DisplayName("Test the velocity addition in the same direction") + void testVelocityAddition() { + double myVelocity = Relativity.velocityAddition(0.8*C, 0.75*C); + assertEquals(0.125*C, myVelocity, DELTA); + } + + @Test + @DisplayName("Test the velocity addition in different directions") + void testVelocityAddition() { + double myVelocity = Relativity.velocityAddition(0.8*C, -0.75*C); + assertEquals(0.96875*C, myVelocity, DELTA); + } + + @Test + @DisplayName("Test the velocity addition with the speed of light") + void testVelocityAddition() { + double myVelocity = Relativity.velocityAddition(C, 0.7*C); + assertEquals(C, myVelocity, DELTA); + } + + @Test + @DisplayName("Test invalid inputs throw exception") + void testInvalidOrbitalVelocityInputs() { + assertThrows(IllegalArgumentException.class, () -> Relativity.gamma(1.2*C)); + assertThrows(IllegalArgumentException.class, () -> Relativity.gamma(-C)); + assertThrows(IllegalArgumentException.class, () -> Relativity.lengthContraction(-1.0, 0.6*C)); + assertThrows(IllegalArgumentException.class, () -> Relativity.lengthContraction(1.0, 1.5*C)); + assertThrows(IllegalArgumentException.class, () -> Relativity.timeDilation(-5.0, -0.8*C)); + assertThrows(IllegalArgumentException.class, () -> Relativity.timeDilation(5.0, C)); + assertThrows(IllegalArgumentException.class, () -> Relativity.velocityAddition(0.3*C, -C)); + assertThrows(IllegalArgumentException.class, () -> Relativity.velocityAddition(1.4*C, 0.2*C)); + assertThrows(IllegalArgumentException.class, () -> Relativity.velocityAddition(-0.4*C, 1.2*C)); + } +} From aed9f496ad116110f01da36321566a879e8de0f0 Mon Sep 17 00:00:00 2001 From: kvadrik <41710943+kvadrik@users.noreply.github.com> Date: Wed, 18 Mar 2026 16:20:31 +0200 Subject: [PATCH 03/13] Update RelativityTest.java Function name ambiguity resolved --- src/test/java/com/thealgorithms/physics/RelativityTest.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/test/java/com/thealgorithms/physics/RelativityTest.java b/src/test/java/com/thealgorithms/physics/RelativityTest.java index 74b1ee1423a5..5cd240d23079 100644 --- a/src/test/java/com/thealgorithms/physics/RelativityTest.java +++ b/src/test/java/com/thealgorithms/physics/RelativityTest.java @@ -39,21 +39,21 @@ void testTimeDilation() { @Test @DisplayName("Test the velocity addition in the same direction") - void testVelocityAddition() { + void testVelocityAdditionSameDirection() { double myVelocity = Relativity.velocityAddition(0.8*C, 0.75*C); assertEquals(0.125*C, myVelocity, DELTA); } @Test @DisplayName("Test the velocity addition in different directions") - void testVelocityAddition() { + void testVelocityAdditionDifferentDirections() { double myVelocity = Relativity.velocityAddition(0.8*C, -0.75*C); assertEquals(0.96875*C, myVelocity, DELTA); } @Test @DisplayName("Test the velocity addition with the speed of light") - void testVelocityAddition() { + void testVelocityAdditionWithSpeedOfLight() { double myVelocity = Relativity.velocityAddition(C, 0.7*C); assertEquals(C, myVelocity, DELTA); } From dbe6f0617def0eecfd0ed52f19238cfc1810f869 Mon Sep 17 00:00:00 2001 From: kvadrik <41710943+kvadrik@users.noreply.github.com> Date: Wed, 18 Mar 2026 16:34:05 +0200 Subject: [PATCH 04/13] Update RelativityTest.java DELTA increased --- src/test/java/com/thealgorithms/physics/RelativityTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/java/com/thealgorithms/physics/RelativityTest.java b/src/test/java/com/thealgorithms/physics/RelativityTest.java index 5cd240d23079..5db97cbbf4b7 100644 --- a/src/test/java/com/thealgorithms/physics/RelativityTest.java +++ b/src/test/java/com/thealgorithms/physics/RelativityTest.java @@ -13,7 +13,7 @@ final class RelativityTest { // A small tolerance (delta) for comparing floating-point numbers - private static final double DELTA = 1e-9; + private static final double DELTA = 1e-6; private static final double C = Relativity.SPEED_OF_LIGHT; @Test From a252e7452adbe3c1efcbbb439bd49937093a4f59 Mon Sep 17 00:00:00 2001 From: kvadrik <41710943+kvadrik@users.noreply.github.com> Date: Wed, 18 Mar 2026 16:48:58 +0200 Subject: [PATCH 05/13] Update Relativity.java Extra white space removed --- .../com/thealgorithms/physics/Relativity.java | 45 ++++++++++--------- 1 file changed, 25 insertions(+), 20 deletions(-) diff --git a/src/main/java/com/thealgorithms/physics/Relativity.java b/src/main/java/com/thealgorithms/physics/Relativity.java index 10f231c43c7d..149796d78dd6 100644 --- a/src/main/java/com/thealgorithms/physics/Relativity.java +++ b/src/main/java/com/thealgorithms/physics/Relativity.java @@ -3,25 +3,25 @@ /** * Implements relativity theory formulae. * Provides simple static methods to calculate length contraction and time dilation - * in the laboratory frame with respect to the object's own frame, and velocity + * in the laboratory frame with respect to the object's own frame, and velocity * with respect to the moving frame. * - * @see + * @see * Wikipedia */ public final class Relativity { /* Speed of light in m s^-1 */ public static final double SPEED_OF_LIGHT = 299792458.0; - + /** * Private constructor to prevent instantiation of this utility class. */ private Relativity() { } - + /** - * Calculates the gamma parameter that is of paramount importance in relativity + * Calculates the gamma parameter that is of paramount importance in relativity * theory. It is a dimensionless parameter that is equal to 1 for zero velocity * but tends to infinity when velocity approaches the speed of light. * @@ -29,49 +29,54 @@ private Relativity() { * @return The value of gamma parameter. */ public static double gamma(double v) { - if (Math.abs(v) >= SPEED_OF_LIGHT) + if (Math.abs(v) >= SPEED_OF_LIGHT) { throw new IllegalArgumentException("Speed must be lower than the speed of light"); - return 1.0/Math.sqrt(1-v*v/(SPEED_OF_LIGHT*SPEED_OF_LIGHT)); + } + return 1.0 / Math.sqrt(1 - v * v / (SPEED_OF_LIGHT * SPEED_OF_LIGHT)); } /** * Calculates the length of an object in the moving frame. - * + * * @param length The length of an object in its own frame (m). * @param v The velocity of the object (m/s). * @return The length of an object in the laboratory frame (m). - */ - public static double lengthContraction (double length, double v) { - if (length < 0) + */ + public static double lengthContraction(double length, double v) { + if (length < 0) { throw new IllegalArgumentException("Length must be non-negative"); + } return length/gamma(v); } /** * Calculates the time that has passed in the moving frame. - * + * * @param length The time that has passed in the object's own frame (s). * @param v The velocity of the object (m/s). * @return The time that has passed in the laboratory frame (s). - */ - public static double timeDilation (double time, double v) { - if (time < 0) + */ + public static double timeDilation(double time, double v) { + if (time < 0) { throw new IllegalArgumentException("Time must be non-negative"); + } return time*gamma(v); } /** * Calculates the velocity with respect to the moving frame. - * + * * @param v1 The velocity of the object with respect to laboratory frame (m/s). * @param v The velocity of the moving frame (m/s). * @return The velocity with respect to the moving frame (m/s). - */ - public static double velocityAddition (double v1, double v) { - if (Math.abs(v1) > SPEED_OF_LIGHT) + */ + public static double velocityAddition(double v1, double v) { + if (Math.abs(v1) > SPEED_OF_LIGHT) { throw new IllegalArgumentException("Speed must not exceed the speed of light"); - if (Math.abs(v) >= SPEED_OF_LIGHT) + } + if (Math.abs(v) >= SPEED_OF_LIGHT) { throw new IllegalArgumentException("Frame speed must be lower than the speed of light"); + } return (v1 - v)/(1 - v1*v/(SPEED_OF_LIGHT*SPEED_OF_LIGHT)); } } From 7ef047f55c82af62353709270d12ad53b93a8a03 Mon Sep 17 00:00:00 2001 From: kvadrik <41710943+kvadrik@users.noreply.github.com> Date: Wed, 18 Mar 2026 16:50:32 +0200 Subject: [PATCH 06/13] Update Relativity.java Extra spaces added --- src/main/java/com/thealgorithms/physics/Relativity.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/com/thealgorithms/physics/Relativity.java b/src/main/java/com/thealgorithms/physics/Relativity.java index 149796d78dd6..3ab13a79afb5 100644 --- a/src/main/java/com/thealgorithms/physics/Relativity.java +++ b/src/main/java/com/thealgorithms/physics/Relativity.java @@ -77,7 +77,7 @@ public static double velocityAddition(double v1, double v) { if (Math.abs(v) >= SPEED_OF_LIGHT) { throw new IllegalArgumentException("Frame speed must be lower than the speed of light"); } - return (v1 - v)/(1 - v1*v/(SPEED_OF_LIGHT*SPEED_OF_LIGHT)); + return (v1 - v) / (1 - v1 * v / (SPEED_OF_LIGHT * SPEED_OF_LIGHT)); } } From f26c544680db78fd452d3139ba95bfd180157b62 Mon Sep 17 00:00:00 2001 From: kvadrik <41710943+kvadrik@users.noreply.github.com> Date: Wed, 18 Mar 2026 16:53:53 +0200 Subject: [PATCH 07/13] Update RelativityTest.java Spaces added and removed --- .../thealgorithms/physics/RelativityTest.java | 39 +++++++++---------- 1 file changed, 19 insertions(+), 20 deletions(-) diff --git a/src/test/java/com/thealgorithms/physics/RelativityTest.java b/src/test/java/com/thealgorithms/physics/RelativityTest.java index 5db97cbbf4b7..d0cac9f1ef5a 100644 --- a/src/test/java/com/thealgorithms/physics/RelativityTest.java +++ b/src/test/java/com/thealgorithms/physics/RelativityTest.java @@ -1,6 +1,5 @@ package com.thealgorithms.physics; -import static org.junit.jupiter.api.Assertions.assertArrayEquals; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertThrows; @@ -15,60 +14,60 @@ final class RelativityTest { // A small tolerance (delta) for comparing floating-point numbers private static final double DELTA = 1e-6; private static final double C = Relativity.SPEED_OF_LIGHT; - + @Test @DisplayName("Test the gamma parameter") void testGamma() { - double myGamma = Relativity.gamma(0.6*C); + double myGamma = Relativity.gamma(0.6 * C); assertEquals(1.25, myGamma, DELTA); } - + @Test @DisplayName("Test the length contraction") void testLengthContraction() { - double myLength = Relativity.lengthContraction(5.0, 0.8*C); + double myLength = Relativity.lengthContraction(5.0, 0.8 * C); assertEquals(3.0, myLength, DELTA); } - + @Test @DisplayName("Test the time dilation") void testTimeDilation() { - double myTime = Relativity.timeDilation(4.0, 0.6*C); + double myTime = Relativity.timeDilation(4.0, 0.6 * C); assertEquals(5.0, myTime, DELTA); } - + @Test @DisplayName("Test the velocity addition in the same direction") void testVelocityAdditionSameDirection() { - double myVelocity = Relativity.velocityAddition(0.8*C, 0.75*C); + double myVelocity = Relativity.velocityAddition(0.8 * C, 0.75 * C); assertEquals(0.125*C, myVelocity, DELTA); } - + @Test @DisplayName("Test the velocity addition in different directions") void testVelocityAdditionDifferentDirections() { - double myVelocity = Relativity.velocityAddition(0.8*C, -0.75*C); + double myVelocity = Relativity.velocityAddition(0.8 * C, -0.75 * C); assertEquals(0.96875*C, myVelocity, DELTA); } @Test @DisplayName("Test the velocity addition with the speed of light") void testVelocityAdditionWithSpeedOfLight() { - double myVelocity = Relativity.velocityAddition(C, 0.7*C); + double myVelocity = Relativity.velocityAddition(C, 0.7 * C); assertEquals(C, myVelocity, DELTA); } - + @Test @DisplayName("Test invalid inputs throw exception") void testInvalidOrbitalVelocityInputs() { - assertThrows(IllegalArgumentException.class, () -> Relativity.gamma(1.2*C)); + assertThrows(IllegalArgumentException.class, () -> Relativity.gamma(1.2 * C)); assertThrows(IllegalArgumentException.class, () -> Relativity.gamma(-C)); - assertThrows(IllegalArgumentException.class, () -> Relativity.lengthContraction(-1.0, 0.6*C)); - assertThrows(IllegalArgumentException.class, () -> Relativity.lengthContraction(1.0, 1.5*C)); - assertThrows(IllegalArgumentException.class, () -> Relativity.timeDilation(-5.0, -0.8*C)); + assertThrows(IllegalArgumentException.class, () -> Relativity.lengthContraction(-1.0, 0.6 * C)); + assertThrows(IllegalArgumentException.class, () -> Relativity.lengthContraction(1.0, 1.5 * C)); + assertThrows(IllegalArgumentException.class, () -> Relativity.timeDilation(-5.0, -0.8 * C)); assertThrows(IllegalArgumentException.class, () -> Relativity.timeDilation(5.0, C)); - assertThrows(IllegalArgumentException.class, () -> Relativity.velocityAddition(0.3*C, -C)); - assertThrows(IllegalArgumentException.class, () -> Relativity.velocityAddition(1.4*C, 0.2*C)); - assertThrows(IllegalArgumentException.class, () -> Relativity.velocityAddition(-0.4*C, 1.2*C)); + assertThrows(IllegalArgumentException.class, () -> Relativity.velocityAddition(0.3 * C, -C)); + assertThrows(IllegalArgumentException.class, () -> Relativity.velocityAddition(1.4 * C, 0.2 * C)); + assertThrows(IllegalArgumentException.class, () -> Relativity.velocityAddition(-0.4 * C, 1.2 * C)); } } From de8c051c710145a53779a1ac94c48497bb673451 Mon Sep 17 00:00:00 2001 From: kvadrik <41710943+kvadrik@users.noreply.github.com> Date: Wed, 18 Mar 2026 17:00:17 +0200 Subject: [PATCH 08/13] Update Relativity.java White spaces added and removed --- src/main/java/com/thealgorithms/physics/Relativity.java | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/main/java/com/thealgorithms/physics/Relativity.java b/src/main/java/com/thealgorithms/physics/Relativity.java index 3ab13a79afb5..c5e5ee17128f 100644 --- a/src/main/java/com/thealgorithms/physics/Relativity.java +++ b/src/main/java/com/thealgorithms/physics/Relativity.java @@ -46,7 +46,7 @@ public static double lengthContraction(double length, double v) { if (length < 0) { throw new IllegalArgumentException("Length must be non-negative"); } - return length/gamma(v); + return length / gamma(v); } /** @@ -60,9 +60,9 @@ public static double timeDilation(double time, double v) { if (time < 0) { throw new IllegalArgumentException("Time must be non-negative"); } - return time*gamma(v); + return time * gamma(v); } - + /** * Calculates the velocity with respect to the moving frame. * @@ -80,4 +80,3 @@ public static double velocityAddition(double v1, double v) { return (v1 - v) / (1 - v1 * v / (SPEED_OF_LIGHT * SPEED_OF_LIGHT)); } } - From 013366bbac194f5d9d7f8763c8fccf818fd31789 Mon Sep 17 00:00:00 2001 From: kvadrik <41710943+kvadrik@users.noreply.github.com> Date: Wed, 18 Mar 2026 17:01:26 +0200 Subject: [PATCH 09/13] Update RelativityTest.java Added and removed spaces --- src/test/java/com/thealgorithms/physics/RelativityTest.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/test/java/com/thealgorithms/physics/RelativityTest.java b/src/test/java/com/thealgorithms/physics/RelativityTest.java index d0cac9f1ef5a..ba6b9186f727 100644 --- a/src/test/java/com/thealgorithms/physics/RelativityTest.java +++ b/src/test/java/com/thealgorithms/physics/RelativityTest.java @@ -40,14 +40,14 @@ void testTimeDilation() { @DisplayName("Test the velocity addition in the same direction") void testVelocityAdditionSameDirection() { double myVelocity = Relativity.velocityAddition(0.8 * C, 0.75 * C); - assertEquals(0.125*C, myVelocity, DELTA); + assertEquals(0.125 * C, myVelocity, DELTA); } @Test @DisplayName("Test the velocity addition in different directions") void testVelocityAdditionDifferentDirections() { double myVelocity = Relativity.velocityAddition(0.8 * C, -0.75 * C); - assertEquals(0.96875*C, myVelocity, DELTA); + assertEquals(0.96875 * C, myVelocity, DELTA); } @Test @@ -55,7 +55,7 @@ void testVelocityAdditionDifferentDirections() { void testVelocityAdditionWithSpeedOfLight() { double myVelocity = Relativity.velocityAddition(C, 0.7 * C); assertEquals(C, myVelocity, DELTA); - } + } @Test @DisplayName("Test invalid inputs throw exception") From 53fc625c16ee5c6ba738426964fdd3997192ba7e Mon Sep 17 00:00:00 2001 From: kvadrik <41710943+kvadrik@users.noreply.github.com> Date: Wed, 18 Mar 2026 17:23:23 +0200 Subject: [PATCH 10/13] Update Relativity.java From c5f18f0e86cfb6f3dc9b5c6871284f47eda99fed Mon Sep 17 00:00:00 2001 From: kvadrik <41710943+kvadrik@users.noreply.github.com> Date: Wed, 18 Mar 2026 17:35:48 +0200 Subject: [PATCH 11/13] Update Relativity.java Space removed --- src/main/java/com/thealgorithms/physics/Relativity.java | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/main/java/com/thealgorithms/physics/Relativity.java b/src/main/java/com/thealgorithms/physics/Relativity.java index c5e5ee17128f..42775d233eab 100644 --- a/src/main/java/com/thealgorithms/physics/Relativity.java +++ b/src/main/java/com/thealgorithms/physics/Relativity.java @@ -6,8 +6,7 @@ * in the laboratory frame with respect to the object's own frame, and velocity * with respect to the moving frame. * - * @see - * Wikipedia + * @see Wikipedia */ public final class Relativity { From 87300f54aa215cef19ee05cbb869364ef02a8b08 Mon Sep 17 00:00:00 2001 From: kvadrik <41710943+kvadrik@users.noreply.github.com> Date: Wed, 18 Mar 2026 17:49:13 +0200 Subject: [PATCH 12/13] Update Relativity.java Replaced tabs by spaces --- .../com/thealgorithms/physics/Relativity.java | 122 +++++++++--------- 1 file changed, 61 insertions(+), 61 deletions(-) diff --git a/src/main/java/com/thealgorithms/physics/Relativity.java b/src/main/java/com/thealgorithms/physics/Relativity.java index 42775d233eab..ed823c2cc879 100644 --- a/src/main/java/com/thealgorithms/physics/Relativity.java +++ b/src/main/java/com/thealgorithms/physics/Relativity.java @@ -10,72 +10,72 @@ */ public final class Relativity { - /* Speed of light in m s^-1 */ - public static final double SPEED_OF_LIGHT = 299792458.0; + /* Speed of light in m s^-1 */ + public static final double SPEED_OF_LIGHT = 299792458.0; - /** + /** * Private constructor to prevent instantiation of this utility class. */ - private Relativity() { - } + private Relativity() { + } - /** - * Calculates the gamma parameter that is of paramount importance in relativity - * theory. It is a dimensionless parameter that is equal to 1 for zero velocity - * but tends to infinity when velocity approaches the speed of light. - * - * @param v The velocity (m/s). - * @return The value of gamma parameter. - */ - public static double gamma(double v) { - if (Math.abs(v) >= SPEED_OF_LIGHT) { - throw new IllegalArgumentException("Speed must be lower than the speed of light"); - } - return 1.0 / Math.sqrt(1 - v * v / (SPEED_OF_LIGHT * SPEED_OF_LIGHT)); - } + /** + * Calculates the gamma parameter that is of paramount importance in relativity + * theory. It is a dimensionless parameter that is equal to 1 for zero velocity + * but tends to infinity when velocity approaches the speed of light. + * + * @param v The velocity (m/s). + * @return The value of gamma parameter. + */ + public static double gamma(double v) { + if (Math.abs(v) >= SPEED_OF_LIGHT) { + throw new IllegalArgumentException("Speed must be lower than the speed of light"); + } + return 1.0 / Math.sqrt(1 - v * v / (SPEED_OF_LIGHT * SPEED_OF_LIGHT)); + } - /** - * Calculates the length of an object in the moving frame. - * - * @param length The length of an object in its own frame (m). - * @param v The velocity of the object (m/s). - * @return The length of an object in the laboratory frame (m). - */ - public static double lengthContraction(double length, double v) { - if (length < 0) { - throw new IllegalArgumentException("Length must be non-negative"); - } - return length / gamma(v); - } + /** + * Calculates the length of an object in the moving frame. + * + * @param length The length of an object in its own frame (m). + * @param v The velocity of the object (m/s). + * @return The length of an object in the laboratory frame (m). + */ + public static double lengthContraction(double length, double v) { + if (length < 0) { + throw new IllegalArgumentException("Length must be non-negative"); + } + return length / gamma(v); + } - /** - * Calculates the time that has passed in the moving frame. - * - * @param length The time that has passed in the object's own frame (s). - * @param v The velocity of the object (m/s). - * @return The time that has passed in the laboratory frame (s). - */ - public static double timeDilation(double time, double v) { - if (time < 0) { - throw new IllegalArgumentException("Time must be non-negative"); - } - return time * gamma(v); - } + /** + * Calculates the time that has passed in the moving frame. + * + * @param length The time that has passed in the object's own frame (s). + * @param v The velocity of the object (m/s). + * @return The time that has passed in the laboratory frame (s). + */ + public static double timeDilation(double time, double v) { + if (time < 0) { + throw new IllegalArgumentException("Time must be non-negative"); + } + return time * gamma(v); + } - /** - * Calculates the velocity with respect to the moving frame. - * - * @param v1 The velocity of the object with respect to laboratory frame (m/s). - * @param v The velocity of the moving frame (m/s). - * @return The velocity with respect to the moving frame (m/s). - */ - public static double velocityAddition(double v1, double v) { - if (Math.abs(v1) > SPEED_OF_LIGHT) { - throw new IllegalArgumentException("Speed must not exceed the speed of light"); - } - if (Math.abs(v) >= SPEED_OF_LIGHT) { - throw new IllegalArgumentException("Frame speed must be lower than the speed of light"); - } - return (v1 - v) / (1 - v1 * v / (SPEED_OF_LIGHT * SPEED_OF_LIGHT)); - } + /** + * Calculates the velocity with respect to the moving frame. + * + * @param v1 The velocity of the object with respect to laboratory frame (m/s). + * @param v The velocity of the moving frame (m/s). + * @return The velocity with respect to the moving frame (m/s). + */ + public static double velocityAddition(double v1, double v) { + if (Math.abs(v1) > SPEED_OF_LIGHT) { + throw new IllegalArgumentException("Speed must not exceed the speed of light"); + } + if (Math.abs(v) >= SPEED_OF_LIGHT) { + throw new IllegalArgumentException("Frame speed must be lower than the speed of light"); + } + return (v1 - v) / (1 - v1 * v / (SPEED_OF_LIGHT * SPEED_OF_LIGHT)); + } } From 67d40fbb9211f1892a4f85a85f620122ed303665 Mon Sep 17 00:00:00 2001 From: kvadrik <41710943+kvadrik@users.noreply.github.com> Date: Wed, 18 Mar 2026 18:08:01 +0200 Subject: [PATCH 13/13] Update RelativityTest.java Tabs replaced by spaces --- .../thealgorithms/physics/RelativityTest.java | 84 +++++++++---------- 1 file changed, 42 insertions(+), 42 deletions(-) diff --git a/src/test/java/com/thealgorithms/physics/RelativityTest.java b/src/test/java/com/thealgorithms/physics/RelativityTest.java index ba6b9186f727..44c17bdbd40f 100644 --- a/src/test/java/com/thealgorithms/physics/RelativityTest.java +++ b/src/test/java/com/thealgorithms/physics/RelativityTest.java @@ -15,59 +15,59 @@ final class RelativityTest { private static final double DELTA = 1e-6; private static final double C = Relativity.SPEED_OF_LIGHT; - @Test - @DisplayName("Test the gamma parameter") - void testGamma() { - double myGamma = Relativity.gamma(0.6 * C); - assertEquals(1.25, myGamma, DELTA); - } + @Test + @DisplayName("Test the gamma parameter") + void testGamma() { + double myGamma = Relativity.gamma(0.6 * C); + assertEquals(1.25, myGamma, DELTA); + } - @Test - @DisplayName("Test the length contraction") - void testLengthContraction() { - double myLength = Relativity.lengthContraction(5.0, 0.8 * C); - assertEquals(3.0, myLength, DELTA); - } + @Test + @DisplayName("Test the length contraction") + void testLengthContraction() { + double myLength = Relativity.lengthContraction(5.0, 0.8 * C); + assertEquals(3.0, myLength, DELTA); + } - @Test - @DisplayName("Test the time dilation") - void testTimeDilation() { - double myTime = Relativity.timeDilation(4.0, 0.6 * C); - assertEquals(5.0, myTime, DELTA); - } + @Test + @DisplayName("Test the time dilation") + void testTimeDilation() { + double myTime = Relativity.timeDilation(4.0, 0.6 * C); + assertEquals(5.0, myTime, DELTA); + } - @Test - @DisplayName("Test the velocity addition in the same direction") - void testVelocityAdditionSameDirection() { - double myVelocity = Relativity.velocityAddition(0.8 * C, 0.75 * C); - assertEquals(0.125 * C, myVelocity, DELTA); - } + @Test + @DisplayName("Test the velocity addition in the same direction") + void testVelocityAdditionSameDirection() { + double myVelocity = Relativity.velocityAddition(0.8 * C, 0.75 * C); + assertEquals(0.125 * C, myVelocity, DELTA); + } - @Test - @DisplayName("Test the velocity addition in different directions") - void testVelocityAdditionDifferentDirections() { - double myVelocity = Relativity.velocityAddition(0.8 * C, -0.75 * C); - assertEquals(0.96875 * C, myVelocity, DELTA); - } + @Test + @DisplayName("Test the velocity addition in different directions") + void testVelocityAdditionDifferentDirections() { + double myVelocity = Relativity.velocityAddition(0.8 * C, -0.75 * C); + assertEquals(0.96875 * C, myVelocity, DELTA); + } - @Test - @DisplayName("Test the velocity addition with the speed of light") - void testVelocityAdditionWithSpeedOfLight() { - double myVelocity = Relativity.velocityAddition(C, 0.7 * C); - assertEquals(C, myVelocity, DELTA); - } + @Test + @DisplayName("Test the velocity addition with the speed of light") + void testVelocityAdditionWithSpeedOfLight() { + double myVelocity = Relativity.velocityAddition(C, 0.7 * C); + assertEquals(C, myVelocity, DELTA); + } - @Test + @Test @DisplayName("Test invalid inputs throw exception") void testInvalidOrbitalVelocityInputs() { assertThrows(IllegalArgumentException.class, () -> Relativity.gamma(1.2 * C)); - assertThrows(IllegalArgumentException.class, () -> Relativity.gamma(-C)); + assertThrows(IllegalArgumentException.class, () -> Relativity.gamma(-C)); assertThrows(IllegalArgumentException.class, () -> Relativity.lengthContraction(-1.0, 0.6 * C)); - assertThrows(IllegalArgumentException.class, () -> Relativity.lengthContraction(1.0, 1.5 * C)); + assertThrows(IllegalArgumentException.class, () -> Relativity.lengthContraction(1.0, 1.5 * C)); assertThrows(IllegalArgumentException.class, () -> Relativity.timeDilation(-5.0, -0.8 * C)); - assertThrows(IllegalArgumentException.class, () -> Relativity.timeDilation(5.0, C)); + assertThrows(IllegalArgumentException.class, () -> Relativity.timeDilation(5.0, C)); assertThrows(IllegalArgumentException.class, () -> Relativity.velocityAddition(0.3 * C, -C)); - assertThrows(IllegalArgumentException.class, () -> Relativity.velocityAddition(1.4 * C, 0.2 * C)); - assertThrows(IllegalArgumentException.class, () -> Relativity.velocityAddition(-0.4 * C, 1.2 * C)); + assertThrows(IllegalArgumentException.class, () -> Relativity.velocityAddition(1.4 * C, 0.2 * C)); + assertThrows(IllegalArgumentException.class, () -> Relativity.velocityAddition(-0.4 * C, 1.2 * C)); } }