From 184f644bd7050f5ee0837b45a44ef5ef1e0eda69 Mon Sep 17 00:00:00 2001 From: yanglbme Date: Fri, 10 May 2019 15:16:01 +0800 Subject: [PATCH] update binaryToGray --- .../java/com/conversions/BinaryToGray.java | 44 +++++++++---------- .../com/conversions/BinaryToGrayTest.java | 21 +++++---- 2 files changed, 31 insertions(+), 34 deletions(-) diff --git a/src/main/java/com/conversions/BinaryToGray.java b/src/main/java/com/conversions/BinaryToGray.java index 1482b8d6..60896e0f 100644 --- a/src/main/java/com/conversions/BinaryToGray.java +++ b/src/main/java/com/conversions/BinaryToGray.java @@ -1,28 +1,26 @@ 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 - */ +/** + * Convert the binary number into gray code + */ +public class BinaryToGray { - 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(); - } + /** + * convert the binary number into gray code + * + * @param binaryCode binary number + * @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(); + } } diff --git a/src/test/java/com/conversions/BinaryToGrayTest.java b/src/test/java/com/conversions/BinaryToGrayTest.java index c44f5470..64de8925 100644 --- a/src/test/java/com/conversions/BinaryToGrayTest.java +++ b/src/test/java/com/conversions/BinaryToGrayTest.java @@ -2,17 +2,16 @@ 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")); - } - +public class BinaryToGrayTest { + + @Test + public void testBinaryToGray() { + BinaryToGray binaryToGray = new BinaryToGray(); + assertEquals("1101", binaryToGray.binaryToGray("1001")); + assertEquals("11010011101", binaryToGray.binaryToGray("10011101001")); + } + }