Skip to content

Commit e8985b3

Browse files
alxkmalxkmBamaCharanChhandogi
authored
refactor: BinaryToOctal (#5338)
* refactor: BinaryToOctal * checkstyle: fix formatting * refactor: adding input correctness case, cover by tests. Renaming variable --------- Co-authored-by: alxkm <[email protected]> Co-authored-by: Bama Charan Chhandogi <[email protected]>
1 parent 25b6aeb commit e8985b3

File tree

2 files changed

+40
-35
lines changed

2 files changed

+40
-35
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,45 @@
11
package com.thealgorithms.conversions;
22

3-
import java.util.Scanner;
4-
5-
/**
6-
* Converts any Binary number to an Octal Number
7-
*
8-
* @author Zachary Jones
9-
*/
103
public final class BinaryToOctal {
11-
private BinaryToOctal() {
12-
}
4+
private static final int BITS_PER_OCTAL_DIGIT = 3;
5+
private static final int BINARY_BASE = 2;
6+
private static final int DECIMAL_BASE = 10;
137

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();
8+
private BinaryToOctal() {
259
}
2610

2711
/**
2812
* This method converts a binary number to an octal number.
2913
*
3014
* @param binary The binary number
3115
* @return The octal number
16+
* @throws IllegalArgumentException if the input is not a valid binary number
3217
*/
3318
public static String convertBinaryToOctal(int binary) {
34-
String octal = "";
35-
int currBit = 0;
36-
int j = 1;
19+
if (binary == 0) {
20+
return "0";
21+
}
22+
23+
if (!String.valueOf(binary).matches("[01]+")) {
24+
throw new IllegalArgumentException("Input is not a valid binary number.");
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_PER_OCTAL_DIGIT && 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
}
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,24 @@
11
package com.thealgorithms.conversions;
22

33
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
import static org.junit.jupiter.api.Assertions.assertThrows;
45

56
import org.junit.jupiter.api.Test;
7+
import org.junit.jupiter.params.ParameterizedTest;
8+
import org.junit.jupiter.params.provider.CsvSource;
69

710
public class BinaryToOctalTest {
811

12+
@ParameterizedTest
13+
@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"})
14+
void testConvertBinaryToOctal(int binary, String expectedOctal) {
15+
assertEquals(expectedOctal, BinaryToOctal.convertBinaryToOctal(binary));
16+
}
17+
918
@Test
10-
public void testBinaryToOctal() {
11-
assertEquals("226", BinaryToOctal.convertBinaryToOctal(10010110));
12-
assertEquals("135", BinaryToOctal.convertBinaryToOctal(1011101));
19+
void testIncorrectInput() {
20+
assertThrows(IllegalArgumentException.class, () -> BinaryToOctal.convertBinaryToOctal(1234));
21+
assertThrows(IllegalArgumentException.class, () -> BinaryToOctal.convertBinaryToOctal(102));
22+
assertThrows(IllegalArgumentException.class, () -> BinaryToOctal.convertBinaryToOctal(-1010));
1323
}
1424
}

0 commit comments

Comments
 (0)