From 06f945fa6effbe568aa943f0e2d7bc722f7f4577 Mon Sep 17 00:00:00 2001 From: sadiul-hakim <92100853+sadiul-hakim@users.noreply.github.com> Date: Mon, 3 Oct 2022 00:25:14 +0600 Subject: [PATCH 01/32] Search algorithm This algorithm is used to search a key from a sorted matrix. --- .../SearchInARowAndColWiseSortedMatrix.java | 49 +++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 src/main/java/com/thealgorithms/searches/SearchInARowAndColWiseSortedMatrix.java diff --git a/src/main/java/com/thealgorithms/searches/SearchInARowAndColWiseSortedMatrix.java b/src/main/java/com/thealgorithms/searches/SearchInARowAndColWiseSortedMatrix.java new file mode 100644 index 000000000000..41f1906ab707 --- /dev/null +++ b/src/main/java/com/thealgorithms/searches/SearchInARowAndColWiseSortedMatrix.java @@ -0,0 +1,49 @@ +package com.thealgorithms.searches; + +public class SearchInARowAndColWiseSortedMatrix { + /** + * Search a key in row and column wise sorted matrix + * + * @param matrix matrix to be searched + * @param value Key being searched for + * @author Sadiul Hakim : https://github.com/sadiul-hakim + */ + + public void search(int[][] matrix, int value) { + int n = matrix.length; + // This variable iterates over rows + int i = 0; + // This variable iterates over columns + int j = n - 1; + + while (i < n && j >= 0) { + + if (matrix[i][j] == value) { + System.out.println(value + " found at position row : " + i + " ,column :" + j); + return; + } + if (value > matrix[i][j]) { + + i++; + + } else { + j--; + } + + } + System.out.println(value + "Not Found."); + } + + public static void main(String[] args) { + int[][] matrix = { + { 3, 4, 5, 6, 7 }, + { 8, 9, 10, 11, 12 }, + { 14, 15, 16, 17, 18 }, + { 23, 24, 25, 26, 27 }, + { 30, 31, 32, 33, 34 } + }; + + var search = new SearchInARowAndColWiseSortedMatrix(); + search.search(matrix, 26); + } +} From fd2e7e8b1349dbe0c452dddb66727bee0f06f030 Mon Sep 17 00:00:00 2001 From: sadiul-hakim <92100853+sadiul-hakim@users.noreply.github.com> Date: Mon, 3 Oct 2022 00:27:11 +0600 Subject: [PATCH 02/32] Printing Matrix This algorithm helps printing a matrix in spiral order. --- .../others/PrintAMatrixInSpiralOrder.java | 74 +++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100644 src/main/java/com/thealgorithms/others/PrintAMatrixInSpiralOrder.java diff --git a/src/main/java/com/thealgorithms/others/PrintAMatrixInSpiralOrder.java b/src/main/java/com/thealgorithms/others/PrintAMatrixInSpiralOrder.java new file mode 100644 index 000000000000..dbd6fb39e478 --- /dev/null +++ b/src/main/java/com/thealgorithms/others/PrintAMatrixInSpiralOrder.java @@ -0,0 +1,74 @@ +package com.thealgorithms.others; + +public class PrintAMatrixInSpiralOrder { + /** + * Search a key in row and column wise sorted matrix + * + * @param matrix matrix to be searched + * @param row number of rows matrix has + * @param col number of columns matrix has + * @author Sadiul Hakim : https://github.com/sadiul-hakim + */ + + public void print(int[][] matrix, int row, int col) { + + //r traverses matrix row wise from first + int r = 0; + //c traverses matrix column wise from first + int c = 0; + int i; + + while (r < row && c < col) { + //print first row of matrix + for (i = c; i < col; i++) { + System.out.print(matrix[r][i] + " "); + + } + + //increase r by one because first row printed + r++; + + //print last column + for (i = r; i < row; i++) { + System.out.print(matrix[i][col - 1] + " "); + } + + //decrease col by one because last column has been printed + col--; + + //print rows from last except printed elements + if (r < row) { + for (i = col - 1; i >= c; i--) { + System.out.print(matrix[row - 1][i] + " "); + } + + row--; + + } + + //print columns from first except printed elements + if (c < col) { + for (i = row - 1; i >= r; i--) { + System.out.print(matrix[i][c] + " "); + } + c++; + } + + } + + } + + public static void main(String[] args) { + int[][] matrix = { + { 3, 4, 5, 6, 7 }, + { 8, 9, 10, 11, 12 }, + { 14, 15, 16, 17, 18 }, + { 23, 24, 25, 26, 27 }, + { 30, 31, 32, 33, 34 } + }; + + var printer = new PrintAMatrixInSpiralOrder(); + printer.print(matrix, matrix.length, matrix[0].length); + + } +} From 40cc87c901274335a866ea8ada84460ff934a483 Mon Sep 17 00:00:00 2001 From: sadiul-hakim <92100853+sadiul-hakim@users.noreply.github.com> Date: Tue, 10 Jan 2023 01:39:16 +0600 Subject: [PATCH 03/32] Update SearchInARowAndColWiseSortedMatrix.java --- .../SearchInARowAndColWiseSortedMatrix.java | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/src/main/java/com/thealgorithms/searches/SearchInARowAndColWiseSortedMatrix.java b/src/main/java/com/thealgorithms/searches/SearchInARowAndColWiseSortedMatrix.java index 41f1906ab707..bc2952c476b2 100644 --- a/src/main/java/com/thealgorithms/searches/SearchInARowAndColWiseSortedMatrix.java +++ b/src/main/java/com/thealgorithms/searches/SearchInARowAndColWiseSortedMatrix.java @@ -1,5 +1,7 @@ package com.thealgorithms.searches; +import java.util.Arrays; + public class SearchInARowAndColWiseSortedMatrix { /** * Search a key in row and column wise sorted matrix @@ -9,18 +11,20 @@ public class SearchInARowAndColWiseSortedMatrix { * @author Sadiul Hakim : https://github.com/sadiul-hakim */ - public void search(int[][] matrix, int value) { + public int[] search(int[][] matrix, int value) { int n = matrix.length; // This variable iterates over rows int i = 0; // This variable iterates over columns int j = n - 1; + int[] result = { -1, -1 }; while (i < n && j >= 0) { if (matrix[i][j] == value) { - System.out.println(value + " found at position row : " + i + " ,column :" + j); - return; + result[0] = i; + result[1] = j; + return result; } if (value > matrix[i][j]) { @@ -31,7 +35,7 @@ public void search(int[][] matrix, int value) { } } - System.out.println(value + "Not Found."); + return result; } public static void main(String[] args) { @@ -44,6 +48,7 @@ public static void main(String[] args) { }; var search = new SearchInARowAndColWiseSortedMatrix(); - search.search(matrix, 26); + int[] res = search.search(matrix, 26); + System.out.println(Arrays.toString(res)); } } From 58168e8e97af8ee2666a80e5052210e463b86d00 Mon Sep 17 00:00:00 2001 From: sadiul-hakim <92100853+sadiul-hakim@users.noreply.github.com> Date: Tue, 10 Jan 2023 01:40:21 +0600 Subject: [PATCH 04/32] Add files via upload --- ...estSearchInARowAndColWiseSortedMatrix.java | 38 +++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 src/test/java/com/thealgorithms/searches/TestSearchInARowAndColWiseSortedMatrix.java diff --git a/src/test/java/com/thealgorithms/searches/TestSearchInARowAndColWiseSortedMatrix.java b/src/test/java/com/thealgorithms/searches/TestSearchInARowAndColWiseSortedMatrix.java new file mode 100644 index 000000000000..c5d37fcb37a0 --- /dev/null +++ b/src/test/java/com/thealgorithms/searches/TestSearchInARowAndColWiseSortedMatrix.java @@ -0,0 +1,38 @@ +package com.thealgorithms.searches; + +import static org.junit.jupiter.api.Assertions.*; +import org.junit.jupiter.api.Test; + +public class TestSearchInARowAndColWiseSortedMatrix { + @Test + public void searchItem() { + int[][] matrix = { + { 3, 4, 5, 6, 7 }, + { 8, 9, 10, 11, 12 }, + { 14, 15, 16, 17, 18 }, + { 23, 24, 25, 26, 27 }, + { 30, 31, 32, 33, 34 } + }; + + var test = new SearchInARowAndColWiseSortedMatrix(); + int[] res = test.search(matrix, 16); + int[] expectedResult = { 2, 2 }; + assertArrayEquals(expectedResult, res); + } + + @Test + public void notFound() { + int[][] matrix = { + { 3, 4, 5, 6, 7 }, + { 8, 9, 10, 11, 12 }, + { 14, 15, 16, 17, 18 }, + { 23, 24, 25, 26, 27 }, + { 30, 31, 32, 33, 34 } + }; + + var test = new SearchInARowAndColWiseSortedMatrix(); + int[] res = test.search(matrix, 96); + int[] expectedResult = { -1, -1 }; + assertArrayEquals(expectedResult, res); + } +} \ No newline at end of file From fc4d76dbb2c05908887cc597cc6253f7b193c2a8 Mon Sep 17 00:00:00 2001 From: Andrii Siriak Date: Tue, 10 Jan 2023 09:16:57 +0200 Subject: [PATCH 05/32] Apply suggestions from code review --- .../SearchInARowAndColWiseSortedMatrix.java | 17 ----------------- .../TestSearchInARowAndColWiseSortedMatrix.java | 2 +- 2 files changed, 1 insertion(+), 18 deletions(-) diff --git a/src/main/java/com/thealgorithms/searches/SearchInARowAndColWiseSortedMatrix.java b/src/main/java/com/thealgorithms/searches/SearchInARowAndColWiseSortedMatrix.java index bc2952c476b2..c92569cf1086 100644 --- a/src/main/java/com/thealgorithms/searches/SearchInARowAndColWiseSortedMatrix.java +++ b/src/main/java/com/thealgorithms/searches/SearchInARowAndColWiseSortedMatrix.java @@ -20,16 +20,13 @@ public int[] search(int[][] matrix, int value) { int[] result = { -1, -1 }; while (i < n && j >= 0) { - if (matrix[i][j] == value) { result[0] = i; result[1] = j; return result; } if (value > matrix[i][j]) { - i++; - } else { j--; } @@ -37,18 +34,4 @@ public int[] search(int[][] matrix, int value) { } return result; } - - public static void main(String[] args) { - int[][] matrix = { - { 3, 4, 5, 6, 7 }, - { 8, 9, 10, 11, 12 }, - { 14, 15, 16, 17, 18 }, - { 23, 24, 25, 26, 27 }, - { 30, 31, 32, 33, 34 } - }; - - var search = new SearchInARowAndColWiseSortedMatrix(); - int[] res = search.search(matrix, 26); - System.out.println(Arrays.toString(res)); - } } diff --git a/src/test/java/com/thealgorithms/searches/TestSearchInARowAndColWiseSortedMatrix.java b/src/test/java/com/thealgorithms/searches/TestSearchInARowAndColWiseSortedMatrix.java index c5d37fcb37a0..0dcc6186fa9b 100644 --- a/src/test/java/com/thealgorithms/searches/TestSearchInARowAndColWiseSortedMatrix.java +++ b/src/test/java/com/thealgorithms/searches/TestSearchInARowAndColWiseSortedMatrix.java @@ -35,4 +35,4 @@ public void notFound() { int[] expectedResult = { -1, -1 }; assertArrayEquals(expectedResult, res); } -} \ No newline at end of file +} From 7a5102c649a502e9fefa903bfb8c0aa72b752961 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?sadiul-hakim=E2=98=AA=EF=B8=8F?= <92100853+sadiul-hakim@users.noreply.github.com> Date: Tue, 10 Jan 2023 14:30:31 +0600 Subject: [PATCH 06/32] Update PrintAMatrixInSpiralOrder.java --- .../others/PrintAMatrixInSpiralOrder.java | 47 ++++++++----------- 1 file changed, 19 insertions(+), 28 deletions(-) diff --git a/src/main/java/com/thealgorithms/others/PrintAMatrixInSpiralOrder.java b/src/main/java/com/thealgorithms/others/PrintAMatrixInSpiralOrder.java index dbd6fb39e478..d2065085d8c6 100644 --- a/src/main/java/com/thealgorithms/others/PrintAMatrixInSpiralOrder.java +++ b/src/main/java/com/thealgorithms/others/PrintAMatrixInSpiralOrder.java @@ -1,5 +1,8 @@ package com.thealgorithms.others; +import java.util.ArrayList; +import java.util.List; + public class PrintAMatrixInSpiralOrder { /** * Search a key in row and column wise sorted matrix @@ -10,65 +13,53 @@ public class PrintAMatrixInSpiralOrder { * @author Sadiul Hakim : https://github.com/sadiul-hakim */ - public void print(int[][] matrix, int row, int col) { + public List print(int[][] matrix, int row, int col) { - //r traverses matrix row wise from first + // r traverses matrix row wise from first int r = 0; - //c traverses matrix column wise from first + // c traverses matrix column wise from first int c = 0; int i; + List result = new ArrayList<>(); + while (r < row && c < col) { - //print first row of matrix + // print first row of matrix for (i = c; i < col; i++) { - System.out.print(matrix[r][i] + " "); - + result.add(matrix[r][i]); } - //increase r by one because first row printed + // increase r by one because first row printed r++; - //print last column + // print last column for (i = r; i < row; i++) { - System.out.print(matrix[i][col - 1] + " "); + result.add(matrix[i][col - 1]); } - //decrease col by one because last column has been printed + // decrease col by one because last column has been printed col--; - //print rows from last except printed elements + // print rows from last except printed elements if (r < row) { for (i = col - 1; i >= c; i--) { - System.out.print(matrix[row - 1][i] + " "); + result.add(matrix[row - 1][i]); } row--; } - //print columns from first except printed elements + // print columns from first except printed elements if (c < col) { for (i = row - 1; i >= r; i--) { - System.out.print(matrix[i][c] + " "); + result.add(matrix[i][c]); } c++; } } - + return result; } - public static void main(String[] args) { - int[][] matrix = { - { 3, 4, 5, 6, 7 }, - { 8, 9, 10, 11, 12 }, - { 14, 15, 16, 17, 18 }, - { 23, 24, 25, 26, 27 }, - { 30, 31, 32, 33, 34 } - }; - - var printer = new PrintAMatrixInSpiralOrder(); - printer.print(matrix, matrix.length, matrix[0].length); - - } } From 3447104d2e19432b4a6a161bff4d07a357cdbb69 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?sadiul-hakim=E2=98=AA=EF=B8=8F?= <92100853+sadiul-hakim@users.noreply.github.com> Date: Tue, 10 Jan 2023 14:32:13 +0600 Subject: [PATCH 07/32] Add files via upload --- .../others/TestPrintMatrixInSpiralOrder.java | 35 +++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 src/test/java/com/thealgorithms/others/TestPrintMatrixInSpiralOrder.java diff --git a/src/test/java/com/thealgorithms/others/TestPrintMatrixInSpiralOrder.java b/src/test/java/com/thealgorithms/others/TestPrintMatrixInSpiralOrder.java new file mode 100644 index 000000000000..867311e1bce1 --- /dev/null +++ b/src/test/java/com/thealgorithms/others/TestPrintMatrixInSpiralOrder.java @@ -0,0 +1,35 @@ +package com.thealgorithms.others; + +import java.util.List; + +import org.junit.jupiter.api.Test; +import static org.junit.jupiter.api.Assertions.*; + +public class TestPrintMatrixInSpiralOrder { + @Test + public void testOne() { + int[][] matrix = { + { 3, 4, 5, 6, 7 }, + { 8, 9, 10, 11, 12 }, + { 14, 15, 16, 17, 18 }, + { 23, 24, 25, 26, 27 }, + { 30, 31, 32, 33, 34 } + }; + var printClass = new PrintAMatrixInSpiralOrder(); + List res = printClass.print(matrix, matrix.length, matrix[0].length); + List list = List.of(3, 4, 5, 6, 7, 12, 18, 27, 34, 33, 32, 31, 30, 23, 14, 8, 9, 10, 11, 17, 26, 25, + 24, 15, 16); + assertIterableEquals(res, list); + } + + @Test + public void testTwo() { + int[][] matrix = { + { 2, 2 } + }; + var printClass = new PrintAMatrixInSpiralOrder(); + List res = printClass.print(matrix, matrix.length, matrix[0].length); + List list = List.of(2, 2); + assertIterableEquals(res, list); + } +} From 886d23d8474a0d5fbea7791858718f6076d13b9c Mon Sep 17 00:00:00 2001 From: Sadiul Hakim Date: Mon, 10 Mar 2025 10:58:57 +0600 Subject: [PATCH 08/32] Added new class MathBuilder --- .../com/thealgorithms/maths/MathBuilder.java | 390 ++++++++++++++++++ .../thealgorithms/maths/MathBuilderTest.java | 68 +++ 2 files changed, 458 insertions(+) create mode 100644 src/main/java/com/thealgorithms/maths/MathBuilder.java create mode 100644 src/test/java/com/thealgorithms/maths/MathBuilderTest.java diff --git a/src/main/java/com/thealgorithms/maths/MathBuilder.java b/src/main/java/com/thealgorithms/maths/MathBuilder.java new file mode 100644 index 000000000000..936a8b0e08e3 --- /dev/null +++ b/src/main/java/com/thealgorithms/maths/MathBuilder.java @@ -0,0 +1,390 @@ +package com.thealgorithms.maths; + +import java.text.DecimalFormat; +import java.util.Random; +import java.util.function.BiFunction; +import java.util.function.Function; + +/** + * Author: Sadiul Hakim : https://github.com/sadiul-hakim + * Profession: Backend Engineer + * Date: Oct 20, 2024 + */ +public class MathBuilder { + private final double number; + + private MathBuilder(Builder builder) { + this.number = builder.NUMBER; + } + + // Returns final result + public double get() { + return number; + } + + // Return result in long + public long toLong() { + try { + if (Double.isNaN(number)) { + throw new IllegalArgumentException("Cannot convert NaN to long"); + } + if (number == Double.POSITIVE_INFINITY) { + return Long.MAX_VALUE; + } + if (number == Double.NEGATIVE_INFINITY) { + return Long.MIN_VALUE; + } + if (number > Long.MAX_VALUE) { + return Long.MAX_VALUE; + } + if (number < Long.MIN_VALUE) { + return Long.MIN_VALUE; + } + return Math.round(number); + } catch (Exception ex) { + return 0; + } + } + + public static class Builder { + private double NUMBER; + private double memory = 0; + + public Builder() { + NUMBER = 0; + } + + public Builder(double num) { + NUMBER = num; + } + + public Builder add(double num) { + NUMBER += num; + return this; + } + + // Takes a number and a condition, only does the operation if condition is true. + public Builder addIf(double num, BiFunction condition) { + + if (condition.apply(NUMBER, num)) { + NUMBER += num; + } + + return this; + } + + public Builder minus(double num) { + NUMBER -= num; + return this; + } + + // Takes a number and a condition, only does the operation if condition is true. + public Builder minusIf(double num, BiFunction condition) { + if (condition.apply(NUMBER, num)) { + NUMBER -= num; + } + + return this; + } + + // Generates a random number and sets to NUMBER + public Builder rand(long seed) { + if (NUMBER != 0) { + throw new RuntimeException("Number must be zero for random assignment!"); + } + + Random random = new Random(); + NUMBER = random.nextDouble(seed); + return this; + } + + // Takes PI value and sets to NUMBER + public Builder PI() { + if (NUMBER != 0) { + throw new RuntimeException("Number must be zero for PI assignment!"); + } + + NUMBER = Math.PI; + return this; + } + + // Takes E value and sets to NUMBER + public Builder E() { + if (NUMBER != 0) { + throw new RuntimeException("Number must be zero for E assignment!"); + } + + NUMBER = Math.E; + return this; + } + + public Builder randomInRange(double min, double max) { + + if (NUMBER != 0) { + throw new RuntimeException("Number must be zero for random assignment!"); + } + + Random random = new Random(); + NUMBER = min + (max - min) * random.nextDouble(); + return this; + } + + public Builder toDegrees() { + NUMBER = Math.toDegrees(NUMBER); + return this; + } + + public Builder max(double num) { + NUMBER = Math.max(NUMBER, num); + return this; + } + + public Builder min(double num) { + NUMBER = Math.min(NUMBER, num); + return this; + } + + public Builder multiply(double num) { + + NUMBER *= num; + return this; + } + + // Takes a number and a condition, only does the operation if condition is true. + public Builder multiplyIf(double num, BiFunction condition) { + + + if (condition.apply(NUMBER, num)) { + NUMBER *= num; + } + + return this; + } + + public Builder divide(double num) { + + if (num == 0) { + return this; + } + + NUMBER /= num; + return this; + } + + // Takes a number and a condition, only does the operation if condition is true. + public Builder divideIf(double num, BiFunction condition) { + + if (num == 0) { + return this; + } + + if (condition.apply(NUMBER, num)) { + NUMBER /= num; + } + return this; + } + + public Builder mod(double num) { + + NUMBER %= num; + return this; + } + + // Takes a number and a condition, only does the operation if condition is true. + public Builder modIf(double num, BiFunction condition) { + + + if (condition.apply(NUMBER, num)) { + NUMBER %= num; + } + return this; + } + + public Builder pow(double num) { + + NUMBER = Math.pow(NUMBER, num); + return this; + } + + public Builder sqrt() { + + NUMBER = Math.sqrt(NUMBER); + return this; + } + + public Builder round() { + + NUMBER = Math.round(NUMBER); + return this; + } + + public Builder floor() { + + NUMBER = Math.floor(NUMBER); + return this; + } + + public Builder ceil() { + + NUMBER = Math.ceil(NUMBER); + return this; + } + + public Builder abs() { + + NUMBER = Math.abs(NUMBER); + return this; + } + + public Builder cbrt() { + + NUMBER = Math.cbrt(NUMBER); + return this; + } + + public Builder log() { + + NUMBER = Math.log(NUMBER); + return this; + } + + public Builder log10() { + + NUMBER = Math.log10(NUMBER); + return this; + } + + public Builder sin() { + + NUMBER = Math.sin(NUMBER); + return this; + } + + public Builder cos() { + + NUMBER = Math.cos(NUMBER); + return this; + } + + public Builder tan() { + + NUMBER = Math.tan(NUMBER); + return this; + } + + public Builder sinh() { + + NUMBER = Math.sinh(NUMBER); + return this; + } + + public Builder cosh() { + + NUMBER = Math.cosh(NUMBER); + return this; + } + + public Builder tanh() { + + NUMBER = Math.tanh(NUMBER); + return this; + } + + public Builder exp() { + + NUMBER = Math.exp(NUMBER); + return this; + } + + public Builder toRadians() { + + NUMBER = Math.toRadians(NUMBER); + return this; + } + + // Remembers the NUMBER + public Builder remember() { + + memory = NUMBER; + return this; + } + + // Recalls the NUMBER + public Builder recall(boolean cleanMemory) { + + NUMBER = memory; + if (cleanMemory) { + memory = 0; + } + + return this; + } + + // Recalls the NUMBER on condition + public Builder recallIf(Function condition, boolean cleanMemory) { + + if (!condition.apply(NUMBER)) { + return this; + } + + NUMBER = memory; + if (cleanMemory) { + memory = 0; + } + + return this; + } + + // Replaces NUMBER with given number + public Builder set(double num) { + + if (NUMBER != 0) { + throw new RuntimeException("Number must be zero to set!"); + } + + NUMBER = num; + return this; + } + + // Replaces NUMBER with given number on condition + public Builder setIf(double num, BiFunction condition) { + + if (NUMBER != 0) { + throw new RuntimeException("Number must be zero to set!"); + } + + if (condition.apply(NUMBER, num)) { + NUMBER = num; + } + + return this; + } + + // Prints current NUMBER + public Builder print() { + System.out.println("MathBuilder Result :: " + NUMBER); + return this; + } + + public Builder format(String format) { + + DecimalFormat formater = new DecimalFormat(format); + String num = formater.format(NUMBER); + NUMBER = Double.parseDouble(num); + return this; + } + + public Builder format(int decimalPlace) { + + String pattern = "." + "#".repeat(decimalPlace); + DecimalFormat formater = new DecimalFormat(pattern); + String num = formater.format(NUMBER); + NUMBER = Double.parseDouble(num); + return this; + } + + public MathBuilder build() { + return new MathBuilder(this); + } + } +} diff --git a/src/test/java/com/thealgorithms/maths/MathBuilderTest.java b/src/test/java/com/thealgorithms/maths/MathBuilderTest.java new file mode 100644 index 000000000000..1588755b618a --- /dev/null +++ b/src/test/java/com/thealgorithms/maths/MathBuilderTest.java @@ -0,0 +1,68 @@ +package com.thealgorithms.maths; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.*; + +class MathBuilderTest { + + @Test + void simpleMath() { + double result = new MathBuilder.Builder(100) + .add(200) + .multiply(10) + .print() + .divideIf(20, (a, b) -> a % 2 == 0) + .sqrt() + .print() + .ceil() + .build() + .get(); + + assertEquals(13, result); + } + + @Test + void memoryTest() { + long result = new MathBuilder.Builder() + .set(100) + .sqrt() + .remember() + .add(21) + .recallIf(a -> a < 50, true) + .mod(2) + .build() + .toLong(); + assertEquals(0, result); + } + + @Test + void freeFallDistance() { + long distance = new MathBuilder.Builder(9.81) + .multiply(0.5) // 0.5 * g + .multiply(5 * 5) // t^2 + .round() + .build() + .toLong(); + + assertEquals(123, distance); // Expected result: 0.5 * 9.81 * 25 = 122.625 ≈ 123 + } + + @Test + void batchSalaryProcessing() { + double[] salaries = {2000, 3000, 4000, 5000}; + long[] processedSalaries = new long[salaries.length]; + + for (int i = 0; i < salaries.length; i++) { + processedSalaries[i] = new MathBuilder.Builder(salaries[i]) + .addIf(salaries[i] * 0.1, (sal, bonus) -> sal > 2500) // Apply 10% bonus if salary > 2500 + .multiply(0.92) // Deduct 8% tax + .round() // Round to the nearest whole number + .build() + .toLong(); + } + + long[] expectedSalaries = {1840, 3036, 4048, 5060}; + assertArrayEquals(expectedSalaries, processedSalaries); + } +} \ No newline at end of file From 490681e80603c98f84501f4c79b7faa97d938397 Mon Sep 17 00:00:00 2001 From: sadiul-hakim Date: Mon, 10 Mar 2025 05:49:48 +0000 Subject: [PATCH 09/32] Update directory --- DIRECTORY.md | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/DIRECTORY.md b/DIRECTORY.md index 009de2044421..f53a6220c517 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -162,6 +162,7 @@ * [MatrixGraphs](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/datastructures/graphs/MatrixGraphs.java) * [PrimMST](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/datastructures/graphs/PrimMST.java) * [TarjansAlgorithm](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/datastructures/graphs/TarjansAlgorithm.java) + * [UndirectedAdjacencyListGraph](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/datastructures/graphs/UndirectedAdjacencyListGraph.java) * [WelshPowell](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/datastructures/graphs/WelshPowell.java) * hashmap * hashing @@ -319,6 +320,7 @@ * [SubsetSum](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/dynamicprogramming/SubsetSum.java) * [SubsetSumSpaceOptimized](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/dynamicprogramming/SubsetSumSpaceOptimized.java) * [SumOfSubset](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/dynamicprogramming/SumOfSubset.java) + * [TreeMatching](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/dynamicprogramming/TreeMatching.java) * [Tribonacci](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/dynamicprogramming/Tribonacci.java) * [UniquePaths](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/dynamicprogramming/UniquePaths.java) * [UniqueSubsequencesCount](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/dynamicprogramming/UniqueSubsequencesCount.java) @@ -423,6 +425,7 @@ * [LongDivision](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/maths/LongDivision.java) * [LucasSeries](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/maths/LucasSeries.java) * [MagicSquare](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/maths/MagicSquare.java) + * [MathBuilder](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/maths/MathBuilder.java) * [MaxValue](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/maths/MaxValue.java) * [Means](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/maths/Means.java) * [Median](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/maths/Median.java) @@ -537,6 +540,7 @@ * [PageRank](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/others/PageRank.java) * [PasswordGen](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/others/PasswordGen.java) * [PerlinNoise](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/others/PerlinNoise.java) + * [PrintAMatrixInSpiralOrder](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/others/PrintAMatrixInSpiralOrder.java) * [QueueUsingTwoStacks](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/others/QueueUsingTwoStacks.java) * [RemoveDuplicateFromString](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/others/RemoveDuplicateFromString.java) * [ReverseStackUsingRecursion](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/others/ReverseStackUsingRecursion.java) @@ -724,7 +728,7 @@ * zigZagPattern * [ZigZagPattern](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/strings/zigZagPattern/ZigZagPattern.java) * tree - * [HeavyLightDecomposition](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/tree/HeavyLightDecomposition.java) + * [HeavyLightDecomposition](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/tree/HeavyLightDecomposition.java) * test * java * com @@ -1003,6 +1007,7 @@ * [SubsetSumSpaceOptimizedTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/dynamicprogramming/SubsetSumSpaceOptimizedTest.java) * [SubsetSumTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/dynamicprogramming/SubsetSumTest.java) * [SumOfSubsetTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/dynamicprogramming/SumOfSubsetTest.java) + * [TreeMatchingTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/dynamicprogramming/TreeMatchingTest.java) * [TribonacciTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/dynamicprogramming/TribonacciTest.java) * [UniquePathsTests](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/dynamicprogramming/UniquePathsTests.java) * [UniqueSubsequencesCountTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/dynamicprogramming/UniqueSubsequencesCountTest.java) @@ -1097,6 +1102,7 @@ * [LeonardoNumberTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/maths/LeonardoNumberTest.java) * [LongDivisionTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/maths/LongDivisionTest.java) * [LucasSeriesTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/maths/LucasSeriesTest.java) + * [MathBuilderTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/maths/MathBuilderTest.java) * [MaxValueTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/maths/MaxValueTest.java) * [MeansTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/maths/MeansTest.java) * [MedianTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/maths/MedianTest.java) @@ -1190,6 +1196,7 @@ * [RemoveDuplicateFromStringTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/others/RemoveDuplicateFromStringTest.java) * [ReverseStackUsingRecursionTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/others/ReverseStackUsingRecursionTest.java) * [SkylineProblemTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/others/SkylineProblemTest.java) + * [TestPrintMatrixInSpiralOrder](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/others/TestPrintMatrixInSpiralOrder.java) * [TwoPointersTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/others/TwoPointersTest.java) * [WorstFitCPUTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/others/WorstFitCPUTest.java) * puzzlesandgames From ca012ada3c0e3f054414819a9cf755892ec93c25 Mon Sep 17 00:00:00 2001 From: Sadiul Hakim Date: Mon, 10 Mar 2025 12:20:24 +0600 Subject: [PATCH 10/32] Removing all CheckStyle issues --- .../com/thealgorithms/maths/MathBuilder.java | 144 +++++++++--------- .../SearchInARowAndColWiseSortedMatrix.java | 7 +- .../thealgorithms/maths/MathBuilderTest.java | 2 +- ...estSearchInARowAndColWiseSortedMatrix.java | 4 +- 4 files changed, 75 insertions(+), 82 deletions(-) diff --git a/src/main/java/com/thealgorithms/maths/MathBuilder.java b/src/main/java/com/thealgorithms/maths/MathBuilder.java index 936a8b0e08e3..4aff9ab2d57a 100644 --- a/src/main/java/com/thealgorithms/maths/MathBuilder.java +++ b/src/main/java/com/thealgorithms/maths/MathBuilder.java @@ -10,78 +10,78 @@ * Profession: Backend Engineer * Date: Oct 20, 2024 */ -public class MathBuilder { - private final double number; +public final class MathBuilder { + private final double result; private MathBuilder(Builder builder) { - this.number = builder.NUMBER; + this.result = builder.number; } // Returns final result public double get() { - return number; + return result; } // Return result in long public long toLong() { try { - if (Double.isNaN(number)) { + if (Double.isNaN(result)) { throw new IllegalArgumentException("Cannot convert NaN to long"); } - if (number == Double.POSITIVE_INFINITY) { + if (result == Double.POSITIVE_INFINITY) { return Long.MAX_VALUE; } - if (number == Double.NEGATIVE_INFINITY) { + if (result == Double.NEGATIVE_INFINITY) { return Long.MIN_VALUE; } - if (number > Long.MAX_VALUE) { + if (result > Long.MAX_VALUE) { return Long.MAX_VALUE; } - if (number < Long.MIN_VALUE) { + if (result < Long.MIN_VALUE) { return Long.MIN_VALUE; } - return Math.round(number); + return Math.round(result); } catch (Exception ex) { return 0; } } public static class Builder { - private double NUMBER; + private double number; private double memory = 0; public Builder() { - NUMBER = 0; + number = 0; } public Builder(double num) { - NUMBER = num; + number = num; } public Builder add(double num) { - NUMBER += num; + number += num; return this; } // Takes a number and a condition, only does the operation if condition is true. public Builder addIf(double num, BiFunction condition) { - if (condition.apply(NUMBER, num)) { - NUMBER += num; + if (condition.apply(number, num)) { + number += num; } return this; } public Builder minus(double num) { - NUMBER -= num; + number -= num; return this; } // Takes a number and a condition, only does the operation if condition is true. public Builder minusIf(double num, BiFunction condition) { - if (condition.apply(NUMBER, num)) { - NUMBER -= num; + if (condition.apply(number, num)) { + number -= num; } return this; @@ -89,64 +89,64 @@ public Builder minusIf(double num, BiFunction condition // Generates a random number and sets to NUMBER public Builder rand(long seed) { - if (NUMBER != 0) { + if (number != 0) { throw new RuntimeException("Number must be zero for random assignment!"); } Random random = new Random(); - NUMBER = random.nextDouble(seed); + number = random.nextDouble(seed); return this; } // Takes PI value and sets to NUMBER - public Builder PI() { - if (NUMBER != 0) { + public Builder pi() { + if (number != 0) { throw new RuntimeException("Number must be zero for PI assignment!"); } - NUMBER = Math.PI; + number = Math.PI; return this; } // Takes E value and sets to NUMBER - public Builder E() { - if (NUMBER != 0) { + public Builder e() { + if (number != 0) { throw new RuntimeException("Number must be zero for E assignment!"); } - NUMBER = Math.E; + number = Math.E; return this; } public Builder randomInRange(double min, double max) { - if (NUMBER != 0) { + if (number != 0) { throw new RuntimeException("Number must be zero for random assignment!"); } Random random = new Random(); - NUMBER = min + (max - min) * random.nextDouble(); + number = min + (max - min) * random.nextDouble(); return this; } public Builder toDegrees() { - NUMBER = Math.toDegrees(NUMBER); + number = Math.toDegrees(number); return this; } public Builder max(double num) { - NUMBER = Math.max(NUMBER, num); + number = Math.max(number, num); return this; } public Builder min(double num) { - NUMBER = Math.min(NUMBER, num); + number = Math.min(number, num); return this; } public Builder multiply(double num) { - NUMBER *= num; + number *= num; return this; } @@ -154,8 +154,8 @@ public Builder multiply(double num) { public Builder multiplyIf(double num, BiFunction condition) { - if (condition.apply(NUMBER, num)) { - NUMBER *= num; + if (condition.apply(number, num)) { + number *= num; } return this; @@ -167,7 +167,7 @@ public Builder divide(double num) { return this; } - NUMBER /= num; + number /= num; return this; } @@ -178,15 +178,15 @@ public Builder divideIf(double num, BiFunction conditio return this; } - if (condition.apply(NUMBER, num)) { - NUMBER /= num; + if (condition.apply(number, num)) { + number /= num; } return this; } public Builder mod(double num) { - NUMBER %= num; + number %= num; return this; } @@ -194,125 +194,125 @@ public Builder mod(double num) { public Builder modIf(double num, BiFunction condition) { - if (condition.apply(NUMBER, num)) { - NUMBER %= num; + if (condition.apply(number, num)) { + number %= num; } return this; } public Builder pow(double num) { - NUMBER = Math.pow(NUMBER, num); + number = Math.pow(number, num); return this; } public Builder sqrt() { - NUMBER = Math.sqrt(NUMBER); + number = Math.sqrt(number); return this; } public Builder round() { - NUMBER = Math.round(NUMBER); + number = Math.round(number); return this; } public Builder floor() { - NUMBER = Math.floor(NUMBER); + number = Math.floor(number); return this; } public Builder ceil() { - NUMBER = Math.ceil(NUMBER); + number = Math.ceil(number); return this; } public Builder abs() { - NUMBER = Math.abs(NUMBER); + number = Math.abs(number); return this; } public Builder cbrt() { - NUMBER = Math.cbrt(NUMBER); + number = Math.cbrt(number); return this; } public Builder log() { - NUMBER = Math.log(NUMBER); + number = Math.log(number); return this; } public Builder log10() { - NUMBER = Math.log10(NUMBER); + number = Math.log10(number); return this; } public Builder sin() { - NUMBER = Math.sin(NUMBER); + number = Math.sin(number); return this; } public Builder cos() { - NUMBER = Math.cos(NUMBER); + number = Math.cos(number); return this; } public Builder tan() { - NUMBER = Math.tan(NUMBER); + number = Math.tan(number); return this; } public Builder sinh() { - NUMBER = Math.sinh(NUMBER); + number = Math.sinh(number); return this; } public Builder cosh() { - NUMBER = Math.cosh(NUMBER); + number = Math.cosh(number); return this; } public Builder tanh() { - NUMBER = Math.tanh(NUMBER); + number = Math.tanh(number); return this; } public Builder exp() { - NUMBER = Math.exp(NUMBER); + number = Math.exp(number); return this; } public Builder toRadians() { - NUMBER = Math.toRadians(NUMBER); + number = Math.toRadians(number); return this; } // Remembers the NUMBER public Builder remember() { - memory = NUMBER; + memory = number; return this; } // Recalls the NUMBER public Builder recall(boolean cleanMemory) { - NUMBER = memory; + number = memory; if (cleanMemory) { memory = 0; } @@ -323,11 +323,11 @@ public Builder recall(boolean cleanMemory) { // Recalls the NUMBER on condition public Builder recallIf(Function condition, boolean cleanMemory) { - if (!condition.apply(NUMBER)) { + if (!condition.apply(number)) { return this; } - NUMBER = memory; + number = memory; if (cleanMemory) { memory = 0; } @@ -338,23 +338,23 @@ public Builder recallIf(Function condition, boolean cleanMemory // Replaces NUMBER with given number public Builder set(double num) { - if (NUMBER != 0) { + if (number != 0) { throw new RuntimeException("Number must be zero to set!"); } - NUMBER = num; + number = num; return this; } // Replaces NUMBER with given number on condition public Builder setIf(double num, BiFunction condition) { - if (NUMBER != 0) { + if (number != 0) { throw new RuntimeException("Number must be zero to set!"); } - if (condition.apply(NUMBER, num)) { - NUMBER = num; + if (condition.apply(number, num)) { + number = num; } return this; @@ -362,15 +362,15 @@ public Builder setIf(double num, BiFunction condition) // Prints current NUMBER public Builder print() { - System.out.println("MathBuilder Result :: " + NUMBER); + System.out.println("MathBuilder Result :: " + number); return this; } public Builder format(String format) { DecimalFormat formater = new DecimalFormat(format); - String num = formater.format(NUMBER); - NUMBER = Double.parseDouble(num); + String num = formater.format(number); + number = Double.parseDouble(num); return this; } @@ -378,8 +378,8 @@ public Builder format(int decimalPlace) { String pattern = "." + "#".repeat(decimalPlace); DecimalFormat formater = new DecimalFormat(pattern); - String num = formater.format(NUMBER); - NUMBER = Double.parseDouble(num); + String num = formater.format(number); + number = Double.parseDouble(num); return this; } diff --git a/src/main/java/com/thealgorithms/searches/SearchInARowAndColWiseSortedMatrix.java b/src/main/java/com/thealgorithms/searches/SearchInARowAndColWiseSortedMatrix.java index da4479eec180..997738cece7d 100644 --- a/src/main/java/com/thealgorithms/searches/SearchInARowAndColWiseSortedMatrix.java +++ b/src/main/java/com/thealgorithms/searches/SearchInARowAndColWiseSortedMatrix.java @@ -9,17 +9,13 @@ public class SearchInARowAndColWiseSortedMatrix { * @param value Key being searched for * @author Sadiul Hakim : https://github.com/sadiul-hakim */ - public int[] search(int[][] matrix, int value) { int n = matrix.length; - // This variable iterates over rows int i = 0; - // This variable iterates over columns int j = n - 1; - int[] result = { -1, -1 }; - + int[] result = {-1, -1}; while (i < n && j >= 0) { if (matrix[i][j] == value) { result[0] = i; @@ -31,7 +27,6 @@ public int[] search(int[][] matrix, int value) { } else { j--; } - } return result; } diff --git a/src/test/java/com/thealgorithms/maths/MathBuilderTest.java b/src/test/java/com/thealgorithms/maths/MathBuilderTest.java index 1588755b618a..7af57b3afc30 100644 --- a/src/test/java/com/thealgorithms/maths/MathBuilderTest.java +++ b/src/test/java/com/thealgorithms/maths/MathBuilderTest.java @@ -65,4 +65,4 @@ void batchSalaryProcessing() { long[] expectedSalaries = {1840, 3036, 4048, 5060}; assertArrayEquals(expectedSalaries, processedSalaries); } -} \ No newline at end of file +} diff --git a/src/test/java/com/thealgorithms/searches/TestSearchInARowAndColWiseSortedMatrix.java b/src/test/java/com/thealgorithms/searches/TestSearchInARowAndColWiseSortedMatrix.java index 0dcc6186fa9b..14d85a538990 100644 --- a/src/test/java/com/thealgorithms/searches/TestSearchInARowAndColWiseSortedMatrix.java +++ b/src/test/java/com/thealgorithms/searches/TestSearchInARowAndColWiseSortedMatrix.java @@ -1,6 +1,6 @@ package com.thealgorithms.searches; -import static org.junit.jupiter.api.Assertions.*; +import static org.junit.jupiter.api.Assertions.assertArrayEquals; import org.junit.jupiter.api.Test; public class TestSearchInARowAndColWiseSortedMatrix { @@ -13,7 +13,6 @@ public void searchItem() { { 23, 24, 25, 26, 27 }, { 30, 31, 32, 33, 34 } }; - var test = new SearchInARowAndColWiseSortedMatrix(); int[] res = test.search(matrix, 16); int[] expectedResult = { 2, 2 }; @@ -29,7 +28,6 @@ public void notFound() { { 23, 24, 25, 26, 27 }, { 30, 31, 32, 33, 34 } }; - var test = new SearchInARowAndColWiseSortedMatrix(); int[] res = test.search(matrix, 96); int[] expectedResult = { -1, -1 }; From 77ba83ebe1bc902847fe2b3fdfc49ea8b5f17c0a Mon Sep 17 00:00:00 2001 From: Sadiul Hakim Date: Mon, 10 Mar 2025 12:33:19 +0600 Subject: [PATCH 11/32] Removing all Clang format issues --- .../com/thealgorithms/maths/MathBuilder.java | 52 ------------------- .../matrix/PrintAMatrixInSpiralOrder.java | 9 ---- .../thealgorithms/maths/MathBuilderTest.java | 42 +++------------ .../others/TestPrintMatrixInSpiralOrder.java | 18 ++----- 4 files changed, 11 insertions(+), 110 deletions(-) diff --git a/src/main/java/com/thealgorithms/maths/MathBuilder.java b/src/main/java/com/thealgorithms/maths/MathBuilder.java index 4aff9ab2d57a..4cdb4d26c8e6 100644 --- a/src/main/java/com/thealgorithms/maths/MathBuilder.java +++ b/src/main/java/com/thealgorithms/maths/MathBuilder.java @@ -12,16 +12,13 @@ */ public final class MathBuilder { private final double result; - private MathBuilder(Builder builder) { this.result = builder.number; } - // Returns final result public double get() { return result; } - // Return result in long public long toLong() { try { @@ -49,15 +46,12 @@ public long toLong() { public static class Builder { private double number; private double memory = 0; - public Builder() { number = 0; } - public Builder(double num) { number = num; } - public Builder add(double num) { number += num; return this; @@ -65,11 +59,9 @@ public Builder add(double num) { // Takes a number and a condition, only does the operation if condition is true. public Builder addIf(double num, BiFunction condition) { - if (condition.apply(number, num)) { number += num; } - return this; } @@ -83,7 +75,6 @@ public Builder minusIf(double num, BiFunction condition if (condition.apply(number, num)) { number -= num; } - return this; } @@ -92,7 +83,6 @@ public Builder rand(long seed) { if (number != 0) { throw new RuntimeException("Number must be zero for random assignment!"); } - Random random = new Random(); number = random.nextDouble(seed); return this; @@ -103,7 +93,6 @@ public Builder pi() { if (number != 0) { throw new RuntimeException("Number must be zero for PI assignment!"); } - number = Math.PI; return this; } @@ -113,7 +102,6 @@ public Builder e() { if (number != 0) { throw new RuntimeException("Number must be zero for E assignment!"); } - number = Math.E; return this; } @@ -123,7 +111,6 @@ public Builder randomInRange(double min, double max) { if (number != 0) { throw new RuntimeException("Number must be zero for random assignment!"); } - Random random = new Random(); number = min + (max - min) * random.nextDouble(); return this; @@ -145,39 +132,31 @@ public Builder min(double num) { } public Builder multiply(double num) { - number *= num; return this; } // Takes a number and a condition, only does the operation if condition is true. public Builder multiplyIf(double num, BiFunction condition) { - - if (condition.apply(number, num)) { number *= num; } - return this; } public Builder divide(double num) { - if (num == 0) { return this; } - number /= num; return this; } // Takes a number and a condition, only does the operation if condition is true. public Builder divideIf(double num, BiFunction condition) { - if (num == 0) { return this; } - if (condition.apply(number, num)) { number /= num; } @@ -185,15 +164,12 @@ public Builder divideIf(double num, BiFunction conditio } public Builder mod(double num) { - number %= num; return this; } // Takes a number and a condition, only does the operation if condition is true. public Builder modIf(double num, BiFunction condition) { - - if (condition.apply(number, num)) { number %= num; } @@ -201,117 +177,98 @@ public Builder modIf(double num, BiFunction condition) } public Builder pow(double num) { - number = Math.pow(number, num); return this; } public Builder sqrt() { - number = Math.sqrt(number); return this; } public Builder round() { - number = Math.round(number); return this; } public Builder floor() { - number = Math.floor(number); return this; } public Builder ceil() { - number = Math.ceil(number); return this; } public Builder abs() { - number = Math.abs(number); return this; } public Builder cbrt() { - number = Math.cbrt(number); return this; } public Builder log() { - number = Math.log(number); return this; } public Builder log10() { - number = Math.log10(number); return this; } public Builder sin() { - number = Math.sin(number); return this; } public Builder cos() { - number = Math.cos(number); return this; } public Builder tan() { - number = Math.tan(number); return this; } public Builder sinh() { - number = Math.sinh(number); return this; } public Builder cosh() { - number = Math.cosh(number); return this; } public Builder tanh() { - number = Math.tanh(number); return this; } public Builder exp() { - number = Math.exp(number); return this; } public Builder toRadians() { - number = Math.toRadians(number); return this; } // Remembers the NUMBER public Builder remember() { - memory = number; return this; } // Recalls the NUMBER public Builder recall(boolean cleanMemory) { - number = memory; if (cleanMemory) { memory = 0; @@ -322,11 +279,9 @@ public Builder recall(boolean cleanMemory) { // Recalls the NUMBER on condition public Builder recallIf(Function condition, boolean cleanMemory) { - if (!condition.apply(number)) { return this; } - number = memory; if (cleanMemory) { memory = 0; @@ -337,26 +292,21 @@ public Builder recallIf(Function condition, boolean cleanMemory // Replaces NUMBER with given number public Builder set(double num) { - if (number != 0) { throw new RuntimeException("Number must be zero to set!"); } - number = num; return this; } // Replaces NUMBER with given number on condition public Builder setIf(double num, BiFunction condition) { - if (number != 0) { throw new RuntimeException("Number must be zero to set!"); } - if (condition.apply(number, num)) { number = num; } - return this; } @@ -367,7 +317,6 @@ public Builder print() { } public Builder format(String format) { - DecimalFormat formater = new DecimalFormat(format); String num = formater.format(number); number = Double.parseDouble(num); @@ -375,7 +324,6 @@ public Builder format(String format) { } public Builder format(int decimalPlace) { - String pattern = "." + "#".repeat(decimalPlace); DecimalFormat formater = new DecimalFormat(pattern); String num = formater.format(number); diff --git a/src/main/java/com/thealgorithms/matrix/PrintAMatrixInSpiralOrder.java b/src/main/java/com/thealgorithms/matrix/PrintAMatrixInSpiralOrder.java index 2e735222b7a6..2757da1f9023 100644 --- a/src/main/java/com/thealgorithms/matrix/PrintAMatrixInSpiralOrder.java +++ b/src/main/java/com/thealgorithms/matrix/PrintAMatrixInSpiralOrder.java @@ -12,7 +12,6 @@ public class PrintAMatrixInSpiralOrder { * @param col number of columns matrix has * @author Sadiul Hakim : https://github.com/sadiul-hakim */ - public List print(int[][] matrix, int row, int col) { // r traverses matrix row wise from first @@ -20,35 +19,27 @@ public List print(int[][] matrix, int row, int col) { // c traverses matrix column wise from first int c = 0; int i; - List result = new ArrayList<>(); - while (r < row && c < col) { // print first row of matrix for (i = c; i < col; i++) { result.add(matrix[r][i]); } - // increase r by one because first row printed r++; - // print last column for (i = r; i < row; i++) { result.add(matrix[i][col - 1]); } - // decrease col by one because last column has been printed col--; - // print rows from last except printed elements if (r < row) { for (i = col - 1; i >= c; i--) { result.add(matrix[row - 1][i]); } - row--; } - // print columns from first except printed elements if (c < col) { for (i = row - 1; i >= r; i--) { diff --git a/src/test/java/com/thealgorithms/maths/MathBuilderTest.java b/src/test/java/com/thealgorithms/maths/MathBuilderTest.java index 7af57b3afc30..2d5638dd10c1 100644 --- a/src/test/java/com/thealgorithms/maths/MathBuilderTest.java +++ b/src/test/java/com/thealgorithms/maths/MathBuilderTest.java @@ -2,49 +2,26 @@ import org.junit.jupiter.api.Test; -import static org.junit.jupiter.api.Assertions.*; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertArrayEquals; class MathBuilderTest { @Test void simpleMath() { - double result = new MathBuilder.Builder(100) - .add(200) - .multiply(10) - .print() - .divideIf(20, (a, b) -> a % 2 == 0) - .sqrt() - .print() - .ceil() - .build() - .get(); - + double result = new MathBuilder.Builder(100).add(200).multiply(10).print().divideIf(20, (a, b) -> a % 2 == 0).sqrt().print().ceil().build().get(); assertEquals(13, result); } @Test void memoryTest() { - long result = new MathBuilder.Builder() - .set(100) - .sqrt() - .remember() - .add(21) - .recallIf(a -> a < 50, true) - .mod(2) - .build() - .toLong(); + long result = new MathBuilder.Builder().set(100).sqrt().remember().add(21).recallIf(a -> a < 50, true).mod(2).build().toLong(); assertEquals(0, result); } @Test void freeFallDistance() { - long distance = new MathBuilder.Builder(9.81) - .multiply(0.5) // 0.5 * g - .multiply(5 * 5) // t^2 - .round() - .build() - .toLong(); - + long distance = new MathBuilder.Builder(9.81).multiply(0.5).multiply(5 * 5).round().build().toLong(); assertEquals(123, distance); // Expected result: 0.5 * 9.81 * 25 = 122.625 ≈ 123 } @@ -52,16 +29,9 @@ void freeFallDistance() { void batchSalaryProcessing() { double[] salaries = {2000, 3000, 4000, 5000}; long[] processedSalaries = new long[salaries.length]; - for (int i = 0; i < salaries.length; i++) { - processedSalaries[i] = new MathBuilder.Builder(salaries[i]) - .addIf(salaries[i] * 0.1, (sal, bonus) -> sal > 2500) // Apply 10% bonus if salary > 2500 - .multiply(0.92) // Deduct 8% tax - .round() // Round to the nearest whole number - .build() - .toLong(); + processedSalaries[i] = new MathBuilder.Builder(salaries[i]).addIf(salaries[i] * 0.1, (sal, bonus) -> sal > 2500).multiply(0.92).round().build().toLong(); } - long[] expectedSalaries = {1840, 3036, 4048, 5060}; assertArrayEquals(expectedSalaries, processedSalaries); } diff --git a/src/test/java/com/thealgorithms/others/TestPrintMatrixInSpiralOrder.java b/src/test/java/com/thealgorithms/others/TestPrintMatrixInSpiralOrder.java index 867311e1bce1..611c36585bcc 100644 --- a/src/test/java/com/thealgorithms/others/TestPrintMatrixInSpiralOrder.java +++ b/src/test/java/com/thealgorithms/others/TestPrintMatrixInSpiralOrder.java @@ -3,30 +3,22 @@ import java.util.List; import org.junit.jupiter.api.Test; -import static org.junit.jupiter.api.Assertions.*; + +import static org.junit.jupiter.api.Assertions.assertIterableEquals; public class TestPrintMatrixInSpiralOrder { @Test public void testOne() { - int[][] matrix = { - { 3, 4, 5, 6, 7 }, - { 8, 9, 10, 11, 12 }, - { 14, 15, 16, 17, 18 }, - { 23, 24, 25, 26, 27 }, - { 30, 31, 32, 33, 34 } - }; + int[][] matrix = {{3, 4, 5, 6, 7}, {8, 9, 10, 11, 12}, {14, 15, 16, 17, 18}, {23, 24, 25, 26, 27}, {30, 31, 32, 33, 34}}; var printClass = new PrintAMatrixInSpiralOrder(); List res = printClass.print(matrix, matrix.length, matrix[0].length); - List list = List.of(3, 4, 5, 6, 7, 12, 18, 27, 34, 33, 32, 31, 30, 23, 14, 8, 9, 10, 11, 17, 26, 25, - 24, 15, 16); + List list = List.of(3, 4, 5, 6, 7, 12, 18, 27, 34, 33, 32, 31, 30, 23, 14, 8, 9, 10, 11, 17, 26, 25, 24, 15, 16); assertIterableEquals(res, list); } @Test public void testTwo() { - int[][] matrix = { - { 2, 2 } - }; + int[][] matrix = {{2, 2}}; var printClass = new PrintAMatrixInSpiralOrder(); List res = printClass.print(matrix, matrix.length, matrix[0].length); List list = List.of(2, 2); From b559923541300142593d4813966b0ca88f6bb806 Mon Sep 17 00:00:00 2001 From: Sadiul Hakim Date: Mon, 10 Mar 2025 12:34:35 +0600 Subject: [PATCH 12/32] Removing all CheckStyle issues --- ...estSearchInARowAndColWiseSortedMatrix.java | 21 +++++-------------- 1 file changed, 5 insertions(+), 16 deletions(-) diff --git a/src/test/java/com/thealgorithms/searches/TestSearchInARowAndColWiseSortedMatrix.java b/src/test/java/com/thealgorithms/searches/TestSearchInARowAndColWiseSortedMatrix.java index 14d85a538990..a56f79670cf3 100644 --- a/src/test/java/com/thealgorithms/searches/TestSearchInARowAndColWiseSortedMatrix.java +++ b/src/test/java/com/thealgorithms/searches/TestSearchInARowAndColWiseSortedMatrix.java @@ -1,36 +1,25 @@ package com.thealgorithms.searches; import static org.junit.jupiter.api.Assertions.assertArrayEquals; + import org.junit.jupiter.api.Test; public class TestSearchInARowAndColWiseSortedMatrix { @Test public void searchItem() { - int[][] matrix = { - { 3, 4, 5, 6, 7 }, - { 8, 9, 10, 11, 12 }, - { 14, 15, 16, 17, 18 }, - { 23, 24, 25, 26, 27 }, - { 30, 31, 32, 33, 34 } - }; + int[][] matrix = {{3, 4, 5, 6, 7}, {8, 9, 10, 11, 12}, {14, 15, 16, 17, 18}, {23, 24, 25, 26, 27}, {30, 31, 32, 33, 34}}; var test = new SearchInARowAndColWiseSortedMatrix(); int[] res = test.search(matrix, 16); - int[] expectedResult = { 2, 2 }; + int[] expectedResult = {2, 2}; assertArrayEquals(expectedResult, res); } @Test public void notFound() { - int[][] matrix = { - { 3, 4, 5, 6, 7 }, - { 8, 9, 10, 11, 12 }, - { 14, 15, 16, 17, 18 }, - { 23, 24, 25, 26, 27 }, - { 30, 31, 32, 33, 34 } - }; + int[][] matrix = {{3, 4, 5, 6, 7}, {8, 9, 10, 11, 12}, {14, 15, 16, 17, 18}, {23, 24, 25, 26, 27}, {30, 31, 32, 33, 34}}; var test = new SearchInARowAndColWiseSortedMatrix(); int[] res = test.search(matrix, 96); - int[] expectedResult = { -1, -1 }; + int[] expectedResult = {-1, -1}; assertArrayEquals(expectedResult, res); } } From a2183e4e2d376ce0afd444c77b29ce61e6f34e5b Mon Sep 17 00:00:00 2001 From: Sadiul Hakim Date: Mon, 10 Mar 2025 12:40:29 +0600 Subject: [PATCH 13/32] Removing all CheckStyle issues --- src/main/java/com/thealgorithms/maths/MathBuilder.java | 9 ++++++++- .../java/com/thealgorithms/maths/MathBuilderTest.java | 4 ++-- .../others/TestPrintMatrixInSpiralOrder.java | 4 ++-- 3 files changed, 12 insertions(+), 5 deletions(-) diff --git a/src/main/java/com/thealgorithms/maths/MathBuilder.java b/src/main/java/com/thealgorithms/maths/MathBuilder.java index 4cdb4d26c8e6..af021a5ffbce 100644 --- a/src/main/java/com/thealgorithms/maths/MathBuilder.java +++ b/src/main/java/com/thealgorithms/maths/MathBuilder.java @@ -12,13 +12,16 @@ */ public final class MathBuilder { private final double result; + private MathBuilder(Builder builder) { this.result = builder.number; } + // Returns final result public double get() { return result; } + // Return result in long public long toLong() { try { @@ -46,12 +49,15 @@ public long toLong() { public static class Builder { private double number; private double memory = 0; + public Builder() { number = 0; } + public Builder(double num) { number = num; } + public Builder add(double num) { number += num; return this; @@ -324,7 +330,8 @@ public Builder format(String format) { } public Builder format(int decimalPlace) { - String pattern = "." + "#".repeat(decimalPlace); + String pattern = "." + + "#".repeat(decimalPlace); DecimalFormat formater = new DecimalFormat(pattern); String num = formater.format(number); number = Double.parseDouble(num); diff --git a/src/test/java/com/thealgorithms/maths/MathBuilderTest.java b/src/test/java/com/thealgorithms/maths/MathBuilderTest.java index 2d5638dd10c1..c4d884d6c07c 100644 --- a/src/test/java/com/thealgorithms/maths/MathBuilderTest.java +++ b/src/test/java/com/thealgorithms/maths/MathBuilderTest.java @@ -1,10 +1,10 @@ package com.thealgorithms.maths; -import org.junit.jupiter.api.Test; - import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertArrayEquals; +import org.junit.jupiter.api.Test; + class MathBuilderTest { @Test diff --git a/src/test/java/com/thealgorithms/others/TestPrintMatrixInSpiralOrder.java b/src/test/java/com/thealgorithms/others/TestPrintMatrixInSpiralOrder.java index 611c36585bcc..b38e1896b612 100644 --- a/src/test/java/com/thealgorithms/others/TestPrintMatrixInSpiralOrder.java +++ b/src/test/java/com/thealgorithms/others/TestPrintMatrixInSpiralOrder.java @@ -1,10 +1,10 @@ package com.thealgorithms.others; -import java.util.List; +import static org.junit.jupiter.api.Assertions.assertIterableEquals; +import java.util.List; import org.junit.jupiter.api.Test; -import static org.junit.jupiter.api.Assertions.assertIterableEquals; public class TestPrintMatrixInSpiralOrder { @Test From bcb26d1db937c3c5a77b4913d1df0031276da70d Mon Sep 17 00:00:00 2001 From: Sadiul Hakim Date: Mon, 10 Mar 2025 12:46:35 +0600 Subject: [PATCH 14/32] Removing all CheckStyle issues --- src/main/java/com/thealgorithms/maths/MathBuilder.java | 2 +- src/test/java/com/thealgorithms/maths/MathBuilderTest.java | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/thealgorithms/maths/MathBuilder.java b/src/main/java/com/thealgorithms/maths/MathBuilder.java index af021a5ffbce..e727a7d2f5f2 100644 --- a/src/main/java/com/thealgorithms/maths/MathBuilder.java +++ b/src/main/java/com/thealgorithms/maths/MathBuilder.java @@ -331,7 +331,7 @@ public Builder format(String format) { public Builder format(int decimalPlace) { String pattern = "." - + "#".repeat(decimalPlace); + +"#".repeat(decimalPlace); DecimalFormat formater = new DecimalFormat(pattern); String num = formater.format(number); number = Double.parseDouble(num); diff --git a/src/test/java/com/thealgorithms/maths/MathBuilderTest.java b/src/test/java/com/thealgorithms/maths/MathBuilderTest.java index c4d884d6c07c..b6ecc6746701 100644 --- a/src/test/java/com/thealgorithms/maths/MathBuilderTest.java +++ b/src/test/java/com/thealgorithms/maths/MathBuilderTest.java @@ -1,7 +1,7 @@ package com.thealgorithms.maths; -import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertArrayEquals; +import static org.junit.jupiter.api.Assertions.assertEquals; import org.junit.jupiter.api.Test; From 4251b76ad4e6ceaf2f08a974b361f9fe5a5a05df Mon Sep 17 00:00:00 2001 From: Sadiul Hakim Date: Mon, 10 Mar 2025 12:49:59 +0600 Subject: [PATCH 15/32] Removing all CheckStyle issues --- .../others/PrintAMatrixInSpiralOrder.java | 13 ------------- .../SearchInARowAndColWiseSortedMatrix.java | 1 - 2 files changed, 14 deletions(-) diff --git a/src/main/java/com/thealgorithms/others/PrintAMatrixInSpiralOrder.java b/src/main/java/com/thealgorithms/others/PrintAMatrixInSpiralOrder.java index d2065085d8c6..abfdd006879e 100644 --- a/src/main/java/com/thealgorithms/others/PrintAMatrixInSpiralOrder.java +++ b/src/main/java/com/thealgorithms/others/PrintAMatrixInSpiralOrder.java @@ -12,44 +12,33 @@ public class PrintAMatrixInSpiralOrder { * @param col number of columns matrix has * @author Sadiul Hakim : https://github.com/sadiul-hakim */ - public List print(int[][] matrix, int row, int col) { - // r traverses matrix row wise from first int r = 0; // c traverses matrix column wise from first int c = 0; int i; - List result = new ArrayList<>(); - while (r < row && c < col) { // print first row of matrix for (i = c; i < col; i++) { result.add(matrix[r][i]); } - // increase r by one because first row printed r++; - // print last column for (i = r; i < row; i++) { result.add(matrix[i][col - 1]); } - // decrease col by one because last column has been printed col--; - // print rows from last except printed elements if (r < row) { for (i = col - 1; i >= c; i--) { result.add(matrix[row - 1][i]); } - row--; - } - // print columns from first except printed elements if (c < col) { for (i = row - 1; i >= r; i--) { @@ -57,9 +46,7 @@ public List print(int[][] matrix, int row, int col) { } c++; } - } return result; } - } diff --git a/src/main/java/com/thealgorithms/searches/SearchInARowAndColWiseSortedMatrix.java b/src/main/java/com/thealgorithms/searches/SearchInARowAndColWiseSortedMatrix.java index 997738cece7d..b53c7e5256ca 100644 --- a/src/main/java/com/thealgorithms/searches/SearchInARowAndColWiseSortedMatrix.java +++ b/src/main/java/com/thealgorithms/searches/SearchInARowAndColWiseSortedMatrix.java @@ -1,6 +1,5 @@ package com.thealgorithms.searches; - public class SearchInARowAndColWiseSortedMatrix { /** * Search a key in row and column wise sorted matrix From c1c2465eaeb56fe626994fb6d008f23ab28da816 Mon Sep 17 00:00:00 2001 From: Sadiul Hakim Date: Mon, 10 Mar 2025 12:51:41 +0600 Subject: [PATCH 16/32] Added white space --- src/main/java/com/thealgorithms/maths/MathBuilder.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/com/thealgorithms/maths/MathBuilder.java b/src/main/java/com/thealgorithms/maths/MathBuilder.java index e727a7d2f5f2..af021a5ffbce 100644 --- a/src/main/java/com/thealgorithms/maths/MathBuilder.java +++ b/src/main/java/com/thealgorithms/maths/MathBuilder.java @@ -331,7 +331,7 @@ public Builder format(String format) { public Builder format(int decimalPlace) { String pattern = "." - +"#".repeat(decimalPlace); + + "#".repeat(decimalPlace); DecimalFormat formater = new DecimalFormat(pattern); String num = formater.format(number); number = Double.parseDouble(num); From 935868a718dcc5e4102f5b5a9d6496e3267da77b Mon Sep 17 00:00:00 2001 From: Sadiul Hakim Date: Mon, 10 Mar 2025 12:53:46 +0600 Subject: [PATCH 17/32] Added white space --- src/main/java/com/thealgorithms/maths/MathBuilder.java | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/main/java/com/thealgorithms/maths/MathBuilder.java b/src/main/java/com/thealgorithms/maths/MathBuilder.java index af021a5ffbce..0ddc3504dbd5 100644 --- a/src/main/java/com/thealgorithms/maths/MathBuilder.java +++ b/src/main/java/com/thealgorithms/maths/MathBuilder.java @@ -330,8 +330,7 @@ public Builder format(String format) { } public Builder format(int decimalPlace) { - String pattern = "." - + "#".repeat(decimalPlace); + String pattern = "." + "#".repeat(decimalPlace); DecimalFormat formater = new DecimalFormat(pattern); String num = formater.format(number); number = Double.parseDouble(num); From 6c464222f4cf647daf21bdc59c8ffd6996f31dc1 Mon Sep 17 00:00:00 2001 From: Sadiul Hakim Date: Tue, 11 Mar 2025 13:58:30 +0600 Subject: [PATCH 18/32] Fixing Clang Issue --- src/main/java/com/thealgorithms/maths/MathBuilder.java | 3 ++- .../com/thealgorithms/others/TestPrintMatrixInSpiralOrder.java | 1 - 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/thealgorithms/maths/MathBuilder.java b/src/main/java/com/thealgorithms/maths/MathBuilder.java index 0ddc3504dbd5..3848445de105 100644 --- a/src/main/java/com/thealgorithms/maths/MathBuilder.java +++ b/src/main/java/com/thealgorithms/maths/MathBuilder.java @@ -330,7 +330,8 @@ public Builder format(String format) { } public Builder format(int decimalPlace) { - String pattern = "." + "#".repeat(decimalPlace); + String pattern = "." + + "#".repeat(decimalPlace); DecimalFormat formater = new DecimalFormat(pattern); String num = formater.format(number); number = Double.parseDouble(num); diff --git a/src/test/java/com/thealgorithms/others/TestPrintMatrixInSpiralOrder.java b/src/test/java/com/thealgorithms/others/TestPrintMatrixInSpiralOrder.java index b38e1896b612..d35d4bb60c73 100644 --- a/src/test/java/com/thealgorithms/others/TestPrintMatrixInSpiralOrder.java +++ b/src/test/java/com/thealgorithms/others/TestPrintMatrixInSpiralOrder.java @@ -5,7 +5,6 @@ import java.util.List; import org.junit.jupiter.api.Test; - public class TestPrintMatrixInSpiralOrder { @Test public void testOne() { From 5c885b41b7bd6aaf7e9a0784444e8974531fcd9f Mon Sep 17 00:00:00 2001 From: Hakim's Garage <92100853+sadiul-hakim@users.noreply.github.com> Date: Tue, 11 Mar 2025 14:01:02 +0600 Subject: [PATCH 19/32] Update MathBuilder.java --- src/main/java/com/thealgorithms/maths/MathBuilder.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/com/thealgorithms/maths/MathBuilder.java b/src/main/java/com/thealgorithms/maths/MathBuilder.java index 3848445de105..9ce283eda5ca 100644 --- a/src/main/java/com/thealgorithms/maths/MathBuilder.java +++ b/src/main/java/com/thealgorithms/maths/MathBuilder.java @@ -331,7 +331,7 @@ public Builder format(String format) { public Builder format(int decimalPlace) { String pattern = "." - + "#".repeat(decimalPlace); + + "#".repeat(decimalPlace); DecimalFormat formater = new DecimalFormat(pattern); String num = formater.format(number); number = Double.parseDouble(num); From 31af39d6a4a52dd49625496d163a28b46753e948 Mon Sep 17 00:00:00 2001 From: Hakim's Garage <92100853+sadiul-hakim@users.noreply.github.com> Date: Tue, 11 Mar 2025 14:05:15 +0600 Subject: [PATCH 20/32] Update MathBuilder.java --- src/main/java/com/thealgorithms/maths/MathBuilder.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/com/thealgorithms/maths/MathBuilder.java b/src/main/java/com/thealgorithms/maths/MathBuilder.java index 9ce283eda5ca..7074333f1ca9 100644 --- a/src/main/java/com/thealgorithms/maths/MathBuilder.java +++ b/src/main/java/com/thealgorithms/maths/MathBuilder.java @@ -330,7 +330,7 @@ public Builder format(String format) { } public Builder format(int decimalPlace) { - String pattern = "." + String pattern = "." + "#".repeat(decimalPlace); DecimalFormat formater = new DecimalFormat(pattern); String num = formater.format(number); From 1725e6681c03c1b360d6f5ade4df986dfe276cac Mon Sep 17 00:00:00 2001 From: Hakim's Garage <92100853+sadiul-hakim@users.noreply.github.com> Date: Tue, 11 Mar 2025 14:07:23 +0600 Subject: [PATCH 21/32] Update MathBuilder.java --- src/main/java/com/thealgorithms/maths/MathBuilder.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/com/thealgorithms/maths/MathBuilder.java b/src/main/java/com/thealgorithms/maths/MathBuilder.java index 7074333f1ca9..96a7058228b0 100644 --- a/src/main/java/com/thealgorithms/maths/MathBuilder.java +++ b/src/main/java/com/thealgorithms/maths/MathBuilder.java @@ -331,7 +331,7 @@ public Builder format(String format) { public Builder format(int decimalPlace) { String pattern = "." - + "#".repeat(decimalPlace); + + "#".repeat(decimalPlace); DecimalFormat formater = new DecimalFormat(pattern); String num = formater.format(number); number = Double.parseDouble(num); From e76f17ffb0f315181eee54289d1297335e025b10 Mon Sep 17 00:00:00 2001 From: Hakim's Garage <92100853+sadiul-hakim@users.noreply.github.com> Date: Wed, 12 Mar 2025 19:14:44 +0600 Subject: [PATCH 22/32] Update MathBuilder.java --- src/main/java/com/thealgorithms/maths/MathBuilder.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/com/thealgorithms/maths/MathBuilder.java b/src/main/java/com/thealgorithms/maths/MathBuilder.java index 96a7058228b0..59f7e6954858 100644 --- a/src/main/java/com/thealgorithms/maths/MathBuilder.java +++ b/src/main/java/com/thealgorithms/maths/MathBuilder.java @@ -331,7 +331,7 @@ public Builder format(String format) { public Builder format(int decimalPlace) { String pattern = "." - + "#".repeat(decimalPlace); + + "#".repeat(decimalPlace); DecimalFormat formater = new DecimalFormat(pattern); String num = formater.format(number); number = Double.parseDouble(num); From f6b3c406bcc2741de328b03f67d4f5fbfec62782 Mon Sep 17 00:00:00 2001 From: Hakim's Garage <92100853+sadiul-hakim@users.noreply.github.com> Date: Wed, 12 Mar 2025 19:17:08 +0600 Subject: [PATCH 23/32] Update MathBuilder.java --- src/main/java/com/thealgorithms/maths/MathBuilder.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/com/thealgorithms/maths/MathBuilder.java b/src/main/java/com/thealgorithms/maths/MathBuilder.java index 59f7e6954858..3534749dd41c 100644 --- a/src/main/java/com/thealgorithms/maths/MathBuilder.java +++ b/src/main/java/com/thealgorithms/maths/MathBuilder.java @@ -331,7 +331,7 @@ public Builder format(String format) { public Builder format(int decimalPlace) { String pattern = "." - + "#".repeat(decimalPlace); + + "#".repeat(decimalPlace); DecimalFormat formater = new DecimalFormat(pattern); String num = formater.format(number); number = Double.parseDouble(num); From b44644bb44f510a9c0a305973cbcf8e4feba3320 Mon Sep 17 00:00:00 2001 From: Sadiul Hakim Date: Thu, 13 Mar 2025 14:57:47 +0600 Subject: [PATCH 24/32] Added Parenthesis in MathBuilder --- .../com/thealgorithms/maths/MathBuilder.java | 208 ++++++++++-------- .../thealgorithms/maths/MathBuilderTest.java | 14 ++ 2 files changed, 131 insertions(+), 91 deletions(-) diff --git a/src/main/java/com/thealgorithms/maths/MathBuilder.java b/src/main/java/com/thealgorithms/maths/MathBuilder.java index 3534749dd41c..5a41f4d39941 100644 --- a/src/main/java/com/thealgorithms/maths/MathBuilder.java +++ b/src/main/java/com/thealgorithms/maths/MathBuilder.java @@ -25,21 +25,11 @@ public double get() { // Return result in long public long toLong() { try { - if (Double.isNaN(result)) { - throw new IllegalArgumentException("Cannot convert NaN to long"); - } - if (result == Double.POSITIVE_INFINITY) { - return Long.MAX_VALUE; - } - if (result == Double.NEGATIVE_INFINITY) { - return Long.MIN_VALUE; - } - if (result > Long.MAX_VALUE) { - return Long.MAX_VALUE; - } - if (result < Long.MIN_VALUE) { - return Long.MIN_VALUE; - } + if (Double.isNaN(result)) throw new IllegalArgumentException("Cannot convert NaN to long!"); + if (result == Double.POSITIVE_INFINITY) return Long.MAX_VALUE; + if (result == Double.NEGATIVE_INFINITY) return Long.MIN_VALUE; + if (result > Long.MAX_VALUE) return Long.MAX_VALUE; + if (result < Long.MIN_VALUE) return Long.MIN_VALUE; return Math.round(result); } catch (Exception ex) { return 0; @@ -48,6 +38,8 @@ public long toLong() { public static class Builder { private double number; + private double sideNumber; + private boolean inParenthesis; private double memory = 0; public Builder() { @@ -59,36 +51,36 @@ public Builder(double num) { } public Builder add(double num) { - number += num; + if (inParenthesis) sideNumber += num; + else number += num; return this; } // Takes a number and a condition, only does the operation if condition is true. public Builder addIf(double num, BiFunction condition) { - if (condition.apply(number, num)) { - number += num; - } + if (!condition.apply(number, num)) return this; + if (inParenthesis) sideNumber += num; + else number += num; return this; } public Builder minus(double num) { - number -= num; + if (inParenthesis) sideNumber -= num; + else number -= num; return this; } // Takes a number and a condition, only does the operation if condition is true. public Builder minusIf(double num, BiFunction condition) { - if (condition.apply(number, num)) { - number -= num; - } + if (!condition.apply(number, num)) return this; + if (inParenthesis) sideNumber -= num; + else number -= num; return this; } // Generates a random number and sets to NUMBER public Builder rand(long seed) { - if (number != 0) { - throw new RuntimeException("Number must be zero for random assignment!"); - } + if (number != 0) throw new RuntimeException("Number must be zero for random assignment!"); Random random = new Random(); number = random.nextDouble(seed); return this; @@ -96,174 +88,186 @@ public Builder rand(long seed) { // Takes PI value and sets to NUMBER public Builder pi() { - if (number != 0) { - throw new RuntimeException("Number must be zero for PI assignment!"); - } + if (number != 0) throw new RuntimeException("Number must be zero for PI assignment!"); number = Math.PI; return this; } // Takes E value and sets to NUMBER public Builder e() { - if (number != 0) { - throw new RuntimeException("Number must be zero for E assignment!"); - } + if (number != 0) throw new RuntimeException("Number must be zero for E assignment!"); number = Math.E; return this; } public Builder randomInRange(double min, double max) { - - if (number != 0) { - throw new RuntimeException("Number must be zero for random assignment!"); - } + if (number != 0) throw new RuntimeException("Number must be zero for random assignment!"); Random random = new Random(); number = min + (max - min) * random.nextDouble(); return this; } public Builder toDegrees() { - number = Math.toDegrees(number); + if (inParenthesis) sideNumber = Math.toDegrees(sideNumber); + else number = Math.toDegrees(number); return this; } public Builder max(double num) { - number = Math.max(number, num); + if (inParenthesis) sideNumber = Math.max(sideNumber, num); + else number = Math.max(number, num); return this; } public Builder min(double num) { - number = Math.min(number, num); + if (inParenthesis) sideNumber = Math.min(sideNumber, num); + else number = Math.min(number, num); return this; } public Builder multiply(double num) { - number *= num; + if (inParenthesis) sideNumber *= num; + else number *= num; return this; } // Takes a number and a condition, only does the operation if condition is true. public Builder multiplyIf(double num, BiFunction condition) { - if (condition.apply(number, num)) { - number *= num; - } + if (!condition.apply(number, num)) return this; + if (inParenthesis) sideNumber *= num; + else number *= num; return this; } public Builder divide(double num) { - if (num == 0) { - return this; - } - number /= num; + if (num == 0) return this; + if (inParenthesis) sideNumber /= num; + else number /= num; return this; } // Takes a number and a condition, only does the operation if condition is true. public Builder divideIf(double num, BiFunction condition) { - if (num == 0) { - return this; - } - if (condition.apply(number, num)) { - number /= num; - } + if (num == 0) return this; + if (!condition.apply(number, num)) return this; + if (inParenthesis) sideNumber /= num; + else number /= num; return this; } public Builder mod(double num) { - number %= num; + if (inParenthesis) sideNumber %= num; + else number %= num; return this; } // Takes a number and a condition, only does the operation if condition is true. public Builder modIf(double num, BiFunction condition) { - if (condition.apply(number, num)) { - number %= num; - } + if (!condition.apply(number, num)) return this; + if (inParenthesis) sideNumber %= num; + else number %= num; return this; } public Builder pow(double num) { - number = Math.pow(number, num); + if (inParenthesis) sideNumber = Math.pow(sideNumber, num); + else number = Math.pow(number, num); return this; } public Builder sqrt() { - number = Math.sqrt(number); + if (inParenthesis) sideNumber = Math.sqrt(sideNumber); + else number = Math.sqrt(number); return this; } public Builder round() { - number = Math.round(number); + if (inParenthesis) sideNumber = Math.round(sideNumber); + else number = Math.round(number); return this; } public Builder floor() { - number = Math.floor(number); + if (inParenthesis) sideNumber = Math.floor(sideNumber); + else number = Math.floor(number); return this; } public Builder ceil() { - number = Math.ceil(number); + if (inParenthesis) sideNumber = Math.ceil(sideNumber); + else number = Math.ceil(number); return this; } public Builder abs() { - number = Math.abs(number); + if (inParenthesis) sideNumber = Math.abs(sideNumber); + else number = Math.abs(number); return this; } public Builder cbrt() { - number = Math.cbrt(number); + if (inParenthesis) sideNumber = Math.cbrt(sideNumber); + else number = Math.cbrt(number); return this; } public Builder log() { - number = Math.log(number); + if (inParenthesis) sideNumber = Math.log(sideNumber); + else number = Math.log(number); return this; } public Builder log10() { - number = Math.log10(number); + if (inParenthesis) sideNumber = Math.log10(sideNumber); + else number = Math.log10(number); return this; } public Builder sin() { - number = Math.sin(number); + if (inParenthesis) sideNumber = Math.sin(sideNumber); + else number = Math.sin(number); return this; } public Builder cos() { - number = Math.cos(number); + if (inParenthesis) sideNumber = Math.cos(sideNumber); + else number = Math.cos(number); return this; } public Builder tan() { - number = Math.tan(number); + if (inParenthesis) sideNumber = Math.tan(sideNumber); + else number = Math.tan(number); return this; } public Builder sinh() { - number = Math.sinh(number); + if (inParenthesis) sideNumber = Math.sinh(sideNumber); + else number = Math.sinh(number); return this; } public Builder cosh() { - number = Math.cosh(number); + if (inParenthesis) sideNumber = Math.cosh(sideNumber); + else number = Math.cosh(number); return this; } public Builder tanh() { - number = Math.tanh(number); + if (inParenthesis) sideNumber = Math.tanh(sideNumber); + else number = Math.tanh(number); return this; } public Builder exp() { - number = Math.exp(number); + if (inParenthesis) sideNumber = Math.exp(sideNumber); + else number = Math.exp(number); return this; } public Builder toRadians() { - number = Math.toRadians(number); + if (inParenthesis) sideNumber = Math.toRadians(sideNumber); + else number = Math.toRadians(number); return this; } @@ -279,40 +283,28 @@ public Builder recall(boolean cleanMemory) { if (cleanMemory) { memory = 0; } - return this; } // Recalls the NUMBER on condition public Builder recallIf(Function condition, boolean cleanMemory) { - if (!condition.apply(number)) { - return this; - } + if (!condition.apply(number)) return this; number = memory; - if (cleanMemory) { - memory = 0; - } - + if (cleanMemory) memory = 0; return this; } // Replaces NUMBER with given number public Builder set(double num) { - if (number != 0) { - throw new RuntimeException("Number must be zero to set!"); - } + if (number != 0) throw new RuntimeException("Number must be zero to set!"); number = num; return this; } // Replaces NUMBER with given number on condition public Builder setIf(double num, BiFunction condition) { - if (number != 0) { - throw new RuntimeException("Number must be zero to set!"); - } - if (condition.apply(number, num)) { - number = num; - } + if (number != 0) throw new RuntimeException("Number must be zero to set!"); + if (condition.apply(number, num)) number = num; return this; } @@ -322,6 +314,40 @@ public Builder print() { return this; } + public Builder openParenthesis(double num) { + sideNumber = num; + inParenthesis = true; + return this; + } + + public Builder closeParenthesisAndPlus() { + number += sideNumber; + inParenthesis = false; + sideNumber = 0; + return this; + } + + public Builder closeParenthesisAndMinus() { + number -= sideNumber; + inParenthesis = false; + sideNumber = 0; + return this; + } + + public Builder closeParenthesisAndMultiply() { + number *= sideNumber; + inParenthesis = false; + sideNumber = 0; + return this; + } + + public Builder closeParenthesisAndDivide() { + number /= sideNumber; + inParenthesis = false; + sideNumber = 0; + return this; + } + public Builder format(String format) { DecimalFormat formater = new DecimalFormat(format); String num = formater.format(number); diff --git a/src/test/java/com/thealgorithms/maths/MathBuilderTest.java b/src/test/java/com/thealgorithms/maths/MathBuilderTest.java index b6ecc6746701..a72ba8cf635d 100644 --- a/src/test/java/com/thealgorithms/maths/MathBuilderTest.java +++ b/src/test/java/com/thealgorithms/maths/MathBuilderTest.java @@ -35,4 +35,18 @@ void batchSalaryProcessing() { long[] expectedSalaries = {1840, 3036, 4048, 5060}; assertArrayEquals(expectedSalaries, processedSalaries); } + + @Test + void parenthesis() { + // 10 + (20*5) - 40 + (100 / 10) = 80 + double result = new MathBuilder.Builder(10).openParenthesis(20).multiply(5).closeParenthesisAndPlus().minus(40).openParenthesis(100).divide(10).closeParenthesisAndPlus().build().get(); + assertEquals(80, result); + } + + @Test + void areaOfCircle(){ + // Radius is 4 + double area = new MathBuilder.Builder().pi().openParenthesis(4).multiply(4).closeParenthesisAndMultiply().build().get(); + assertEquals((Math.PI*(4*4)),area); + } } From e3f3b4f3c35d6e3723c373f7857572fee5c40fe6 Mon Sep 17 00:00:00 2001 From: Hakim's Garage <92100853+sadiul-hakim@users.noreply.github.com> Date: Thu, 13 Mar 2025 15:09:24 +0600 Subject: [PATCH 25/32] Update MathBuilder.java --- .../com/thealgorithms/maths/MathBuilder.java | 243 ++++++++++++------ 1 file changed, 162 insertions(+), 81 deletions(-) diff --git a/src/main/java/com/thealgorithms/maths/MathBuilder.java b/src/main/java/com/thealgorithms/maths/MathBuilder.java index 5a41f4d39941..9baa64044891 100644 --- a/src/main/java/com/thealgorithms/maths/MathBuilder.java +++ b/src/main/java/com/thealgorithms/maths/MathBuilder.java @@ -25,11 +25,16 @@ public double get() { // Return result in long public long toLong() { try { - if (Double.isNaN(result)) throw new IllegalArgumentException("Cannot convert NaN to long!"); - if (result == Double.POSITIVE_INFINITY) return Long.MAX_VALUE; - if (result == Double.NEGATIVE_INFINITY) return Long.MIN_VALUE; - if (result > Long.MAX_VALUE) return Long.MAX_VALUE; - if (result < Long.MIN_VALUE) return Long.MIN_VALUE; + if (Double.isNaN(result)) + throw new IllegalArgumentException("Cannot convert NaN to long!"); + if (result == Double.POSITIVE_INFINITY) + return Long.MAX_VALUE; + if (result == Double.NEGATIVE_INFINITY) + return Long.MIN_VALUE; + if (result > Long.MAX_VALUE) + return Long.MAX_VALUE; + if (result < Long.MIN_VALUE) + return Long.MIN_VALUE; return Math.round(result); } catch (Exception ex) { return 0; @@ -51,36 +56,47 @@ public Builder(double num) { } public Builder add(double num) { - if (inParenthesis) sideNumber += num; - else number += num; + if (inParenthesis) + sideNumber += num; + else + number += num; return this; } // Takes a number and a condition, only does the operation if condition is true. public Builder addIf(double num, BiFunction condition) { - if (!condition.apply(number, num)) return this; - if (inParenthesis) sideNumber += num; - else number += num; + if (!condition.apply(number, num)) + return this; + if (inParenthesis) + sideNumber += num; + else + number += num; return this; } public Builder minus(double num) { - if (inParenthesis) sideNumber -= num; - else number -= num; + if (inParenthesis) + sideNumber -= num; + else + number -= num; return this; } // Takes a number and a condition, only does the operation if condition is true. public Builder minusIf(double num, BiFunction condition) { - if (!condition.apply(number, num)) return this; - if (inParenthesis) sideNumber -= num; - else number -= num; + if (!condition.apply(number, num)) + return this; + if (inParenthesis) + sideNumber -= num; + else + number -= num; return this; } // Generates a random number and sets to NUMBER public Builder rand(long seed) { - if (number != 0) throw new RuntimeException("Number must be zero for random assignment!"); + if (number != 0) + throw new RuntimeException("Number must be zero for random assignment!"); Random random = new Random(); number = random.nextDouble(seed); return this; @@ -88,186 +104,246 @@ public Builder rand(long seed) { // Takes PI value and sets to NUMBER public Builder pi() { - if (number != 0) throw new RuntimeException("Number must be zero for PI assignment!"); + if (number != 0) + throw new RuntimeException("Number must be zero for PI assignment!"); number = Math.PI; return this; } // Takes E value and sets to NUMBER public Builder e() { - if (number != 0) throw new RuntimeException("Number must be zero for E assignment!"); + if (number != 0) + throw new RuntimeException("Number must be zero for E assignment!"); number = Math.E; return this; } public Builder randomInRange(double min, double max) { - if (number != 0) throw new RuntimeException("Number must be zero for random assignment!"); + if (number != 0) + throw new RuntimeException("Number must be zero for random assignment!"); Random random = new Random(); number = min + (max - min) * random.nextDouble(); return this; } public Builder toDegrees() { - if (inParenthesis) sideNumber = Math.toDegrees(sideNumber); - else number = Math.toDegrees(number); + if (inParenthesis) + sideNumber = Math.toDegrees(sideNumber); + else + number = Math.toDegrees(number); return this; } public Builder max(double num) { - if (inParenthesis) sideNumber = Math.max(sideNumber, num); - else number = Math.max(number, num); + if (inParenthesis) + sideNumber = Math.max(sideNumber, num); + else + number = Math.max(number, num); return this; } public Builder min(double num) { - if (inParenthesis) sideNumber = Math.min(sideNumber, num); - else number = Math.min(number, num); + if (inParenthesis) + sideNumber = Math.min(sideNumber, num); + else + number = Math.min(number, num); return this; } public Builder multiply(double num) { - if (inParenthesis) sideNumber *= num; - else number *= num; + if (inParenthesis) + sideNumber *= num; + else + number *= num; return this; } // Takes a number and a condition, only does the operation if condition is true. public Builder multiplyIf(double num, BiFunction condition) { - if (!condition.apply(number, num)) return this; - if (inParenthesis) sideNumber *= num; - else number *= num; + if (!condition.apply(number, num)) + return this; + if (inParenthesis) + sideNumber *= num; + else + number *= num; return this; } public Builder divide(double num) { - if (num == 0) return this; - if (inParenthesis) sideNumber /= num; - else number /= num; + if (num == 0) + return this; + if (inParenthesis) + sideNumber /= num; + else + number /= num; return this; } // Takes a number and a condition, only does the operation if condition is true. public Builder divideIf(double num, BiFunction condition) { - if (num == 0) return this; - if (!condition.apply(number, num)) return this; - if (inParenthesis) sideNumber /= num; - else number /= num; + if (num == 0) + return this; + if (!condition.apply(number, num)) + return this; + if (inParenthesis) + sideNumber /= num; + else + number /= num; return this; } public Builder mod(double num) { - if (inParenthesis) sideNumber %= num; - else number %= num; + if (inParenthesis) + sideNumber %= num; + else + number %= num; return this; } // Takes a number and a condition, only does the operation if condition is true. public Builder modIf(double num, BiFunction condition) { - if (!condition.apply(number, num)) return this; - if (inParenthesis) sideNumber %= num; - else number %= num; + if (!condition.apply(number, num)) + return this; + if (inParenthesis) + sideNumber %= num; + else + number %= num; return this; } public Builder pow(double num) { - if (inParenthesis) sideNumber = Math.pow(sideNumber, num); - else number = Math.pow(number, num); + if (inParenthesis) + sideNumber = Math.pow(sideNumber, num); + else + number = Math.pow(number, num); return this; } public Builder sqrt() { - if (inParenthesis) sideNumber = Math.sqrt(sideNumber); - else number = Math.sqrt(number); + if (inParenthesis) + sideNumber = Math.sqrt(sideNumber); + else + number = Math.sqrt(number); return this; } public Builder round() { - if (inParenthesis) sideNumber = Math.round(sideNumber); - else number = Math.round(number); + if (inParenthesis) + sideNumber = Math.round(sideNumber); + else + number = Math.round(number); return this; } public Builder floor() { - if (inParenthesis) sideNumber = Math.floor(sideNumber); - else number = Math.floor(number); + if (inParenthesis) + sideNumber = Math.floor(sideNumber); + else + number = Math.floor(number); return this; } public Builder ceil() { - if (inParenthesis) sideNumber = Math.ceil(sideNumber); - else number = Math.ceil(number); + if (inParenthesis) + sideNumber = Math.ceil(sideNumber); + else + number = Math.ceil(number); return this; } public Builder abs() { - if (inParenthesis) sideNumber = Math.abs(sideNumber); - else number = Math.abs(number); + if (inParenthesis) + sideNumber = Math.abs(sideNumber); + else + number = Math.abs(number); return this; } public Builder cbrt() { - if (inParenthesis) sideNumber = Math.cbrt(sideNumber); - else number = Math.cbrt(number); + if (inParenthesis) + sideNumber = Math.cbrt(sideNumber); + else + number = Math.cbrt(number); return this; } public Builder log() { - if (inParenthesis) sideNumber = Math.log(sideNumber); - else number = Math.log(number); + if (inParenthesis) + sideNumber = Math.log(sideNumber); + else + number = Math.log(number); return this; } public Builder log10() { - if (inParenthesis) sideNumber = Math.log10(sideNumber); - else number = Math.log10(number); + if (inParenthesis) + sideNumber = Math.log10(sideNumber); + else + number = Math.log10(number); return this; } public Builder sin() { - if (inParenthesis) sideNumber = Math.sin(sideNumber); - else number = Math.sin(number); + if (inParenthesis) + sideNumber = Math.sin(sideNumber); + else + number = Math.sin(number); return this; } public Builder cos() { - if (inParenthesis) sideNumber = Math.cos(sideNumber); - else number = Math.cos(number); + if (inParenthesis) + sideNumber = Math.cos(sideNumber); + else + number = Math.cos(number); return this; } public Builder tan() { - if (inParenthesis) sideNumber = Math.tan(sideNumber); - else number = Math.tan(number); + if (inParenthesis) + sideNumber = Math.tan(sideNumber); + else + number = Math.tan(number); return this; } public Builder sinh() { - if (inParenthesis) sideNumber = Math.sinh(sideNumber); - else number = Math.sinh(number); + if (inParenthesis) + sideNumber = Math.sinh(sideNumber); + else + number = Math.sinh(number); return this; } public Builder cosh() { - if (inParenthesis) sideNumber = Math.cosh(sideNumber); - else number = Math.cosh(number); + if (inParenthesis) + sideNumber = Math.cosh(sideNumber); + else + number = Math.cosh(number); return this; } public Builder tanh() { - if (inParenthesis) sideNumber = Math.tanh(sideNumber); - else number = Math.tanh(number); + if (inParenthesis) + sideNumber = Math.tanh(sideNumber); + else + number = Math.tanh(number); return this; } public Builder exp() { - if (inParenthesis) sideNumber = Math.exp(sideNumber); - else number = Math.exp(number); + if (inParenthesis) + sideNumber = Math.exp(sideNumber); + else + number = Math.exp(number); return this; } public Builder toRadians() { - if (inParenthesis) sideNumber = Math.toRadians(sideNumber); - else number = Math.toRadians(number); + if (inParenthesis) + sideNumber = Math.toRadians(sideNumber); + else + number = Math.toRadians(number); return this; } @@ -288,23 +364,28 @@ public Builder recall(boolean cleanMemory) { // Recalls the NUMBER on condition public Builder recallIf(Function condition, boolean cleanMemory) { - if (!condition.apply(number)) return this; + if (!condition.apply(number)) + return this; number = memory; - if (cleanMemory) memory = 0; + if (cleanMemory) + memory = 0; return this; } // Replaces NUMBER with given number public Builder set(double num) { - if (number != 0) throw new RuntimeException("Number must be zero to set!"); + if (number != 0) + throw new RuntimeException("Number must be zero to set!"); number = num; return this; } // Replaces NUMBER with given number on condition public Builder setIf(double num, BiFunction condition) { - if (number != 0) throw new RuntimeException("Number must be zero to set!"); - if (condition.apply(number, num)) number = num; + if (number != 0) + throw new RuntimeException("Number must be zero to set!"); + if (condition.apply(number, num)) + number = num; return this; } From 982745eeb636276bf6c2f2e16bc8bb27b4baad81 Mon Sep 17 00:00:00 2001 From: Hakim's Garage <92100853+sadiul-hakim@users.noreply.github.com> Date: Thu, 13 Mar 2025 15:10:28 +0600 Subject: [PATCH 26/32] Update MathBuilderTest.java --- src/test/java/com/thealgorithms/maths/MathBuilderTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/java/com/thealgorithms/maths/MathBuilderTest.java b/src/test/java/com/thealgorithms/maths/MathBuilderTest.java index a72ba8cf635d..ed5c25d1c4a0 100644 --- a/src/test/java/com/thealgorithms/maths/MathBuilderTest.java +++ b/src/test/java/com/thealgorithms/maths/MathBuilderTest.java @@ -47,6 +47,6 @@ void parenthesis() { void areaOfCircle(){ // Radius is 4 double area = new MathBuilder.Builder().pi().openParenthesis(4).multiply(4).closeParenthesisAndMultiply().build().get(); - assertEquals((Math.PI*(4*4)),area); + assertEquals((Math.PI * (4 * 4)), area); } } From 199f3ad4b2c313cb2ac7ae5db351da23545237b2 Mon Sep 17 00:00:00 2001 From: Hakim's Garage <92100853+sadiul-hakim@users.noreply.github.com> Date: Thu, 13 Mar 2025 15:12:06 +0600 Subject: [PATCH 27/32] Update MathBuilderTest.java --- src/test/java/com/thealgorithms/maths/MathBuilderTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/java/com/thealgorithms/maths/MathBuilderTest.java b/src/test/java/com/thealgorithms/maths/MathBuilderTest.java index ed5c25d1c4a0..1550a90cf8d5 100644 --- a/src/test/java/com/thealgorithms/maths/MathBuilderTest.java +++ b/src/test/java/com/thealgorithms/maths/MathBuilderTest.java @@ -44,7 +44,7 @@ void parenthesis() { } @Test - void areaOfCircle(){ + void areaOfCircle() { // Radius is 4 double area = new MathBuilder.Builder().pi().openParenthesis(4).multiply(4).closeParenthesisAndMultiply().build().get(); assertEquals((Math.PI * (4 * 4)), area); From 1558d13ad693d5f79dfd14b125d0f29063a5cbf8 Mon Sep 17 00:00:00 2001 From: Hakim's Garage <92100853+sadiul-hakim@users.noreply.github.com> Date: Thu, 13 Mar 2025 15:18:17 +0600 Subject: [PATCH 28/32] Update MathBuilder.java --- .../com/thealgorithms/maths/MathBuilder.java | 181 ++++++++---------- 1 file changed, 81 insertions(+), 100 deletions(-) diff --git a/src/main/java/com/thealgorithms/maths/MathBuilder.java b/src/main/java/com/thealgorithms/maths/MathBuilder.java index 9baa64044891..e5308fc2e933 100644 --- a/src/main/java/com/thealgorithms/maths/MathBuilder.java +++ b/src/main/java/com/thealgorithms/maths/MathBuilder.java @@ -25,16 +25,11 @@ public double get() { // Return result in long public long toLong() { try { - if (Double.isNaN(result)) - throw new IllegalArgumentException("Cannot convert NaN to long!"); - if (result == Double.POSITIVE_INFINITY) - return Long.MAX_VALUE; - if (result == Double.NEGATIVE_INFINITY) - return Long.MIN_VALUE; - if (result > Long.MAX_VALUE) - return Long.MAX_VALUE; - if (result < Long.MIN_VALUE) - return Long.MIN_VALUE; + if (Double.isNaN(result)) throw new IllegalArgumentException("Cannot convert NaN to long!"); + if (result == Double.POSITIVE_INFINITY) return Long.MAX_VALUE; + if (result == Double.NEGATIVE_INFINITY) return Long.MIN_VALUE; + if (result > Long.MAX_VALUE) return Long.MAX_VALUE; + if (result < Long.MIN_VALUE) return Long.MIN_VALUE; return Math.round(result); } catch (Exception ex) { return 0; @@ -56,47 +51,44 @@ public Builder(double num) { } public Builder add(double num) { - if (inParenthesis) + if (inParenthesis) sideNumber += num; - else + else number += num; return this; } // Takes a number and a condition, only does the operation if condition is true. public Builder addIf(double num, BiFunction condition) { - if (!condition.apply(number, num)) - return this; - if (inParenthesis) + if (!condition.apply(number, num)) return this; + if (inParenthesis) sideNumber += num; - else + else number += num; return this; } public Builder minus(double num) { - if (inParenthesis) + if (inParenthesis) sideNumber -= num; - else + else number -= num; return this; } // Takes a number and a condition, only does the operation if condition is true. public Builder minusIf(double num, BiFunction condition) { - if (!condition.apply(number, num)) - return this; - if (inParenthesis) + if (!condition.apply(number, num)) return this; + if (inParenthesis) sideNumber -= num; - else + else number -= num; return this; } // Generates a random number and sets to NUMBER public Builder rand(long seed) { - if (number != 0) - throw new RuntimeException("Number must be zero for random assignment!"); + if (number != 0) throw new RuntimeException("Number must be zero for random assignment!"); Random random = new Random(); number = random.nextDouble(seed); return this; @@ -104,245 +96,237 @@ public Builder rand(long seed) { // Takes PI value and sets to NUMBER public Builder pi() { - if (number != 0) - throw new RuntimeException("Number must be zero for PI assignment!"); + if (number != 0) throw new RuntimeException("Number must be zero for PI assignment!"); number = Math.PI; return this; } // Takes E value and sets to NUMBER public Builder e() { - if (number != 0) - throw new RuntimeException("Number must be zero for E assignment!"); + if (number != 0) throw new RuntimeException("Number must be zero for E assignment!"); number = Math.E; return this; } public Builder randomInRange(double min, double max) { - if (number != 0) - throw new RuntimeException("Number must be zero for random assignment!"); + if (number != 0) throw new RuntimeException("Number must be zero for random assignment!"); Random random = new Random(); number = min + (max - min) * random.nextDouble(); return this; } public Builder toDegrees() { - if (inParenthesis) + if (inParenthesis) sideNumber = Math.toDegrees(sideNumber); - else + else number = Math.toDegrees(number); return this; } public Builder max(double num) { - if (inParenthesis) + if (inParenthesis) sideNumber = Math.max(sideNumber, num); - else + else number = Math.max(number, num); return this; } public Builder min(double num) { - if (inParenthesis) + if (inParenthesis) sideNumber = Math.min(sideNumber, num); - else + else number = Math.min(number, num); return this; } public Builder multiply(double num) { - if (inParenthesis) + if (inParenthesis) sideNumber *= num; - else + else number *= num; return this; } // Takes a number and a condition, only does the operation if condition is true. public Builder multiplyIf(double num, BiFunction condition) { - if (!condition.apply(number, num)) - return this; - if (inParenthesis) + if (!condition.apply(number, num)) return this; + if (inParenthesis) sideNumber *= num; - else + else number *= num; return this; } public Builder divide(double num) { - if (num == 0) - return this; - if (inParenthesis) + if (num == 0) return this; + if (inParenthesis) sideNumber /= num; - else + else number /= num; return this; } // Takes a number and a condition, only does the operation if condition is true. public Builder divideIf(double num, BiFunction condition) { - if (num == 0) - return this; - if (!condition.apply(number, num)) - return this; - if (inParenthesis) + if (num == 0) return this; + if (!condition.apply(number, num)) return this; + if (inParenthesis) sideNumber /= num; - else + else number /= num; return this; } public Builder mod(double num) { - if (inParenthesis) + if (inParenthesis) sideNumber %= num; - else + else number %= num; return this; } // Takes a number and a condition, only does the operation if condition is true. public Builder modIf(double num, BiFunction condition) { - if (!condition.apply(number, num)) - return this; - if (inParenthesis) + if (!condition.apply(number, num)) return this; + if (inParenthesis) sideNumber %= num; - else + else number %= num; return this; } public Builder pow(double num) { - if (inParenthesis) + if (inParenthesis) sideNumber = Math.pow(sideNumber, num); - else + else number = Math.pow(number, num); return this; } public Builder sqrt() { - if (inParenthesis) + if (inParenthesis) sideNumber = Math.sqrt(sideNumber); - else + else number = Math.sqrt(number); return this; } public Builder round() { - if (inParenthesis) + if (inParenthesis) sideNumber = Math.round(sideNumber); - else + else number = Math.round(number); return this; } public Builder floor() { - if (inParenthesis) + if (inParenthesis) sideNumber = Math.floor(sideNumber); - else + else number = Math.floor(number); return this; } public Builder ceil() { - if (inParenthesis) + if (inParenthesis) sideNumber = Math.ceil(sideNumber); - else + else number = Math.ceil(number); return this; } public Builder abs() { - if (inParenthesis) + if (inParenthesis) sideNumber = Math.abs(sideNumber); - else + else number = Math.abs(number); return this; } public Builder cbrt() { - if (inParenthesis) + if (inParenthesis) sideNumber = Math.cbrt(sideNumber); - else + else number = Math.cbrt(number); return this; } public Builder log() { - if (inParenthesis) + if (inParenthesis) sideNumber = Math.log(sideNumber); - else + else number = Math.log(number); return this; } public Builder log10() { - if (inParenthesis) + if (inParenthesis) sideNumber = Math.log10(sideNumber); - else + else number = Math.log10(number); return this; } public Builder sin() { - if (inParenthesis) + if (inParenthesis) sideNumber = Math.sin(sideNumber); - else + else number = Math.sin(number); return this; } public Builder cos() { - if (inParenthesis) + if (inParenthesis) sideNumber = Math.cos(sideNumber); - else + else number = Math.cos(number); return this; } public Builder tan() { - if (inParenthesis) + if (inParenthesis) sideNumber = Math.tan(sideNumber); - else + else number = Math.tan(number); return this; } public Builder sinh() { - if (inParenthesis) + if (inParenthesis) sideNumber = Math.sinh(sideNumber); - else + else number = Math.sinh(number); return this; } public Builder cosh() { - if (inParenthesis) + if (inParenthesis) sideNumber = Math.cosh(sideNumber); - else + else number = Math.cosh(number); return this; } public Builder tanh() { - if (inParenthesis) + if (inParenthesis) sideNumber = Math.tanh(sideNumber); - else + else number = Math.tanh(number); return this; } public Builder exp() { - if (inParenthesis) + if (inParenthesis) sideNumber = Math.exp(sideNumber); - else + else number = Math.exp(number); return this; } public Builder toRadians() { - if (inParenthesis) + if (inParenthesis) sideNumber = Math.toRadians(sideNumber); - else + else number = Math.toRadians(number); return this; } @@ -364,27 +348,24 @@ public Builder recall(boolean cleanMemory) { // Recalls the NUMBER on condition public Builder recallIf(Function condition, boolean cleanMemory) { - if (!condition.apply(number)) - return this; + if (!condition.apply(number)) return this; number = memory; - if (cleanMemory) + if (cleanMemory) memory = 0; return this; } // Replaces NUMBER with given number public Builder set(double num) { - if (number != 0) - throw new RuntimeException("Number must be zero to set!"); + if (number != 0) throw new RuntimeException("Number must be zero to set!"); number = num; return this; } // Replaces NUMBER with given number on condition public Builder setIf(double num, BiFunction condition) { - if (number != 0) - throw new RuntimeException("Number must be zero to set!"); - if (condition.apply(number, num)) + if (number != 0) throw new RuntimeException("Number must be zero to set!"); + if (condition.apply(number, num)) number = num; return this; } From 9d5f2342761d50a1ebd9948d3d12ca39cc16c9a4 Mon Sep 17 00:00:00 2001 From: Hakim's Garage <92100853+sadiul-hakim@users.noreply.github.com> Date: Thu, 13 Mar 2025 15:20:33 +0600 Subject: [PATCH 29/32] Update MathBuilder.java --- src/main/java/com/thealgorithms/maths/MathBuilder.java | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/main/java/com/thealgorithms/maths/MathBuilder.java b/src/main/java/com/thealgorithms/maths/MathBuilder.java index e5308fc2e933..1a4b9f4e6b75 100644 --- a/src/main/java/com/thealgorithms/maths/MathBuilder.java +++ b/src/main/java/com/thealgorithms/maths/MathBuilder.java @@ -350,8 +350,7 @@ public Builder recall(boolean cleanMemory) { public Builder recallIf(Function condition, boolean cleanMemory) { if (!condition.apply(number)) return this; number = memory; - if (cleanMemory) - memory = 0; + if (cleanMemory) memory = 0; return this; } @@ -365,8 +364,7 @@ public Builder set(double num) { // Replaces NUMBER with given number on condition public Builder setIf(double num, BiFunction condition) { if (number != 0) throw new RuntimeException("Number must be zero to set!"); - if (condition.apply(number, num)) - number = num; + if (condition.apply(number, num)) number = num; return this; } From 0b66931db3fce056351d242fd62d676521893554 Mon Sep 17 00:00:00 2001 From: Sadiul Hakim Date: Thu, 13 Mar 2025 15:32:59 +0600 Subject: [PATCH 30/32] Added Parenthesis in MathBuilder --- .../com/thealgorithms/maths/MathBuilder.java | 150 +++++++++++------- 1 file changed, 90 insertions(+), 60 deletions(-) diff --git a/src/main/java/com/thealgorithms/maths/MathBuilder.java b/src/main/java/com/thealgorithms/maths/MathBuilder.java index 1a4b9f4e6b75..159bd0a0d708 100644 --- a/src/main/java/com/thealgorithms/maths/MathBuilder.java +++ b/src/main/java/com/thealgorithms/maths/MathBuilder.java @@ -51,38 +51,42 @@ public Builder(double num) { } public Builder add(double num) { - if (inParenthesis) + if (inParenthesis) { sideNumber += num; - else + } else { number += num; + } return this; } // Takes a number and a condition, only does the operation if condition is true. public Builder addIf(double num, BiFunction condition) { if (!condition.apply(number, num)) return this; - if (inParenthesis) + if (inParenthesis) { sideNumber += num; - else + } else { number += num; + } return this; } public Builder minus(double num) { - if (inParenthesis) + if (inParenthesis) { sideNumber -= num; - else + } else { number -= num; + } return this; } // Takes a number and a condition, only does the operation if condition is true. public Builder minusIf(double num, BiFunction condition) { if (!condition.apply(number, num)) return this; - if (inParenthesis) + if (inParenthesis) { sideNumber -= num; - else + } else { number -= num; + } return this; } @@ -116,53 +120,59 @@ public Builder randomInRange(double min, double max) { } public Builder toDegrees() { - if (inParenthesis) + if (inParenthesis) { sideNumber = Math.toDegrees(sideNumber); - else + } else { number = Math.toDegrees(number); + } return this; } public Builder max(double num) { - if (inParenthesis) + if (inParenthesis) { sideNumber = Math.max(sideNumber, num); - else + } else { number = Math.max(number, num); + } return this; } public Builder min(double num) { - if (inParenthesis) + if (inParenthesis) { sideNumber = Math.min(sideNumber, num); - else + } else { number = Math.min(number, num); + } return this; } public Builder multiply(double num) { - if (inParenthesis) + if (inParenthesis) { sideNumber *= num; - else + } else { number *= num; + } return this; } // Takes a number and a condition, only does the operation if condition is true. public Builder multiplyIf(double num, BiFunction condition) { if (!condition.apply(number, num)) return this; - if (inParenthesis) + if (inParenthesis) { sideNumber *= num; - else + } else { number *= num; + } return this; } public Builder divide(double num) { if (num == 0) return this; - if (inParenthesis) + if (inParenthesis) { sideNumber /= num; - else + } else { number /= num; + } return this; } @@ -170,164 +180,184 @@ public Builder divide(double num) { public Builder divideIf(double num, BiFunction condition) { if (num == 0) return this; if (!condition.apply(number, num)) return this; - if (inParenthesis) + if (inParenthesis) { sideNumber /= num; - else + } else { number /= num; + } return this; } public Builder mod(double num) { - if (inParenthesis) + if (inParenthesis) { sideNumber %= num; - else + } else { number %= num; + } return this; } // Takes a number and a condition, only does the operation if condition is true. public Builder modIf(double num, BiFunction condition) { if (!condition.apply(number, num)) return this; - if (inParenthesis) + if (inParenthesis) { sideNumber %= num; - else + } else { number %= num; + } return this; } public Builder pow(double num) { - if (inParenthesis) + if (inParenthesis) { sideNumber = Math.pow(sideNumber, num); - else + } else { number = Math.pow(number, num); + } return this; } public Builder sqrt() { - if (inParenthesis) + if (inParenthesis) { sideNumber = Math.sqrt(sideNumber); - else + } else { number = Math.sqrt(number); + } return this; } public Builder round() { - if (inParenthesis) + if (inParenthesis) { sideNumber = Math.round(sideNumber); - else + } else { number = Math.round(number); + } return this; } public Builder floor() { - if (inParenthesis) + if (inParenthesis) { sideNumber = Math.floor(sideNumber); - else + } else { number = Math.floor(number); + } return this; } public Builder ceil() { - if (inParenthesis) + if (inParenthesis) { sideNumber = Math.ceil(sideNumber); - else + } else { number = Math.ceil(number); + } return this; } public Builder abs() { - if (inParenthesis) + if (inParenthesis) { sideNumber = Math.abs(sideNumber); - else + } else { number = Math.abs(number); + } return this; } public Builder cbrt() { - if (inParenthesis) + if (inParenthesis) { sideNumber = Math.cbrt(sideNumber); - else + } else { number = Math.cbrt(number); + } return this; } public Builder log() { - if (inParenthesis) + if (inParenthesis) { sideNumber = Math.log(sideNumber); - else + } else { number = Math.log(number); + } return this; } public Builder log10() { - if (inParenthesis) + if (inParenthesis) { sideNumber = Math.log10(sideNumber); - else + } else { number = Math.log10(number); + } return this; } public Builder sin() { - if (inParenthesis) + if (inParenthesis) { sideNumber = Math.sin(sideNumber); - else + } else { number = Math.sin(number); + } return this; } public Builder cos() { - if (inParenthesis) + if (inParenthesis) { sideNumber = Math.cos(sideNumber); - else + } else { number = Math.cos(number); + } return this; } public Builder tan() { - if (inParenthesis) + if (inParenthesis) { sideNumber = Math.tan(sideNumber); - else + } else { number = Math.tan(number); + } return this; } public Builder sinh() { - if (inParenthesis) + if (inParenthesis) { sideNumber = Math.sinh(sideNumber); - else + } else { number = Math.sinh(number); + } return this; } public Builder cosh() { - if (inParenthesis) + if (inParenthesis) { sideNumber = Math.cosh(sideNumber); - else + } else { number = Math.cosh(number); + } return this; } public Builder tanh() { - if (inParenthesis) + if (inParenthesis) { sideNumber = Math.tanh(sideNumber); - else + } else { number = Math.tanh(number); + } return this; } public Builder exp() { - if (inParenthesis) + if (inParenthesis) { sideNumber = Math.exp(sideNumber); - else + } else { number = Math.exp(number); + } return this; } public Builder toRadians() { - if (inParenthesis) + if (inParenthesis) { sideNumber = Math.toRadians(sideNumber); - else + } else { number = Math.toRadians(number); + } return this; } From fe4cccaf23a412fbf957cb70ef61c634c6606b22 Mon Sep 17 00:00:00 2001 From: Sadiul Hakim Date: Thu, 13 Mar 2025 15:40:39 +0600 Subject: [PATCH 31/32] Added Parenthesis in MathBuilder --- .../com/thealgorithms/maths/MathBuilder.java | 84 ++++++++++++++----- 1 file changed, 63 insertions(+), 21 deletions(-) diff --git a/src/main/java/com/thealgorithms/maths/MathBuilder.java b/src/main/java/com/thealgorithms/maths/MathBuilder.java index 159bd0a0d708..1cf3d8b7fc9a 100644 --- a/src/main/java/com/thealgorithms/maths/MathBuilder.java +++ b/src/main/java/com/thealgorithms/maths/MathBuilder.java @@ -25,11 +25,21 @@ public double get() { // Return result in long public long toLong() { try { - if (Double.isNaN(result)) throw new IllegalArgumentException("Cannot convert NaN to long!"); - if (result == Double.POSITIVE_INFINITY) return Long.MAX_VALUE; - if (result == Double.NEGATIVE_INFINITY) return Long.MIN_VALUE; - if (result > Long.MAX_VALUE) return Long.MAX_VALUE; - if (result < Long.MIN_VALUE) return Long.MIN_VALUE; + if (Double.isNaN(result)) { + throw new IllegalArgumentException("Cannot convert NaN to long!"); + } + if (result == Double.POSITIVE_INFINITY) { + return Long.MAX_VALUE; + } + if (result == Double.NEGATIVE_INFINITY) { + return Long.MIN_VALUE; + } + if (result > Long.MAX_VALUE) { + return Long.MAX_VALUE; + } + if (result < Long.MIN_VALUE) { + return Long.MIN_VALUE; + } return Math.round(result); } catch (Exception ex) { return 0; @@ -61,7 +71,9 @@ public Builder add(double num) { // Takes a number and a condition, only does the operation if condition is true. public Builder addIf(double num, BiFunction condition) { - if (!condition.apply(number, num)) return this; + if (!condition.apply(number, num)) { + return this; + } if (inParenthesis) { sideNumber += num; } else { @@ -81,7 +93,9 @@ public Builder minus(double num) { // Takes a number and a condition, only does the operation if condition is true. public Builder minusIf(double num, BiFunction condition) { - if (!condition.apply(number, num)) return this; + if (!condition.apply(number, num)) { + return this; + } if (inParenthesis) { sideNumber -= num; } else { @@ -92,7 +106,9 @@ public Builder minusIf(double num, BiFunction condition // Generates a random number and sets to NUMBER public Builder rand(long seed) { - if (number != 0) throw new RuntimeException("Number must be zero for random assignment!"); + if (number != 0) { + throw new RuntimeException("Number must be zero for random assignment!"); + } Random random = new Random(); number = random.nextDouble(seed); return this; @@ -100,20 +116,26 @@ public Builder rand(long seed) { // Takes PI value and sets to NUMBER public Builder pi() { - if (number != 0) throw new RuntimeException("Number must be zero for PI assignment!"); + if (number != 0) { + throw new RuntimeException("Number must be zero for PI assignment!"); + } number = Math.PI; return this; } // Takes E value and sets to NUMBER public Builder e() { - if (number != 0) throw new RuntimeException("Number must be zero for E assignment!"); + if (number != 0) { + throw new RuntimeException("Number must be zero for E assignment!"); + } number = Math.E; return this; } public Builder randomInRange(double min, double max) { - if (number != 0) throw new RuntimeException("Number must be zero for random assignment!"); + if (number != 0) { + throw new RuntimeException("Number must be zero for random assignment!"); + } Random random = new Random(); number = min + (max - min) * random.nextDouble(); return this; @@ -157,7 +179,9 @@ public Builder multiply(double num) { // Takes a number and a condition, only does the operation if condition is true. public Builder multiplyIf(double num, BiFunction condition) { - if (!condition.apply(number, num)) return this; + if (!condition.apply(number, num)) { + return this; + } if (inParenthesis) { sideNumber *= num; } else { @@ -167,7 +191,9 @@ public Builder multiplyIf(double num, BiFunction condit } public Builder divide(double num) { - if (num == 0) return this; + if (num == 0) { + return this; + } if (inParenthesis) { sideNumber /= num; } else { @@ -178,8 +204,12 @@ public Builder divide(double num) { // Takes a number and a condition, only does the operation if condition is true. public Builder divideIf(double num, BiFunction condition) { - if (num == 0) return this; - if (!condition.apply(number, num)) return this; + if (num == 0) { + return this; + } + if (!condition.apply(number, num)) { + return this; + } if (inParenthesis) { sideNumber /= num; } else { @@ -199,7 +229,9 @@ public Builder mod(double num) { // Takes a number and a condition, only does the operation if condition is true. public Builder modIf(double num, BiFunction condition) { - if (!condition.apply(number, num)) return this; + if (!condition.apply(number, num)) { + return this; + } if (inParenthesis) { sideNumber %= num; } else { @@ -378,23 +410,33 @@ public Builder recall(boolean cleanMemory) { // Recalls the NUMBER on condition public Builder recallIf(Function condition, boolean cleanMemory) { - if (!condition.apply(number)) return this; + if (!condition.apply(number)) { + return this; + } number = memory; - if (cleanMemory) memory = 0; + if (cleanMemory) { + memory = 0; + } return this; } // Replaces NUMBER with given number public Builder set(double num) { - if (number != 0) throw new RuntimeException("Number must be zero to set!"); + if (number != 0) { + throw new RuntimeException("Number must be zero to set!"); + } number = num; return this; } // Replaces NUMBER with given number on condition public Builder setIf(double num, BiFunction condition) { - if (number != 0) throw new RuntimeException("Number must be zero to set!"); - if (condition.apply(number, num)) number = num; + if (number != 0) { + throw new RuntimeException("Number must be zero to set!"); + } + if (condition.apply(number, num)) { + number = num; + } return this; } From b4a85e6704f650fd38a49df5d00c189de1561d13 Mon Sep 17 00:00:00 2001 From: Hakim's Garage <92100853+sadiul-hakim@users.noreply.github.com> Date: Thu, 13 Mar 2025 15:44:55 +0600 Subject: [PATCH 32/32] Update MathBuilderTest.java --- src/test/java/com/thealgorithms/maths/MathBuilderTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/java/com/thealgorithms/maths/MathBuilderTest.java b/src/test/java/com/thealgorithms/maths/MathBuilderTest.java index 1550a90cf8d5..dc381bfca5d3 100644 --- a/src/test/java/com/thealgorithms/maths/MathBuilderTest.java +++ b/src/test/java/com/thealgorithms/maths/MathBuilderTest.java @@ -47,6 +47,6 @@ void parenthesis() { void areaOfCircle() { // Radius is 4 double area = new MathBuilder.Builder().pi().openParenthesis(4).multiply(4).closeParenthesisAndMultiply().build().get(); - assertEquals((Math.PI * (4 * 4)), area); + assertEquals(Math.PI * 4 * 4, area); } }