Skip to content
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
28 changes: 28 additions & 0 deletions src/main/java/com/conversions/BinaryToGray.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package src.main.java.com.conversions;

public class BinaryToGray
{
/* This method convert the binary number into gray code
@param binarycode need to convert binary number into gray code
@return graycode return as string
*/

public String binaryToGray(String binarycode)
{

StringBuilder graycode = new StringBuilder(Character.toString(binarycode.charAt(0)));

for(int i = 0; i < binarycode.length() - 1; i++)
{

if (binarycode.charAt(i) == binarycode.charAt(i+1))
graycode.append("0");
else
graycode.append("1");

}

return graycode.toString();
}

}
18 changes: 18 additions & 0 deletions src/test/java/com/conversions/BinaryToGrayTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package src.test.java.com.conversions;

import src.main.java.com.conversions.BinaryToGray;
import org.junit.Test;
import static org.junit.Assert.assertEquals;

public class BinaryToGrayTest
{

@Test
public void testBinaryToGray()
{
BinaryToGray btog = new BinaryToGray();
assertEquals("1101", btog.binaryToGray("1001"));
assertEquals("11010011101",btog.binaryToGray("10011101001"));
}

}