Skip to content

Commit

Permalink
Code implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
msaf9 committed May 3, 2022
1 parent f6b0f39 commit c36de8f
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/bin/
.classpath
17 changes: 17 additions & 0 deletions src/.project
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>src</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
</projectDescription>
37 changes: 37 additions & 0 deletions src/src/biDirectionalCodeConverter/BinaryToGrey.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package biDirectionalCodeConverter;

import java.util.ArrayList;
import java.util.Collections;

public class BinaryToGrey {

public static int toGrey(int decimal) {

int i = 0;
int greyInteger = 0;
int binary = DecimalToBinary.toBinary(decimal);

ArrayList<Integer> binaryArrayList = new ArrayList<>();
ArrayList<Integer> greyArrayList = new ArrayList<>();

while (binary > 0) {
binaryArrayList.add(i++, binary % 10);
binary /= 10;
}

Collections.reverse(binaryArrayList);

for (int j = 0; j < binaryArrayList.size(); j++) {
if (j == 0) {
greyArrayList.add(j, binaryArrayList.get(j));
} else {
greyArrayList.add(j, (binaryArrayList.get(j - 1) ^ binaryArrayList.get(j)));
}
}

for (int j = 0; j < greyArrayList.size(); j++) {
greyInteger = greyArrayList.get(j) + greyInteger * 10;
}
return greyInteger;
}
}
22 changes: 22 additions & 0 deletions src/src/biDirectionalCodeConverter/DecimalToBinary.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package biDirectionalCodeConverter;

import java.util.ArrayList;

public class DecimalToBinary {

public static int toBinary(int decimal) {

int i = 0;
int binaryInteger = 0;
ArrayList<Integer> binary = new ArrayList<>();
while (decimal > 0) {
binary.add(i++, decimal % 2);
decimal /= 2;
}

for (int j = binary.size() - 1; j >= 0; j--) {
binaryInteger = binary.get(j) + binaryInteger * 10;
}
return binaryInteger;
}
}
16 changes: 16 additions & 0 deletions src/src/biDirectionalCodeConverter/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package biDirectionalCodeConverter;

public class Main {
public static void main(String args[]) {

for (int i = 0; i < 16; i++) {
int binary = DecimalToBinary.toBinary(i);
int grey = BinaryToGrey.toGrey(i);

System.out.print("The binary code of the decimal number " + i + " is ");
System.out.println(String.format("%04d", binary).substring(0));
System.out.print("The grey code of the decimal number " + i + " is ");
System.out.println(String.format("%04d", grey).substring(0));
}
}
}

0 comments on commit c36de8f

Please sign in to comment.