Skip to content

Commit 1b6c651

Browse files
author
alxkm
committed
refactor: BinaryToOctal
1 parent 7c58b19 commit 1b6c651

File tree

2 files changed

+28
-32
lines changed

2 files changed

+28
-32
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,16 @@
11
package com.thealgorithms.conversions;
22

3-
import java.util.Scanner;
4-
53
/**
64
* Converts any Binary number to an Octal Number
75
*
86
* @author Zachary Jones
97
*/
108
public final class BinaryToOctal {
11-
private BinaryToOctal() {
12-
}
9+
private static final int BITS_IN_GROUP = 3;
10+
private static final int BINARY_BASE = 2;
11+
private static final int DECIMAL_BASE = 10;
1312

14-
/**
15-
* Main method
16-
*
17-
* @param args Command line arguments
18-
*/
19-
public static void main(String[] args) {
20-
Scanner sc = new Scanner(System.in);
21-
System.out.println("Input the binary number: ");
22-
int b = sc.nextInt();
23-
System.out.println("Octal equivalent: " + convertBinaryToOctal(b));
24-
sc.close();
13+
private BinaryToOctal() {
2514
}
2615

2716
/**
@@ -31,20 +20,26 @@ public static void main(String[] args) {
3120
* @return The octal number
3221
*/
3322
public static String convertBinaryToOctal(int binary) {
34-
String octal = "";
35-
int currBit = 0;
36-
int j = 1;
23+
if (binary == 0) {
24+
return "0";
25+
}
26+
27+
StringBuilder octal = new StringBuilder();
28+
int currentBit;
29+
int bitValueMultiplier = 1;
30+
3731
while (binary != 0) {
38-
int code3 = 0;
39-
for (int i = 0; i < 3; i++) {
40-
currBit = binary % 10;
41-
binary = binary / 10;
42-
code3 += currBit * j;
43-
j *= 2;
32+
int octalDigit = 0;
33+
for (int i = 0; i < BITS_IN_GROUP && binary != 0; i++) {
34+
currentBit = binary % DECIMAL_BASE;
35+
binary /= DECIMAL_BASE;
36+
octalDigit += currentBit * bitValueMultiplier;
37+
bitValueMultiplier *= BINARY_BASE;
4438
}
45-
octal = code3 + octal;
46-
j = 1;
39+
octal.insert(0, octalDigit);
40+
bitValueMultiplier = 1; // Reset multiplier for the next group
4741
}
48-
return octal;
42+
43+
return octal.toString();
4944
}
5045
}

src/test/java/com/thealgorithms/conversions/BinaryToOctalTest.java

+6-5
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,14 @@
22

33
import static org.junit.jupiter.api.Assertions.assertEquals;
44

5-
import org.junit.jupiter.api.Test;
5+
import org.junit.jupiter.params.ParameterizedTest;
6+
import org.junit.jupiter.params.provider.CsvSource;
67

78
public class BinaryToOctalTest {
89

9-
@Test
10-
public void testBinaryToOctal() {
11-
assertEquals("226", BinaryToOctal.convertBinaryToOctal(10010110));
12-
assertEquals("135", BinaryToOctal.convertBinaryToOctal(1011101));
10+
@ParameterizedTest
11+
@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"})
12+
void testConvertBinaryToOctal(int binary, String expectedOctal) {
13+
assertEquals(expectedOctal, BinaryToOctal.convertBinaryToOctal(binary));
1314
}
1415
}

0 commit comments

Comments
 (0)