diff --git a/checkstyle.xml b/checkstyle.xml index 48c8a4f1..45431b39 100644 --- a/checkstyle.xml +++ b/checkstyle.xml @@ -152,7 +152,7 @@ - + diff --git a/src/main/java/com/thealgorithms/datastructures/hashmap/hashing/Main.java b/src/main/java/com/thealgorithms/datastructures/hashmap/hashing/Main.java index 082fd4b5..4d9b33b1 100644 --- a/src/main/java/com/thealgorithms/datastructures/hashmap/hashing/Main.java +++ b/src/main/java/com/thealgorithms/datastructures/hashmap/hashing/Main.java @@ -23,31 +23,30 @@ public final class Main { choice = scan.nextInt(); switch (choice) { - case 1: { + case 1: System.out.println("Enter the Key: "); key = scan.nextInt(); h.insertHash(key); break; - } - case 2: { + + case 2: System.out.println("Enter the Key delete: "); key = scan.nextInt(); h.deleteHash(key); break; - } - case 3: { + + case 3: System.out.println("Print table"); h.displayHashtable(); break; - } - case 4: { + + case 4: scan.close(); return; - } - default: { + + default: throw new IllegalArgumentException("Unexpected value: " + choice); } - } } } } diff --git a/src/main/java/com/thealgorithms/datastructures/hashmap/hashing/MainCuckooHashing.java b/src/main/java/com/thealgorithms/datastructures/hashmap/hashing/MainCuckooHashing.java index 5a4a56e9..ff4c69a5 100644 --- a/src/main/java/com/thealgorithms/datastructures/hashmap/hashing/MainCuckooHashing.java +++ b/src/main/java/com/thealgorithms/datastructures/hashmap/hashing/MainCuckooHashing.java @@ -27,45 +27,44 @@ public final class MainCuckooHashing { choice = scan.nextInt(); switch (choice) { - case 1: { + case 1: System.out.println("Enter the Key: "); key = scan.nextInt(); h.insertKey2HashTable(key); break; - } - case 2: { + + case 2: System.out.println("Enter the Key delete: "); key = scan.nextInt(); h.deleteKeyFromHashTable(key); break; - } - case 3: { + + case 3: System.out.println("Print table:\n"); h.displayHashtable(); break; - } - case 4: { + + case 4: scan.close(); return; - } - case 5: { + + case 5: System.out.println("Enter the Key to find and print: "); key = scan.nextInt(); System.out.println("Key: " + key + " is at index: " + h.findKeyInTable(key) + "\n"); break; - } - case 6: { + + case 6: System.out.printf("Load factor is: %.2f%n", h.checkLoadFactor()); break; - } - case 7: { + + case 7: h.reHashTableIncreasesTableSize(); break; - } - default: { + + default: throw new IllegalArgumentException("Unexpected value: " + choice); } - } } } } diff --git a/src/main/java/com/thealgorithms/maths/MatrixUtil.java b/src/main/java/com/thealgorithms/maths/MatrixUtil.java index 0759853d..7e462f92 100644 --- a/src/main/java/com/thealgorithms/maths/MatrixUtil.java +++ b/src/main/java/com/thealgorithms/maths/MatrixUtil.java @@ -1,8 +1,6 @@ package com.thealgorithms.maths; import java.math.BigDecimal; -import java.util.Arrays; -import java.util.Objects; import java.util.Optional; import java.util.function.BiFunction; import java.util.stream.IntStream; @@ -15,19 +13,19 @@ public final class MatrixUtil { private MatrixUtil() { } - public static boolean isValid(final BigDecimal[][] matrix) { + private static boolean isValid(final BigDecimal[][] matrix) { return matrix != null && matrix.length > 0 && matrix[0].length > 0; } - public static boolean hasEqualSizes(final BigDecimal[][] matrix1, final BigDecimal[][] matrix2) { + private static boolean hasEqualSizes(final BigDecimal[][] matrix1, final BigDecimal[][] matrix2) { return (isValid(matrix1) && isValid(matrix2) && matrix1.length == matrix2.length && matrix1[0].length == matrix2[0].length); } - public static boolean canMultiply(final BigDecimal[][] matrix1, final BigDecimal[][] matrix2) { + private static boolean canMultiply(final BigDecimal[][] matrix1, final BigDecimal[][] matrix2) { return (isValid(matrix1) && isValid(matrix2) && matrix1[0].length == matrix2.length); } - public static Optional operate(final BigDecimal[][] matrix1, final BigDecimal[][] matrix2, final BiFunction operation) { + private static Optional operate(final BigDecimal[][] matrix1, final BigDecimal[][] matrix2, final BiFunction operation) { if (!hasEqualSizes(matrix1, matrix2)) { return Optional.empty(); } @@ -82,78 +80,4 @@ public final class MatrixUtil { return Optional.of(result); } - - public static void assertThat(final BigDecimal[][] actual, final BigDecimal[][] expected) { - if (!Objects.deepEquals(actual, expected)) { - throw new AssertionError(String.format("expected=%s but was actual=%s", Arrays.deepToString(expected), Arrays.deepToString(actual))); - } - } - - public static void main(final String[] args) { - { - final BigDecimal[][] matrix1 = { - {new BigDecimal(3), new BigDecimal(2)}, - {new BigDecimal(0), new BigDecimal(1)}, - }; - - final BigDecimal[][] matrix2 = { - {new BigDecimal(1), new BigDecimal(3)}, - {new BigDecimal(2), new BigDecimal(0)}, - }; - - final BigDecimal[][] actual = add(matrix1, matrix2).orElseThrow(() -> new AssertionError("Could not compute matrix!")); - - final BigDecimal[][] expected = { - {new BigDecimal(4), new BigDecimal(5)}, - {new BigDecimal(2), new BigDecimal(1)}, - }; - - assertThat(actual, expected); - } - - { - final BigDecimal[][] matrix1 = { - {new BigDecimal(1), new BigDecimal(4)}, - {new BigDecimal(5), new BigDecimal(6)}, - }; - - final BigDecimal[][] matrix2 = { - {new BigDecimal(2), new BigDecimal(0)}, - {new BigDecimal(-2), new BigDecimal(-3)}, - }; - - final BigDecimal[][] actual = subtract(matrix1, matrix2).orElseThrow(() -> new AssertionError("Could not compute matrix!")); - - final BigDecimal[][] expected = { - {new BigDecimal(-1), new BigDecimal(4)}, - {new BigDecimal(7), new BigDecimal(9)}, - }; - - assertThat(actual, expected); - } - - { - final BigDecimal[][] matrix1 = { - {new BigDecimal(1), new BigDecimal(2), new BigDecimal(3)}, - {new BigDecimal(4), new BigDecimal(5), new BigDecimal(6)}, - {new BigDecimal(7), new BigDecimal(8), new BigDecimal(9)}, - }; - - final BigDecimal[][] matrix2 = { - {new BigDecimal(1), new BigDecimal(2)}, - {new BigDecimal(3), new BigDecimal(4)}, - {new BigDecimal(5), new BigDecimal(6)}, - }; - - final BigDecimal[][] actual = multiply(matrix1, matrix2).orElseThrow(() -> new AssertionError("Could not compute matrix!")); - - final BigDecimal[][] expected = { - {new BigDecimal(22), new BigDecimal(28)}, - {new BigDecimal(49), new BigDecimal(64)}, - {new BigDecimal(76), new BigDecimal(100)}, - }; - - assertThat(actual, expected); - } - } } diff --git a/src/main/java/com/thealgorithms/misc/Sort012D.java b/src/main/java/com/thealgorithms/misc/Sort012D.java index febe13f4..706e877e 100644 --- a/src/main/java/com/thealgorithms/misc/Sort012D.java +++ b/src/main/java/com/thealgorithms/misc/Sort012D.java @@ -33,28 +33,27 @@ public final class Sort012D { int temp; while (mid <= h) { switch (a[mid]) { - case 0: { + case 0: temp = a[l]; a[l] = a[mid]; a[mid] = temp; l++; mid++; break; - } + case 1: mid++; break; - case 2: { + case 2: temp = a[mid]; a[mid] = a[h]; a[h] = temp; h--; break; - } - default: { + + default: throw new IllegalArgumentException("Unexpected value: " + a[mid]); } - } } System.out.println("the Sorted array is "); for (int i = 0; i < a.length; i++) { diff --git a/src/main/java/com/thealgorithms/sorts/DNFSort.java b/src/main/java/com/thealgorithms/sorts/DNFSort.java index 50ba8c89..4b1e913c 100644 --- a/src/main/java/com/thealgorithms/sorts/DNFSort.java +++ b/src/main/java/com/thealgorithms/sorts/DNFSort.java @@ -13,24 +13,24 @@ public final class DNFSort { int temp; while (mid <= high) { switch (a[mid]) { - case 0: { + case 0: temp = a[low]; a[low] = a[mid]; a[mid] = temp; low++; mid++; break; - } + case 1: mid++; break; - case 2: { + case 2: temp = a[mid]; a[mid] = a[high]; a[high] = temp; high--; break; - } + default: throw new IllegalArgumentException("Unexpected value: " + a[mid]); } diff --git a/src/test/java/com/thealgorithms/maths/MatrixUtilTest.java b/src/test/java/com/thealgorithms/maths/MatrixUtilTest.java new file mode 100644 index 00000000..f61ebe6a --- /dev/null +++ b/src/test/java/com/thealgorithms/maths/MatrixUtilTest.java @@ -0,0 +1,79 @@ +package com.thealgorithms.maths; + +import static org.junit.jupiter.api.Assertions.assertTrue; + +import java.math.BigDecimal; +import java.util.Objects; +import org.junit.jupiter.api.Test; + +class MatrixUtilTest { + + @Test + void add() { + final BigDecimal[][] matrix1 = { + {new BigDecimal(3), new BigDecimal(2)}, + {BigDecimal.ZERO, BigDecimal.ONE}, + }; + + final BigDecimal[][] matrix2 = { + {BigDecimal.ONE, new BigDecimal(3)}, + {new BigDecimal(2), BigDecimal.ZERO}, + }; + + final BigDecimal[][] actual = MatrixUtil.add(matrix1, matrix2).orElseThrow(() -> new AssertionError("Could not compute matrix!")); + + final BigDecimal[][] expected = { + {new BigDecimal(4), new BigDecimal(5)}, + {new BigDecimal(2), BigDecimal.ONE}, + }; + + assertTrue(Objects.deepEquals(actual, expected)); + } + @Test + void subtract() { + final BigDecimal[][] matrix1 = { + {BigDecimal.ONE, new BigDecimal(4)}, + {new BigDecimal(5), new BigDecimal(6)}, + }; + + final BigDecimal[][] matrix2 = { + {new BigDecimal(2), BigDecimal.ZERO}, + {new BigDecimal(-2), new BigDecimal(-3)}, + }; + + final BigDecimal[][] actual = MatrixUtil.subtract(matrix1, matrix2).orElseThrow(() -> new AssertionError("Could not compute matrix!")); + + final BigDecimal[][] expected = { + {new BigDecimal(-1), new BigDecimal(4)}, + {new BigDecimal(7), new BigDecimal(9)}, + }; + + assertTrue(Objects.deepEquals(actual, expected)); + } + + @Test + void multiply() { + + final BigDecimal[][] matrix1 = { + {BigDecimal.ONE, new BigDecimal(2), new BigDecimal(3)}, + {new BigDecimal(4), new BigDecimal(5), new BigDecimal(6)}, + {new BigDecimal(7), new BigDecimal(8), new BigDecimal(9)}, + }; + + final BigDecimal[][] matrix2 = { + {BigDecimal.ONE, new BigDecimal(2)}, + {new BigDecimal(3), new BigDecimal(4)}, + {new BigDecimal(5), new BigDecimal(6)}, + }; + + final BigDecimal[][] actual = MatrixUtil.multiply(matrix1, matrix2).orElseThrow(() -> new AssertionError("Could not compute matrix!")); + + final BigDecimal[][] expected = { + {new BigDecimal(22), new BigDecimal(28)}, + {new BigDecimal(49), new BigDecimal(64)}, + {new BigDecimal(76), new BigDecimal(100)}, + }; + + assertTrue(Objects.deepEquals(actual, expected)); + } +}