Java code Standard telephone keypads contain the digits zero through nine. The n
ID: 3920361 • Letter: J
Question
Java code
Standard telephone keypads contain the digits zero through nine. The numbers two through nine each have 3~4 letters (case insensitive) associated with them. 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 remember it as "NUMBERS."
2 ABC
3 DEF
4 GHI
5 JKL
6 MNO
7 PQRS 8 TUV
9 WXYZ
Each seven-letter word corresponds to exactly one seven-digit telephone number, but a seven-digit number corresponds to many seven-letter strings, most of which are not words.
Create a Project2 class under the project package.
In this project you will develop a program which will find all the corresponding English
words given any specific telephone number. Here are the detailed requirements:
In the Project2 class you will create a static void test() method.
The test method will first ask the user to type in through keyboard a 7-digit telephone
number.
If the number typed in by the user is not 7 digits or the number contains some number
of the digit ‘0’ or ‘1’, then your program reports an error and asks to type in another
number.
Use binary search to search through the list in the file and match with the 7 letter words the numbers correspond to.
If the number typed in by the user is a 7-digit telephone number made up of the digits
‘2’ through ‘9’, then your program will print out all the English words corresponding to the telephone number (and the total number of them), using the given English word library in the “COSC240_P2_EnglishWordList.txt”.
Word List:
abandon
blossom
cancans
cappella
intrude
Explanation / Answer
Solution: Phone.java
// Note: phone number must be input in the form #######.
// Only the digits 2 thru 9 are recognized.
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.