Problem 9.3
Problem 9.3: More Phone Codes
If you did Problem 9.1, you translated a phone number containing letters into
one with just digits. In this problem, we'll do the reverse. That is,
we will generate all of the letter sequences that could produce a
given phone number.
In this problem, you are to program a class named PhoneToCode
with a method printWords
that will print out all of the possible letter combinations that
map to the phone number given as a command line argument,
using the mapping between letters and digits on a telephone keypad.
As a bonus, the method will return a count of the number
of combinations printed.
The specific requirements about the listing are:
-
All letters appearing in the list are upper case.
-
The digits zero and one in the original number are left as digits since
there is no letter mapping for them on the keypad.
-
Each digit (other than zero and one) in the original number lead
to combinations using the corresponding keypad letters.
-
All other characters in the original number are left as is.
-
The PhoneToCode constructor takes a String value representing the original
phone number.
-
The list should be presented as shown, with 3 or 4 combinations per line,
separated by blanks.
A count of the total number of combinations should also be presented.
Here are some examples:
> java PhoneToCode 364
DMG DMH DMI
DNG DNH DNI
DOG DOH DOI
EMG EMH EMI
ENG ENH ENI
EOG EOH EOI
FMG FMH FMI
FNG FNH FNI
FOG FOH FOI
Total combinations: 27
This example has zeroes:
> java PhoneToCode 40-4003
G0-G00D G0-G00E G0-G00F
G0-H00D G0-H00E G0-H00F
G0-I00D G0-I00E G0-I00F
H0-G00D H0-G00E H0-G00F
H0-H00D H0-H00E H0-H00F
H0-I00D H0-I00E H0-I00F
I0-G00D I0-G00E I0-G00F
I0-H00D I0-H00E I0-H00F
I0-I00D I0-I00E I0-I00F
Total combinations: 27
The phone number to be translated is given as a command line argument,
or will be prompted for.
Start with the code in PhoneToCode.java.
Constraints and Hints
-
The PhoneToCode constructor should translate any lower case characters
to upper case.
-
The printWords method is public and takes no arguments.
It should call a private recursive method to print all the possibilities.
Hint on the recursion: successively map digit to letter possibilities
by using two arguments to the recursive method, the mapped and unmapped
portion of string.
Note that you cannot print anything until you have mapped all digits.
-
Use an array of Strings to encode the mapping between letters and digits.
-
As in problem 8.2, although the characters Q and Z do not appear on the standard keypad,
assume that they do map to digits by placing them with the group of
characters in which they belong.
Turn-in
Turn in your PhoneToCode.java.