Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

import java.io.*; import java.util.*; public class PhoneMnemonics { public static String generateRandomMnemonic(String digits) { //Modify this method if(digits.isEmpty()) { return ; } else { String

image text in transcribed

import java.io.*; import java.util.*;

public class PhoneMnemonics { public static String generateRandomMnemonic(String digits) { //Modify this method if(digits.isEmpty()) { return ""; } else { String s = getCode(digits.charAt(0)); return s.charAt(new Random().nextInt(s.length())) + generateRandomMnemonic(digits.substring(1)); } } public static void recursiveMnemonics(Vector result, String mnemonicSoFar, String digitsLeft) { if(digitsLeft.length() == 0) { result.add(mnemonicSoFar); return; } else { int numLetters = digitLetters(digitsLeft.charAt(0)).length(); // Try all combinations for single digit String letters = digitLetters(digitsLeft.charAt(0)); if (digitsLeft.length() > 1 ) { digitsLeft = digitsLeft.substring(1); } else { digitsLeft = ""; //if the left digit is empty or space } for (int i = 0; i listMenemonics(String number) { Vector result = new Vector(); recursiveMnemonics(result, "", number); return result; } public static String digitLetters(char ch) { String str = ""; switch (ch) { case '2': str = "ABC"; break; case '3': str = "DEF"; break; case '4': str = "GHI"; break; case '5': str = "JKL"; break; case '6': str = "MNO"; break; case '7': str = "PQRS"; break; case '8': str = "TUV"; break; case '9': str = "WXYZ"; break; } return str; } public static void main(String[] args) { //Add code here } }

The digits on the dial pad of a phone are associated with letters that can be used to generate mnemonics for telephone numbers. Mnemonics are helpful to remember the phone number, and also provide associations such as 1-800-FLOWERS that can help with promotions. Your task is to design and implement a program that will use recursion to Your program will take the set of digits as a command line argument and print out all the possible mnemonics on the console. Note that a mnemonic is simply a combination of letters associated with the digits and does not have to make sense. For example, few mnemonic possibilities for 423 are "GAD "GAE" and "GCD Your program should be able to deal with different number of digits. You can assume that the input will only contains digits 0 to 9. Use the digit and letter associations as shown in the figure. Ignore a digit if it does not have any letter associations. Your recursive function should have the following signature Vector listMnemonics(String number) ABC DEF 1 2 3 GHI JKL I MNO 4 6 PQRS TUV WXYZ 8 7 9 The digits on the dial pad of a phone are associated with letters that can be used to generate mnemonics for telephone numbers. Mnemonics are helpful to remember the phone number, and also provide associations such as 1-800-FLOWERS that can help with promotions. Your task is to design and implement a program that will use recursion to Your program will take the set of digits as a command line argument and print out all the possible mnemonics on the console. Note that a mnemonic is simply a combination of letters associated with the digits and does not have to make sense. For example, few mnemonic possibilities for 423 are "GAD "GAE" and "GCD Your program should be able to deal with different number of digits. You can assume that the input will only contains digits 0 to 9. Use the digit and letter associations as shown in the figure. Ignore a digit if it does not have any letter associations. Your recursive function should have the following signature Vector listMnemonics(String number) ABC DEF 1 2 3 GHI JKL I MNO 4 6 PQRS TUV WXYZ 8 7 9

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions

Question

=+1 Where are the best places in the world to live (and work)?

Answered: 1 week ago