Skip to content

Commit 6d6cb7c

Browse files
Merge branch 'master' into add_temperature_converter
2 parents 5d61e90 + 4c65530 commit 6d6cb7c

File tree

76 files changed

+2917
-2010
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

76 files changed

+2917
-2010
lines changed

DIRECTORY.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -501,6 +501,7 @@
501501
* [DualPivotQuickSort](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/sorts/DualPivotQuickSort.java)
502502
* [DutchNationalFlagSort](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/sorts/DutchNationalFlagSort.java)
503503
* [ExchangeSort](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/sorts/ExchangeSort.java)
504+
* [FlashSort](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/sorts/FlashSort.java)
504505
* [GnomeSort](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/sorts/GnomeSort.java)
505506
* [HeapSort](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/sorts/HeapSort.java)
506507
* [InsertionSort](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/sorts/InsertionSort.java)
@@ -874,6 +875,7 @@
874875
* [DualPivotQuickSortTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/sorts/DualPivotQuickSortTest.java)
875876
* [DutchNationalFlagSortTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/sorts/DutchNationalFlagSortTest.java)
876877
* [ExchangeSortTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/sorts/ExchangeSortTest.java)
878+
* [FlashSortTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/sorts/FlashSortTest.java)
877879
* [GnomeSortTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/sorts/GnomeSortTest.java)
878880
* [HeapSortTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/sorts/HeapSortTest.java)
879881
* [InsertionSortTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/sorts/InsertionSortTest.java)

pom.xml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
<dependency>
2121
<groupId>org.junit</groupId>
2222
<artifactId>junit-bom</artifactId>
23-
<version>5.10.3</version>
23+
<version>5.11.0</version>
2424
<type>pom</type>
2525
<scope>import</scope>
2626
</dependency>
@@ -31,7 +31,7 @@
3131
<dependency>
3232
<groupId>org.junit.jupiter</groupId>
3333
<artifactId>junit-jupiter</artifactId>
34-
<version>5.10.3</version>
34+
<version>5.11.0</version>
3535
<scope>test</scope>
3636
</dependency>
3737
<dependency>
@@ -44,7 +44,7 @@
4444
<dependency>
4545
<groupId>org.junit.jupiter</groupId>
4646
<artifactId>junit-jupiter-api</artifactId>
47-
<version>5.10.3</version>
47+
<version>5.11.0</version>
4848
<scope>test</scope>
4949
</dependency>
5050
<dependency>
@@ -63,7 +63,7 @@
6363
<plugins>
6464
<plugin>
6565
<artifactId>maven-surefire-plugin</artifactId>
66-
<version>3.3.1</version>
66+
<version>3.4.0</version>
6767
<configuration>
6868
<forkNode implementation="org.apache.maven.plugin.surefire.extensions.SurefireForkNodeFactory"/>
6969
</configuration>

