Skip to content

Commit 9d89e94

Browse files
committed
Refactor and optimize repository
1 parent 4d667e1 commit 9d89e94

31 files changed

+175
-220
lines changed

src/main/java/com/thealgorithms/backtracking/ParenthesesGenerator.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.thealgorithms.backtracking;
22

3+
import static com.thealgorithms.maths.utils.MathsUtil.checkInputIsPositive;
4+
35
import java.util.ArrayList;
46
import java.util.List;
57

@@ -17,10 +19,9 @@ private ParenthesesGenerator() {
1719
* @return A list of strings representing valid combinations of parentheses.
1820
* @throws IllegalArgumentException if n is less than 0.
1921
*/
20-
public static List<String> generateParentheses(final int n) {
21-
if (n < 0) {
22-
throw new IllegalArgumentException("The number of pairs of parentheses cannot be negative");
23-
}
22+
public static List<String> generateParentheses(final int n) {
23+
checkInputIsPositive(n, "The number of pairs of parentheses cannot be negative.");
24+
2425
List<String> result = new ArrayList<>();
2526
generateParenthesesHelper(result, "", 0, 0, n);
2627
return result;

src/main/java/com/thealgorithms/bitmanipulation/HighestSetBit.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.thealgorithms.bitmanipulation;
22

3+
import static com.thealgorithms.maths.utils.MathsUtil.checkInputIsPositive;
4+
35
import java.util.Optional;
46

57
/**
@@ -34,10 +36,8 @@ private HighestSetBit() {
3436
* Returns {@link Optional#empty()} if the number is 0.
3537
* @throws IllegalArgumentException if the input number is negative.
3638
*/
37-
public static Optional<Integer> findHighestSetBit(int num) {
38-
if (num < 0) {
39-
throw new IllegalArgumentException("Input cannot be negative");
40-
}
39+
public static Optional<Integer> findHighestSetBit(int num) {
40+
checkInputIsPositive(num, "Input cannot be negative.");
4141

4242
if (num == 0) {
4343
return Optional.empty();

src/main/java/com/thealgorithms/datastructures/buffers/CircularBuffer.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.thealgorithms.datastructures.buffers;
22

3+
import static com.thealgorithms.maths.utils.MathsUtil.checkInputIsPositive;
4+
35
import java.util.concurrent.atomic.AtomicInteger;
46

57
/**
@@ -23,9 +25,8 @@ public class CircularBuffer<Item> {
2325
* @throws IllegalArgumentException if the size is zero or negative.
2426
*/
2527
public CircularBuffer(int size) {
26-
if (size <= 0) {
27-
throw new IllegalArgumentException("Buffer size must be positive");
28-
}
28+
checkInputIsPositive(size, "Buffer size must be positive.");
29+
2930
// noinspection unchecked
3031
this.buffer = (Item[]) new Object[size];
3132
this.putPointer = new CircularPointer(0, size);

src/main/java/com/thealgorithms/datastructures/graphs/BoruvkaAlgorithm.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.thealgorithms.datastructures.graphs;
22

3+
import static com.thealgorithms.maths.utils.MathsUtil.checkInputIsPositive;
4+
35
import java.util.ArrayList;
46
import java.util.List;
57

@@ -43,9 +45,8 @@ static class Graph {
4345
* @param edges list of edges
4446
*/
4547
Graph(final int vertex, final List<Edge> edges) {
46-
if (vertex < 0) {
47-
throw new IllegalArgumentException("Number of vertices must be positive");
48-
}
48+
checkInputIsPositive(vertex, "Number of vertices must be positive.");
49+
4950
if (edges == null || edges.isEmpty()) {
5051
throw new IllegalArgumentException("Edges list must not be null or empty");
5152
}

src/main/java/com/thealgorithms/maths/Area.java renamed to src/main/java/com/thealgorithms/geometry/Area.java

Lines changed: 17 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
package com.thealgorithms.maths;
1+
package com.thealgorithms.geometry;
2+
3+
import static com.thealgorithms.maths.utils.MathsUtil.checkInputIsPositive;
24

35
/**
46
* Find the area of various geometric shapes
@@ -89,9 +91,8 @@ public static double surfaceAreaCylinder(final double radius, final double heigh
8991
* @return area of given square
9092
*/
9193
public static double surfaceAreaSquare(final double sideLength) {
92-
if (sideLength <= 0) {
93-
throw new IllegalArgumentException("Must be a positive sideLength");
94-
}
94+
checkInputIsPositive(sideLength, "Must be a positive sideLength.");
95+
9596
return sideLength * sideLength;
9697
}
9798

@@ -103,12 +104,8 @@ public static double surfaceAreaSquare(final double sideLength) {
103104
* @return area of given triangle
104105
*/
105106
public static double surfaceAreaTriangle(final double base, final double height) {
106-
if (base <= 0) {
107-
throw new IllegalArgumentException(POSITIVE_BASE);
108-
}
109-
if (height <= 0) {
110-
throw new IllegalArgumentException(POSITIVE_HEIGHT);
111-
}
107+
checkInputIsPositive(base, POSITIVE_BASE);
108+
checkInputIsPositive(height, POSITIVE_HEIGHT);
112109
return base * height / 2;
113110
}
114111

@@ -120,12 +117,8 @@ public static double surfaceAreaTriangle(final double base, final double height)
120117
* @return area of given parallelogram
121118
*/
122119
public static double surfaceAreaParallelogram(final double base, final double height) {
123-
if (base <= 0) {
124-
throw new IllegalArgumentException(POSITIVE_BASE);
125-
}
126-
if (height <= 0) {
127-
throw new IllegalArgumentException(POSITIVE_HEIGHT);
128-
}
120+
checkInputIsPositive(base, POSITIVE_BASE);
121+
checkInputIsPositive(height, POSITIVE_HEIGHT);
129122
return base * height;
130123
}
131124

@@ -144,9 +137,7 @@ public static double surfaceAreaTrapezium(final double base1, final double base2
144137
if (base2 <= 0) {
145138
throw new IllegalArgumentException(POSITIVE_BASE + 2);
146139
}
147-
if (height <= 0) {
148-
throw new IllegalArgumentException(POSITIVE_HEIGHT);
149-
}
140+
checkInputIsPositive(height, POSITIVE_HEIGHT);
150141
return (base1 + base2) * height / 2;
151142
}
152143

@@ -157,9 +148,8 @@ public static double surfaceAreaTrapezium(final double base1, final double base2
157148
* @return area of given circle
158149
*/
159150
public static double surfaceAreaCircle(final double radius) {
160-
if (radius <= 0) {
161-
throw new IllegalArgumentException(POSITIVE_RADIUS);
162-
}
151+
checkInputIsPositive(radius, POSITIVE_RADIUS);
152+
163153
return Math.PI * radius * radius;
164154
}
165155

@@ -170,9 +160,8 @@ public static double surfaceAreaCircle(final double radius) {
170160
* @return surface area of given hemisphere
171161
*/
172162
public static double surfaceAreaHemisphere(final double radius) {
173-
if (radius <= 0) {
174-
throw new IllegalArgumentException(POSITIVE_RADIUS);
175-
}
163+
checkInputIsPositive(radius, POSITIVE_RADIUS);
164+
176165
return 3 * Math.PI * radius * radius;
177166
}
178167

@@ -184,12 +173,9 @@ public static double surfaceAreaHemisphere(final double radius) {
184173
* @return surface area of given cone.
185174
*/
186175
public static double surfaceAreaCone(final double radius, final double height) {
187-
if (radius <= 0) {
188-
throw new IllegalArgumentException(POSITIVE_RADIUS);
189-
}
190-
if (height <= 0) {
191-
throw new IllegalArgumentException(POSITIVE_HEIGHT);
192-
}
176+
checkInputIsPositive(radius, POSITIVE_RADIUS);
177+
checkInputIsPositive(height, POSITIVE_HEIGHT);
178+
193179
return Math.PI * radius * (radius + Math.pow(height * height + radius * radius, 0.5));
194180
}
195181
}

src/main/java/com/thealgorithms/maths/Volume.java renamed to src/main/java/com/thealgorithms/geometry/Volume.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.thealgorithms.maths;
1+
package com.thealgorithms.geometry;
22

33
/* Calculate the volume of various shapes.*/
44
public final class Volume {

src/main/java/com/thealgorithms/maths/Armstrong.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.thealgorithms.maths;
22

3+
import static com.thealgorithms.maths.utils.MathsUtil.checkInputIsPositive;
4+
35
/**
46
* This class checks whether a given number is an Armstrong number or not.
57
* An Armstrong number is a number that is equal to the sum of its own digits,
@@ -21,9 +23,8 @@ public class Armstrong {
2123
* @return {@code true} if the given number is an Armstrong number, {@code false} otherwise
2224
*/
2325
public boolean isArmstrong(int number) {
24-
if (number < 0) {
25-
return false; // Negative numbers cannot be Armstrong numbers
26-
}
26+
checkInputIsPositive(number, "Negative numbers cannot be Armstrong numbers.");
27+
2728
long sum = 0;
2829
int totalDigits = (int) Math.log10(number) + 1; // get the length of the number (number of digits)
2930
long originalNumber = number;

src/main/java/com/thealgorithms/maths/Combinations.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.thealgorithms.maths;
22

3+
import static com.thealgorithms.maths.utils.MathsUtil.checkInputIsPositive;
4+
35
/**
46
* @see <a href="https://en.wikipedia.org/wiki/Combination">Combination</a>
57
*/
@@ -14,9 +16,8 @@ private Combinations() {
1416
* @return factorial of given number
1517
*/
1618
public static long factorial(int n) {
17-
if (n < 0) {
18-
throw new IllegalArgumentException("number is negative");
19-
}
19+
checkInputIsPositive(n, "Input number cannot be negative.");
20+
2021
return n == 0 || n == 1 ? 1 : n * factorial(n - 1);
2122
}
2223

src/main/java/com/thealgorithms/maths/DudeneyNumber.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,18 @@
66
*/
77
package com.thealgorithms.maths;
88

9+
import static com.thealgorithms.maths.PerfectCube.getCubeRoot;
10+
import static com.thealgorithms.maths.utils.MathsUtil.checkInputIsPositive;
11+
912
public final class DudeneyNumber {
1013
private DudeneyNumber() {
1114
}
1215

1316
// returns True if the number is a Dudeney number and False if it is not a Dudeney number.
1417
public static boolean isDudeney(final int n) {
15-
if (n <= 0) {
16-
throw new IllegalArgumentException("Input must me positive.");
17-
}
18+
checkInputIsPositive(n, "Input number should be positive.");
1819
// Calculating Cube Root
19-
final int cubeRoot = (int) Math.round(Math.pow(n, 1.0 / 3.0));
20+
final int cubeRoot = getCubeRoot(n);
2021
// If the number is not a perfect cube the method returns false.
2122
if (cubeRoot * cubeRoot * cubeRoot != n) {
2223
return false;

src/main/java/com/thealgorithms/maths/EulersFunction.java

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,14 @@
11
package com.thealgorithms.maths;
22

3+
import static com.thealgorithms.maths.utils.MathsUtil.checkInputIsPositive;
4+
35
/**
46
* Utility class for computing
57
* <a href="https://en.wikipedia.org/wiki/Euler%27s_totient_function">Euler's totient function</a>.
68
*/
79
public final class EulersFunction {
810
private EulersFunction() {
9-
}
10-
11-
/**
12-
* Validates that the input is a positive integer.
13-
*
14-
* @param n the input number to validate
15-
* @throws IllegalArgumentException if {@code n} is non-positive
16-
*/
17-
private static void checkInput(int n) {
18-
if (n <= 0) {
19-
throw new IllegalArgumentException("n must be positive.");
20-
}
21-
}
11+
}
2212

2313
/**
2414
* Computes the value of Euler's totient function for a given input.
@@ -29,7 +19,7 @@ private static void checkInput(int n) {
2919
* @throws IllegalArgumentException if {@code n} is non-positive
3020
*/
3121
public static int getEuler(int n) {
32-
checkInput(n);
22+
checkInputIsPositive(n, "Input number should be positive.");
3323
int result = n;
3424
for (int i = 2; i * i <= n; i++) {
3525
if (n % i == 0) {

0 commit comments

Comments
 (0)