Phone numbers and PIN codes can be easier to remember when you find words that spell out the numberon a standard phone pad. For example, instead of remembering the combination 5282, you can just think of JAVA.Write a recursive method that given a number, yields all possible spellings (which may or may not be real words).

Answer :

temmydbrain

Answer:

Kindly note that, you're to replace "at" with shift 2 as the brainly text editor can't take the symbol

Explanation:

Here is the completed code for the problem in the question above.

// PinWordEnumerator.java

import java.util.Scanner;

public class PinWordEnumerator {

               public static void main(String[] args) {

                               Scanner scanner = new Scanner(System.in);

                               System.out.print("Enter pin number: ");

                               String num = scanner.nextLine();

                               System.out.println();

                               System.out.printf("The keypad encodings for %s are:%n", num);

                               enumerateWords(num);

               }

               /**

               * method to return a character array containing all possible keypad

               * encodings for a digit in a standard phone

               */

               static char[] getKeysForButton(int n) {

                               switch (n) {

                               case 0:

                                               return new char[] { ' ' }; //returning only space

                               case 1:

                                               return new char[] { '.' }; //returning only dot

                               case 2:

                                               //button 2 contains A, B and C keys

                                               return new char[] { 'A', 'B', 'C' };

                               case 3:

                                               return new char[] { 'D', 'E', 'F' };

                               case 4:

                                               return new char[] { 'G', 'H', 'I' };

                               case 5:

                                               return new char[] { 'J', 'K', 'L' };

                               case 6:

                                               return new char[] { 'M', 'N', 'O' };

                               case 7:

                                               return new char[] { 'P', 'Q', 'R', 'S' };

                               case 8:

                                               return new char[] { 'T', 'U', 'V' };

                               case 9:

                                               return new char[] { 'W', 'X', 'Y', 'Z' };

                               }

                               return null;

               }

               /**

               * method to enumerate words

               *

               * "at"param num

               *            - String containing pin numbers, assuming it has only numbers

               */

               static void enumerateWords(String num) {

                               /**

                               * calling the recursive method to perform the enumeration

                               */

                               if (num != null)

                                               enumerateWords(num, "");

               }

               /**

               * the main method which performs the recursion

               *

               * "at"param num

               *            - current number

               * "at"param text

               *            - text containing converted spellings

               */

               static void enumerateWords(String num, String text) {

                               if (num.length() == 0) {

                                               // base case, displaying the text

                                               System.out.println(text);

                               } else {

                                               // finding the digit at 0th position

                                               int digit = num.charAt(0) - '0';

                                               // finding possible phone keys for this digit

                                               char letters[] = getKeysForButton(digit);

                                               if (letters != null) {

                                                               // looping through all possible keys

                                                               for (int i = 0; i < letters.length; i++) {

                                                                               /**

                                                                               * appending the current letter to the text and calling the

                                                                               * recursive method also neglecting the first letter of

                                                                               * current 'num' string

                                                                               */

                                                                               enumerateWords(num.substring(1), text + letters[i]);

                                                               }

                                               }

                               }

               }

}

/*OUTPUT*/

Enter pin number: 5282

The keypad encodings for 5282 are:

JATA

JATB

JATC

JAUA

JAUB

JAUC

JAVA

JAVB

JAVC

JBTA

JBTB

JBTC

JBUA

JBUB

JBUC

JBVA

JBVB

JBVC

JCTA

JCTB

JCTC

JCUA

JCUB

JCUC

JCVA

JCVB

JCVC

KATA

KATB

KATC

KAUA

KAUB

KAUC

KAVA

KAVB

KAVC

KBTA

KBTB

KBTC

KBUA

KBUB

KBUC

KBVA

KBVB

KBVC

KCTA

KCTB

KCTC

KCUA

KCUB

KCUC

KCVA

KCVB

KCVC

LATA

LATB

LATC

LAUA

LAUB

LAUC

LAVA

LAVB

LAVC

LBTA

LBTB

LBTC

LBUA

LBUB

LBUC

LBVA

LBVB

LBVC

LCTA

LCTB

LCTC

LCUA

LCUB

LCUC

LCVA

LCVB

LCVC

Other Questions