From 4c2dcb190fed95ba605d30b9ef3c5bff3d198aea Mon Sep 17 00:00:00 2001 From: Kai Yuan Date: Tue, 21 Mar 2023 01:15:16 +0100 Subject: [PATCH] [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 --- .../core-java-numbers-conversions/README.md | 1 + .../core-java-numbers-conversions/pom.xml | 25 +++++++++ .../ConvertNumberToLetterUnitTest.java | 52 +++++++++++++++++++ core-java-modules/pom.xml | 1 + 4 files changed, 79 insertions(+) create mode 100644 core-java-modules/core-java-numbers-conversions/README.md create mode 100644 core-java-modules/core-java-numbers-conversions/pom.xml create mode 100644 core-java-modules/core-java-numbers-conversions/src/test/java/com/baeldung/numtoletter/ConvertNumberToLetterUnitTest.java diff --git a/core-java-modules/core-java-numbers-conversions/README.md b/core-java-modules/core-java-numbers-conversions/README.md new file mode 100644 index 0000000000..e320af31b4 --- /dev/null +++ b/core-java-modules/core-java-numbers-conversions/README.md @@ -0,0 +1 @@ +### Relevant Articles: \ No newline at end of file diff --git a/core-java-modules/core-java-numbers-conversions/pom.xml b/core-java-modules/core-java-numbers-conversions/pom.xml new file mode 100644 index 0000000000..55df86d5c7 --- /dev/null +++ b/core-java-modules/core-java-numbers-conversions/pom.xml @@ -0,0 +1,25 @@ + + 4.0.0 + core-java-numbers-conversions + core-java-numbers-conversions + jar + + + com.baeldung.core-java-modules + core-java-modules + 0.0.1-SNAPSHOT + + + + core-java-numbers-conversions + + + src/main/resources + true + + + + + diff --git a/core-java-modules/core-java-numbers-conversions/src/test/java/com/baeldung/numtoletter/ConvertNumberToLetterUnitTest.java b/core-java-modules/core-java-numbers-conversions/src/test/java/com/baeldung/numtoletter/ConvertNumberToLetterUnitTest.java new file mode 100644 index 0000000000..5652969761 --- /dev/null +++ b/core-java-modules/core-java-numbers-conversions/src/test/java/com/baeldung/numtoletter/ConvertNumberToLetterUnitTest.java @@ -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)); + } +} \ No newline at end of file diff --git a/core-java-modules/pom.xml b/core-java-modules/pom.xml index ac8b3115b5..f2db98dbfb 100644 --- a/core-java-modules/pom.xml +++ b/core-java-modules/pom.xml @@ -108,6 +108,7 @@ core-java-numbers-3 core-java-numbers-4 core-java-numbers-5 + core-java-numbers-conversions core-java-optional core-java-perf core-java-reflection