Skip to content

refactor: OctalToDecimal #5344

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Aug 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 25 additions & 30 deletions src/main/java/com/thealgorithms/conversions/OctalToDecimal.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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());
}
}