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

(Telephone-Number Word Generator) Standard telephone keypads contain the digits

ID: 3741042 • Letter: #

Question

(Telephone-Number Word Generator) Standard telephone keypads contain the digits zero through nine. The numbers two through nine each have three letters associated with them Fig. 1 below. Many people find it difficult to memorize phone numbers, so they use the correspondence between digits and letters to develop seven-letter words that correspond to their phone numbers. For example, a person whose telephone number is 686-2377 might use the correspondence indicated in Fig. 1 to develop the seven-letter word “NUMBERS.” Every seven-letter word corresponds to exactly one seven-digit telephone number. A restaurant wishing to increase its takeout business could surely do so with the number 825-3688 (i.e., “TAKEOUT”).

Every seven-letter phone number corresponds to many different seven-letter words, but most of these words represent unrecognizable juxtapositions of letters. It’s possible, however, that the owner of a barbershop would be pleased to know that the shop’s telephone number, 424-7288, corresponds to “HAIRCUT.” A veterinarian with the phone number 738-2273 would be pleased to know that the number corresponds to the letters “PETCARE.” An automotive dealership would be pleased to know that the dealership number, 639-2277, corresponds to “NEWCARS.”

Digit

Letters

Digit

Letters

Digit

Letters

2

A B C

5

J K L

8

T U V

3

D E F

6

M N O

9

W X Y

4

G H I

7

P R S

Fig. 1 Telephone keypad digits and letters.

Write a program in Java that, given a seven-digit number, uses a Formatter object to write to a file every possible seven-letter word combination corresponding to that number. There are 2,187 (37)(37) such combinations. Avoid phone numbers with the digits 0 and 1.

Use the following test class to test your program:

import java.util.Scanner;
import java.util.NoSuchElementException;

public class PhoneTest {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);

Phone application = new Phone();
System.out.print(
"Enter phone number (digits greater than 1 only): ");

try {
application.calculate(scanner.nextInt());
}
catch (NoSuchElementException elementException) {
System.err.println("Error inputting data.");
}
}
}

Digit

Letters

Digit

Letters

Digit

Letters

2

A B C

5

J K L

8

T U V

3

D E F

6

M N O

9

W X Y

4

G H I

7

P R S

Explanation / Answer


import java.util.Scanner;
import java.util.NoSuchElementException;
import java.io.*;
import java.util.HashMap;
import java.util.Map;
import java.util.Formatter;
import java.io.File;
import java.io.IOException;

class Phone
{
// Here we will take the 7 digit number and we create all combination for
// that number
public static void calculate(int number){
//using dictionary for number combination so we can retrieve according to
// user input
Map<String, String> dictionary = new HashMap<String, String>();
dictionary.put("2", "ABC");
dictionary.put("3", "DEF");
dictionary.put("4", "GHI");
dictionary.put("5", "JKL");
dictionary.put("6", "MNO");
dictionary.put("7", "PRS");
dictionary.put("8", "TUV");
dictionary.put("9", "WXY");
String[] alphabetArray=new String[7];
String numberString = String.valueOf(number);
for(int i =0; i < 7; i++){
alphabetArray[i] = dictionary.get(""+numberString.charAt(i));
}
// creating file for our words
File newFile = new File("words.txt");
Formatter output;
try{
output= new Formatter("words.txt");
// here we will loop through every possible seven letter
// word for given 7 numbers
for(int a1 =0 ; a1<3 ; a1++){
for(int a2=0; a2<3; a2++){
for(int a3=0; a3<3; a3++){
for(int a4=0; a4<3; a4++){
for(int a5=0; a5<3; a5++){
for(int a6=0; a6<3; a6++){
for(int a7=0; a7<3; a7++){
String seven_leter_word = ""+alphabetArray[0].charAt(a1)
+ alphabetArray[1].charAt(a2)+alphabetArray[2].charAt(a3)
+ alphabetArray[3].charAt(a4)+alphabetArray[4].charAt(a5)
+ alphabetArray[5].charAt(a6) + alphabetArray[6].charAt(a7)+" ";
//here we are adding in format object every seven letter word
output.format("%s", seven_leter_word);
}
}
}
}
}
}
}
output.close();
}catch(FileNotFoundException e){
System.err.println("Error opening file data.");
}
}

}


public class PhoneTest {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);

Phone application = new Phone();
System.out.print(
"Enter phone number (digits greater than 1 only): ");

try {
application.calculate(scanner.nextInt());
}
catch (NoSuchElementException elementException) {
System.err.println("Error inputting data.");
}
}
}
//if you any questions than please let me know.