Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Phone numbers and PIN codes can be easier to remember when you find words that s

ID: 3911316 • Letter: P

Question

Phone numbers and PIN codes can be easier to remember when you find words that spell out the number on 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).

Please use the file included as a starting point.

Possible testing values are:

5282
26678837
7764726

import java.util.Scanner;

/**
Converts a numeric pin to an equivalent word using the digit to
letter mapping on a standard telephone keypad.
*/
public class PinWordEnumerator
{
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
System.out.print("Enter a pin number-> ");
String num = scan.nextLine();
System.out.println();
System.out.printf("The keypad encodings for %s are:%n",num);
enumerateWords(num);
}
  
/**
A wrapper for a recursive method that enumerates all the
phone keypad encodings for a number.
@param n a string representing the number
*/
public static void enumerateWords(String n)
{
// Implement a recursive method that generates
       // all possible phone keypad encodings for a
       // number. Implement additional auxiliary methods
       // if necessary.
}
  
...  
  
}

Phone numbers and PIN codes can be easier to remember when you find words that spell out the number on 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).

Please use the file included as a starting point.

Possible testing values are:

5282
26678837
7764726

import java.util.Scanner;

/**
Converts a numeric pin to an equivalent word using the digit to
letter mapping on a standard telephone keypad.
*/
public class PinWordEnumerator
{
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
System.out.print("Enter a pin number-> ");
String num = scan.nextLine();
System.out.println();
System.out.printf("The keypad encodings for %s are:%n",num);
enumerateWords(num);
}
  
/**
A wrapper for a recursive method that enumerates all the
phone keypad encodings for a number.
@param n a string representing the number
*/
public static void enumerateWords(String n)
{
// Implement a recursive method that generates
       // all possible phone keypad encodings for a
       // number. Implement additional auxiliary methods
       // if necessary.
}
  
...  
  
}

Explanation / Answer

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;{

case 0:

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

cas e 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

*

* @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

*

*@param num

* - current number

* @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:

$javac PinWordEnumerator.java

$java -Xmx128M -Xms16M PinWordEnumerator

Enter pin number: Exception in thread "main" java.util.NoSuchElementException: No line found

at java.util.Scanner.nextLine(Scanner.java:1540)

at PinWordEnumerator.main(PinWordEnumerator.java:12)