Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit184f644

Browse files
committed
update binaryToGray
1 parent0c015fc commit184f644

File tree

2 files changed

+31
-34
lines changed

2 files changed

+31
-34
lines changed
Lines changed: 21 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,26 @@
11
packagesrc.main.java.com.conversions;
22

3-
publicclassBinaryToGray
4-
{
5-
/*This method convert the binary number into gray code
6-
@param binarycode need to convert binary number into gray code
7-
@return graycode return as string
8-
*/
3+
/**
4+
* Convert the binary number into gray code
5+
*/
6+
publicclassBinaryToGray {
97

10-
publicStringbinaryToGray(Stringbinarycode)
11-
{
12-
13-
StringBuildergraycode =newStringBuilder(Character.toString(binarycode.charAt(0)));
14-
15-
for(inti =0;i <binarycode.length() -1;i++)
16-
{
17-
18-
if (binarycode.charAt(i) ==binarycode.charAt(i+1))
19-
graycode.append("0");
20-
else
21-
graycode.append("1");
22-
23-
}
24-
25-
returngraycode.toString();
26-
}
8+
/**
9+
* convert the binary number into gray code
10+
*
11+
* @param binaryCode binary number
12+
* @return grayCode return as string
13+
*/
14+
publicStringbinaryToGray(StringbinaryCode){
15+
StringBuildergrayCode =newStringBuilder(Character.toString(binaryCode.charAt(0)));
16+
17+
for (inti =0;i <binaryCode.length() -1;i++) {
18+
if (binaryCode.charAt(i) ==binaryCode.charAt(i +1))
19+
grayCode.append("0");
20+
else
21+
grayCode.append("1");
22+
}
23+
returngrayCode.toString();
24+
}
2725

2826
}

‎src/test/java/com/conversions/BinaryToGrayTest.java‎

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,16 @@
22

33
importsrc.main.java.com.conversions.BinaryToGray;
44
importorg.junit.Test;
5+
56
importstaticorg.junit.Assert.assertEquals;
67

7-
publicclassBinaryToGrayTest
8-
{
9-
10-
@Test
11-
publicvoidtestBinaryToGray()
12-
{
13-
BinaryToGraybtog =newBinaryToGray();
14-
assertEquals("1101",btog.binaryToGray("1001"));
15-
assertEquals("11010011101",btog.binaryToGray("10011101001"));
16-
}
17-
8+
publicclassBinaryToGrayTest {
9+
10+
@Test
11+
publicvoidtestBinaryToGray() {
12+
BinaryToGraybinaryToGray =newBinaryToGray();
13+
assertEquals("1101",binaryToGray.binaryToGray("1001"));
14+
assertEquals("11010011101",binaryToGray.binaryToGray("10011101001"));
15+
}
16+
1817
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp