Skip to content

Commit 132060f

Browse files
authored
Merge branch 'Development' into Development
2 parents 4fd7698 + 04f6924 commit 132060f

27 files changed

+633
-513
lines changed

src/main/java/com/conversions/AnyBaseToDecimal.java

+7-6
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
public class AnyBaseToDecimal {
44
/**
55
* This method produces a decimal value of any given input number of any base
6+
*
67
* @param inpNum String of which we need the decimal value and base in integer format
78
* @return string format of the decimal value
89
*/
@@ -12,28 +13,28 @@ public String convertToDecimal(String inpNum, int base) {
1213
int num = 0;
1314
int pow = 1;
1415

15-
for (int i=len-1; i>=0; i--) {
16+
for (int i = len - 1; i >= 0; i--) {
1617
if (valOfChar(inpNum.charAt(i)) >= base) {
1718
return "Invalid Number";
1819
}
19-
num += valOfChar(inpNum.charAt(i))*pow;
20+
num += valOfChar(inpNum.charAt(i)) * pow;
2021
pow *= base;
2122
}
2223
return String.valueOf(num);
2324
}
2425

2526
/**
2627
* This method produces integer value of the input character and returns it
28+
*
2729
* @param c Char of which we need the integer value of
2830
* @return integer value of input char
2931
*/
3032

3133
private static int valOfChar(char c) {
3234
if (c >= '0' && c <= '9') {
33-
return (int)c - '0';
34-
}
35-
else {
36-
return (int)c - 'A' + 10;
35+
return (int) c - '0';
36+
} else {
37+
return (int) c - 'A' + 10;
3738
}
3839
}
3940
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package src.main.java.com.conversions;
2+
3+
/**
4+
* Convert the binary number into gray code
5+
*/
6+
public class BinaryToGray {
7+
8+
/**
9+
* convert the binary number into gray code
10+
*
11+
* @param binaryCode binary number
12+
* @return grayCode return as string
13+
*/
14+
public String binaryToGray(String binaryCode) {
15+
StringBuilder grayCode = new StringBuilder(Character.toString(binaryCode.charAt(0)));
16+
17+
for (int i = 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+
return grayCode.toString();
24+
}
25+
26+
}

src/main/java/com/conversions/BinaryToHexadecimal.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ public class BinaryToHexadecimal {
1111
* hm to store hexadecimal codes for binary numbers
1212
* within the range: 0000 to 1111 i.e. for decimal numbers 0 to 15
1313
*/
14-
private static Map<Integer, String> hmHexadecimal = new HashMap<>(16);
14+
private static Map<Integer, String> hmHexadecimal = new HashMap<>(16);
1515

1616
static {
1717
int i;

src/main/java/com/conversions/DecimalToAnyBase.java

+8-6
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,24 @@
33
import java.util.ArrayList;
44

55
public class DecimalToAnyBase {
6+
67
/**
78
* This method produces a String value of any given input decimal in any base
8-
* @param inp Decimal of which we need the value in base in String format
9+
*
10+
* @param inp Decimal of which we need the value in base in String format
911
* @param base base in which we want the decimal value to be converted into
1012
* @return string format of the converted value in the given base
1113
*/
12-
1314
public String convertToAnyBase(int inp, int base) {
1415
ArrayList<Character> charArr = new ArrayList<>();
1516

1617
while (inp > 0) {
17-
charArr.add(reVal(inp%base));
18+
charArr.add(reVal(inp % base));
1819
inp /= base;
1920
}
2021

2122
StringBuilder str = new StringBuilder(charArr.size());
22-
for(Character ch: charArr) {
23+
for (Character ch : charArr) {
2324
str.append(ch);
2425
}
2526

@@ -28,14 +29,15 @@ public String convertToAnyBase(int inp, int base) {
2829

2930
/**
3031
* This method produces character value of the input integer and returns it
32+
*
3133
* @param num integer of which we need the character value of
3234
* @return character value of input integer
3335
*/
3436

3537
private char reVal(int num) {
3638
if (num >= 0 && num <= 9)
37-
return (char)(num + '0');
39+
return (char) (num + '0');
3840
else
39-
return (char)(num - 10 + 'A');
41+
return (char) (num - 10 + 'A');
4042
}
4143
}

src/main/java/com/conversions/DecimalToHexadecimal.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,16 @@
33
import java.math.BigInteger;
44

55
public class DecimalToHexadecimal {
6-
private static final char hexChars[] = {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
6+
private static final char[] hexChars = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
77
private static final BigInteger valueHex = new BigInteger("16");
88

99
/**
1010
* This method converts and decimal number to a Hexadecimal number
11+
*
1112
* @param decimalStr
1213
* @return hexadecimal number
1314
*/
14-
public String decimalToHex(String decimalStr){
15+
public String decimalToHex(String decimalStr) {
1516
BigInteger decimal = new BigInteger(decimalStr);
1617

1718
int rem;

src/main/java/com/conversions/DecimalToOctal.java

+4-3
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,21 @@
33
import java.math.BigInteger;
44

55
public class DecimalToOctal {
6-
private static final char octalChars[] = {'0','1','2','3','4','5','6','7'};
6+
private static final char[] octalChars = {'0', '1', '2', '3', '4', '5', '6', '7'};
77
private static final BigInteger valueOctal = new BigInteger("8");
88

99
/**
1010
* This method converts and decimal number to a octal number
11+
*
1112
* @param decimalStr
1213
* @return octal number
1314
*/
14-
public String decimalToOctal(String decimalStr){
15+
public String decimalToOctal(String decimalStr) {
1516
BigInteger decimal = new BigInteger(decimalStr);
1617

1718
int rem;
1819
String octal = "";
19-
while(decimal.compareTo(BigInteger.ZERO) > 0) {
20+
while (decimal.compareTo(BigInteger.ZERO) > 0) {
2021
rem = decimal.mod(valueOctal).intValueExact();
2122
octal = octalChars[rem] + octal;
2223
decimal = decimal.divide(valueOctal);

src/main/java/com/dataStructures/BinaryTree.java

+32-26
Original file line numberDiff line numberDiff line change
@@ -2,39 +2,42 @@
22

33
/**
44
* Binary tree for general value type, without redundancy
5-
* @author RICARDO
5+
*
66
* @param <T> root data
77
*/
8+
89
public class BinaryTree<T extends Comparable> {
910
private final T data;
10-
private BinaryTree right, // the upper binary tree
11-
left; // the lower binary tree
11+
private BinaryTree right, // the upper binary tree
12+
left; // the lower binary tree
1213

1314
public BinaryTree(T data) {
1415
this.data = data;
1516
}
1617

1718
@Override
18-
public String toString(){
19+
public String toString() {
1920
return this.data.toString();
2021
}
21-
22+
2223
/**
2324
* inserts a new value in it's correspondant place
24-
* @param newDataValue value of the new banary tree to add on this tree
25+
*
26+
* @param newDataValue value of the new binary tree to add on this tree
2527
*/
26-
public void insert(T newDataValue){
28+
public void insert(T newDataValue) {
2729
this.insert(new BinaryTree(newDataValue));
2830
}
29-
31+
3032
/**
3133
* inserts a new binary tree in it's correspondant place
32-
* @param newData new value to add on this tree
34+
*
35+
* @param newData new value to add on this tree
3336
*/
34-
public void insert(BinaryTree newData){
35-
37+
public void insert(BinaryTree newData) {
38+
3639
int cpr = newData.data.compareTo(this.data); //new value comparission respect to actual value
37-
40+
3841
if (cpr < 0)
3942
if (this.left == null)
4043
this.setLeft(newData);
@@ -51,12 +54,13 @@ else if (cpr > 0)
5154

5255
/**
5356
* search and specific value on the tree
54-
* @param data Searched value
55-
* @return Binary tree wich contains the value, null if it doesn't exist
57+
*
58+
* @param data Searched value
59+
* @return Binary tree which contains the value, null if it doesn't exist
5660
*/
57-
public BinaryTree search(T data){
61+
public BinaryTree search(T data) {
5862
int cpr = data.compareTo(this.data); //new value comparission respect to actual value
59-
63+
6064
if (cpr < 0) {
6165
if (this.left == null)
6266
return null; //the value doesn't exist
@@ -65,43 +69,45 @@ public BinaryTree search(T data){
6569
if (cpr > 0) {
6670
if (this.right == null)
6771
return null; //the value doesn't exist
68-
return this.right.search(data);
72+
return this.right.search(data);
6973
}
7074
return this;
7175
}
72-
76+
7377
/**
7478
* Checks if the data value exist in the tree
79+
*
7580
* @param data data to be searched
7681
* @return true if this tree contains the data value, false if not.
7782
*/
78-
public boolean contains(T data){
83+
public boolean contains(T data) {
7984
return this.search(data) != null;
8085
}
81-
86+
8287
/**
8388
* uses recursive black magic to print this tree in console
89+
*
8490
* @param tabCounter prev tabs
8591
*/
86-
private void print(int tabCounter){
92+
private void print(int tabCounter) {
8793
for (int i = 0; i < tabCounter; i++)
8894
System.out.print("\t");
89-
95+
9096
System.out.println(this);
91-
97+
9298
if (this.left != null)
9399
this.left.print(tabCounter + 1); //it can't be ++ , pls don't change it
94100
if (this.right != null)
95101
this.right.print(tabCounter + 1); //it can't be ++ , pls don't change it
96102
}
97-
103+
98104
/**
99105
* uses black magic to print this tree in console
100106
*/
101-
public void print(){
107+
public void print() {
102108
this.print(0);
103109
}
104-
110+
105111
//getters and setters
106112
public T getData() {
107113
return data;

src/main/java/com/dataStructures/DisjointSet.java

-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
* <p>
1616
* 2. quickly query two elements whether contained in the same set, requiring about O(1) time.
1717
*
18-
* @author yangxf
1918
*/
2019
public class DisjointSet<T> implements Serializable {
2120
private static final long serialVersionUID = 3134700471905625636L;

0 commit comments

Comments
 (0)