Skip to content

Commit dbcdb7f

Browse files
author
Priyanshu1303d
committed
[FEAT] Add Newton's Law of Gravitation algorithm
1 parent 8678c63 commit dbcdb7f

File tree

2 files changed

+6
-6
lines changed

2 files changed

+6
-6
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public static double[] calculateGravitationalForce(double m1, double x1, double
3636

3737
// If bodies are at the same position, force is zero to avoid division by zero.
3838
if (distanceSq == 0) {
39-
return new double[]{0, 0};
39+
return new double[] {0, 0};
4040
}
4141

4242
double distance = Math.sqrt(distanceSq);
@@ -46,7 +46,7 @@ public static double[] calculateGravitationalForce(double m1, double x1, double
4646
double fx = forceMagnitude * (dx / distance);
4747
double fy = forceMagnitude * (dy / distance);
4848

49-
return new double[]{fx, fy};
49+
return new double[] {fx, fy};
5050
}
5151

5252
/**

src/test/java/com/thealgorithms/physics/GravitationTest.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,11 @@ final class GravitationTest {
2121
void testSimpleForceCalculation() {
2222
// Force on body 2 should be F = G*1*1 / 1^2 = G, directed towards body 1 (negative x)
2323
double[] forceOnB = Gravitation.calculateGravitationalForce(1.0, 0, 0, 1.0, 1, 0);
24-
assertArrayEquals(new double[]{-G, 0.0}, forceOnB, DELTA);
24+
assertArrayEquals(new double[] {-G, 0.0}, forceOnB, DELTA);
2525

2626
// Force on body 1 should be equal and opposite (positive x)
2727
double[] forceOnA = Gravitation.calculateGravitationalForce(1.0, 1, 0, 1.0, 0, 0);
28-
assertArrayEquals(new double[]{G, 0.0}, forceOnA, DELTA);
28+
assertArrayEquals(new double[] {G, 0.0}, forceOnA, DELTA);
2929
}
3030

3131
@Test
@@ -40,14 +40,14 @@ void test2DForceCalculation() {
4040
double expectedFy = magnitude * (-4.0 / 5.0); // -8G / 125
4141

4242
double[] forceOnB = Gravitation.calculateGravitationalForce(2.0, 0, 0, 1.0, 3, 4);
43-
assertArrayEquals(new double[]{expectedFx, expectedFy}, forceOnB, DELTA);
43+
assertArrayEquals(new double[] {expectedFx, expectedFy}, forceOnB, DELTA);
4444
}
4545

4646
@Test
4747
@DisplayName("Test overlapping bodies should result in zero force")
4848
void testOverlappingBodies() {
4949
double[] force = Gravitation.calculateGravitationalForce(1000.0, 1.5, -2.5, 500.0, 1.5, -2.5);
50-
assertArrayEquals(new double[]{0.0, 0.0}, force, DELTA);
50+
assertArrayEquals(new double[] {0.0, 0.0}, force, DELTA);
5151
}
5252

5353
@Test

0 commit comments

Comments
 (0)