From 19aa44adffc9d01b5e36f6e6d450ab97f961487c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20S=C3=A1nchez=20N=C3=BA=C3=B1ez?= Date: Thu, 4 Dec 2025 20:12:50 +0100 Subject: [PATCH 01/12] Add Snell's Law implementation for refraction angle --- .../com/thealgorithms/physics/SnellsLaw.java | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 src/main/java/com/thealgorithms/physics/SnellsLaw.java diff --git a/src/main/java/com/thealgorithms/physics/SnellsLaw.java b/src/main/java/com/thealgorithms/physics/SnellsLaw.java new file mode 100644 index 000000000000..75627f4b5f7e --- /dev/null +++ b/src/main/java/com/thealgorithms/physics/SnellsLaw.java @@ -0,0 +1,28 @@ +package com.thealgorithms.physics; + +/** + * Calculates refraction angle using Snell's Law: + * n1 * sin(theta1) = n2 * sin(theta2) + */ +public class SnellLaw { + + /** + * Computes the refracted angle (theta2) in radians. + * + * @param n1 index of refraction of medium 1 + * @param n2 index of refraction of medium 2 + * @param theta1 incident angle in radians + * @return refracted angle (theta2) in radians + * @throws IllegalArgumentException if total internal reflection occurs + */ + public static double refractedAngle(double n1, double n2, double theta1) { + double ratio = n1 / n2; + double sinTheta2 = ratio * Math.sin(theta1); + + if (Math.abs(sinTheta2) > 1.0) { + throw new IllegalArgumentException("Total internal reflection: no refraction possible."); + } + + return Math.asin(sinTheta2); + } +} From c2fd9c2a7d640e1de86da82beae2a6a955406d45 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20S=C3=A1nchez=20N=C3=BA=C3=B1ez?= Date: Thu, 4 Dec 2025 21:29:37 +0100 Subject: [PATCH 02/12] Add tests for Snell's Law calculations --- .../thealgorithms/physics/SnellLawTest.java | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 src/test/java/com/thealgorithms/physics/SnellLawTest.java diff --git a/src/test/java/com/thealgorithms/physics/SnellLawTest.java b/src/test/java/com/thealgorithms/physics/SnellLawTest.java new file mode 100644 index 000000000000..e6044a8ab24d --- /dev/null +++ b/src/test/java/com/thealgorithms/physics/SnellLawTest.java @@ -0,0 +1,32 @@ +package com.thealgorithms.physics; + +import static org.junit.jupiter.api.Assertions.*; +import org.junit.jupiter.api.Test; + +public class SnellsLawTest { +//tests for example environmet + @Test + public void testCalculateRefractionAngle() { + double n1 = 1.0; // air + double n2 = 1.5; // glass + double theta1 = Math.toRadians(30); // 30 grados + + double theta2 = SnellsLaw.calculateRefractionAngle(n1, n2, theta1); + + // expected value using: sin(theta2) = n1/n2 * sin(theta1) + double expected = Math.asin((n1 / n2) * Math.sin(theta1)); + + assertEquals(expected, theta2, 1e-9); + } +//tests for total refraction + @Test + public void testInvalidRefractiveIndex() { + assertThrows(IllegalArgumentException.class, () -> { + SnellsLaw.calculateRefractionAngle(-1, 1.5, 0.5); + }); + + assertThrows(IllegalArgumentException.class, () -> { + SnellsLaw.calculateRefractionAngle(1, 0, 0.5); + }); + } +} From c77227731a6f941fb1a529a2aba6361749aa4d36 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20S=C3=A1nchez=20N=C3=BA=C3=B1ez?= Date: Thu, 4 Dec 2025 21:35:35 +0100 Subject: [PATCH 03/12] Update documentation with reference link to Snell's Law Added a reference link to Snell's Law in the documentation. --- src/main/java/com/thealgorithms/physics/SnellsLaw.java | 1 + 1 file changed, 1 insertion(+) diff --git a/src/main/java/com/thealgorithms/physics/SnellsLaw.java b/src/main/java/com/thealgorithms/physics/SnellsLaw.java index 75627f4b5f7e..2570be4fc73b 100644 --- a/src/main/java/com/thealgorithms/physics/SnellsLaw.java +++ b/src/main/java/com/thealgorithms/physics/SnellsLaw.java @@ -3,6 +3,7 @@ /** * Calculates refraction angle using Snell's Law: * n1 * sin(theta1) = n2 * sin(theta2) + * @see Snell's Law */ public class SnellLaw { From 2023730b5f97c483616fbf01f6299e4644cd1c2d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20S=C3=A1nchez=20N=C3=BA=C3=B1ez?= Date: Thu, 4 Dec 2025 21:43:23 +0100 Subject: [PATCH 04/12] Prevent instantiation of SnellLaw class Make SnellLaw class non-instantiable by adding a private constructor. --- src/main/java/com/thealgorithms/physics/SnellsLaw.java | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/thealgorithms/physics/SnellsLaw.java b/src/main/java/com/thealgorithms/physics/SnellsLaw.java index 2570be4fc73b..c31500eac2ca 100644 --- a/src/main/java/com/thealgorithms/physics/SnellsLaw.java +++ b/src/main/java/com/thealgorithms/physics/SnellsLaw.java @@ -5,8 +5,10 @@ * n1 * sin(theta1) = n2 * sin(theta2) * @see Snell's Law */ -public class SnellLaw { - +public final class SnellLaw { + private SnellLaw() { + throw new AssertionError("No instances."); + } /** * Computes the refracted angle (theta2) in radians. * From bc2b66c9425f37cdc65b99aa7ced2ed2780ff5da Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20S=C3=A1nchez=20N=C3=BA=C3=B1ez?= Date: Thu, 4 Dec 2025 21:47:13 +0100 Subject: [PATCH 05/12] Rename SnellsLawTest to SnellLawTest --- src/test/java/com/thealgorithms/physics/SnellLawTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/java/com/thealgorithms/physics/SnellLawTest.java b/src/test/java/com/thealgorithms/physics/SnellLawTest.java index e6044a8ab24d..9eef08e3829b 100644 --- a/src/test/java/com/thealgorithms/physics/SnellLawTest.java +++ b/src/test/java/com/thealgorithms/physics/SnellLawTest.java @@ -3,7 +3,7 @@ import static org.junit.jupiter.api.Assertions.*; import org.junit.jupiter.api.Test; -public class SnellsLawTest { +public class SnellLawTest { //tests for example environmet @Test public void testCalculateRefractionAngle() { From 31c8df5c4dc70e47aa7277a5f4f8249c98aa4aa9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20S=C3=A1nchez=20N=C3=BA=C3=B1ez?= Date: Thu, 4 Dec 2025 21:53:11 +0100 Subject: [PATCH 06/12] Refactor SnellLawTest for clarity and accuracy --- .../thealgorithms/physics/SnellLawTest.java | 37 ++++++++++++------- 1 file changed, 24 insertions(+), 13 deletions(-) diff --git a/src/test/java/com/thealgorithms/physics/SnellLawTest.java b/src/test/java/com/thealgorithms/physics/SnellLawTest.java index 9eef08e3829b..85677d63918c 100644 --- a/src/test/java/com/thealgorithms/physics/SnellLawTest.java +++ b/src/test/java/com/thealgorithms/physics/SnellLawTest.java @@ -4,29 +4,40 @@ import org.junit.jupiter.api.Test; public class SnellLawTest { -//tests for example environmet + @Test - public void testCalculateRefractionAngle() { + public void testRefractedAngle() { double n1 = 1.0; // air double n2 = 1.5; // glass - double theta1 = Math.toRadians(30); // 30 grados - - double theta2 = SnellsLaw.calculateRefractionAngle(n1, n2, theta1); - - // expected value using: sin(theta2) = n1/n2 * sin(theta1) + double theta1 = Math.toRadians(30); + + double theta2 = SnellLaw.refractedAngle(n1, n2, theta1); + double expected = Math.asin((n1 / n2) * Math.sin(theta1)); - assertEquals(expected, theta2, 1e-9); + assertEquals(expected, theta2, 1e-12); } -//tests for total refraction + @Test - public void testInvalidRefractiveIndex() { + public void testTotalInternalReflection() { + // total internal reflection happens when n1 > n2 AND theta1 is large + double n1 = 1.5; + double n2 = 1.0; + double theta1 = Math.toRadians(60); // large enough angle + assertThrows(IllegalArgumentException.class, () -> { - SnellsLaw.calculateRefractionAngle(-1, 1.5, 0.5); + SnellLaw.refractedAngle(n1, n2, theta1); }); + } - assertThrows(IllegalArgumentException.class, () -> { - SnellsLaw.calculateRefractionAngle(1, 0, 0.5); + @Test + public void testNoTotalInternalReflectionAtLowAngles() { + double n1 = 1.5; + double n2 = 1.0; + double theta1 = Math.toRadians(10); + + assertDoesNotThrow(() -> { + SnellLaw.refractedAngle(n1, n2, theta1); }); } } From cfe8791e86ce92abf9c597dcf33c81f349303517 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20S=C3=A1nchez=20N=C3=BA=C3=B1ez?= Date: Fri, 5 Dec 2025 11:19:26 +0100 Subject: [PATCH 07/12] Rename SnellsLaw.java to SnellLaw.java --- .../com/thealgorithms/physics/{SnellsLaw.java => SnellLaw.java} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename src/main/java/com/thealgorithms/physics/{SnellsLaw.java => SnellLaw.java} (100%) diff --git a/src/main/java/com/thealgorithms/physics/SnellsLaw.java b/src/main/java/com/thealgorithms/physics/SnellLaw.java similarity index 100% rename from src/main/java/com/thealgorithms/physics/SnellsLaw.java rename to src/main/java/com/thealgorithms/physics/SnellLaw.java From 2251f27d26d4c5b49a0ecf8394eaaab3e07fef46 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20S=C3=A1nchez=20N=C3=BA=C3=B1ez?= Date: Fri, 5 Dec 2025 11:37:15 +0100 Subject: [PATCH 08/12] Refactor SnellLawTest with additional assertions --- .../java/com/thealgorithms/physics/SnellLawTest.java | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/src/test/java/com/thealgorithms/physics/SnellLawTest.java b/src/test/java/com/thealgorithms/physics/SnellLawTest.java index 85677d63918c..a07122e4f06a 100644 --- a/src/test/java/com/thealgorithms/physics/SnellLawTest.java +++ b/src/test/java/com/thealgorithms/physics/SnellLawTest.java @@ -1,14 +1,17 @@ package com.thealgorithms.physics; -import static org.junit.jupiter.api.Assertions.*; +import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; + import org.junit.jupiter.api.Test; public class SnellLawTest { @Test public void testRefractedAngle() { - double n1 = 1.0; // air - double n2 = 1.5; // glass + double n1 = 1.0; // air + double n2 = 1.5; // glass double theta1 = Math.toRadians(30); double theta2 = SnellLaw.refractedAngle(n1, n2, theta1); @@ -20,10 +23,9 @@ public void testRefractedAngle() { @Test public void testTotalInternalReflection() { - // total internal reflection happens when n1 > n2 AND theta1 is large double n1 = 1.5; double n2 = 1.0; - double theta1 = Math.toRadians(60); // large enough angle + double theta1 = Math.toRadians(60); // large angle assertThrows(IllegalArgumentException.class, () -> { SnellLaw.refractedAngle(n1, n2, theta1); From cbdfa53a1a4182cc6ecc31b039363069719bcd66 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20S=C3=A1nchez=20N=C3=BA=C3=B1ez?= Date: Fri, 5 Dec 2025 11:41:12 +0100 Subject: [PATCH 09/12] Refactor SnellLaw class constructor and error handling Refactor SnellLaw constructor and error message formatting. --- src/main/java/com/thealgorithms/physics/SnellLaw.java | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/thealgorithms/physics/SnellLaw.java b/src/main/java/com/thealgorithms/physics/SnellLaw.java index c31500eac2ca..7d4e1e9cca20 100644 --- a/src/main/java/com/thealgorithms/physics/SnellLaw.java +++ b/src/main/java/com/thealgorithms/physics/SnellLaw.java @@ -6,9 +6,11 @@ * @see Snell's Law */ public final class SnellLaw { - private SnellLaw() { + + private SnellLaw() { throw new AssertionError("No instances."); } + /** * Computes the refracted angle (theta2) in radians. * @@ -23,7 +25,9 @@ public static double refractedAngle(double n1, double n2, double theta1) { double sinTheta2 = ratio * Math.sin(theta1); if (Math.abs(sinTheta2) > 1.0) { - throw new IllegalArgumentException("Total internal reflection: no refraction possible."); + throw new IllegalArgumentException( + "Total internal reflection: no refraction possible." + ); } return Math.asin(sinTheta2); From e131ff9aa1c2379ff87d6c63909ddd236141a99e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20S=C3=A1nchez=20N=C3=BA=C3=B1ez?= Date: Fri, 5 Dec 2025 11:42:59 +0100 Subject: [PATCH 10/12] Fix missing newline at end of SnellLawTest.java Ensure that the SnellLawTest class has a newline at the end of the file. From 0d7efd2deb64177b545c281a6be5bc3258c740ea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20S=C3=A1nchez=20N=C3=BA=C3=B1ez?= Date: Fri, 5 Dec 2025 11:58:34 +0100 Subject: [PATCH 11/12] Simplify assertions in SnellLawTest --- .../java/com/thealgorithms/physics/SnellLawTest.java | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/src/test/java/com/thealgorithms/physics/SnellLawTest.java b/src/test/java/com/thealgorithms/physics/SnellLawTest.java index a07122e4f06a..ddd5fb1d5af7 100644 --- a/src/test/java/com/thealgorithms/physics/SnellLawTest.java +++ b/src/test/java/com/thealgorithms/physics/SnellLawTest.java @@ -16,7 +16,7 @@ public void testRefractedAngle() { double theta2 = SnellLaw.refractedAngle(n1, n2, theta1); - double expected = Math.asin((n1 / n2) * Math.sin(theta1)); + double expected = Math.asin(n1 / n2 * Math.sin(theta1)); assertEquals(expected, theta2, 1e-12); } @@ -27,9 +27,7 @@ public void testTotalInternalReflection() { double n2 = 1.0; double theta1 = Math.toRadians(60); // large angle - assertThrows(IllegalArgumentException.class, () -> { - SnellLaw.refractedAngle(n1, n2, theta1); - }); + assertThrows(IllegalArgumentException.class, () -> SnellLaw.refractedAngle(n1, n2, theta1)); } @Test @@ -38,8 +36,6 @@ public void testNoTotalInternalReflectionAtLowAngles() { double n2 = 1.0; double theta1 = Math.toRadians(10); - assertDoesNotThrow(() -> { - SnellLaw.refractedAngle(n1, n2, theta1); - }); + assertDoesNotThrow(() -> SnellLaw.refractedAngle(n1, n2, theta1)); } } From 796cfe147f5b35499c413df8d21ce4e6e92e1eb9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20S=C3=A1nchez=20N=C3=BA=C3=B1ez?= Date: Fri, 5 Dec 2025 12:10:44 +0100 Subject: [PATCH 12/12] Simplify exception throwing for total internal reflection --- src/main/java/com/thealgorithms/physics/SnellLaw.java | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/main/java/com/thealgorithms/physics/SnellLaw.java b/src/main/java/com/thealgorithms/physics/SnellLaw.java index 7d4e1e9cca20..2736984814fd 100644 --- a/src/main/java/com/thealgorithms/physics/SnellLaw.java +++ b/src/main/java/com/thealgorithms/physics/SnellLaw.java @@ -25,9 +25,7 @@ public static double refractedAngle(double n1, double n2, double theta1) { double sinTheta2 = ratio * Math.sin(theta1); if (Math.abs(sinTheta2) > 1.0) { - throw new IllegalArgumentException( - "Total internal reflection: no refraction possible." - ); + throw new IllegalArgumentException("Total internal reflection: no refraction possible."); } return Math.asin(sinTheta2);