From f86cc75eba9042cd899ad285cc44afd779bffe79 Mon Sep 17 00:00:00 2001 From: alxkm Date: Sat, 24 Aug 2024 22:20:12 +0200 Subject: [PATCH 1/2] refactor: RootPrecision --- .../thealgorithms/others/RootPrecision.java | 43 ++++++------------- .../others/RootPrecisionTest.java | 33 ++++++++++++++ 2 files changed, 46 insertions(+), 30 deletions(-) create mode 100644 src/test/java/com/thealgorithms/others/RootPrecisionTest.java diff --git a/src/main/java/com/thealgorithms/others/RootPrecision.java b/src/main/java/com/thealgorithms/others/RootPrecision.java index bc195ffca5ae..f04f91aaa550 100644 --- a/src/main/java/com/thealgorithms/others/RootPrecision.java +++ b/src/main/java/com/thealgorithms/others/RootPrecision.java @@ -1,38 +1,21 @@ package com.thealgorithms.others; -import java.util.Scanner; - public final class RootPrecision { private RootPrecision() { } - public static void main(String[] args) { - // take input - Scanner scn = new Scanner(System.in); - - // n is the input number - int n = scn.nextInt(); - - // p is precision value for eg - p is 3 in 2.564 and 5 in 3.80870. - int p = scn.nextInt(); - System.out.println(squareRoot(n, p)); - - scn.close(); - } - - public static double squareRoot(int n, int p) { - // rv means return value - double rv; - - double root = Math.pow(n, 0.5); - - // calculate precision to power of 10 and then multiply it with root value. - int precision = (int) Math.pow(10, p); - root = root * precision; - /*typecast it into integer then divide by precision and again typecast into double - so as to have decimal points upto p precision */ - - rv = (int) root; - return rv / precision; + /** + * Calculates the square root of a number with the specified precision. + * + * @param number The number to calculate the square root of. + * @param precision The number of decimal places of precision. + * @return The square root of the number with the specified precision. + */ + public static double calculateSquareRoot(int number, int precision) { + double rawRoot = Math.sqrt(number); // Calculate the square root using Math.sqrt + double scalingFactor = Math.pow(10, precision); // Calculate the scaling factor for precision + + // Scale the square root, truncate the extra decimals, and rescale back + return Math.round(rawRoot * scalingFactor) / scalingFactor; } } diff --git a/src/test/java/com/thealgorithms/others/RootPrecisionTest.java b/src/test/java/com/thealgorithms/others/RootPrecisionTest.java new file mode 100644 index 000000000000..7631948cda92 --- /dev/null +++ b/src/test/java/com/thealgorithms/others/RootPrecisionTest.java @@ -0,0 +1,33 @@ +package com.thealgorithms.others; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import org.junit.jupiter.api.Test; + +public class RootPrecisionTest { + + @Test + public void testSquareRootWithZeroPrecision() { + assertEquals(2.0, RootPrecision.calculateSquareRoot(4, 0)); + assertEquals(3.0, RootPrecision.calculateSquareRoot(9, 0)); + assertEquals(5.0, RootPrecision.calculateSquareRoot(25, 0)); + } + + @Test + public void testSquareRootWithPrecision() { + assertEquals(1.414, RootPrecision.calculateSquareRoot(2, 3)); + assertEquals(3.162, RootPrecision.calculateSquareRoot(10, 3)); + assertEquals(5.000, RootPrecision.calculateSquareRoot(25, 3)); + } + + @Test + public void testSquareRootWithHighPrecision() { + assertEquals(1.41421, RootPrecision.calculateSquareRoot(2, 5)); + assertEquals(3.16228, RootPrecision.calculateSquareRoot(10, 5)); + } + + @Test + public void testSquareRootOfZero() { + assertEquals(0.0, RootPrecision.calculateSquareRoot(0, 3)); + } +} From 27c776ef0fc5831cab085c88bab3737a467903c7 Mon Sep 17 00:00:00 2001 From: alxkm Date: Sun, 25 Aug 2024 10:00:10 +0200 Subject: [PATCH 2/2] refactor: remove RootPrecision class --- DIRECTORY.md | 1 - .../thealgorithms/others/RootPrecision.java | 21 ------------ .../others/RootPrecisionTest.java | 33 ------------------- 3 files changed, 55 deletions(-) delete mode 100644 src/main/java/com/thealgorithms/others/RootPrecision.java delete mode 100644 src/test/java/com/thealgorithms/others/RootPrecisionTest.java diff --git a/DIRECTORY.md b/DIRECTORY.md index 656597c3b20a..30b107a177fb 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -439,7 +439,6 @@ * [RemoveDuplicateFromString](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/others/RemoveDuplicateFromString.java) * [ReturnSubsequence](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/others/ReturnSubsequence.java) * [ReverseStackUsingRecursion](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/others/ReverseStackUsingRecursion.java) - * [RootPrecision](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/others/RootPrecision.java) * [RotateMatrixBy90Degrees](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/others/RotateMatrixBy90Degrees.java) * [SieveOfEratosthenes](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/others/SieveOfEratosthenes.java) * [SkylineProblem](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/others/SkylineProblem.java) diff --git a/src/main/java/com/thealgorithms/others/RootPrecision.java b/src/main/java/com/thealgorithms/others/RootPrecision.java deleted file mode 100644 index f04f91aaa550..000000000000 --- a/src/main/java/com/thealgorithms/others/RootPrecision.java +++ /dev/null @@ -1,21 +0,0 @@ -package com.thealgorithms.others; - -public final class RootPrecision { - private RootPrecision() { - } - - /** - * Calculates the square root of a number with the specified precision. - * - * @param number The number to calculate the square root of. - * @param precision The number of decimal places of precision. - * @return The square root of the number with the specified precision. - */ - public static double calculateSquareRoot(int number, int precision) { - double rawRoot = Math.sqrt(number); // Calculate the square root using Math.sqrt - double scalingFactor = Math.pow(10, precision); // Calculate the scaling factor for precision - - // Scale the square root, truncate the extra decimals, and rescale back - return Math.round(rawRoot * scalingFactor) / scalingFactor; - } -} diff --git a/src/test/java/com/thealgorithms/others/RootPrecisionTest.java b/src/test/java/com/thealgorithms/others/RootPrecisionTest.java deleted file mode 100644 index 7631948cda92..000000000000 --- a/src/test/java/com/thealgorithms/others/RootPrecisionTest.java +++ /dev/null @@ -1,33 +0,0 @@ -package com.thealgorithms.others; - -import static org.junit.jupiter.api.Assertions.assertEquals; - -import org.junit.jupiter.api.Test; - -public class RootPrecisionTest { - - @Test - public void testSquareRootWithZeroPrecision() { - assertEquals(2.0, RootPrecision.calculateSquareRoot(4, 0)); - assertEquals(3.0, RootPrecision.calculateSquareRoot(9, 0)); - assertEquals(5.0, RootPrecision.calculateSquareRoot(25, 0)); - } - - @Test - public void testSquareRootWithPrecision() { - assertEquals(1.414, RootPrecision.calculateSquareRoot(2, 3)); - assertEquals(3.162, RootPrecision.calculateSquareRoot(10, 3)); - assertEquals(5.000, RootPrecision.calculateSquareRoot(25, 3)); - } - - @Test - public void testSquareRootWithHighPrecision() { - assertEquals(1.41421, RootPrecision.calculateSquareRoot(2, 5)); - assertEquals(3.16228, RootPrecision.calculateSquareRoot(10, 5)); - } - - @Test - public void testSquareRootOfZero() { - assertEquals(0.0, RootPrecision.calculateSquareRoot(0, 3)); - } -}