mirror of
https://github.com/TheAlgorithms/Java.git
synced 2025-07-08 02:04:31 +08:00
refactor: DecimalToOctal
(#5332)
This commit is contained in:
@ -1,38 +1,38 @@
|
|||||||
package com.thealgorithms.conversions;
|
package com.thealgorithms.conversions;
|
||||||
|
|
||||||
import java.util.Scanner;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This class converts Decimal numbers to Octal Numbers
|
* This class converts Decimal numbers to Octal Numbers
|
||||||
*/
|
*/
|
||||||
public final class DecimalToOctal {
|
public final class DecimalToOctal {
|
||||||
|
private static final int OCTAL_BASE = 8;
|
||||||
|
private static final int INITIAL_OCTAL_VALUE = 0;
|
||||||
|
private static final int INITIAL_PLACE_VALUE = 1;
|
||||||
|
|
||||||
private DecimalToOctal() {
|
private DecimalToOctal() {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Main Method
|
* Converts a decimal number to its octal equivalent.
|
||||||
*
|
*
|
||||||
* @param args Command line Arguments
|
* @param decimal The decimal number to convert.
|
||||||
|
* @return The octal equivalent as an integer.
|
||||||
|
* @throws IllegalArgumentException if the decimal number is negative.
|
||||||
*/
|
*/
|
||||||
|
public static int convertToOctal(int decimal) {
|
||||||
// enter in a decimal value to get Octal output
|
if (decimal < 0) {
|
||||||
public static void main(String[] args) {
|
throw new IllegalArgumentException("Decimal number cannot be negative.");
|
||||||
Scanner sc = new Scanner(System.in);
|
|
||||||
int n;
|
|
||||||
int k;
|
|
||||||
int d;
|
|
||||||
int s = 0;
|
|
||||||
int c = 0;
|
|
||||||
System.out.print("Decimal number: ");
|
|
||||||
n = sc.nextInt();
|
|
||||||
k = n;
|
|
||||||
while (k != 0) {
|
|
||||||
d = k % 8;
|
|
||||||
s += d * (int) Math.pow(10, c++);
|
|
||||||
k /= 8;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
System.out.println("Octal equivalent:" + s);
|
int octal = INITIAL_OCTAL_VALUE;
|
||||||
sc.close();
|
int placeValue = INITIAL_PLACE_VALUE;
|
||||||
|
|
||||||
|
while (decimal != 0) {
|
||||||
|
int remainder = decimal % OCTAL_BASE;
|
||||||
|
octal += remainder * placeValue;
|
||||||
|
decimal /= OCTAL_BASE;
|
||||||
|
placeValue *= 10;
|
||||||
|
}
|
||||||
|
|
||||||
|
return octal;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,21 @@
|
|||||||
|
package com.thealgorithms.conversions;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.junit.jupiter.params.ParameterizedTest;
|
||||||
|
import org.junit.jupiter.params.provider.CsvSource;
|
||||||
|
|
||||||
|
class DecimalToOctalTest {
|
||||||
|
@ParameterizedTest
|
||||||
|
@CsvSource({"0, 0", "7, 7", "8, 10", "10, 12", "64, 100", "83, 123", "7026, 15562"})
|
||||||
|
void testConvertToOctal(int decimal, int expectedOctal) {
|
||||||
|
assertEquals(expectedOctal, DecimalToOctal.convertToOctal(decimal));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testConvertToOctalNegativeNumber() {
|
||||||
|
assertThrows(IllegalArgumentException.class, () -> DecimalToOctal.convertToOctal(-10));
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user