File tree Expand file tree Collapse file tree
main/java/com/conversions
test/java/com/conversions Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ package src .main .java .com .conversions ;
2+
3+ public class BinaryToGray
4+ {
5+ /* This method convert the binary number into gray code
6+ @param binarycode need to convert binary number into gray code
7+ @return graycode return as string
8+ */
9+
10+ public String binaryToGray (String binarycode )
11+ {
12+
13+ StringBuilder graycode = new StringBuilder (Character .toString (binarycode .charAt (0 )));
14+
15+ for (int i = 0 ; i < binarycode .length () - 1 ; i ++)
16+ {
17+
18+ if (binarycode .charAt (i ) == binarycode .charAt (i +1 ))
19+ graycode .append ("0" );
20+ else
21+ graycode .append ("1" );
22+
23+ }
24+
25+ return graycode .toString ();
26+ }
27+
28+ }
Original file line number Diff line number Diff line change 1+ package src .test .java .com .conversions ;
2+
3+ import src .main .java .com .conversions .BinaryToGray ;
4+ import org .junit .Test ;
5+ import static org .junit .Assert .assertEquals ;
6+
7+ public class BinaryToGrayTest
8+ {
9+
10+ @ Test
11+ public void testBinaryToGray ()
12+ {
13+ BinaryToGray btog = new BinaryToGray ();
14+ assertEquals ("1101" , btog .binaryToGray ("1001" ));
15+ assertEquals ("11010011101" ,btog .binaryToGray ("10011101001" ));
16+ }
17+
18+ }
You can’t perform that action at this time.
0 commit comments