Skip to content

Commit a9f5b82

Browse files
authored
refactor: OctalToDecimal (#5344)
1 parent 2905ccb commit a9f5b82

File tree

2 files changed

+39
-37
lines changed

2 files changed

+39
-37
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,42 @@
11
package com.thealgorithms.conversions;
22

3-
import java.util.Scanner;
4-
53
/**
6-
* Converts any Octal Number to a Decimal Number
4+
* Class for converting an octal number to a decimal number. Octal numbers are based on 8, using digits from 0 to 7.
75
*
8-
* @author Zachary Jones
96
*/
107
public final class OctalToDecimal {
8+
private static final int OCTAL_BASE = 8;
9+
1110
private OctalToDecimal() {
1211
}
1312

1413
/**
15-
* Main method
14+
* Converts a given octal number (as a string) to its decimal representation.
15+
* If the input is not a valid octal number (i.e., contains characters other than 0-7),
16+
* the method throws an IllegalArgumentException.
1617
*
17-
* @param args Command line arguments
18+
* @param inputOctal The octal number as a string
19+
* @return The decimal equivalent of the octal number
20+
* @throws IllegalArgumentException if the input is not a valid octal number
1821
*/
19-
public static void main(String[] args) {
20-
Scanner sc = new Scanner(System.in);
21-
System.out.print("Octal Input: ");
22-
String inputOctal = sc.nextLine();
23-
int result = convertOctalToDecimal(inputOctal);
24-
if (result != -1) {
25-
System.out.println("Result convertOctalToDecimal : " + result);
22+
public static int convertOctalToDecimal(String inputOctal) {
23+
if (inputOctal == null || inputOctal.isEmpty()) {
24+
throw new IllegalArgumentException("Input cannot be null or empty");
2625
}
27-
sc.close();
28-
}
2926

30-
/**
31-
* This method converts an octal number to a decimal number.
32-
*
33-
* @param inputOctal The octal number
34-
* @return The decimal number
35-
*/
36-
public static int convertOctalToDecimal(String inputOctal) {
37-
try {
38-
// Actual conversion of Octal to Decimal:
39-
return Integer.parseInt(inputOctal, 8);
40-
} catch (NumberFormatException ne) {
41-
// Printing a warning message if the input is not a valid octal
42-
// number:
43-
System.out.println("Invalid Input, Expecting octal number 0-7");
44-
return -1;
27+
int decimalValue = 0;
28+
29+
for (int i = 0; i < inputOctal.length(); i++) {
30+
char currentChar = inputOctal.charAt(i);
31+
32+
if (currentChar < '0' || currentChar > '7') {
33+
throw new IllegalArgumentException("Incorrect input: Expecting an octal number (digits 0-7)");
34+
}
35+
36+
int currentDigit = currentChar - '0';
37+
decimalValue = decimalValue * OCTAL_BASE + currentDigit;
4538
}
39+
40+
return decimalValue;
4641
}
4742
}
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,21 @@
11
package com.thealgorithms.conversions;
22

3-
import static org.junit.jupiter.api.Assertions.assertEquals;
4-
5-
import org.junit.jupiter.api.Test;
3+
import org.junit.jupiter.api.Assertions;
4+
import org.junit.jupiter.params.ParameterizedTest;
5+
import org.junit.jupiter.params.provider.CsvSource;
66

77
public class OctalToDecimalTest {
88

9-
@Test
10-
public void testOctalToDecimal() {
11-
assertEquals(1465, OctalToDecimal.convertOctalToDecimal("2671"));
12-
assertEquals(189, OctalToDecimal.convertOctalToDecimal("275"));
9+
@ParameterizedTest
10+
@CsvSource({"10, 8", "7, 7", "77, 63", "123, 83", "0, 0", "777, 511", "2671, 1465", "275, 189"})
11+
void testConvertOctalToDecimal(String inputOctal, int expectedDecimal) {
12+
Assertions.assertEquals(expectedDecimal, OctalToDecimal.convertOctalToDecimal(inputOctal));
13+
}
14+
15+
@ParameterizedTest
16+
@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)"})
17+
void testIncorrectInput(String inputOctal, String expectedMessage) {
18+
IllegalArgumentException exception = Assertions.assertThrows(IllegalArgumentException.class, () -> OctalToDecimal.convertOctalToDecimal(inputOctal));
19+
Assertions.assertEquals(expectedMessage, exception.getMessage());
1320
}
1421
}

0 commit comments

Comments
 (0)