[BAEL-6115_Num2Letter] Convert a Number to a Letter in Java (#13638)
* [BAEL-6115_Num2Letter] Convert a Number to a Letter in Java * [BAEL-6115_Num2Letter] Change the 1st method's return type to char
This commit is contained in:
parent
806bb68887
commit
4c2dcb190f
|
@ -0,0 +1 @@
|
|||
### Relevant Articles:
|
|
@ -0,0 +1,25 @@
|
|||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-conversions.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<artifactId>core-java-numbers-conversions</artifactId>
|
||||
<name>core-java-numbers-conversions</name>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung.core-java-modules</groupId>
|
||||
<artifactId>core-java-modules</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<build>
|
||||
<finalName>core-java-numbers-conversions</finalName>
|
||||
<resources>
|
||||
<resource>
|
||||
<directory>src/main/resources</directory>
|
||||
<filtering>true</filtering>
|
||||
</resource>
|
||||
</resources>
|
||||
</build>
|
||||
|
||||
</project>
|
|
@ -0,0 +1,52 @@
|
|||
package com.baeldung.numtoletter;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
|
||||
public class ConvertNumberToLetterUnitTest {
|
||||
|
||||
static char numToLetterBySubstr(int i) {
|
||||
String LETTERS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
|
||||
if (i > 0 && i <= 25) {
|
||||
return LETTERS.substring(i, i + 1).charAt(0);
|
||||
} else {
|
||||
return '?';
|
||||
}
|
||||
}
|
||||
|
||||
static char numToLetterByAsciiCode(int i) {
|
||||
if (i > 0 && i <= 25) {
|
||||
return (char) ('A' + i);
|
||||
} else {
|
||||
return '?';
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenANumber_whenConvertToLetterUsingSubstring_shouldGetExpectedResult() {
|
||||
char negativeInputResult = numToLetterBySubstr(-7);
|
||||
assertEquals('?', negativeInputResult);
|
||||
|
||||
char tooLargeInputResult = numToLetterBySubstr(42);
|
||||
assertEquals('?', tooLargeInputResult);
|
||||
|
||||
char result = numToLetterBySubstr(10);
|
||||
assertEquals('K', result);
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenANumber_whenConvertToLetterUsingAscii_shouldGetExpectedResult() {
|
||||
char negativeInputResult = numToLetterByAsciiCode(-7);
|
||||
assertEquals('?', negativeInputResult);
|
||||
|
||||
char tooLargeInputResult = numToLetterByAsciiCode(42);
|
||||
assertEquals('?', tooLargeInputResult);
|
||||
|
||||
char charResult = numToLetterByAsciiCode(10);
|
||||
assertEquals('K', charResult);
|
||||
|
||||
assertEquals("K", String.valueOf(charResult));
|
||||
}
|
||||
}
|
|
@ -108,6 +108,7 @@
|
|||
<module>core-java-numbers-3</module>
|
||||
<module>core-java-numbers-4</module>
|
||||
<module>core-java-numbers-5</module>
|
||||
<module>core-java-numbers-conversions</module>
|
||||
<module>core-java-optional</module>
|
||||
<module>core-java-perf</module>
|
||||
<module>core-java-reflection</module>
|
||||
|
|
Loading…
Reference in New Issue