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 11package src .main .java .com .conversions ;
22
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- */
3+ /**
4+ * Convert the binary number into gray code
5+ */
6+ public class BinaryToGray {
97
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- }
8+ /**
9+ * convert the binary number into gray code
10+ *
11+ * @param binaryCode binary number
12+ * @return grayCode return as string
13+ */
14+ public String binaryToGray ( String binaryCode ) {
15+ StringBuilder grayCode = new StringBuilder ( Character . toString ( binaryCode . charAt ( 0 )));
16+
17+ for ( int i = 0 ; i < binaryCode . length () - 1 ; i ++) {
18+ if ( binaryCode . charAt ( i ) == binaryCode . charAt ( i + 1 ))
19+ grayCode .append ("0 " );
20+ else
21+ grayCode . append ( "1" );
22+ }
23+ return grayCode .toString ();
24+ }
2725
2826}
Original file line number Diff line number Diff line change 22
33import src .main .java .com .conversions .BinaryToGray ;
44import org .junit .Test ;
5+
56import static org .junit .Assert .assertEquals ;
67
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-
8+ public class BinaryToGrayTest {
9+
10+ @ Test
11+ public void testBinaryToGray () {
12+ BinaryToGray binaryToGray = new BinaryToGray ();
13+ assertEquals ("1101" , binaryToGray .binaryToGray ("1001" ));
14+ assertEquals ("11010011101" , binaryToGray .binaryToGray ("10011101001" ));
15+ }
16+
1817}
You can’t perform that action at this time.
0 commit comments