Skip to content

Commit 33fd79a

Browse files
authored
refactor: OctalToHexadecimal (#5345)
1 parent a9f5b82 commit 33fd79a

File tree

2 files changed

+52
-47
lines changed

2 files changed

+52
-47
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,65 +1,61 @@
11
package com.thealgorithms.conversions;
22

3-
import java.util.Scanner;
4-
53
/**
6-
* Converts any Octal Number to HexaDecimal
4+
* Class for converting an Octal number to its Hexadecimal equivalent.
75
*
86
* @author Tanmay Joshi
97
*/
108
public final class OctalToHexadecimal {
9+
private static final int OCTAL_BASE = 8;
10+
private static final int HEX_BASE = 16;
11+
private static final String HEX_DIGITS = "0123456789ABCDEF";
12+
1113
private OctalToHexadecimal() {
1214
}
1315

1416
/**
15-
* This method converts a Octal number to a decimal number
17+
* Converts an Octal number (as a string) to its Decimal equivalent.
1618
*
17-
* @param s The Octal Number
18-
* @return The Decimal number
19+
* @param octalNumber The Octal number as a string
20+
* @return The Decimal equivalent of the Octal number
21+
* @throws IllegalArgumentException if the input contains invalid octal digits
1922
*/
20-
public static int octToDec(String s) {
21-
int i = 0;
22-
for (int j = 0; j < s.length(); j++) {
23-
char num = s.charAt(j);
24-
num -= '0';
25-
i *= 8;
26-
i += num;
23+
public static int octalToDecimal(String octalNumber) {
24+
if (octalNumber == null || octalNumber.isEmpty()) {
25+
throw new IllegalArgumentException("Input cannot be null or empty");
2726
}
28-
return i;
27+
28+
int decimalValue = 0;
29+
for (int i = 0; i < octalNumber.length(); i++) {
30+
char currentChar = octalNumber.charAt(i);
31+
if (currentChar < '0' || currentChar > '7') {
32+
throw new IllegalArgumentException("Incorrect octal digit: " + currentChar);
33+
}
34+
int currentDigit = currentChar - '0';
35+
decimalValue = decimalValue * OCTAL_BASE + currentDigit;
36+
}
37+
38+
return decimalValue;
2939
}
3040

3141
/**
32-
* This method converts a Decimal number to a Hexadecimal number
42+
* Converts a Decimal number to its Hexadecimal equivalent.
3343
*
34-
* @param d The Decimal Number
35-
* @return The Hexadecimal number
44+
* @param decimalNumber The Decimal number
45+
* @return The Hexadecimal equivalent of the Decimal number
3646
*/
37-
public static String decimalToHex(int d) {
38-
String digits = "0123456789ABCDEF";
39-
if (d <= 0) {
47+
public static String decimalToHexadecimal(int decimalNumber) {
48+
if (decimalNumber == 0) {
4049
return "0";
4150
}
42-
String hex = "";
43-
while (d > 0) {
44-
int digit = d % 16;
45-
hex = digits.charAt(digit) + hex;
46-
d = d / 16;
47-
}
48-
return hex;
49-
}
5051

51-
public static void main(String[] args) {
52-
Scanner input = new Scanner(System.in);
53-
System.out.print("Enter the Octal number: ");
54-
// Take octal number as input from user in a string
55-
String oct = input.next();
56-
57-
// Pass the octal number to function and get converted decimal form
58-
int decimal = octToDec(oct);
52+
StringBuilder hexValue = new StringBuilder();
53+
while (decimalNumber > 0) {
54+
int digit = decimalNumber % HEX_BASE;
55+
hexValue.insert(0, HEX_DIGITS.charAt(digit));
56+
decimalNumber /= HEX_BASE;
57+
}
5958

60-
// Pass the decimal number to function and get converted Hex form of the number
61-
String hex = decimalToHex(decimal);
62-
System.out.println("The Hexadecimal equivalant is: " + hex);
63-
input.close();
59+
return hexValue.toString();
6460
}
6561
}
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,23 @@
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 OctalToHexadecimalTest {
88

9-
@Test
10-
public void testOctalToHexadecimal() {
11-
assertEquals("1EA", OctalToHexadecimal.decimalToHex(OctalToHexadecimal.octToDec("752")));
12-
assertEquals("15E", OctalToHexadecimal.decimalToHex(OctalToHexadecimal.octToDec("536")));
9+
@ParameterizedTest
10+
@CsvSource({"0, 0", "7, 7", "10, 8", "17, F", "20, 10", "777, 1FF", "1234, 29C", "752, 1EA", "536, 15E"})
11+
void testCorrectInputs(String inputOctal, String expectedHex) {
12+
int decimal = OctalToHexadecimal.octalToDecimal(inputOctal);
13+
String hex = OctalToHexadecimal.decimalToHexadecimal(decimal);
14+
Assertions.assertEquals(expectedHex, hex);
15+
}
16+
17+
@ParameterizedTest
18+
@CsvSource({"'', Input cannot be null or empty", "'8', Incorrect octal digit: 8", "'19', Incorrect octal digit: 9"})
19+
void testIncorrectInputs(String inputOctal, String expectedMessage) {
20+
IllegalArgumentException exception = Assertions.assertThrows(IllegalArgumentException.class, () -> OctalToHexadecimal.octalToDecimal(inputOctal));
21+
Assertions.assertEquals(expectedMessage, exception.getMessage());
1322
}
1423
}

0 commit comments

Comments
 (0)