Merge pull request #734 from nbdgit/Development
Added Binary to Gray code conversion
This commit is contained in:
commit
0c015fc605
28
src/main/java/com/conversions/BinaryToGray.java
Normal file
28
src/main/java/com/conversions/BinaryToGray.java
Normal file
@ -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
src/test/java/com/conversions/BinaryToGrayTest.java
Normal file
18
src/test/java/com/conversions/BinaryToGrayTest.java
Normal file
@ -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"));
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user