refactor: OctalToHexadecimal (#5345)

This commit is contained in:
Alex Klymenko 2024-08-18 21:03:28 +02:00 committed by GitHub
parent a9f5b82708
commit 33fd79ad55
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 52 additions and 47 deletions

View File

@ -1,65 +1,61 @@
package com.thealgorithms.conversions; package com.thealgorithms.conversions;
import java.util.Scanner;
/** /**
* Converts any Octal Number to HexaDecimal * Class for converting an Octal number to its Hexadecimal equivalent.
* *
* @author Tanmay Joshi * @author Tanmay Joshi
*/ */
public final class OctalToHexadecimal { public final class OctalToHexadecimal {
private static final int OCTAL_BASE = 8;
private static final int HEX_BASE = 16;
private static final String HEX_DIGITS = "0123456789ABCDEF";
private OctalToHexadecimal() { private OctalToHexadecimal() {
} }
/** /**
* This method converts a Octal number to a decimal number * Converts an Octal number (as a string) to its Decimal equivalent.
* *
* @param s The Octal Number * @param octalNumber The Octal number as a string
* @return The Decimal number * @return The Decimal equivalent of the Octal number
* @throws IllegalArgumentException if the input contains invalid octal digits
*/ */
public static int octToDec(String s) { public static int octalToDecimal(String octalNumber) {
int i = 0; if (octalNumber == null || octalNumber.isEmpty()) {
for (int j = 0; j < s.length(); j++) { throw new IllegalArgumentException("Input cannot be null or empty");
char num = s.charAt(j);
num -= '0';
i *= 8;
i += num;
} }
return i;
int decimalValue = 0;
for (int i = 0; i < octalNumber.length(); i++) {
char currentChar = octalNumber.charAt(i);
if (currentChar < '0' || currentChar > '7') {
throw new IllegalArgumentException("Incorrect octal digit: " + currentChar);
}
int currentDigit = currentChar - '0';
decimalValue = decimalValue * OCTAL_BASE + currentDigit;
}
return decimalValue;
} }
/** /**
* This method converts a Decimal number to a Hexadecimal number * Converts a Decimal number to its Hexadecimal equivalent.
* *
* @param d The Decimal Number * @param decimalNumber The Decimal number
* @return The Hexadecimal number * @return The Hexadecimal equivalent of the Decimal number
*/ */
public static String decimalToHex(int d) { public static String decimalToHexadecimal(int decimalNumber) {
String digits = "0123456789ABCDEF"; if (decimalNumber == 0) {
if (d <= 0) {
return "0"; return "0";
} }
String hex = "";
while (d > 0) { StringBuilder hexValue = new StringBuilder();
int digit = d % 16; while (decimalNumber > 0) {
hex = digits.charAt(digit) + hex; int digit = decimalNumber % HEX_BASE;
d = d / 16; hexValue.insert(0, HEX_DIGITS.charAt(digit));
decimalNumber /= HEX_BASE;
} }
return hex;
}
public static void main(String[] args) { return hexValue.toString();
Scanner input = new Scanner(System.in);
System.out.print("Enter the Octal number: ");
// Take octal number as input from user in a string
String oct = input.next();
// Pass the octal number to function and get converted decimal form
int decimal = octToDec(oct);
// Pass the decimal number to function and get converted Hex form of the number
String hex = decimalToHex(decimal);
System.out.println("The Hexadecimal equivalant is: " + hex);
input.close();
} }
} }

View File

@ -1,14 +1,23 @@
package com.thealgorithms.conversions; package com.thealgorithms.conversions;
import static org.junit.jupiter.api.Assertions.assertEquals; import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.api.Test; import org.junit.jupiter.params.provider.CsvSource;
public class OctalToHexadecimalTest { public class OctalToHexadecimalTest {
@Test @ParameterizedTest
public void testOctalToHexadecimal() { @CsvSource({"0, 0", "7, 7", "10, 8", "17, F", "20, 10", "777, 1FF", "1234, 29C", "752, 1EA", "536, 15E"})
assertEquals("1EA", OctalToHexadecimal.decimalToHex(OctalToHexadecimal.octToDec("752"))); void testCorrectInputs(String inputOctal, String expectedHex) {
assertEquals("15E", OctalToHexadecimal.decimalToHex(OctalToHexadecimal.octToDec("536"))); int decimal = OctalToHexadecimal.octalToDecimal(inputOctal);
String hex = OctalToHexadecimal.decimalToHexadecimal(decimal);
Assertions.assertEquals(expectedHex, hex);
}
@ParameterizedTest
@CsvSource({"'', Input cannot be null or empty", "'8', Incorrect octal digit: 8", "'19', Incorrect octal digit: 9"})
void testIncorrectInputs(String inputOctal, String expectedMessage) {
IllegalArgumentException exception = Assertions.assertThrows(IllegalArgumentException.class, () -> OctalToHexadecimal.octalToDecimal(inputOctal));
Assertions.assertEquals(expectedMessage, exception.getMessage());
} }
} }