src/main/java/com/thealgorithms/backtracking/ParenthesesGenerator.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ private ParenthesesGenerator() {
1919
*/
2020
public static List<String> generateParentheses(final int n) {
2121
if (n < 0) {
22-
throw new IllegalArgumentException("The number of pairs of parentheses cannot be nagative");
22+
throw new IllegalArgumentException("The number of pairs of parentheses cannot be negative");
2323
}
2424
List<String> result = new ArrayList<>();
2525
generateParenthesesHelper(result, "", 0, 0, n);
Lines changed: 19 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,33 @@
11
package com.thealgorithms.conversions;
22

3-
import java.util.Scanner;
4-
53
/**
64
* This class converts a Binary number to a Decimal number
75
*/
86
final class BinaryToDecimal {
9-
private BinaryToDecimal() {
10-
}
7+
private static final int BINARY_BASE = 2;
118

12-
public static long binaryToDecimal(long binNum) {
13-
long binCopy;
14-
long d;
15-
long s = 0;
16-
long power = 0;
17-
binCopy = binNum;
18-
while (binCopy != 0) {
19-
d = binCopy % 10;
20-
s += d * (long) Math.pow(2, power++);
21-
binCopy /= 10;
22-
}
23-
return s;
9+
private BinaryToDecimal() {
2410
}
2511

2612
/**
27-
* Main Method
13+
* Converts a binary number to its decimal equivalent.
2814
*
29-
* @param args Command line arguments
15+
* @param binaryNumber The binary number to convert.
16+
* @return The decimal equivalent of the binary number.
17+
* @throws IllegalArgumentException If the binary number contains digits other than 0 and 1.
3018
*/
31-
public static void main(String[] args) {
32-
Scanner sc = new Scanner(System.in);
33-
System.out.print("Binary number: ");
34-
System.out.println("Decimal equivalent:" + binaryToDecimal(sc.nextLong()));
35-
sc.close();
19+
public static long binaryToDecimal(long binaryNumber) {
20+
long decimalValue = 0;
21+
long power = 0;
22+
23+
while (binaryNumber != 0) {
24+
long digit = binaryNumber % 10;
25+
if (digit > 1) {
26+
throw new IllegalArgumentException("Incorrect binary digit: " + digit);
27+
}
28+
decimalValue += (long) (digit * Math.pow(BINARY_BASE, power++));
29+
binaryNumber /= 10;
30+
}
31+
return decimalValue;
3632
}
3733
}
Lines changed: 37 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,60 +1,63 @@
11
package com.thealgorithms.conversions;
22

33
import java.util.HashMap;
4-
import java.util.Scanner;
4+
import java.util.Map;
55

66
/**
77
* Converts any Binary Number to a Hexadecimal Number
88
*
99
* @author Nishita Aggarwal
1010
*/
1111
public final class BinaryToHexadecimal {
12+
private static final int BITS_IN_HEX_DIGIT = 4;
13+
private static final int BASE_BINARY = 2;
14+
private static final int BASE_DECIMAL = 10;
15+
private static final int HEX_START_DECIMAL = 10;
16+
private static final int HEX_END_DECIMAL = 15;
17+
1218
private BinaryToHexadecimal() {
1319
}
1420

1521
/**
16-
* This method converts a binary number to a hexadecimal number.
22+
* Converts a binary number to a hexadecimal number.
1723
*
18-
* @param binary The binary number
19-
* @return The hexadecimal number
24+
* @param binary The binary number to convert.
25+
* @return The hexadecimal representation of the binary number.
26+
* @throws IllegalArgumentException If the binary number contains digits other than 0 and 1.
2027
*/
21-
static String binToHex(int binary) {
22-
// hm to store hexadecimal codes for binary numbers within the range: 0000 to 1111 i.e. for
23-
// decimal numbers 0 to 15
24-
HashMap<Integer, String> hm = new HashMap<>();
25-
// String to store hexadecimal code
26-
String hex = "";
27-
int i;
28-
for (i = 0; i < 10; i++) {
29-
hm.put(i, String.valueOf(i));
30-
}
31-
for (i = 10; i < 16; i++) {
32-
hm.put(i, String.valueOf((char) ('A' + i - 10)));
33-
}
34-
int currbit;
28+
public static String binToHex(int binary) {
29+
Map<Integer, String> hexMap = initializeHexMap();
30+
StringBuilder hex = new StringBuilder();
31+
3532
while (binary != 0) {
36-
int code4 = 0; // to store decimal equivalent of number formed by 4 decimal digits
37-
for (i = 0; i < 4; i++) {
38-
currbit = binary % 10;
39-
binary = binary / 10;
40-
code4 += currbit * (int) Math.pow(2, i);
33+
int decimalValue = 0;
34+
for (int i = 0; i < BITS_IN_HEX_DIGIT; i++) {
35+
int currentBit = binary % BASE_DECIMAL;
36+
if (currentBit > 1) {
37+
throw new IllegalArgumentException("Incorrect binary digit: " + currentBit);
38+
}
39+
binary /= BASE_DECIMAL;
40+
decimalValue += (int) (currentBit * Math.pow(BASE_BINARY, i));
4141
}
42-
hex = hm.get(code4) + hex;
42+
hex.insert(0, hexMap.get(decimalValue));
4343
}
44-
return hex;
44+
45+
return !hex.isEmpty() ? hex.toString() : "0";
4546
}
4647

4748
/**
48-
* Main method
49+
* Initializes the hexadecimal map with decimal to hexadecimal mappings.
4950
*
50-
* @param args Command line arguments
51+
* @return The initialized map containing mappings from decimal numbers to hexadecimal digits.
5152
*/
52-
public static void main(String[] args) {
53-
Scanner sc = new Scanner(System.in);
54-
System.out.println("Enter binary number:");
55-
int binary = sc.nextInt();
56-
String hex = binToHex(binary);
57-
System.out.println("Hexadecimal Code:" + hex);
58-
sc.close();
53+
private static Map<Integer, String> initializeHexMap() {
54+
Map<Integer, String> hexMap = new HashMap<>();
55+
for (int i = 0; i < BASE_DECIMAL; i++) {
56+
hexMap.put(i, String.valueOf(i));
57+
}
58+
for (int i = HEX_START_DECIMAL; i <= HEX_END_DECIMAL; i++) {
59+
hexMap.put(i, String.valueOf((char) ('A' + i - HEX_START_DECIMAL)));
60+
}
61+
return hexMap;
5962
}
6063
}
Lines changed: 27 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,45 @@
11
package com.thealgorithms.conversions;
22

3-
import java.util.Scanner;
4-
5-
/**
6-
* Converts any Binary number to an Octal Number
7-
*
8-
* @author Zachary Jones
9-
*/
103
public final class BinaryToOctal {
11-
private BinaryToOctal() {
12-
}
4+
private static final int BITS_PER_OCTAL_DIGIT = 3;
5+
private static final int BINARY_BASE = 2;
6+
private static final int DECIMAL_BASE = 10;
137

14-
/**
15-
* Main method
16-
*
17-
* @param args Command line arguments
18-
*/
19-
public static void main(String[] args) {
20-
Scanner sc = new Scanner(System.in);
21-
System.out.println("Input the binary number: ");
22-
int b = sc.nextInt();
23-
System.out.println("Octal equivalent: " + convertBinaryToOctal(b));
24-
sc.close();
8+
private BinaryToOctal() {
259
}
2610

2711
/**
2812
* This method converts a binary number to an octal number.
2913
*
3014
* @param binary The binary number
3115
* @return The octal number
16+
* @throws IllegalArgumentException if the input is not a valid binary number
3217
*/
3318
public static String convertBinaryToOctal(int binary) {
34-
String octal = "";
35-
int currBit = 0;
36-
int j = 1;
19+
if (binary == 0) {
20+
return "0";
21+
}
22+
23+
if (!String.valueOf(binary).matches("[01]+")) {
24+
throw new IllegalArgumentException("Input is not a valid binary number.");
25+
}
26+
27+
StringBuilder octal = new StringBuilder();
28+
int currentBit;
29+
int bitValueMultiplier = 1;
30+
3731
while (binary != 0) {
38-
int code3 = 0;
39-
for (int i = 0; i < 3; i++) {
40-
currBit = binary % 10;
41-
binary = binary / 10;
42-
code3 += currBit * j;
43-
j *= 2;
32+
int octalDigit = 0;
33+
for (int i = 0; i < BITS_PER_OCTAL_DIGIT && binary != 0; i++) {
34+
currentBit = binary % DECIMAL_BASE;
35+
binary /= DECIMAL_BASE;
36+
octalDigit += currentBit * bitValueMultiplier;
37+
bitValueMultiplier *= BINARY_BASE;
4438
}
45-
octal = code3 + octal;
46-
j = 1;
39+
octal.insert(0, octalDigit);
40+
bitValueMultiplier = 1; // Reset multiplier for the next group
4741
}
48-
return octal;
42+
43+
return octal.toString();
4944
}
5045
}

0 commit comments

Comments
 (0)