Repository: TheAlgorithms/Java
Pull request #5344 (2024-08-18 14:00:10)

refactor: `OctalToDecimal`
Improving readability.
Improving tests.

<!--
Thank you for your contribution!
In order to reduce the number of notifications sent to the maintainers, please:
- create your PR as draft, cf. https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/proposing-changes-to-your-work-with-pull-requests/about-pull-requests#draft-pull-requests,
- make sure that all of the CI checks pass,
- mark your PR as ready for review, cf. https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/proposing-changes-to-your-work-with-pull-requests/changing-the-stage-of-a-pull-request#marking-a-pull-request-as-ready-for-review
-->

<!-- 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.
- [x] All new code is formatted with `clang-format -i --style=file path/to/your/file.java`

codecov-commenter: ## [Codecov](https://app.codecov.io/gh/TheAlgorithms/Java/pull/5344?dropdown=coverage&src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms) Report
Attention: Patch coverage is `80.00000%` with `2 lines` in your changes missing coverage. Please review.
> Project coverage is 43.33%. Comparing base [(`404ad72`)](https://app.codecov.io/gh/TheAlgorithms/Java/commit/404ad7272f18c8e48b5c65c69e117b449ea553c6?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms) to head [(`7692525`)](https://app.codecov.io/gh/TheAlgorithms/Java/commit/7692525592907d507cac11cdcad9abf4192989b3?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms).

| [Files](https://app.codecov.io/gh/TheAlgorithms/Java/pull/5344?dropdown=coverage&src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms) | Patch % | Lines |
|---|---|---|
| [.../com/thealgorithms/conversions/OctalToDecimal.java](https://app.codecov.io/gh/TheAlgorithms/Java/pull/5344?src=pr&el=tree&filepath=src%2Fmain%2Fjava%2Fcom%2Fthealgorithms%2Fconversions%2FOctalToDecimal.java&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms#diff-c3JjL21haW4vamF2YS9jb20vdGhlYWxnb3JpdGhtcy9jb252ZXJzaW9ucy9PY3RhbFRvRGVjaW1hbC5qYXZh) | 80.00% | [0 Missing and 2 partials :warning: ](https://app.codecov.io/gh/TheAlgorithms/Java/pull/5344?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms) |

<details><summary>Additional details and impacted files</summary>


```diff
@@             Coverage Diff              @@
##             master    #5344      +/-   ##
============================================
+ Coverage     43.27%   43.33%   +0.05%     
- Complexity     2681     2685       +4     
============================================
  Files           522      522              
  Lines         15456    15454       -2     
  Branches       2952     2953       +1     
============================================
+ Hits           6689     6697       +8     
+ Misses         8481     8470      -11     
- Partials        286      287       +1     
```

</details>

[:umbrella: View full report in Codecov by Sentry](https://app.codecov.io/gh/TheAlgorithms/Java/pull/5344?dropdown=coverage&src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms).   
:loudspeaker: Have feedback on the report? [Share it here](https://about.codecov.io/codecov-pr-comment-feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms).

alxkm: PMD warning can't be fixed.

`Warning: PMD Failure: com.thealgorithms.conversions.OctalToDecimalTest:21 Rule:UnnecessaryFullyQualifiedName Priority:4 Unnecessary qualifier 'Assertions': 'assertEquals' is already in scope.`

Tried different approaches to fix it, but the warning persists even those files does not changed.

Patch:
diff --git a/src/main/java/com/thealgorithms/conversions/OctalToDecimal.java b/src/main/java/com/thealgorithms/conversions/OctalToDecimal.java
index 187f0ed1..d91ce6eb 100644
--- a/src/main/java/com/thealgorithms/conversions/OctalToDecimal.java
+++ b/src/main/java/com/thealgorithms/conversions/OctalToDecimal.java
@@ -1,47 +1,42 @@
 package com.thealgorithms.conversions;
 
-import java.util.Scanner;
-
 /**
- * Converts any Octal Number to a Decimal Number
+ * Class for converting an octal number to a decimal number. Octal numbers are based on 8, using digits from 0 to 7.
  *
- * @author Zachary Jones
  */
 public final class OctalToDecimal {
+    private static final int OCTAL_BASE = 8;
+
     private OctalToDecimal() {
     }
 
     /**
-     * Main method
+     * Converts a given octal number (as a string) to its decimal representation.
+     * If the input is not a valid octal number (i.e., contains characters other than 0-7),
+     * the method throws an IllegalArgumentException.
      *
-     * @param args Command line arguments
+     * @param inputOctal The octal number as a string
+     * @return The decimal equivalent of the octal number
+     * @throws IllegalArgumentException if the input is not a valid octal number
      */
-    public static void main(String[] args) {
-        Scanner sc = new Scanner(System.in);
-        System.out.print("Octal Input: ");
-        String inputOctal = sc.nextLine();
-        int result = convertOctalToDecimal(inputOctal);
-        if (result != -1) {
-            System.out.println("Result convertOctalToDecimal : " + result);
+    public static int convertOctalToDecimal(String inputOctal) {
+        if (inputOctal == null || inputOctal.isEmpty()) {
+            throw new IllegalArgumentException("Input cannot be null or empty");
         }
-        sc.close();
-    }
 
-    /**
-     * This method converts an octal number to a decimal number.
-     *
-     * @param inputOctal The octal number
-     * @return The decimal number
-     */
-    public static int convertOctalToDecimal(String inputOctal) {
-        try {
-            // Actual conversion of Octal to Decimal:
-            return Integer.parseInt(inputOctal, 8);
-        } catch (NumberFormatException ne) {
-            // Printing a warning message if the input is not a valid octal
-            // number:
-            System.out.println("Invalid Input, Expecting octal number 0-7");
-            return -1;
+        int decimalValue = 0;
+
+        for (int i = 0; i < inputOctal.length(); i++) {
+            char currentChar = inputOctal.charAt(i);
+
+            if (currentChar < '0' || currentChar > '7') {
+                throw new IllegalArgumentException("Incorrect input: Expecting an octal number (digits 0-7)");
+            }
+
+            int currentDigit = currentChar - '0';
+            decimalValue = decimalValue * OCTAL_BASE + currentDigit;
         }
+
+        return decimalValue;
     }
 }


Test patch:
diff --git a/src/test/java/com/thealgorithms/conversions/OctalToDecimalTest.java b/src/test/java/com/thealgorithms/conversions/OctalToDecimalTest.java
index 6e17ea14..20f0fa40 100644
--- a/src/test/java/com/thealgorithms/conversions/OctalToDecimalTest.java
+++ b/src/test/java/com/thealgorithms/conversions/OctalToDecimalTest.java
@@ -1,14 +1,21 @@
 package com.thealgorithms.conversions;
 
-import static org.junit.jupiter.api.Assertions.assertEquals;
-
-import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.CsvSource;
 
 public class OctalToDecimalTest {
 
-    @Test
-    public void testOctalToDecimal() {
-        assertEquals(1465, OctalToDecimal.convertOctalToDecimal("2671"));
-        assertEquals(189, OctalToDecimal.convertOctalToDecimal("275"));
+    @ParameterizedTest
+    @CsvSource({"10, 8", "7, 7", "77, 63", "123, 83", "0, 0", "777, 511", "2671, 1465", "275, 189"})
+    void testConvertOctalToDecimal(String inputOctal, int expectedDecimal) {
+        Assertions.assertEquals(expectedDecimal, OctalToDecimal.convertOctalToDecimal(inputOctal));
+    }
+
+    @ParameterizedTest
+    @CsvSource({"'', Input cannot be null or empty", "'8', Incorrect input: Expecting an octal number (digits 0-7)", "'19', Incorrect input: Expecting an octal number (digits 0-7)"})
+    void testIncorrectInput(String inputOctal, String expectedMessage) {
+        IllegalArgumentException exception = Assertions.assertThrows(IllegalArgumentException.class, () -> OctalToDecimal.convertOctalToDecimal(inputOctal));
+        Assertions.assertEquals(expectedMessage, exception.getMessage());
     }
 }