diff --git a/src/main/java/com/thealgorithms/backtracking/ParenthesesGenerator.java b/src/main/java/com/thealgorithms/backtracking/ParenthesesGenerator.java index bf93f946ab7b..3939f20b28b3 100644 --- a/src/main/java/com/thealgorithms/backtracking/ParenthesesGenerator.java +++ b/src/main/java/com/thealgorithms/backtracking/ParenthesesGenerator.java @@ -1,5 +1,7 @@ package com.thealgorithms.backtracking; +import static com.thealgorithms.maths.utils.MathsUtil.checkInputIsPositive; + import java.util.ArrayList; import java.util.List; @@ -17,10 +19,9 @@ private ParenthesesGenerator() { * @return A list of strings representing valid combinations of parentheses. * @throws IllegalArgumentException if n is less than 0. */ - public static List generateParentheses(final int n) { - if (n < 0) { - throw new IllegalArgumentException("The number of pairs of parentheses cannot be negative"); - } + public static List generateParentheses(final int n) { + checkInputIsPositive(n, "The number of pairs of parentheses cannot be negative."); + List result = new ArrayList<>(); generateParenthesesHelper(result, "", 0, 0, n); return result; diff --git a/src/main/java/com/thealgorithms/bitmanipulation/HighestSetBit.java b/src/main/java/com/thealgorithms/bitmanipulation/HighestSetBit.java index 2398b8214371..6e5e10ecab68 100644 --- a/src/main/java/com/thealgorithms/bitmanipulation/HighestSetBit.java +++ b/src/main/java/com/thealgorithms/bitmanipulation/HighestSetBit.java @@ -1,5 +1,7 @@ package com.thealgorithms.bitmanipulation; +import static com.thealgorithms.maths.utils.MathsUtil.checkInputIsPositive; + import java.util.Optional; /** @@ -34,10 +36,8 @@ private HighestSetBit() { * Returns {@link Optional#empty()} if the number is 0. * @throws IllegalArgumentException if the input number is negative. */ - public static Optional findHighestSetBit(int num) { - if (num < 0) { - throw new IllegalArgumentException("Input cannot be negative"); - } + public static Optional findHighestSetBit(int num) { + checkInputIsPositive(num, "Input cannot be negative."); if (num == 0) { return Optional.empty(); diff --git a/src/main/java/com/thealgorithms/datastructures/buffers/CircularBuffer.java b/src/main/java/com/thealgorithms/datastructures/buffers/CircularBuffer.java index b709e16fd1f6..11acfb46a471 100644 --- a/src/main/java/com/thealgorithms/datastructures/buffers/CircularBuffer.java +++ b/src/main/java/com/thealgorithms/datastructures/buffers/CircularBuffer.java @@ -1,5 +1,7 @@ package com.thealgorithms.datastructures.buffers; +import static com.thealgorithms.maths.utils.MathsUtil.checkInputIsPositive; + import java.util.concurrent.atomic.AtomicInteger; /** @@ -23,9 +25,8 @@ public class CircularBuffer { * @throws IllegalArgumentException if the size is zero or negative. */ public CircularBuffer(int size) { - if (size <= 0) { - throw new IllegalArgumentException("Buffer size must be positive"); - } + checkInputIsPositive(size, "Buffer size must be positive."); + // noinspection unchecked this.buffer = (Item[]) new Object[size]; this.putPointer = new CircularPointer(0, size); diff --git a/src/main/java/com/thealgorithms/datastructures/graphs/BoruvkaAlgorithm.java b/src/main/java/com/thealgorithms/datastructures/graphs/BoruvkaAlgorithm.java index dcdb08ad133e..f462bb00bbba 100644 --- a/src/main/java/com/thealgorithms/datastructures/graphs/BoruvkaAlgorithm.java +++ b/src/main/java/com/thealgorithms/datastructures/graphs/BoruvkaAlgorithm.java @@ -1,5 +1,7 @@ package com.thealgorithms.datastructures.graphs; +import static com.thealgorithms.maths.utils.MathsUtil.checkInputIsPositive; + import java.util.ArrayList; import java.util.List; @@ -43,9 +45,8 @@ static class Graph { * @param edges list of edges */ Graph(final int vertex, final List edges) { - if (vertex < 0) { - throw new IllegalArgumentException("Number of vertices must be positive"); - } + checkInputIsPositive(vertex, "Number of vertices must be positive."); + if (edges == null || edges.isEmpty()) { throw new IllegalArgumentException("Edges list must not be null or empty"); } diff --git a/src/main/java/com/thealgorithms/maths/Area.java b/src/main/java/com/thealgorithms/geometry/Area.java similarity index 82% rename from src/main/java/com/thealgorithms/maths/Area.java rename to src/main/java/com/thealgorithms/geometry/Area.java index 7a06fd5e5fa0..3e530f4d6a93 100644 --- a/src/main/java/com/thealgorithms/maths/Area.java +++ b/src/main/java/com/thealgorithms/geometry/Area.java @@ -1,4 +1,6 @@ -package com.thealgorithms.maths; +package com.thealgorithms.geometry; + +import static com.thealgorithms.maths.utils.MathsUtil.checkInputIsPositive; /** * Find the area of various geometric shapes @@ -89,9 +91,8 @@ public static double surfaceAreaCylinder(final double radius, final double heigh * @return area of given square */ public static double surfaceAreaSquare(final double sideLength) { - if (sideLength <= 0) { - throw new IllegalArgumentException("Must be a positive sideLength"); - } + checkInputIsPositive(sideLength, "Must be a positive sideLength."); + return sideLength * sideLength; } @@ -103,12 +104,8 @@ public static double surfaceAreaSquare(final double sideLength) { * @return area of given triangle */ public static double surfaceAreaTriangle(final double base, final double height) { - if (base <= 0) { - throw new IllegalArgumentException(POSITIVE_BASE); - } - if (height <= 0) { - throw new IllegalArgumentException(POSITIVE_HEIGHT); - } + checkInputIsPositive(base, POSITIVE_BASE); + checkInputIsPositive(height, POSITIVE_HEIGHT); return base * height / 2; } @@ -120,12 +117,8 @@ public static double surfaceAreaTriangle(final double base, final double height) * @return area of given parallelogram */ public static double surfaceAreaParallelogram(final double base, final double height) { - if (base <= 0) { - throw new IllegalArgumentException(POSITIVE_BASE); - } - if (height <= 0) { - throw new IllegalArgumentException(POSITIVE_HEIGHT); - } + checkInputIsPositive(base, POSITIVE_BASE); + checkInputIsPositive(height, POSITIVE_HEIGHT); return base * height; } @@ -144,9 +137,7 @@ public static double surfaceAreaTrapezium(final double base1, final double base2 if (base2 <= 0) { throw new IllegalArgumentException(POSITIVE_BASE + 2); } - if (height <= 0) { - throw new IllegalArgumentException(POSITIVE_HEIGHT); - } + checkInputIsPositive(height, POSITIVE_HEIGHT); return (base1 + base2) * height / 2; } @@ -157,9 +148,8 @@ public static double surfaceAreaTrapezium(final double base1, final double base2 * @return area of given circle */ public static double surfaceAreaCircle(final double radius) { - if (radius <= 0) { - throw new IllegalArgumentException(POSITIVE_RADIUS); - } + checkInputIsPositive(radius, POSITIVE_RADIUS); + return Math.PI * radius * radius; } @@ -170,9 +160,8 @@ public static double surfaceAreaCircle(final double radius) { * @return surface area of given hemisphere */ public static double surfaceAreaHemisphere(final double radius) { - if (radius <= 0) { - throw new IllegalArgumentException(POSITIVE_RADIUS); - } + checkInputIsPositive(radius, POSITIVE_RADIUS); + return 3 * Math.PI * radius * radius; } @@ -184,12 +173,9 @@ public static double surfaceAreaHemisphere(final double radius) { * @return surface area of given cone. */ public static double surfaceAreaCone(final double radius, final double height) { - if (radius <= 0) { - throw new IllegalArgumentException(POSITIVE_RADIUS); - } - if (height <= 0) { - throw new IllegalArgumentException(POSITIVE_HEIGHT); - } + checkInputIsPositive(radius, POSITIVE_RADIUS); + checkInputIsPositive(height, POSITIVE_HEIGHT); + return Math.PI * radius * (radius + Math.pow(height * height + radius * radius, 0.5)); } } diff --git a/src/main/java/com/thealgorithms/maths/Volume.java b/src/main/java/com/thealgorithms/geometry/Volume.java similarity index 98% rename from src/main/java/com/thealgorithms/maths/Volume.java rename to src/main/java/com/thealgorithms/geometry/Volume.java index 0f282b2abae2..42029d0a4b32 100644 --- a/src/main/java/com/thealgorithms/maths/Volume.java +++ b/src/main/java/com/thealgorithms/geometry/Volume.java @@ -1,4 +1,4 @@ -package com.thealgorithms.maths; +package com.thealgorithms.geometry; /* Calculate the volume of various shapes.*/ public final class Volume { diff --git a/src/main/java/com/thealgorithms/maths/Armstrong.java b/src/main/java/com/thealgorithms/maths/Armstrong.java index 9a7a014ec99f..758c4a6ffd42 100644 --- a/src/main/java/com/thealgorithms/maths/Armstrong.java +++ b/src/main/java/com/thealgorithms/maths/Armstrong.java @@ -1,5 +1,7 @@ package com.thealgorithms.maths; +import static com.thealgorithms.maths.utils.MathsUtil.checkInputIsPositive; + /** * This class checks whether a given number is an Armstrong number or not. * An Armstrong number is a number that is equal to the sum of its own digits, @@ -21,9 +23,8 @@ public class Armstrong { * @return {@code true} if the given number is an Armstrong number, {@code false} otherwise */ public boolean isArmstrong(int number) { - if (number < 0) { - return false; // Negative numbers cannot be Armstrong numbers - } + checkInputIsPositive(number, "Negative numbers cannot be Armstrong numbers."); + long sum = 0; int totalDigits = (int) Math.log10(number) + 1; // get the length of the number (number of digits) long originalNumber = number; diff --git a/src/main/java/com/thealgorithms/maths/Combinations.java b/src/main/java/com/thealgorithms/maths/Combinations.java index 2b4a78613190..97924e94fae0 100644 --- a/src/main/java/com/thealgorithms/maths/Combinations.java +++ b/src/main/java/com/thealgorithms/maths/Combinations.java @@ -1,5 +1,7 @@ package com.thealgorithms.maths; +import static com.thealgorithms.maths.utils.MathsUtil.checkInputIsPositive; + /** * @see Combination */ @@ -14,9 +16,8 @@ private Combinations() { * @return factorial of given number */ public static long factorial(int n) { - if (n < 0) { - throw new IllegalArgumentException("number is negative"); - } + checkInputIsPositive(n, "Input number cannot be negative."); + return n == 0 || n == 1 ? 1 : n * factorial(n - 1); } diff --git a/src/main/java/com/thealgorithms/maths/DudeneyNumber.java b/src/main/java/com/thealgorithms/maths/DudeneyNumber.java index acf1e55d49c8..6a47f139b322 100644 --- a/src/main/java/com/thealgorithms/maths/DudeneyNumber.java +++ b/src/main/java/com/thealgorithms/maths/DudeneyNumber.java @@ -6,17 +6,18 @@ */ package com.thealgorithms.maths; +import static com.thealgorithms.maths.PerfectCube.getCubeRoot; +import static com.thealgorithms.maths.utils.MathsUtil.checkInputIsPositive; + public final class DudeneyNumber { private DudeneyNumber() { } // returns True if the number is a Dudeney number and False if it is not a Dudeney number. public static boolean isDudeney(final int n) { - if (n <= 0) { - throw new IllegalArgumentException("Input must me positive."); - } + checkInputIsPositive(n, "Input number should be positive."); // Calculating Cube Root - final int cubeRoot = (int) Math.round(Math.pow(n, 1.0 / 3.0)); + final int cubeRoot = getCubeRoot(n); // If the number is not a perfect cube the method returns false. if (cubeRoot * cubeRoot * cubeRoot != n) { return false; diff --git a/src/main/java/com/thealgorithms/maths/EulersFunction.java b/src/main/java/com/thealgorithms/maths/EulersFunction.java index 3a6bc8756681..d312f72ef447 100644 --- a/src/main/java/com/thealgorithms/maths/EulersFunction.java +++ b/src/main/java/com/thealgorithms/maths/EulersFunction.java @@ -1,24 +1,14 @@ package com.thealgorithms.maths; +import static com.thealgorithms.maths.utils.MathsUtil.checkInputIsPositive; + /** * Utility class for computing * Euler's totient function. */ public final class EulersFunction { private EulersFunction() { - } - - /** - * Validates that the input is a positive integer. - * - * @param n the input number to validate - * @throws IllegalArgumentException if {@code n} is non-positive - */ - private static void checkInput(int n) { - if (n <= 0) { - throw new IllegalArgumentException("n must be positive."); - } - } + } /** * Computes the value of Euler's totient function for a given input. @@ -29,7 +19,7 @@ private static void checkInput(int n) { * @throws IllegalArgumentException if {@code n} is non-positive */ public static int getEuler(int n) { - checkInput(n); + checkInputIsPositive(n, "Input number should be positive."); int result = n; for (int i = 2; i * i <= n; i++) { if (n % i == 0) { diff --git a/src/main/java/com/thealgorithms/maths/Factorial.java b/src/main/java/com/thealgorithms/maths/Factorial.java index 511cc1f84f05..f0c67167cf53 100644 --- a/src/main/java/com/thealgorithms/maths/Factorial.java +++ b/src/main/java/com/thealgorithms/maths/Factorial.java @@ -1,5 +1,7 @@ package com.thealgorithms.maths; +import static com.thealgorithms.maths.utils.MathsUtil.checkInputIsPositive; + public final class Factorial { private Factorial() { } @@ -11,9 +13,7 @@ private Factorial() { * @return the factorial of {@code n} */ public static long factorial(int n) { - if (n < 0) { - throw new IllegalArgumentException("Input number cannot be negative"); - } + checkInputIsPositive(n, ""); long factorial = 1; for (int i = 1; i <= n; ++i) { factorial *= i; diff --git a/src/main/java/com/thealgorithms/maths/FibonacciNumberCheck.java b/src/main/java/com/thealgorithms/maths/FibonacciNumberCheck.java index 781275d3130d..af587de92060 100644 --- a/src/main/java/com/thealgorithms/maths/FibonacciNumberCheck.java +++ b/src/main/java/com/thealgorithms/maths/FibonacciNumberCheck.java @@ -1,5 +1,7 @@ package com.thealgorithms.maths; +import static com.thealgorithms.maths.PerfectSquare.isPerfectSquare; + /** * Fibonacci: 0 1 1 2 3 5 8 13 21 ... * This code checks Fibonacci Numbers up to 45th number. @@ -8,17 +10,6 @@ public final class FibonacciNumberCheck { private FibonacciNumberCheck() { } - /** - * Check if a number is perfect square number - * - * @param number the number to be checked - * @return true if {@code number} is a perfect square, otherwise - * false - */ - public static boolean isPerfectSquare(long number) { - long sqrt = (long) Math.sqrt(number); - return sqrt * sqrt == number; - } /** * Check if a number is a Fibonacci number. This is true if and only if at @@ -29,9 +20,9 @@ public static boolean isPerfectSquare(long number) { * false * @link https://en.wikipedia.org/wiki/Fibonacci_number#Identification */ - public static boolean isFibonacciNumber(long number) { - long value1 = 5 * number * number + 4; - long value2 = 5 * number * number - 4; + public static boolean isFibonacciNumber(int number) { + int value1 = 5 * number * number + 4; + int value2 = 5 * number * number - 4; return isPerfectSquare(value1) || isPerfectSquare(value2); } } diff --git a/src/main/java/com/thealgorithms/maths/PalindromeNumber.java b/src/main/java/com/thealgorithms/maths/PalindromeNumber.java index a22d63897b37..014b34ff8e63 100644 --- a/src/main/java/com/thealgorithms/maths/PalindromeNumber.java +++ b/src/main/java/com/thealgorithms/maths/PalindromeNumber.java @@ -10,17 +10,8 @@ private PalindromeNumber() { * @return {@code true} if {@code n} is palindrome number, otherwise * {@code false} */ - public static boolean isPalindrome(int number) { - if (number < 0) { - throw new IllegalArgumentException("Input parameter must not be negative!"); - } - int numberCopy = number; - int reverseNumber = 0; - while (numberCopy != 0) { - int remainder = numberCopy % 10; - reverseNumber = reverseNumber * 10 + remainder; - numberCopy /= 10; - } + public static boolean isPalindrome(int number) { + int reverseNumber = ReverseNumber.reverseNumber(number); return number == reverseNumber; } } diff --git a/src/main/java/com/thealgorithms/maths/PerfectCube.java b/src/main/java/com/thealgorithms/maths/PerfectCube.java index 4104c6238580..960de32caab9 100644 --- a/src/main/java/com/thealgorithms/maths/PerfectCube.java +++ b/src/main/java/com/thealgorithms/maths/PerfectCube.java @@ -7,6 +7,16 @@ public final class PerfectCube { private PerfectCube() { } + /** + * Get cuberoot of a number + * + * @param number number to get cube root of + */ + public static int getCubeRoot(int number) { + number = Math.abs(number); // converting negative number to positive number + return (int) Math.pow(number, 1.0 / 3); + } + /** * Check if a number is perfect cube or not * @@ -15,8 +25,7 @@ private PerfectCube() { * {@code false} */ public static boolean isPerfectCube(int number) { - number = Math.abs(number); // converting negative number to positive number - int a = (int) Math.pow(number, 1.0 / 3); + int a = getCubeRoot(number); return a * a * a == number; } diff --git a/src/main/java/com/thealgorithms/maths/Pow.java b/src/main/java/com/thealgorithms/maths/Pow.java index 377b37ac4569..78ebd7680f86 100644 --- a/src/main/java/com/thealgorithms/maths/Pow.java +++ b/src/main/java/com/thealgorithms/maths/Pow.java @@ -1,5 +1,7 @@ package com.thealgorithms.maths; +import static com.thealgorithms.maths.utils.MathsUtil.checkInputIsPositive; + /** * A utility class for computing exponentiation (power) of integers. *

@@ -24,9 +26,8 @@ private Pow() { * @throws IllegalArgumentException if {@code b} is negative. */ public static long pow(int a, int b) { - if (b < 0) { - throw new IllegalArgumentException("Exponent must be non-negative."); - } + checkInputIsPositive(b, "Exponent must be non-negative."); + long result = 1; for (int i = 1; i <= b; i++) { result *= a; diff --git a/src/main/java/com/thealgorithms/misc/PalindromePrime.java b/src/main/java/com/thealgorithms/maths/Prime/PalindromePrime.java similarity index 80% rename from src/main/java/com/thealgorithms/misc/PalindromePrime.java rename to src/main/java/com/thealgorithms/maths/Prime/PalindromePrime.java index 164e957a9d12..879ef3a98735 100644 --- a/src/main/java/com/thealgorithms/misc/PalindromePrime.java +++ b/src/main/java/com/thealgorithms/maths/Prime/PalindromePrime.java @@ -1,8 +1,10 @@ -package com.thealgorithms.misc; +package com.thealgorithms.maths.Prime; import java.util.ArrayList; import java.util.List; +import com.thealgorithms.maths.ReverseNumber; + public final class PalindromePrime { private PalindromePrime() { } @@ -26,15 +28,6 @@ public static boolean prime(int num) { return true; } - public static int reverse(int n) { - int reverse = 0; - while (n != 0) { - reverse = reverse * 10 + (n % 10); - n /= 10; - } - return reverse; - } - public static List generatePalindromePrimes(int n) { List palindromicPrimes = new ArrayList<>(); if (n <= 0) { @@ -46,7 +39,7 @@ public static List generatePalindromePrimes(int n) { int num = 3; while (count < n) { - if (num == reverse(num) && prime(num)) { + if (num == ReverseNumber.reverseNumber(num) && prime(num)) { palindromicPrimes.add(num); count++; } diff --git a/src/main/java/com/thealgorithms/maths/TwinPrime.java b/src/main/java/com/thealgorithms/maths/Prime/TwinPrime.java similarity index 88% rename from src/main/java/com/thealgorithms/maths/TwinPrime.java rename to src/main/java/com/thealgorithms/maths/Prime/TwinPrime.java index f4e546a2d7a4..cf7fd6554a3e 100644 --- a/src/main/java/com/thealgorithms/maths/TwinPrime.java +++ b/src/main/java/com/thealgorithms/maths/Prime/TwinPrime.java @@ -1,4 +1,4 @@ -package com.thealgorithms.maths; +package com.thealgorithms.maths.Prime; /* * Java program to find 'twin prime' of a prime number * Twin Prime: Twin prime of a number n is (n+2) @@ -9,8 +9,6 @@ * * */ -import com.thealgorithms.maths.Prime.PrimeCheck; - public final class TwinPrime { private TwinPrime() { } @@ -21,7 +19,7 @@ private TwinPrime() { * @param inputNumber Integer value of which twin prime is to be found * @return (number + 2) if number and (number + 2) are prime, -1 otherwise */ - static int getTwinPrime(int inputNumber) { + public static int getTwinPrime(int inputNumber) { // if inputNumber and (inputNumber + 2) are both prime // then return (inputNumber + 2) as a result diff --git a/src/main/java/com/thealgorithms/maths/ReverseNumber.java b/src/main/java/com/thealgorithms/maths/ReverseNumber.java index 667a0a43fc2c..5b0ed5c5a3c1 100644 --- a/src/main/java/com/thealgorithms/maths/ReverseNumber.java +++ b/src/main/java/com/thealgorithms/maths/ReverseNumber.java @@ -1,5 +1,7 @@ package com.thealgorithms.maths; +import static com.thealgorithms.maths.utils.MathsUtil.checkInputIsPositive; + /** * @brief utility class reversing numbers */ @@ -13,10 +15,8 @@ private ReverseNumber() { * @exception IllegalArgumentException number is negative * @return the number created by reversing the order of digits of the input number */ - public static int reverseNumber(int number) { - if (number < 0) { - throw new IllegalArgumentException("number must be nonnegative."); - } + public static int reverseNumber(int number) { + checkInputIsPositive(number, "Input number should be positive."); int result = 0; while (number > 0) { diff --git a/src/main/java/com/thealgorithms/maths/utils/MathsUtil.java b/src/main/java/com/thealgorithms/maths/utils/MathsUtil.java new file mode 100644 index 000000000000..7fd42810fcb1 --- /dev/null +++ b/src/main/java/com/thealgorithms/maths/utils/MathsUtil.java @@ -0,0 +1,17 @@ +package com.thealgorithms.maths.utils; + +public class MathsUtil { + private MathsUtil() { + } + /** + * Validates that the input is a positive integer. + * + * @param n the input number to validate + * @throws IllegalArgumentException if {@code n} is non-positive + */ + public static void checkInputIsPositive(double n, String errorMessage) { + if (n <= 0) { + throw new IllegalArgumentException(errorMessage); + } + } +} diff --git a/src/main/java/com/thealgorithms/matrix/MatrixTranspose.java b/src/main/java/com/thealgorithms/matrix/MatrixTranspose.java index f91ebc10b8a9..b42c144cea68 100644 --- a/src/main/java/com/thealgorithms/matrix/MatrixTranspose.java +++ b/src/main/java/com/thealgorithms/matrix/MatrixTranspose.java @@ -1,5 +1,7 @@ package com.thealgorithms.matrix; +import static com.thealgorithms.matrix.utils.MatrixUtil.validateInputMatrix; + /** * * @@ -28,14 +30,12 @@ private MatrixTranspose() { * @throws NullPointerException if the matrix is null * @return The transposed matrix */ - public static int[][] transpose(int[][] matrix) { - if (matrix == null || matrix.length == 0) { - throw new IllegalArgumentException("Matrix is empty"); - } + public static double[][] transpose(double[][] matrix) { + validateInputMatrix(matrix); int rows = matrix.length; int cols = matrix[0].length; - int[][] transposedMatrix = new int[cols][rows]; + double[][] transposedMatrix = new double[cols][rows]; for (int i = 0; i < cols; i++) { for (int j = 0; j < rows; j++) { transposedMatrix[i][j] = matrix[j][i]; diff --git a/src/main/java/com/thealgorithms/matrix/RotateMatrixBy90Degrees.java b/src/main/java/com/thealgorithms/matrix/RotateMatrixBy90Degrees.java index 9a7f255282ac..821d2ce60ed4 100644 --- a/src/main/java/com/thealgorithms/matrix/RotateMatrixBy90Degrees.java +++ b/src/main/java/com/thealgorithms/matrix/RotateMatrixBy90Degrees.java @@ -1,73 +1,35 @@ -package com.thealgorithms.matrix; - -import java.util.Scanner; -/** - * Given a matrix of size n x n We have to rotate this matrix by 90 Degree Here - * is the algorithm for this problem . - */ -final class RotateMatrixBy90Degrees { - private RotateMatrixBy90Degrees() { - } - - public static void main(String[] args) { - Scanner sc = new Scanner(System.in); - int t = sc.nextInt(); - - while (t-- > 0) { - int n = sc.nextInt(); - int[][] arr = new int[n][n]; - - for (int i = 0; i < n; i++) { - for (int j = 0; j < n; j++) { - arr[i][j] = sc.nextInt(); - } - } - - Rotate.rotate(arr); - printMatrix(arr); - } - sc.close(); - } - - static void printMatrix(int[][] arr) { - for (int i = 0; i < arr.length; i++) { - for (int j = 0; j < arr[0].length; j++) { - System.out.print(arr[i][j] + " "); - } - System.out.println(""); - } - } -} - -/** - * Class containing the algo to roate matrix by 90 degree - */ -final class Rotate { - private Rotate() { - } - - static void rotate(int[][] a) { - int n = a.length; - for (int i = 0; i < n; i++) { - for (int j = 0; j < n; j++) { - if (i > j) { - int temp = a[i][j]; - a[i][j] = a[j][i]; - a[j][i] = temp; - } - } - } - int i = 0; - int k = n - 1; - while (i < k) { - for (int j = 0; j < n; j++) { - int temp = a[i][j]; - a[i][j] = a[k][j]; - a[k][j] = temp; - } - - i++; - k--; - } - } -} +package com.thealgorithms.matrix; + +/** + * Given a matrix of size n x n We have to rotate this matrix by 90 Degree Here + * is the algorithm for this problem . + */ +final class RotateMatrixBy90Degrees { + private RotateMatrixBy90Degrees() { + } + + public static void rotate(int[][] a) { + int n = a.length; + for (int i = 0; i < n; i++) { + for (int j = 0; j < n; j++) { + if (i > j) { + int temp = a[i][j]; + a[i][j] = a[j][i]; + a[j][i] = temp; + } + } + } + int i = 0; + int k = n - 1; + while (i < k) { + for (int j = 0; j < n; j++) { + int temp = a[i][j]; + a[i][j] = a[k][j]; + a[k][j] = temp; + } + + i++; + k--; + } + } +} \ No newline at end of file diff --git a/src/main/java/com/thealgorithms/misc/Sparsity.java b/src/main/java/com/thealgorithms/matrix/Sparsity.java similarity index 85% rename from src/main/java/com/thealgorithms/misc/Sparsity.java rename to src/main/java/com/thealgorithms/matrix/Sparsity.java index 08e50a121da4..d327f148b448 100644 --- a/src/main/java/com/thealgorithms/misc/Sparsity.java +++ b/src/main/java/com/thealgorithms/matrix/Sparsity.java @@ -1,4 +1,6 @@ -package com.thealgorithms.misc; +package com.thealgorithms.matrix; + +import static com.thealgorithms.matrix.utils.MatrixUtil.validateInputMatrix; /* *A matrix is sparse if many of its coefficients are zero (In general if 2/3rd of matrix elements @@ -21,9 +23,7 @@ private Sparsity() { * */ static double sparsity(double[][] mat) { - if (mat == null || mat.length == 0) { - throw new IllegalArgumentException("Matrix cannot be null or empty"); - } + validateInputMatrix(mat); int zero = 0; // Traversing the matrix to count number of zeroes diff --git a/src/main/java/com/thealgorithms/matrix/utils/MatrixUtil.java b/src/main/java/com/thealgorithms/matrix/utils/MatrixUtil.java index 5ff9e37f6b9a..73e4c611afc2 100644 --- a/src/main/java/com/thealgorithms/matrix/utils/MatrixUtil.java +++ b/src/main/java/com/thealgorithms/matrix/utils/MatrixUtil.java @@ -26,9 +26,16 @@ private static boolean canMultiply(final BigDecimal[][] matrix1, final BigDecima return isValid(matrix1) && isValid(matrix2) && matrix1[0].length == matrix2.length; } + /** + * Validate the transpose of the given matrix. + * + * @param matrix The matrix to be validated + * @throws IllegalArgumentException if the matrix is empty + * @throws NullPointerException if the matrix is null + */ public static void validateInputMatrix(double[][] matrix) { if (matrix == null) { - throw new IllegalArgumentException("The input matrix cannot be null"); + throw new NullPointerException("The input matrix cannot be null"); } if (matrix.length == 0) { throw new IllegalArgumentException("The input matrix cannot be empty"); diff --git a/src/main/java/com/thealgorithms/maths/FactorialRecursion.java b/src/main/java/com/thealgorithms/recursion/FactorialRecursion.java similarity index 64% rename from src/main/java/com/thealgorithms/maths/FactorialRecursion.java rename to src/main/java/com/thealgorithms/recursion/FactorialRecursion.java index d9bafd1e39e9..2023e0275e78 100644 --- a/src/main/java/com/thealgorithms/maths/FactorialRecursion.java +++ b/src/main/java/com/thealgorithms/recursion/FactorialRecursion.java @@ -1,4 +1,6 @@ -package com.thealgorithms.maths; +package com.thealgorithms.recursion; + +import static com.thealgorithms.maths.utils.MathsUtil.checkInputIsPositive; public final class FactorialRecursion { private FactorialRecursion() { @@ -10,9 +12,8 @@ private FactorialRecursion() { * @return The factorial of the number */ public static long factorial(int n) { - if (n < 0) { - throw new IllegalArgumentException("number is negative"); - } + checkInputIsPositive(n, "Input number should be positive."); + return n == 0 || n == 1 ? 1 : n * factorial(n - 1); } } diff --git a/src/test/java/com/thealgorithms/maths/AreaTest.java b/src/test/java/com/thealgorithms/geometry/AreaTest.java similarity index 99% rename from src/test/java/com/thealgorithms/maths/AreaTest.java rename to src/test/java/com/thealgorithms/geometry/AreaTest.java index b28afb85fbc3..5ecee12473ca 100644 --- a/src/test/java/com/thealgorithms/maths/AreaTest.java +++ b/src/test/java/com/thealgorithms/geometry/AreaTest.java @@ -1,4 +1,4 @@ -package com.thealgorithms.maths; +package com.thealgorithms.geometry; import static org.junit.jupiter.api.Assertions.assertAll; import static org.junit.jupiter.api.Assertions.assertEquals; diff --git a/src/test/java/com/thealgorithms/maths/VolumeTest.java b/src/test/java/com/thealgorithms/geometry/VolumeTest.java similarity index 96% rename from src/test/java/com/thealgorithms/maths/VolumeTest.java rename to src/test/java/com/thealgorithms/geometry/VolumeTest.java index 7cd0c6716147..ecfdd6d992fc 100644 --- a/src/test/java/com/thealgorithms/maths/VolumeTest.java +++ b/src/test/java/com/thealgorithms/geometry/VolumeTest.java @@ -1,4 +1,4 @@ -package com.thealgorithms.maths; +package com.thealgorithms.geometry; import static org.junit.jupiter.api.Assertions.assertTrue; diff --git a/src/test/java/com/thealgorithms/misc/PalindromePrimeTest.java b/src/test/java/com/thealgorithms/maths/prime/PalindromePrimeTest.java similarity index 82% rename from src/test/java/com/thealgorithms/misc/PalindromePrimeTest.java rename to src/test/java/com/thealgorithms/maths/prime/PalindromePrimeTest.java index 130cd19b47b1..d6899381fc56 100644 --- a/src/test/java/com/thealgorithms/misc/PalindromePrimeTest.java +++ b/src/test/java/com/thealgorithms/maths/prime/PalindromePrimeTest.java @@ -1,9 +1,11 @@ -package com.thealgorithms.misc; +package com.thealgorithms.maths.prime; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertTrue; +import static com.thealgorithms.maths.ReverseNumber.reverseNumber; +import com.thealgorithms.maths.Prime.PalindromePrime; import java.util.List; import org.junit.jupiter.api.Test; @@ -27,9 +29,9 @@ public void testPrimeWithNonPrimeNumbers() { @Test public void testReverse() { - assertEquals(123, PalindromePrime.reverse(321), "Reverse of 321 should be 123"); - assertEquals(7, PalindromePrime.reverse(7), "Reverse of 7 should be 7"); - assertEquals(1221, PalindromePrime.reverse(1221), "Reverse of 1221 should be 1221"); + assertEquals(123, reverseNumber(321), "Reverse of 321 should be 123"); + assertEquals(7, reverseNumber(7), "Reverse of 7 should be 7"); + assertEquals(1221, reverseNumber(1221), "Reverse of 1221 should be 1221"); } @Test diff --git a/src/test/java/com/thealgorithms/maths/TwinPrimeTest.java b/src/test/java/com/thealgorithms/maths/prime/TwinPrimeTest.java similarity index 93% rename from src/test/java/com/thealgorithms/maths/TwinPrimeTest.java rename to src/test/java/com/thealgorithms/maths/prime/TwinPrimeTest.java index e214b0ada347..e055bab504b0 100644 --- a/src/test/java/com/thealgorithms/maths/TwinPrimeTest.java +++ b/src/test/java/com/thealgorithms/maths/prime/TwinPrimeTest.java @@ -1,7 +1,8 @@ -package com.thealgorithms.maths; +package com.thealgorithms.maths.prime; import static org.junit.jupiter.api.Assertions.assertEquals; +import com.thealgorithms.maths.Prime.TwinPrime; import org.junit.jupiter.api.Test; class TwinPrimeTest { diff --git a/src/test/java/com/thealgorithms/matrix/MatrixTransposeTest.java b/src/test/java/com/thealgorithms/matrix/MatrixTransposeTest.java index 0457f31418cf..71057a1c7622 100644 --- a/src/test/java/com/thealgorithms/matrix/MatrixTransposeTest.java +++ b/src/test/java/com/thealgorithms/matrix/MatrixTransposeTest.java @@ -21,13 +21,13 @@ private static Stream provideInvalidMatrixTestCases() { @ParameterizedTest(name = "Test case {index}: {2}") @MethodSource("provideValidMatrixTestCases") - void testValidMatrixTranspose(int[][] input, int[][] expected, String description) { + void testValidMatrixTranspose(double[][] input, double[][] expected, String description) { assertArrayEquals(expected, MatrixTranspose.transpose(input), description); } @ParameterizedTest(name = "Test case {index}: {1}") @MethodSource("provideInvalidMatrixTestCases") - void testInvalidMatrixTranspose(int[][] input, String description) { + void testInvalidMatrixTranspose(double[][] input, String description) { assertThrows(IllegalArgumentException.class, () -> MatrixTranspose.transpose(input), description); } } diff --git a/src/test/java/com/thealgorithms/misc/SparsityTest.java b/src/test/java/com/thealgorithms/matrix/SparsityTest.java similarity index 98% rename from src/test/java/com/thealgorithms/misc/SparsityTest.java rename to src/test/java/com/thealgorithms/matrix/SparsityTest.java index b93e4f44937d..057ea1fd7672 100644 --- a/src/test/java/com/thealgorithms/misc/SparsityTest.java +++ b/src/test/java/com/thealgorithms/matrix/SparsityTest.java @@ -1,4 +1,4 @@ -package com.thealgorithms.misc; +package com.thealgorithms.matrix; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertThrows; diff --git a/src/test/java/com/thealgorithms/maths/FactorialRecursionTest.java b/src/test/java/com/thealgorithms/recursion/FactorialRecursionTest.java similarity index 96% rename from src/test/java/com/thealgorithms/maths/FactorialRecursionTest.java rename to src/test/java/com/thealgorithms/recursion/FactorialRecursionTest.java index db18b46356b4..198fcd558f63 100644 --- a/src/test/java/com/thealgorithms/maths/FactorialRecursionTest.java +++ b/src/test/java/com/thealgorithms/recursion/FactorialRecursionTest.java @@ -1,4 +1,4 @@ -package com.thealgorithms.maths; +package com.thealgorithms.recursion; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertThrows;