Skip to content

Commit a252e74

Browse files
authored
Update Relativity.java
Extra white space removed
1 parent dbe6f06 commit a252e74

File tree

1 file changed

+25
-20
lines changed

1 file changed

+25
-20
lines changed

src/main/java/com/thealgorithms/physics/Relativity.java

Lines changed: 25 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,75 +3,80 @@
33
/**
44
* Implements relativity theory formulae.
55
* 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
6+
* in the laboratory frame with respect to the object's own frame, and velocity
77
* with respect to the moving frame.
88
*
9-
* @see
9+
* @see
1010
* <a href="https://en.wikipedia.org/wiki/List_of_relativistic_equations">Wikipedia</a>
1111
*/
1212
public final class Relativity {
1313

1414
/* Speed of light in m s^-1 */
1515
public static final double SPEED_OF_LIGHT = 299792458.0;
16-
16+
1717
/**
1818
* Private constructor to prevent instantiation of this utility class.
1919
*/
2020
private Relativity() {
2121
}
22-
22+
2323
/**
24-
* Calculates the gamma parameter that is of paramount importance in relativity
24+
* Calculates the gamma parameter that is of paramount importance in relativity
2525
* theory. It is a dimensionless parameter that is equal to 1 for zero velocity
2626
* but tends to infinity when velocity approaches the speed of light.
2727
*
2828
* @param v The velocity (m/s).
2929
* @return The value of gamma parameter.
3030
*/
3131
public static double gamma(double v) {
32-
if (Math.abs(v) >= SPEED_OF_LIGHT)
32+
if (Math.abs(v) >= SPEED_OF_LIGHT) {
3333
throw new IllegalArgumentException("Speed must be lower than the speed of light");
34-
return 1.0/Math.sqrt(1-v*v/(SPEED_OF_LIGHT*SPEED_OF_LIGHT));
34+
}
35+
return 1.0 / Math.sqrt(1 - v * v / (SPEED_OF_LIGHT * SPEED_OF_LIGHT));
3536
}
3637

3738
/**
3839
* Calculates the length of an object in the moving frame.
39-
*
40+
*
4041
* @param length The length of an object in its own frame (m).
4142
* @param v The velocity of the object (m/s).
4243
* @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)
44+
*/
45+
public static double lengthContraction(double length, double v) {
46+
if (length < 0) {
4647
throw new IllegalArgumentException("Length must be non-negative");
48+
}
4749
return length/gamma(v);
4850
}
4951

5052
/**
5153
* Calculates the time that has passed in the moving frame.
52-
*
54+
*
5355
* @param length The time that has passed in the object's own frame (s).
5456
* @param v The velocity of the object (m/s).
5557
* @return The time that has passed in the laboratory frame (s).
56-
*/
57-
public static double timeDilation (double time, double v) {
58-
if (time < 0)
58+
*/
59+
public static double timeDilation(double time, double v) {
60+
if (time < 0) {
5961
throw new IllegalArgumentException("Time must be non-negative");
62+
}
6063
return time*gamma(v);
6164
}
6265

6366
/**
6467
* Calculates the velocity with respect to the moving frame.
65-
*
68+
*
6669
* @param v1 The velocity of the object with respect to laboratory frame (m/s).
6770
* @param v The velocity of the moving frame (m/s).
6871
* @return The velocity with respect to the moving frame (m/s).
69-
*/
70-
public static double velocityAddition (double v1, double v) {
71-
if (Math.abs(v1) > SPEED_OF_LIGHT)
72+
*/
73+
public static double velocityAddition(double v1, double v) {
74+
if (Math.abs(v1) > SPEED_OF_LIGHT) {
7275
throw new IllegalArgumentException("Speed must not exceed the speed of light");
73-
if (Math.abs(v) >= SPEED_OF_LIGHT)
76+
}
77+
if (Math.abs(v) >= SPEED_OF_LIGHT) {
7478
throw new IllegalArgumentException("Frame speed must be lower than the speed of light");
79+
}
7580
return (v1 - v)/(1 - v1*v/(SPEED_OF_LIGHT*SPEED_OF_LIGHT));
7681
}
7782
}

0 commit comments

Comments
 (0)