refactor: OctalToDecimal (#5344)

This commit is contained in:
Alex Klymenko
2024-08-18 20:58:57 +02:00
committed by GitHub
parent 2905ccbb20
commit a9f5b82708
2 changed files with 40 additions and 38 deletions

View File

@ -1,47 +1,42 @@
package com.thealgorithms.conversions; 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 { public final class OctalToDecimal {
private static final int OCTAL_BASE = 8;
private OctalToDecimal() { 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
public static void main(String[] args) { * @throws IllegalArgumentException if the input is not a valid octal number
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);
}
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) { public static int convertOctalToDecimal(String inputOctal) {
try { if (inputOctal == null || inputOctal.isEmpty()) {
// Actual conversion of Octal to Decimal: throw new IllegalArgumentException("Input cannot be null or empty");
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;
} }
} }

View File

@ -1,14 +1,21 @@
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 OctalToDecimalTest { public class OctalToDecimalTest {
@Test @ParameterizedTest
public void testOctalToDecimal() { @CsvSource({"10, 8", "7, 7", "77, 63", "123, 83", "0, 0", "777, 511", "2671, 1465", "275, 189"})
assertEquals(1465, OctalToDecimal.convertOctalToDecimal("2671")); void testConvertOctalToDecimal(String inputOctal, int expectedDecimal) {
assertEquals(189, OctalToDecimal.convertOctalToDecimal("275")); 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());
} }
} }