Using Java:
On most telephone keypads the letters of the alphabet are mapped
to various digits.
In order to make their phone numbers more memorable, service
providers like to find numbers that spell out some word (called a
mnemonic) appropriate to their business that makes that phone
number easier to remember. For example, the phone number for a
physician called Smith could be 377 -6484 (DRSMITH). Write a method
listMnemonics that will generate all possible letter combinations
that correspond to a given number, represented as a string of
digits using a recursive function. This is a recursive backtracking
problem, but you are trying to find all the “solutions”, not just
one. In fact you are trying to find all possible Strings using the
English alphabet, not just Strings that are actual “words”.
For example, if you call the method listMnemonics with the value
623 your method shall generate and return the following 27 possible
letter combinations that correspond to that prefix:
MAD MBD MCD NAD NBD NCD OAD OBD OCD
MAE MBE MCE NAE NBE NCE OAE OBE OCE
MAF MBF MCF NAF NBF NCF OAF OBF OCF
Note, the order of these returned strings is unimportant.
Use the GUI depicted above to input required data. (you do not
have to input a 7 – 10 digit number. Three digits will
suffice)
I will let you decide how you wish to display the out from the
inputted string.
Please add comments to your program!
Tony SparkEnlightened
//Include libraries
import java.io.*;
import java.util.*;
//Define a class “PhoneMnemonics”
public class PhoneMnemonics
{
/* Define a method “listMnemonics()”
to list mnemonics for number */
public ArrayList<String>
listMnemonics(String number)
{
//Define a new array variable
ArrayList<String> lresult = new
ArrayList<String>();
//Call “recursiveMnemonics()”
recursiveMnemonics(lresult, “”,
number);
//Return lresult
return lresult;
}
/* Define a method
“recursiveMnemonics()” to add mnemonics recursively */
private static void
recursiveMnemonics(ArrayList<String> lresult, String
lmnemonicSoFar, String ldigitsLeft)
{
//If string length is 0
if (ldigitsLeft.length() == 0)
{
//Add current mnemonic
lresult.add(lmnemonicSoFar);
}
//If string length is not 0
else
{
//Declare variable
int numLetters =
digitLetters(ldigitsLeft.charAt(0)).length();
//Define a string variable
String letters =
digitLetters(ldigitsLeft.charAt(0));
//If string length is greater than
0
if (ldigitsLeft.length() > 1 )
{
//Take substring
ldigitsLeft =
ldigitsLeft.substring(1);
}
//Otherwise
else
{
//If left digit is empty
ldigitsLeft = “”;
}
//Loop until length
for (int i = 0; i < numLetters;
i++)
{
//Declare variable
char c = letters.charAt(i);
//Update value
lmnemonicSoFar = lmnemonicSoFar +
Character.toString(c);
//Call “recursiveMnemonics()”
recursively
recursiveMnemonics(lresult,
lmnemonicSoFar, ldigitsLeft);
}
}
}
/* Define a method “digitLetters()” to
check for possible combinations */
public static String digitLetters(char
ch)
{
//Define a string variable
String lresult = “”;
//Define a switch case
switch (ch)
{
//Define case 2
case ‘2’: lresult = “ABC”;
//Break
break;
//Define case 3
case ‘3’: lresult = “DEF”;
//Break
break;
//Define case 4
case ‘4’: lresult = “GHI”;
//Break
break;
//Define case 5
case ‘5’: lresult = “JKL”;
//Break
break;
//Define case 6
case ‘6’: lresult = “MNO”;
//Break
break;
//Define case 7
case ‘7’: lresult = “PQRS”;
//Break
break;
//Define case 8
case ‘8’: lresult = “TUV”;
//Break
break;
//Define case 9
case ‘9’: lresult = “WXYZ”;
//Break
break;
}
//Return lresult
return lresult;
}
//Define a main method
public static void main(String[]
args)
{
//Define a variable “p”
PhoneMnemonics p = new PhoneMnemonics()
;
//Define new test array
ArrayList<String> test =
p.listMnemonics(“623”);
//Display message
System.out.println(“The 27 combinations
are shown below: n”);
//Display lresult
System.out.print(test);
}
}