Lab 21 Text File Output for Phone Number Words Description: Standard telephone k
ID: 3688698 • Letter: L
Question
Lab 21
Text File Output for Phone Number Words
Description:
Standard telephone keypads containe the digits zero through nine. The numbers two through nine each have three letters 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".
Each seven-letter word or word combination corresponds to exactly one seven-digit telephone number, but a seven-digit number corresponds to many seven-letter words, most of which are not English words. It is possible, for example, that the owner of a barbershop would be pleased to know that the shop's telephone number 424-7288 corresponds to "HAIRCUT", or a liquor store's number 233-7226 corresponds to "BEERCAN".
Objective:
Write a program that asks a user to enter a seven-digit phone number and then writes to the file 'numbers.txt' every possible seven-letter word combination corresponding to that number. There are 2187 (i.e. 37) such combinations. Treat the digits 0 & 1 as spaces, and the rest as shown:
Finally:
Submit both PhoneNumbers.java and an example numbers.txt to the dropbox
Remember that your source code MUST have appropriate headers and MUST contain appropriate comments.
Standard telephone keypads contained the digits zero through nine. The numbers two through nine each have three letters 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". Each seven-letter word or word combination corresponds to exactly one seven-digit telephone number, but a seven-digit number corresponds to many seven-letter words, most of which are not English words. It is possible, for example, that the owner of a barbershop would be pleased to know that the shop's telephone number 424-7288 corresponds to "HAIRCUT", or a liquor store's number 233-7226 corresponds to "BEERCAN". bjective: Write a program that asks a user to enter a seven-digit phone number and then writes to the file 'numbers.txt' every possible seven-letter word combination corresponding to that number. There are 2187 (i.e. 37) such combinations. Treat the digits 0 & 1 as spaces, and the rest as shown: Finally: Submit both PhoneNumbers.java and an example numbers.txt to the dropbox Remember that your source code MUST have appropriate headers and MUST contain appropriate comments.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()
public void actionPerformed( ActionEvent event )
{
calculate();
}
}
); // 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 ];
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.println();
}
input.setText( "Done" );
output.close();
}
public static void main( String args[] )
{
Phone application = new Phone();
application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.