Java Program help!!!!! Description: Standard telephone keypads containe the digi
ID: 3688606 • Letter: J
Question
Java Program help!!!!!
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:
Digit | Letters 1[space] 2 3 4. 5 6 A B C D E F G H I J K L M N O P R S [space]Explanation / Answer
Main.java
import java.util.Scanner;
public class Main {
public static void main(String args[]) {
Scanner input = new Scanner(System.in);
Telephone.run(input.nextLong());
}
}
TelephoneNumberGenerator.java
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintStream;
public class TelephoneNumberGenerator {
private static int counter=0;
private static int[] digits, arrayLoop;
private static String stringToWrite = "";
/**
* @param filename
* Method: enumerateAllPossibleOutputsAndWriteToFile
* Description: Handles the filename by enumerating all possibilities to stringToWrite, then write it to the file.
*
*/
public static void enumerateAllPossibleOutputsAndWriteToFile(String filename) {
PrintStream output = null;
try {
output = new PrintStream(new FileOutputStream(filename));
enumerateAllPossibleOutputsToString();
writeStringToFile(output);
} catch (IOException exception) {
exception.printStackTrace();
System.exit(1);
} finally {
output.close();
}
}
/**
* @param output
* Method: enumerateAllPossibleOutputsToString
* Description: Appends to the string all of the possible combinations of the Constants 2D array.
* Counter is used below to assert if it is equal to the required number (2187)
*/
private static void enumerateAllPossibleOutputsToString() {
arrayLoop = new int[Constants.LENGTH_OF_TELEPHONE_NUMBER];
int upperBound = Constants.LETTERS[0].length-1, lowerBound = 0;
for (arrayLoop[0] = lowerBound; arrayLoop[0] <= upperBound; arrayLoop[0]++) {
for (arrayLoop[1] = lowerBound; arrayLoop[1] <= upperBound; arrayLoop[1]++) {
for (arrayLoop[2] = lowerBound; arrayLoop[2] <= upperBound; arrayLoop[2]++) {
for (arrayLoop[3] = lowerBound; arrayLoop[3] <= upperBound; arrayLoop[3]++) {
for (arrayLoop[4] = lowerBound; arrayLoop[4] <= upperBound; arrayLoop[4]++) {
for (arrayLoop[5] = lowerBound; arrayLoop[5] <= upperBound; arrayLoop[5]++) {
for (arrayLoop[6] = lowerBound; arrayLoop[6] <= upperBound; arrayLoop[6]++) {
for (int j = arrayLoop.length-1; j >= 0; --j) {
stringToWrite += Constants.LETTERS[digits[j]][arrayLoop[j]];
counter++;
}
stringToWrite += " ";
}
}
}
}
}
}
}
}
/**
* @param number
* Method: handleZerosOrOnes
* Description: Check if there is a 0 or 1 in the number. If so, then we quit the application.
*
*/
public static void handleZerosOrOnes(long number) {
String str = String.valueOf(number);
if (str.contains("0") || str.contains("1")) {
System.err.print("Phone number cannot have a 0 or 1!");
System.exit(1);
}
}
/**
* @param number
* Method: insertDigits
* Description: Inserts all of the digits of @param in backwards order (simple algorithm)
* Also has a beginning assert to see if the number is within correct bounds.
*/
public static void insertDigits(long number) {
String str = String.valueOf(number);
if (str.length() != Constants.LENGTH_OF_TELEPHONE_NUMBER) {
System.err.print("Number not in correct bounds!");
System.exit(1);
}
digits = new int[Constants.LENGTH_OF_TELEPHONE_NUMBER];
for (int i = 0; i < Constants.LENGTH_OF_TELEPHONE_NUMBER; i++) {
digits[i] = (int) (number % 10);
number /= 10;
}
}
/**
* @param output
* Method: writeStringToFile
* Description: After enumerating all possibilities, then write getBytes() to the PrintStream.
*
*/
private static void writeStringToFile(PrintStream output) {
try {
if (counter != Constants.NUMBER_OF_TIMES_LOOPED) {
System.err.print("Did not loop correctly!");
assert(counter != Constants.NUMBER_OF_TIMES_LOOPED);
} else {
output.write(stringToWrite.getBytes());
}
} catch (IOException e) {
e.printStackTrace();
} finally {
output.close();
}
}
}
Telephone.java
public class Telephone {
public static void run(long number) {
TelephoneNumberGenerator.handleZerosOrOnes(number);
TelephoneNumberGenerator.insertDigits(number);
TelephoneNumberGenerator.enumerateAllPossibleOutputsAndWriteToFile(Constants.getNAME_OF_FILE());
}
}
Constants.java
public class Constants {
public static final int LENGTH_OF_TELEPHONE_NUMBER = 7;
public static final String[][] LETTERS =
{
{"","",""}, // 0 (doesn't count)
{"","",""}, // 1 (doesn't count)
{"A","B","C"}, // 2
{"D","E","F"}, // 3
{"G","H","I"}, // 4
{"J","K","L"}, // 5
{"M","N","O"}, // 6
{"P","R","S"}, // 7
{"T","U","V"}, // 8
{"W","X","Y"} // 9
};
public static final int LOWER_BOUND = (int) 1e7;
public static final int UPPER_BOUND = LOWER_BOUND*10;
private static String NAME_OF_FILE = "phone.txt";
public static final int NUMBER_OF_TIMES_LOOPED = 2187*7;
public static String getNAME_OF_FILE() {
return NAME_OF_FILE;
}
public static void setNAME_OF_FILE(String nAME_OF_FILE) {
NAME_OF_FILE = nAME_OF_FILE;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.