Repository: TheAlgorithms/Java
Pull request #4208 (2023-05-30 18:06:03)

refactor: `ReverseNumber`
This PR:
- refactors the class [`ReverseNumber`](https://github.com/TheAlgorithms/Java/blob/b6e78a45ac007570df4da7574b111a05000204d3/src/main/java/com/thealgorithms/maths/ReverseNumber.java#L7),
- adds tests for it. 

<!-- For completed items, change [ ] to [x] -->

- [x] I have read [CONTRIBUTING.md](https://github.com/TheAlgorithms/Java/blob/master/CONTRIBUTING.md).
- [x] This pull request is all my own work -- I have not plagiarized it.
- [x] All filenames are in PascalCase.
- [x] All functions and variable names follow Java naming conventions.
- [x] All new algorithms have a URL in their comments that points to Wikipedia or other similar explanations.



Patch:
diff --git a/src/main/java/com/thealgorithms/maths/ReverseNumber.java b/src/main/java/com/thealgorithms/maths/ReverseNumber.java
index a78c4de8..8c74bfdf 100644
--- a/src/main/java/com/thealgorithms/maths/ReverseNumber.java
+++ b/src/main/java/com/thealgorithms/maths/ReverseNumber.java
@@ -1,30 +1,31 @@
 package com.thealgorithms.maths;
 
-import java.lang.IllegalStateException;
-import java.util.NoSuchElementException;
-import java.util.Scanner;
+import java.lang.IllegalArgumentException;
 
-public class ReverseNumber {
-
-    public static void main(String[] args) {
-        int number;
-        int reverse = 0;
+/**
+ * @brief utility class reversing numbers
+ */
+final public class ReverseNumber {
+    private ReverseNumber() {
+    }
 
-        try (Scanner sc = new Scanner(System.in)) {
-            System.out.println("Enter a number:");
-            number = sc.nextInt();
-        } catch (NoSuchElementException | IllegalStateException e) {
-            System.out.println("ERROR: Invalid input");
-            return;
+    /**
+     * @brief reverses the input number
+     * @param number the input number
+     * @exception IllegalArgumentException number is negative
+     * @return the number created by reversing the order of digits of the input number
+     */
+    public static int reverseNumber(int number) {
+        if (number < 0) {
+            throw new IllegalArgumentException("number must be nonnegative.");
         }
 
-        while (number != 0) {
-            int remainder = number % 10;
-
-            reverse = reverse * 10 + remainder;
-            number = number / 10;
+        int result = 0;
+        while (number > 0) {
+            result *= 10;
+            result += number % 10;
+            number /= 10;
         }
-
-        System.out.println("The reverse of the given number is: " + reverse);
+        return result;
     }
 }


Test patch:
diff --git a/src/test/java/com/thealgorithms/maths/ReverseNumberTest.java b/src/test/java/com/thealgorithms/maths/ReverseNumberTest.java
new file mode 100644
index 00000000..a6c25df5
--- /dev/null
+++ b/src/test/java/com/thealgorithms/maths/ReverseNumberTest.java
@@ -0,0 +1,33 @@
+package com.thealgorithms.maths;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+
+import java.util.HashMap;
+
+import org.junit.jupiter.api.Test;
+
+public class ReverseNumberTest {
+
+    @Test
+    public void testReverseNumber() {
+        HashMap<Integer, Integer> testCases = new HashMap<>();
+        testCases.put(0, 0);
+        testCases.put(1, 1);
+        testCases.put(10, 1);
+        testCases.put(123, 321);
+        testCases.put(7890, 987);
+
+        for (final var tc : testCases.entrySet()) {
+            assertEquals(ReverseNumber.reverseNumber(tc.getKey()), tc.getValue());
+        }
+    }
+
+    @Test
+    public void testReverseNumberThrowsExceptionForNegativeInput() {
+        assertThrows(
+            IllegalArgumentException.class,
+            () -> ReverseNumber.reverseNumber(-1)
+        );
+    }
+}