update binaryToGray

This commit is contained in:
yanglbme 2019-05-10 15:16:01 +08:00
parent 0c015fc605
commit 184f644bd7
2 changed files with 31 additions and 34 deletions

View File

@ -1,28 +1,26 @@
package src.main.java.com.conversions; package src.main.java.com.conversions;
public class BinaryToGray /**
{ * Convert the binary number into gray code
/* This method convert the binary number into gray code */
@param binarycode need to convert binary number into gray code public class BinaryToGray {
@return graycode return as string
*/
public String binaryToGray(String binarycode) /**
{ * convert the binary number into gray code
*
StringBuilder graycode = new StringBuilder(Character.toString(binarycode.charAt(0))); * @param binaryCode binary number
* @return grayCode return as string
for(int i = 0; i < binarycode.length() - 1; i++) */
{ public String binaryToGray(String binaryCode) {
StringBuilder grayCode = new StringBuilder(Character.toString(binaryCode.charAt(0)));
if (binarycode.charAt(i) == binarycode.charAt(i+1))
graycode.append("0"); for (int i = 0; i < binaryCode.length() - 1; i++) {
else if (binaryCode.charAt(i) == binaryCode.charAt(i + 1))
graycode.append("1"); grayCode.append("0");
else
} grayCode.append("1");
}
return graycode.toString(); return grayCode.toString();
} }
} }

View File

@ -2,17 +2,16 @@ package src.test.java.com.conversions;
import src.main.java.com.conversions.BinaryToGray; import src.main.java.com.conversions.BinaryToGray;
import org.junit.Test; import org.junit.Test;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
public class BinaryToGrayTest public class BinaryToGrayTest {
{
@Test
@Test public void testBinaryToGray() {
public void testBinaryToGray() BinaryToGray binaryToGray = new BinaryToGray();
{ assertEquals("1101", binaryToGray.binaryToGray("1001"));
BinaryToGray btog = new BinaryToGray(); assertEquals("11010011101", binaryToGray.binaryToGray("10011101001"));
assertEquals("1101", btog.binaryToGray("1001")); }
assertEquals("11010011101",btog.binaryToGray("10011101001"));
}
} }