Repository: TheAlgorithms/Java
Pull request #3662 (2022-10-25 19:41:11)

Created LetterCombinationsOfPhoneNumberTest.java
<!-- For completed items, change [ ] to [x] -->
 Renamed List_all_Possible_Words_From_Phone_Digits.java  to LetterCombinationsOfPhoneNumber.java and Created LetterCombinationsOfPhoneNumberTest.java for JUnit Tests. Also changed the access specifier of functions in LetterCombinationsOfPhoneNumber.java from private to protected for JUnit Tests.

- [x] I have read [CONTRIBUTING.md](https://github.com/TheAlgorithms/Java/blob/master/CONTRIBUTING.md).
- [x] This pull request is all my own work -- I have not plagiarized it.
- [x] I know that pull requests will not be merged if they fail the automated tests.
- [x] This PR only changes one algorithm file. To ease review, please open separate PRs for separate algorithms.
- [x] All new Java files are placed inside an existing directory.
- [x] All filenames are in all uppercase characters with no spaces or dashes.
- [x] All functions and variable names follow Java naming conventions.
- [x] All new algorithms have a URL in their comments that points to Wikipedia or other similar explanations.
- [x] If this pull request resolves one or more open issues then the commit message contains `Fixes: #{$ISSUE_NO}`.


TaranjeetSinghKalsi: > Move `[]` to the variable type would be better :)
Done


Patch:
diff --git a/src/main/java/com/thealgorithms/strings/List_all_Possible_Words_From_Phone_Digits.java b/src/main/java/com/thealgorithms/strings/LetterCombinationsOfPhoneNumber.java
similarity index 87%
rename from src/main/java/com/thealgorithms/strings/List_all_Possible_Words_From_Phone_Digits.java
rename to src/main/java/com/thealgorithms/strings/LetterCombinationsOfPhoneNumber.java
index 7cb3b4aa..59ba954e 100644
--- a/src/main/java/com/thealgorithms/strings/List_all_Possible_Words_From_Phone_Digits.java
+++ b/src/main/java/com/thealgorithms/strings/LetterCombinationsOfPhoneNumber.java
@@ -2,16 +2,11 @@ package com.thealgorithms.strings;
 
 import java.util.*;
 
-public class List_all_Possible_Words_From_Phone_Digits {
+public class LetterCombinationsOfPhoneNumber {
 
     static Character[][] numberToCharMap;
 
-    private static List<String> printWords(
-        int[] numbers,
-        int len,
-        int numIndex,
-        String s
-    ) {
+    protected static List<String> printWords(int[] numbers, int len, int numIndex, String s) {
         if (len == numIndex) {
             return new ArrayList<>(Collections.singleton(s));
         }
@@ -33,7 +28,7 @@ public class List_all_Possible_Words_From_Phone_Digits {
         stringList.stream().forEach(System.out::println);
     }
 
-    private static void generateNumberToCharMap() {
+    protected static void generateNumberToCharMap() {
         numberToCharMap = new Character[10][5];
         numberToCharMap[0] = new Character[] { '\0' };
         numberToCharMap[1] = new Character[] { '\0' };


Test patch:
diff --git a/src/test/java/com/thealgorithms/strings/LetterCombinationsOfPhoneNumberTest.java b/src/test/java/com/thealgorithms/strings/LetterCombinationsOfPhoneNumberTest.java
new file mode 100644
index 00000000..08d94250
--- /dev/null
+++ b/src/test/java/com/thealgorithms/strings/LetterCombinationsOfPhoneNumberTest.java
@@ -0,0 +1,46 @@
+package com.thealgorithms.strings;
+
+import java.util.*;
+import static org.junit.jupiter.api.Assertions.*;
+import org.junit.jupiter.api.Test;
+
+public class LetterCombinationsOfPhoneNumberTest {
+
+    @Test
+    public void letterCombinationsOfPhoneNumber() {
+        LetterCombinationsOfPhoneNumber ob = new LetterCombinationsOfPhoneNumber();
+        ob.generateNumberToCharMap();
+
+        // ** Test 1 **
+        // Input: digits = ""
+        // Output: []
+        int[] numbers1 = {};
+        List<String> output1 = Arrays.asList("");
+        assertTrue(ob.printWords(numbers1, numbers1.length, 0, "").equals(output1));
+
+        // ** Test 2 **
+        // Input: digits = "2"
+        // Output: ["a","b","c"]
+        int[] numbers2 = { 2 };
+        List<String> output2 = Arrays.asList("a", "b", "c");
+        assertTrue(ob.printWords(numbers2, numbers2.length, 0, "").equals(output2));
+
+        // ** Test 3 **
+        // Input: digits = "23"
+        // Output: ["ad","ae","af","bd","be","bf","cd","ce","cf"]
+        int[] numbers3 = { 2, 3 };
+        List<String> output3 = Arrays.asList("ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf");
+        assertTrue(ob.printWords(numbers3, numbers3.length, 0, "").equals(output3));
+
+        // ** Test 4 **
+        // Input: digits = "234"
+        // Output: ["adg", "adh", "adi", "aeg", "aeh", "aei", "afg", "afh", "afi",
+        // "bdg", "bdh", "bdi", "beg", "beh", "bei", "bfg", "bfh", "bfi", "cdg", "cdh",
+        // "cdi", "ceg", "ceh", "cei", "cfg", "cfh", "cfi"]
+        int[] numbers4 = { 2, 3, 4 };
+        List<String> output4 = Arrays.asList("adg", "adh", "adi", "aeg", "aeh", "aei", "afg", "afh", "afi", "bdg",
+                "bdh", "bdi", "beg", "beh", "bei", "bfg", "bfh", "bfi", "cdg", "cdh", "cdi", "ceg", "ceh", "cei", "cfg",
+                "cfh", "cfi");
+        assertTrue(ob.printWords(numbers4, numbers4.length, 0, "").equals(output4));
+    }
+}