mirror of
https://github.com/TheAlgorithms/Java.git
synced 2025-07-07 17:56:02 +08:00
refactor: BinaryToOctal
(#5338)
* refactor: BinaryToOctal * checkstyle: fix formatting * refactor: adding input correctness case, cover by tests. Renaming variable --------- Co-authored-by: alxkm <alx@alx.com> Co-authored-by: Bama Charan Chhandogi <b.c.chhandogi@gmail.com>
This commit is contained in:
@ -1,27 +1,11 @@
|
|||||||
package com.thealgorithms.conversions;
|
package com.thealgorithms.conversions;
|
||||||
|
|
||||||
import java.util.Scanner;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Converts any Binary number to an Octal Number
|
|
||||||
*
|
|
||||||
* @author Zachary Jones
|
|
||||||
*/
|
|
||||||
public final class BinaryToOctal {
|
public final class BinaryToOctal {
|
||||||
private BinaryToOctal() {
|
private static final int BITS_PER_OCTAL_DIGIT = 3;
|
||||||
}
|
private static final int BINARY_BASE = 2;
|
||||||
|
private static final int DECIMAL_BASE = 10;
|
||||||
|
|
||||||
/**
|
private BinaryToOctal() {
|
||||||
* Main method
|
|
||||||
*
|
|
||||||
* @param args Command line arguments
|
|
||||||
*/
|
|
||||||
public static void main(String[] args) {
|
|
||||||
Scanner sc = new Scanner(System.in);
|
|
||||||
System.out.println("Input the binary number: ");
|
|
||||||
int b = sc.nextInt();
|
|
||||||
System.out.println("Octal equivalent: " + convertBinaryToOctal(b));
|
|
||||||
sc.close();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -29,22 +13,33 @@ public final class BinaryToOctal {
|
|||||||
*
|
*
|
||||||
* @param binary The binary number
|
* @param binary The binary number
|
||||||
* @return The octal number
|
* @return The octal number
|
||||||
|
* @throws IllegalArgumentException if the input is not a valid binary number
|
||||||
*/
|
*/
|
||||||
public static String convertBinaryToOctal(int binary) {
|
public static String convertBinaryToOctal(int binary) {
|
||||||
String octal = "";
|
if (binary == 0) {
|
||||||
int currBit = 0;
|
return "0";
|
||||||
int j = 1;
|
|
||||||
while (binary != 0) {
|
|
||||||
int code3 = 0;
|
|
||||||
for (int i = 0; i < 3; i++) {
|
|
||||||
currBit = binary % 10;
|
|
||||||
binary = binary / 10;
|
|
||||||
code3 += currBit * j;
|
|
||||||
j *= 2;
|
|
||||||
}
|
|
||||||
octal = code3 + octal;
|
|
||||||
j = 1;
|
|
||||||
}
|
}
|
||||||
return octal;
|
|
||||||
|
if (!String.valueOf(binary).matches("[01]+")) {
|
||||||
|
throw new IllegalArgumentException("Input is not a valid binary number.");
|
||||||
|
}
|
||||||
|
|
||||||
|
StringBuilder octal = new StringBuilder();
|
||||||
|
int currentBit;
|
||||||
|
int bitValueMultiplier = 1;
|
||||||
|
|
||||||
|
while (binary != 0) {
|
||||||
|
int octalDigit = 0;
|
||||||
|
for (int i = 0; i < BITS_PER_OCTAL_DIGIT && binary != 0; i++) {
|
||||||
|
currentBit = binary % DECIMAL_BASE;
|
||||||
|
binary /= DECIMAL_BASE;
|
||||||
|
octalDigit += currentBit * bitValueMultiplier;
|
||||||
|
bitValueMultiplier *= BINARY_BASE;
|
||||||
|
}
|
||||||
|
octal.insert(0, octalDigit);
|
||||||
|
bitValueMultiplier = 1; // Reset multiplier for the next group
|
||||||
|
}
|
||||||
|
|
||||||
|
return octal.toString();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,14 +1,24 @@
|
|||||||
package com.thealgorithms.conversions;
|
package com.thealgorithms.conversions;
|
||||||
|
|
||||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
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.api.Test;
|
||||||
|
import org.junit.jupiter.params.ParameterizedTest;
|
||||||
|
import org.junit.jupiter.params.provider.CsvSource;
|
||||||
|
|
||||||
public class BinaryToOctalTest {
|
public class BinaryToOctalTest {
|
||||||
|
|
||||||
|
@ParameterizedTest
|
||||||
|
@CsvSource({"0, 0", "1, 1", "10, 2", "111, 7", "1000, 10", "1111, 17", "110101, 65", "1010101, 125", "110110011, 663", "111111111, 777", "10010110, 226", "1011101, 135"})
|
||||||
|
void testConvertBinaryToOctal(int binary, String expectedOctal) {
|
||||||
|
assertEquals(expectedOctal, BinaryToOctal.convertBinaryToOctal(binary));
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testBinaryToOctal() {
|
void testIncorrectInput() {
|
||||||
assertEquals("226", BinaryToOctal.convertBinaryToOctal(10010110));
|
assertThrows(IllegalArgumentException.class, () -> BinaryToOctal.convertBinaryToOctal(1234));
|
||||||
assertEquals("135", BinaryToOctal.convertBinaryToOctal(1011101));
|
assertThrows(IllegalArgumentException.class, () -> BinaryToOctal.convertBinaryToOctal(102));
|
||||||
|
assertThrows(IllegalArgumentException.class, () -> BinaryToOctal.convertBinaryToOctal(-1010));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user