From 91c19921ef99ada3cf676afa861bef9483eec0d6 Mon Sep 17 00:00:00 2001 From: nbdgit Date: Sun, 14 Apr 2019 16:55:05 +0530 Subject: [PATCH 1/4] Commit BinaryToGray --- .../java/com/conversions/BinaryToGray.java | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 src/main/java/com/conversions/BinaryToGray.java diff --git a/src/main/java/com/conversions/BinaryToGray.java b/src/main/java/com/conversions/BinaryToGray.java new file mode 100644 index 00000000..7b14c2a1 --- /dev/null +++ b/src/main/java/com/conversions/BinaryToGray.java @@ -0,0 +1,30 @@ +package src.main.java.com.conversions; + +public class BinaryToGray +{ + /* This method convert the binary number into gray code + @param binaryno need to convert binary number into gray code + @return graycode return as string + */ + + public String binaryToGray(String binarycode) + { + + String graycode=Character.toString(binarycode.charAt(0)); + + + for(int i=0;i Date: Sun, 14 Apr 2019 16:56:52 +0530 Subject: [PATCH 2/4] Commit BinaryToGrayTest --- .../com/conversions/BinaryToGrayTest.java | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 src/test/java/com/conversions/BinaryToGrayTest.java diff --git a/src/test/java/com/conversions/BinaryToGrayTest.java b/src/test/java/com/conversions/BinaryToGrayTest.java new file mode 100644 index 00000000..25ededee --- /dev/null +++ b/src/test/java/com/conversions/BinaryToGrayTest.java @@ -0,0 +1,19 @@ +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")); + } + +} From 230f04f6141565f242c6832ff752d93134a0ac52 Mon Sep 17 00:00:00 2001 From: Havan Agrawal Date: Fri, 3 May 2019 00:31:43 +0530 Subject: [PATCH 3/4] Apply suggestions from code review Co-Authored-By: nbdgit <48550673+nbdgit@users.noreply.github.com> --- src/main/java/com/conversions/BinaryToGray.java | 14 ++++++-------- .../java/com/conversions/BinaryToGrayTest.java | 5 ++--- 2 files changed, 8 insertions(+), 11 deletions(-) diff --git a/src/main/java/com/conversions/BinaryToGray.java b/src/main/java/com/conversions/BinaryToGray.java index 7b14c2a1..cf44b82e 100644 --- a/src/main/java/com/conversions/BinaryToGray.java +++ b/src/main/java/com/conversions/BinaryToGray.java @@ -3,28 +3,26 @@ package src.main.java.com.conversions; public class BinaryToGray { /* This method convert the binary number into gray code - @param binaryno need to convert 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) { - String graycode=Character.toString(binarycode.charAt(0)); - + String graycode = Character.toString(binarycode.charAt(0)); - for(int i=0;i Date: Fri, 3 May 2019 22:16:06 +0530 Subject: [PATCH 4/4] Add StringBuilder class --- src/main/java/com/conversions/BinaryToGray.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/main/java/com/conversions/BinaryToGray.java b/src/main/java/com/conversions/BinaryToGray.java index cf44b82e..1482b8d6 100644 --- a/src/main/java/com/conversions/BinaryToGray.java +++ b/src/main/java/com/conversions/BinaryToGray.java @@ -10,19 +10,19 @@ public class BinaryToGray public String binaryToGray(String binarycode) { - String graycode = Character.toString(binarycode.charAt(0)); + 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 = graycode + "0"; + graycode.append("0"); else - graycode = graycode + "1"; + graycode.append("1"); } - return graycode; + return graycode.toString(); } }