Skip to content

Commit abd1c47

Browse files
authored
Create Relativity.java (#7323)
* Create Relativity.java Created the file that implements relativity formulae. * Create RelativityTest.java Test file created for Relativity.java. * Update RelativityTest.java Function name ambiguity resolved * Update RelativityTest.java DELTA increased * Update Relativity.java Extra white space removed * Update Relativity.java Extra spaces added * Update RelativityTest.java Spaces added and removed * Update Relativity.java White spaces added and removed * Update RelativityTest.java Added and removed spaces * Update Relativity.java * Update Relativity.java Space removed * Update Relativity.java Replaced tabs by spaces * Update RelativityTest.java Tabs replaced by spaces
1 parent b7e9c85 commit abd1c47

File tree

2 files changed

+154
-0
lines changed

2 files changed

+154
-0
lines changed
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
package com.thealgorithms.physics;
2+
3+
/**
4+
* Implements relativity theory formulae.
5+
* Provides simple static methods to calculate length contraction and time dilation
6+
* in the laboratory frame with respect to the object's own frame, and velocity
7+
* with respect to the moving frame.
8+
*
9+
* @see <a href="https://en.wikipedia.org/wiki/List_of_relativistic_equations">Wikipedia</a>
10+
*/
11+
public final class Relativity {
12+
13+
/* Speed of light in m s^-1 */
14+
public static final double SPEED_OF_LIGHT = 299792458.0;
15+
16+
/**
17+
* Private constructor to prevent instantiation of this utility class.
18+
*/
19+
private Relativity() {
20+
}
21+
22+
/**
23+
* Calculates the gamma parameter that is of paramount importance in relativity
24+
* theory. It is a dimensionless parameter that is equal to 1 for zero velocity
25+
* but tends to infinity when velocity approaches the speed of light.
26+
*
27+
* @param v The velocity (m/s).
28+
* @return The value of gamma parameter.
29+
*/
30+
public static double gamma(double v) {
31+
if (Math.abs(v) >= SPEED_OF_LIGHT) {
32+
throw new IllegalArgumentException("Speed must be lower than the speed of light");
33+
}
34+
return 1.0 / Math.sqrt(1 - v * v / (SPEED_OF_LIGHT * SPEED_OF_LIGHT));
35+
}
36+
37+
/**
38+
* Calculates the length of an object in the moving frame.
39+
*
40+
* @param length The length of an object in its own frame (m).
41+
* @param v The velocity of the object (m/s).
42+
* @return The length of an object in the laboratory frame (m).
43+
*/
44+
public static double lengthContraction(double length, double v) {
45+
if (length < 0) {
46+
throw new IllegalArgumentException("Length must be non-negative");
47+
}
48+
return length / gamma(v);
49+
}
50+
51+
/**
52+
* Calculates the time that has passed in the moving frame.
53+
*
54+
* @param length The time that has passed in the object's own frame (s).
55+
* @param v The velocity of the object (m/s).
56+
* @return The time that has passed in the laboratory frame (s).
57+
*/
58+
public static double timeDilation(double time, double v) {
59+
if (time < 0) {
60+
throw new IllegalArgumentException("Time must be non-negative");
61+
}
62+
return time * gamma(v);
63+
}
64+
65+
/**
66+
* Calculates the velocity with respect to the moving frame.
67+
*
68+
* @param v1 The velocity of the object with respect to laboratory frame (m/s).
69+
* @param v The velocity of the moving frame (m/s).
70+
* @return The velocity with respect to the moving frame (m/s).
71+
*/
72+
public static double velocityAddition(double v1, double v) {
73+
if (Math.abs(v1) > SPEED_OF_LIGHT) {
74+
throw new IllegalArgumentException("Speed must not exceed the speed of light");
75+
}
76+
if (Math.abs(v) >= SPEED_OF_LIGHT) {
77+
throw new IllegalArgumentException("Frame speed must be lower than the speed of light");
78+
}
79+
return (v1 - v) / (1 - v1 * v / (SPEED_OF_LIGHT * SPEED_OF_LIGHT));
80+
}
81+
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package com.thealgorithms.physics;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
import static org.junit.jupiter.api.Assertions.assertThrows;
5+
6+
import org.junit.jupiter.api.DisplayName;
7+
import org.junit.jupiter.api.Test;
8+
9+
/**
10+
* Unit tests for the Relativity utility class.
11+
*/
12+
final class RelativityTest {
13+
14+
// A small tolerance (delta) for comparing floating-point numbers
15+
private static final double DELTA = 1e-6;
16+
private static final double C = Relativity.SPEED_OF_LIGHT;
17+
18+
@Test
19+
@DisplayName("Test the gamma parameter")
20+
void testGamma() {
21+
double myGamma = Relativity.gamma(0.6 * C);
22+
assertEquals(1.25, myGamma, DELTA);
23+
}
24+
25+
@Test
26+
@DisplayName("Test the length contraction")
27+
void testLengthContraction() {
28+
double myLength = Relativity.lengthContraction(5.0, 0.8 * C);
29+
assertEquals(3.0, myLength, DELTA);
30+
}
31+
32+
@Test
33+
@DisplayName("Test the time dilation")
34+
void testTimeDilation() {
35+
double myTime = Relativity.timeDilation(4.0, 0.6 * C);
36+
assertEquals(5.0, myTime, DELTA);
37+
}
38+
39+
@Test
40+
@DisplayName("Test the velocity addition in the same direction")
41+
void testVelocityAdditionSameDirection() {
42+
double myVelocity = Relativity.velocityAddition(0.8 * C, 0.75 * C);
43+
assertEquals(0.125 * C, myVelocity, DELTA);
44+
}
45+
46+
@Test
47+
@DisplayName("Test the velocity addition in different directions")
48+
void testVelocityAdditionDifferentDirections() {
49+
double myVelocity = Relativity.velocityAddition(0.8 * C, -0.75 * C);
50+
assertEquals(0.96875 * C, myVelocity, DELTA);
51+
}
52+
53+
@Test
54+
@DisplayName("Test the velocity addition with the speed of light")
55+
void testVelocityAdditionWithSpeedOfLight() {
56+
double myVelocity = Relativity.velocityAddition(C, 0.7 * C);
57+
assertEquals(C, myVelocity, DELTA);
58+
}
59+
60+
@Test
61+
@DisplayName("Test invalid inputs throw exception")
62+
void testInvalidOrbitalVelocityInputs() {
63+
assertThrows(IllegalArgumentException.class, () -> Relativity.gamma(1.2 * C));
64+
assertThrows(IllegalArgumentException.class, () -> Relativity.gamma(-C));
65+
assertThrows(IllegalArgumentException.class, () -> Relativity.lengthContraction(-1.0, 0.6 * C));
66+
assertThrows(IllegalArgumentException.class, () -> Relativity.lengthContraction(1.0, 1.5 * C));
67+
assertThrows(IllegalArgumentException.class, () -> Relativity.timeDilation(-5.0, -0.8 * C));
68+
assertThrows(IllegalArgumentException.class, () -> Relativity.timeDilation(5.0, C));
69+
assertThrows(IllegalArgumentException.class, () -> Relativity.velocityAddition(0.3 * C, -C));
70+
assertThrows(IllegalArgumentException.class, () -> Relativity.velocityAddition(1.4 * C, 0.2 * C));
71+
assertThrows(IllegalArgumentException.class, () -> Relativity.velocityAddition(-0.4 * C, 1.2 * C));
72+
}
73+
}

0 commit comments

Comments
 (0)