In Java: (Telephone-Number Word Generator) Standard telephone keypads contain th
ID: 3740883 • Letter: I
Question
In Java:
(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 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.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Phone extends JFrame
{
private int phoneNumber[];
private JTextField input;
private JLabel prompt;
public Phone()
{
super( "Phone" );
input = new JTextField( 15 );
input.addActionListener(
new ActionListener() { // anonymous inner class
public void actionPerformed( ActionEvent event )
{
calculate(); // calculate character sequences
}
} // end anonymous inner class
); // end call to addActionListener
prompt = new JLabel(
"Enter phone number (digits greater than 1 only):" );
Container container = getContentPane();
container.setLayout( new FlowLayout() );
container.add( prompt );
container.add( input );
setSize( 300, 100 );
setVisible( true );
}
// output letter combinations to file
private void calculate()
{
String letters[][] = { { "" },
{ "" }, { "A", "B", "C" }, { "D", "E", "F" },
{ "G", "H", "I" }, { "J", "K", "L" }, { "M", "N", "O" },
{ "P", "R", "S" }, { "T", "U", "V" }, { "W", "X", "Y" } };
long phoneNumber = Long.parseLong( input.getText() );
int digits[] = new int[ 7 ];
for ( int i = 0; i < 7; i++ ) {
digits[i] = ( int )(phoneNumber % 10);
phoneNumber /= 10;
}
PrintStream output = null;
try {
output = new PrintStream( new FileOutputStream( "phone.dat" ) );
}
catch( IOException exception ) {
JOptionPane.showMessageDialog( null,
exception.toString(),
"Exception", JOptionPane.ERROR_MESSAGE );
System.exit( 1 );
}
input.setText( "Please wait..." );
int loop[] = new int[ 7 ];
// output all possible combinations
for ( loop[ 0 ] = 0; loop[ 0 ] <= 2; loop[ 0 ]++ )
for ( loop[ 1 ] = 0; loop[ 1 ] <= 2; loop[ 1 ]++ )
for ( loop[ 2 ] = 0; loop[ 2 ] <= 2; loop[ 2 ]++ )
for ( loop[ 3 ] = 0; loop[ 3 ] <= 2; loop[ 3 ]++ )
for ( loop[ 4 ] = 0; loop[ 4 ] <= 2; loop[ 4 ]++ )
for ( loop[ 5 ] = 0; loop[ 5 ] <= 2; loop[ 5 ]++ )
for ( loop[ 6 ] = 0; loop[ 6 ] <= 2; loop[6]++ ) {
for ( int i = 6; i >= 0; i-- )
output.print(
letters[ digits[ i ] ][ loop[ i ] ] );
output.println();
}
input.setText( "Done" );
output.close(); // close output stream
} // end method actionPerformed
public static void main( String args[] )
{
Phone application = new Phone();
application.setDefaultCloseOperation
( JFrame.EXIT_ON_CLOSE );
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.