Skip to content

refactor: BinaryToOctal #5338

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 5 commits into from
Aug 17, 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
59 changes: 27 additions & 32 deletions src/main/java/com/thealgorithms/conversions/BinaryToOctal.java
Original file line number Diff line number Diff line change
@@ -1,50 +1,45 @@
package com.thealgorithms.conversions;

import java.util.Scanner;

/**
* Converts any Binary number to an Octal Number
*
* @author Zachary Jones
*/
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;

/**
* 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();
private BinaryToOctal() {
}

/**
* This method converts a binary number to an octal number.
*
* @param binary The binary number
* @return The octal number
* @throws IllegalArgumentException if the input is not a valid binary number
*/
public static String convertBinaryToOctal(int binary) {
String octal = "";
int currBit = 0;
int j = 1;
if (binary == 0) {
return "0";
}

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 code3 = 0;
for (int i = 0; i < 3; i++) {
currBit = binary % 10;
binary = binary / 10;
code3 += currBit * j;
j *= 2;
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 = code3 + octal;
j = 1;
octal.insert(0, octalDigit);
bitValueMultiplier = 1; // Reset multiplier for the next group
}
return octal;

return octal.toString();
}
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,24 @@
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;

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
public void testBinaryToOctal() {
assertEquals("226", BinaryToOctal.convertBinaryToOctal(10010110));
assertEquals("135", BinaryToOctal.convertBinaryToOctal(1011101));
void testIncorrectInput() {
assertThrows(IllegalArgumentException.class, () -> BinaryToOctal.convertBinaryToOctal(1234));
assertThrows(IllegalArgumentException.class, () -> BinaryToOctal.convertBinaryToOctal(102));
assertThrows(IllegalArgumentException.class, () -> BinaryToOctal.convertBinaryToOctal(-1010));
}
}