Skip to content

Commit c59cc05

Browse files
author
Alex Klymenko
committed
Merge branch 'refs/heads/master' into refactor/DynamicArray
2 parents ebe5c5a + 33fd79a commit c59cc05

File tree

6 files changed

+154
-124
lines changed

6 files changed

+154
-124
lines changed
Lines changed: 40 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,69 +1,69 @@
11
package com.thealgorithms.conversions;
22

3-
import java.io.BufferedReader;
4-
import java.io.InputStreamReader;
53
import java.util.ArrayList;
4+
import java.util.List;
65

76
/**
7+
* Class that provides methods to convert a decimal number to a string representation
8+
* in any specified base between 2 and 36.
9+
*
810
* @author Varun Upadhyay (<a href="https://github.com/varunu28">...</a>)
911
*/
10-
// Driver Program
1112
public final class DecimalToAnyBase {
12-
private DecimalToAnyBase() {
13-
}
14-
15-
public static void main(String[] args) throws Exception {
16-
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
17-
System.out.println("Enter the decimal input below: ");
18-
int decInput = Integer.parseInt(br.readLine());
19-
System.out.println();
20-
21-
System.out.println("Enter the base below: ");
22-
int base = Integer.parseInt(br.readLine());
23-
System.out.println();
13+
private static final int MIN_BASE = 2;
14+
private static final int MAX_BASE = 36;
15+
private static final char ZERO_CHAR = '0';
16+
private static final char A_CHAR = 'A';
17+
private static final int DIGIT_OFFSET = 10;
2418

25-
System.out.println("Decimal Input"
26-
+ " is: " + decInput);
27-
System.out.println("Value of " + decInput + " in base " + base + " is: " + convertToAnyBase(decInput, base));
28-
29-
br.close();
19+
private DecimalToAnyBase() {
3020
}
3121

3222
/**
33-
* This method produces a String value of any given input decimal in any
34-
* base
23+
* Converts a decimal number to a string representation in the specified base.
24+
* For example, converting the decimal number 10 to base 2 would return "1010".
3525
*
36-
* @param inp Decimal of which we need the value in base in String format
37-
* @return string format of the converted value in the given base
26+
* @param decimal the decimal number to convert
27+
* @param base the base to convert to (must be between {@value #MIN_BASE} and {@value #MAX_BASE})
28+
* @return the string representation of the number in the specified base
29+
* @throws IllegalArgumentException if the base is out of the supported range
3830
*/
39-
public static String convertToAnyBase(int inp, int base) {
40-
ArrayList<Character> charArr = new ArrayList<>();
31+
public static String convertToAnyBase(int decimal, int base) {
32+
if (base < MIN_BASE || base > MAX_BASE) {
33+
throw new IllegalArgumentException("Base must be between " + MIN_BASE + " and " + MAX_BASE);
34+
}
4135

42-
while (inp > 0) {
43-
charArr.add(reVal(inp % base));
44-
inp /= base;
36+
if (decimal == 0) {
37+
return String.valueOf(ZERO_CHAR);
4538
}
4639

47-
StringBuilder str = new StringBuilder(charArr.size());
40+
List<Character> digits = new ArrayList<>();
41+
while (decimal > 0) {
42+
digits.add(convertToChar(decimal % base));
43+
decimal /= base;
44+
}
4845

49-
for (Character ch : charArr) {
50-
str.append(ch);
46+
StringBuilder result = new StringBuilder(digits.size());
47+
for (int i = digits.size() - 1; i >= 0; i--) {
48+
result.append(digits.get(i));
5149
}
5250

53-
return str.reverse().toString();
51+
return result.toString();
5452
}
5553

5654
/**
57-
* This method produces character value of the input integer and returns it
55+
* Converts an integer value to its corresponding character in the specified base.
56+
* This method is used to convert values from 0 to 35 into their appropriate character representation.
57+
* For example, 0-9 are represented as '0'-'9', and 10-35 are represented as 'A'-'Z'.
5858
*
59-
* @param num integer of which we need the character value of
60-
* @return character value of input integer
59+
* @param value the integer value to convert (should be less than the base value)
60+
* @return the character representing the value in the specified base
6161
*/
62-
public static char reVal(int num) {
63-
if (num >= 0 && num <= 9) {
64-
return (char) (num + '0');
62+
private static char convertToChar(int value) {
63+
if (value >= 0 && value <= 9) {
64+
return (char) (ZERO_CHAR + value);
6565
} else {
66-
return (char) (num - 10 + 'A');
66+
return (char) (A_CHAR + value - DIGIT_OFFSET);
6767
}
6868
}
6969
}
Lines changed: 25 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,42 @@
11
package com.thealgorithms.conversions;
22

3-
import java.util.Scanner;
4-
53
/**
6-
* Converts any Octal Number to a Decimal Number
4+
* Class for converting an octal number to a decimal number. Octal numbers are based on 8, using digits from 0 to 7.
75
*
8-
* @author Zachary Jones
96
*/
107
public final class OctalToDecimal {
8+
private static final int OCTAL_BASE = 8;
9+
1110
private OctalToDecimal() {
1211
}
1312

1413
/**
15-
* Main method
14+
* Converts a given octal number (as a string) to its decimal representation.
15+
* If the input is not a valid octal number (i.e., contains characters other than 0-7),
16+
* the method throws an IllegalArgumentException.
1617
*
17-
* @param args Command line arguments
18+
* @param inputOctal The octal number as a string
19+
* @return The decimal equivalent of the octal number
20+
* @throws IllegalArgumentException if the input is not a valid octal number
1821
*/
19-
public static void main(String[] args) {
20-
Scanner sc = new Scanner(System.in);
21-
System.out.print("Octal Input: ");
22-
String inputOctal = sc.nextLine();
23-
int result = convertOctalToDecimal(inputOctal);
24-
if (result != -1) {
25-
System.out.println("Result convertOctalToDecimal : " + result);
22+
public static int convertOctalToDecimal(String inputOctal) {
23+
if (inputOctal == null || inputOctal.isEmpty()) {
24+
throw new IllegalArgumentException("Input cannot be null or empty");
2625
}
27-
sc.close();
28-
}
2926

30-
/**
31-
* This method converts an octal number to a decimal number.
32-
*
33-
* @param inputOctal The octal number
34-
* @return The decimal number
35-
*/
36-
public static int convertOctalToDecimal(String inputOctal) {
37-
try {
38-
// Actual conversion of Octal to Decimal:
39-
return Integer.parseInt(inputOctal, 8);
40-
} catch (NumberFormatException ne) {
41-
// Printing a warning message if the input is not a valid octal
42-
// number:
43-
System.out.println("Invalid Input, Expecting octal number 0-7");
44-
return -1;
27+
int decimalValue = 0;
28+
29+
for (int i = 0; i < inputOctal.length(); i++) {
30+
char currentChar = inputOctal.charAt(i);
31+
32+
if (currentChar < '0' || currentChar > '7') {
33+
throw new IllegalArgumentException("Incorrect input: Expecting an octal number (digits 0-7)");
34+
}
35+
36+
int currentDigit = currentChar - '0';
37+
decimalValue = decimalValue * OCTAL_BASE + currentDigit;
4538
}
39+
40+
return decimalValue;
4641
}
4742
}
Lines changed: 36 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,65 +1,61 @@
11
package com.thealgorithms.conversions;
22

3-
import java.util.Scanner;
4-
53
/**
6-
* Converts any Octal Number to HexaDecimal
4+
* Class for converting an Octal number to its Hexadecimal equivalent.
75
*
86
* @author Tanmay Joshi
97
*/
108
public final class OctalToHexadecimal {
9+
private static final int OCTAL_BASE = 8;
10+
private static final int HEX_BASE = 16;
11+
private static final String HEX_DIGITS = "0123456789ABCDEF";
12+
1113
private OctalToHexadecimal() {
1214
}
1315

1416
/**
15-
* This method converts a Octal number to a decimal number
17+
* Converts an Octal number (as a string) to its Decimal equivalent.
1618
*
17-
* @param s The Octal Number
18-
* @return The Decimal number
19+
* @param octalNumber The Octal number as a string
20+
* @return The Decimal equivalent of the Octal number
21+
* @throws IllegalArgumentException if the input contains invalid octal digits
1922
*/
20-
public static int octToDec(String s) {
21-
int i = 0;
22-
for (int j = 0; j < s.length(); j++) {
23-
char num = s.charAt(j);
24-
num -= '0';
25-
i *= 8;
26-
i += num;
23+
public static int octalToDecimal(String octalNumber) {
24+
if (octalNumber == null || octalNumber.isEmpty()) {
25+
throw new IllegalArgumentException("Input cannot be null or empty");
2726
}
28-
return i;
27+
28+
int decimalValue = 0;
29+
for (int i = 0; i < octalNumber.length(); i++) {
30+
char currentChar = octalNumber.charAt(i);
31+
if (currentChar < '0' || currentChar > '7') {
32+
throw new IllegalArgumentException("Incorrect octal digit: " + currentChar);
33+
}
34+
int currentDigit = currentChar - '0';
35+
decimalValue = decimalValue * OCTAL_BASE + currentDigit;
36+
}
37+
38+
return decimalValue;
2939
}
3040

3141
/**
32-
* This method converts a Decimal number to a Hexadecimal number
42+
* Converts a Decimal number to its Hexadecimal equivalent.
3343
*
34-
* @param d The Decimal Number
35-
* @return The Hexadecimal number
44+
* @param decimalNumber The Decimal number
45+
* @return The Hexadecimal equivalent of the Decimal number
3646
*/
37-
public static String decimalToHex(int d) {
38-
String digits = "0123456789ABCDEF";
39-
if (d <= 0) {
47+
public static String decimalToHexadecimal(int decimalNumber) {
48+
if (decimalNumber == 0) {
4049
return "0";
4150
}
42-
String hex = "";
43-
while (d > 0) {
44-
int digit = d % 16;
45-
hex = digits.charAt(digit) + hex;
46-
d = d / 16;
47-
}
48-
return hex;
49-
}
5051

51-
public static void main(String[] args) {
52-
Scanner input = new Scanner(System.in);
53-
System.out.print("Enter the Octal number: ");
54-
// Take octal number as input from user in a string
55-
String oct = input.next();
56-
57-
// Pass the octal number to function and get converted decimal form
58-
int decimal = octToDec(oct);
52+
StringBuilder hexValue = new StringBuilder();
53+
while (decimalNumber > 0) {
54+
int digit = decimalNumber % HEX_BASE;
55+
hexValue.insert(0, HEX_DIGITS.charAt(digit));
56+
decimalNumber /= HEX_BASE;
57+
}
5958

60-
// Pass the decimal number to function and get converted Hex form of the number
61-
String hex = decimalToHex(decimal);
62-
System.out.println("The Hexadecimal equivalant is: " + hex);
63-
input.close();
59+
return hexValue.toString();
6460
}
6561
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.thealgorithms.conversions;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
import static org.junit.jupiter.api.Assertions.assertThrows;
5+
6+
import org.junit.jupiter.api.Test;
7+
import org.junit.jupiter.params.ParameterizedTest;
8+
import org.junit.jupiter.params.provider.CsvSource;
9+
10+
public class DecimalToAnyBaseTest {
11+
12+
@ParameterizedTest
13+
@CsvSource({"0, 2, 0", "0, 16, 0", "0, 36, 0", "10, 2, 1010", "255, 16, FF", "100, 8, 144", "42, 2, 101010", "1234, 16, 4D2", "1234, 36, YA"})
14+
void testConvertToAnyBase(int decimal, int base, String expected) {
15+
assertEquals(expected, DecimalToAnyBase.convertToAnyBase(decimal, base));
16+
}
17+
18+
@Test
19+
void testBaseOutOfRange() {
20+
assertThrows(IllegalArgumentException.class, () -> DecimalToAnyBase.convertToAnyBase(10, 1));
21+
assertThrows(IllegalArgumentException.class, () -> DecimalToAnyBase.convertToAnyBase(10, 37));
22+
}
23+
}
Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,21 @@
11
package com.thealgorithms.conversions;
22

3-
import static org.junit.jupiter.api.Assertions.assertEquals;
4-
5-
import org.junit.jupiter.api.Test;
3+
import org.junit.jupiter.api.Assertions;
4+
import org.junit.jupiter.params.ParameterizedTest;
5+
import org.junit.jupiter.params.provider.CsvSource;
66

77
public class OctalToDecimalTest {
88

9-
@Test
10-
public void testOctalToDecimal() {
11-
assertEquals(1465, OctalToDecimal.convertOctalToDecimal("2671"));
12-
assertEquals(189, OctalToDecimal.convertOctalToDecimal("275"));
9+
@ParameterizedTest
10+
@CsvSource({"10, 8", "7, 7", "77, 63", "123, 83", "0, 0", "777, 511", "2671, 1465", "275, 189"})
11+
void testConvertOctalToDecimal(String inputOctal, int expectedDecimal) {
12+
Assertions.assertEquals(expectedDecimal, OctalToDecimal.convertOctalToDecimal(inputOctal));
13+
}
14+
15+
@ParameterizedTest
16+
@CsvSource({"'', Input cannot be null or empty", "'8', Incorrect input: Expecting an octal number (digits 0-7)", "'19', Incorrect input: Expecting an octal number (digits 0-7)"})
17+
void testIncorrectInput(String inputOctal, String expectedMessage) {
18+
IllegalArgumentException exception = Assertions.assertThrows(IllegalArgumentException.class, () -> OctalToDecimal.convertOctalToDecimal(inputOctal));
19+
Assertions.assertEquals(expectedMessage, exception.getMessage());
1320
}
1421
}
Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,23 @@
11
package com.thealgorithms.conversions;
22

3-
import static org.junit.jupiter.api.Assertions.assertEquals;
4-
5-
import org.junit.jupiter.api.Test;
3+
import org.junit.jupiter.api.Assertions;
4+
import org.junit.jupiter.params.ParameterizedTest;
5+
import org.junit.jupiter.params.provider.CsvSource;
66

77
public class OctalToHexadecimalTest {
88

9-
@Test
10-
public void testOctalToHexadecimal() {
11-
assertEquals("1EA", OctalToHexadecimal.decimalToHex(OctalToHexadecimal.octToDec("752")));
12-
assertEquals("15E", OctalToHexadecimal.decimalToHex(OctalToHexadecimal.octToDec("536")));
9+
@ParameterizedTest
10+
@CsvSource({"0, 0", "7, 7", "10, 8", "17, F", "20, 10", "777, 1FF", "1234, 29C", "752, 1EA", "536, 15E"})
11+
void testCorrectInputs(String inputOctal, String expectedHex) {
12+
int decimal = OctalToHexadecimal.octalToDecimal(inputOctal);
13+
String hex = OctalToHexadecimal.decimalToHexadecimal(decimal);
14+
Assertions.assertEquals(expectedHex, hex);
15+
}
16+
17+
@ParameterizedTest
18+
@CsvSource({"'', Input cannot be null or empty", "'8', Incorrect octal digit: 8", "'19', Incorrect octal digit: 9"})
19+
void testIncorrectInputs(String inputOctal, String expectedMessage) {
20+
IllegalArgumentException exception = Assertions.assertThrows(IllegalArgumentException.class, () -> OctalToHexadecimal.octalToDecimal(inputOctal));
21+
Assertions.assertEquals(expectedMessage, exception.getMessage());
1322
}
1423
}

0 commit comments

Comments
 (0)