Question

In: Computer Science

Problem in chapter 15 of Java book. Need a solution please! Problem: Standard telephone keypads contain...

Problem in chapter 15 of Java book. Need a solution please! Problem: Standard telephone keypads contain the digits zero through nine. The numbers two through nine each have three letters associated with them (as seen 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 this tool to develop the seven-letter word “NUMBERS.” 2: A B C 3: D E F 4: G H I 5: J K L 6: M N 0 7: P R S 8: T U V 9: W X Y 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.” Write a program that prompts the user to enter a seven-digit telephone number as input and calculates all possible seven-letter word combinations. After sorting the result, print the first and last 10 combinations.

Please include pictures of compilation

Solutions

Expert Solution

//PhoneNumber.java

import java.io.*;

import java.util.Scanner;

public class PhoneNumber {

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

// Array to store the digit
Character phoneNumbersInDigits[] = new Character[7];

// user input
String userNumber;

// Create one-dimensional arrays for letters according to digits except 0 and 1
char[] two = { 'A', 'B', 'C' };
char[] three = { 'D', 'E', 'F' };
char[] four = { 'G', 'H', 'I' };
char[] five = { 'J', 'K', 'L' };
char[] six = { 'M', 'N', 'O' };
char[] seven = { 'P', 'R', 'S' };
char[] eight = { 'T', 'U', 'V' };
char[] nine = { 'W', 'X', 'Y' };

// Create one-dimensional arrays to store the letters according to the
// digits position

char charAt0[] = new char[3];
char charAt1[] = new char[3];
char charAt2[] = new char[3];
char charAt3[] = new char[3];
char charAt4[] = new char[3];
char charAt5[] = new char[3];
char charAt6[] = new char[3];

// Initialize a count variable to count the number of phone numbers as strings
int count = 0;

// user to enter the phone number
System.out.print("Enter Phone Number:");
userNumber = input.nextLine();

//check user input a 0 or 1 and to check the dash after the third digit

for (int i = 0; i < userNumber.length(); i++) {
if (i != 3) {
if (!Character.isDigit(userNumber.charAt(i))) {
System.out.println("Invalid phone number format ###-####, # is a digit");
System.exit(0);
}

}

if (userNumber.charAt(i) == '0' || userNumber.charAt(i) == '1'
|| userNumber.charAt(3) != '-') {

System.out.println("Invalid phone number with 0 or 1 or without '-' in correct position");
System.exit(0);
}
}
int index = 0;
for (int i = 0; i < userNumber.length(); i++) {
if (i == 3)
continue;
else {
phoneNumbersInDigits[index] = userNumber.charAt(i);
index++;
}
}

// Compare the first digit of user entered phone number and copy the elements into another array

if (phoneNumbersInDigits[0].equals('2'))
System.arraycopy(two, 0, charAt0, 0, charAt0.length);
else if (phoneNumbersInDigits[0].equals('3'))
System.arraycopy(three, 0, charAt0, 0, charAt0.length);
else if (phoneNumbersInDigits[0].equals('4'))
System.arraycopy(four, 0, charAt0, 0, charAt0.length);
else if (phoneNumbersInDigits[0].equals('5'))
System.arraycopy(five, 0, charAt0, 0, charAt0.length);
else if (phoneNumbersInDigits[0].equals('6'))
System.arraycopy(six, 0, charAt0, 0, charAt0.length);
else if (phoneNumbersInDigits[0].equals('7'))

System.arraycopy(seven, 0, charAt0, 0, charAt0.length);
else if (phoneNumbersInDigits[0].equals('8'))
System.arraycopy(eight, 0, charAt0, 0, charAt0.length);
else if (phoneNumbersInDigits[0].equals('9'))
System.arraycopy(nine, 0, charAt0, 0, charAt0.length);
// Compare the second digit of user entered phone number and copy the elements into another array

if (phoneNumbersInDigits[1].equals('2'))
System.arraycopy(two, 0, charAt1, 0, charAt1.length);
else if (phoneNumbersInDigits[1].equals('3'))
System.arraycopy(three, 0, charAt1, 0, charAt1.length);
else if (phoneNumbersInDigits[1].equals('4'))
System.arraycopy(four, 0, charAt1, 0, charAt1.length);
else if (phoneNumbersInDigits[1].equals('5'))
System.arraycopy(five, 0, charAt1, 0, charAt1.length);
else if (phoneNumbersInDigits[1].equals('6'))
System.arraycopy(six, 0, charAt1, 0, charAt1.length);
else if (phoneNumbersInDigits[1].equals('7'))
System.arraycopy(seven, 0, charAt1, 0, charAt1.length);
else if (phoneNumbersInDigits[1].equals('8'))
System.arraycopy(eight, 0, charAt1, 0, charAt1.length);
else if (phoneNumbersInDigits[1].equals('9'))
System.arraycopy(nine, 0, charAt1, 0, charAt1.length);
// Compare the third digit of user entered phone number and copy the elements into another array

if (phoneNumbersInDigits[2].equals('2'))
System.arraycopy(two, 0, charAt2, 0, charAt2.length);
else if (phoneNumbersInDigits[2].equals('3'))
System.arraycopy(three, 0, charAt2, 0, charAt2.length);
else if (phoneNumbersInDigits[2].equals('4'))
System.arraycopy(four, 0, charAt2, 0, charAt2.length);
else if (phoneNumbersInDigits[2].equals('5'))
System.arraycopy(five, 0, charAt2, 0, charAt2.length);
else if (phoneNumbersInDigits[2].equals('6'))
System.arraycopy(six, 0, charAt2, 0, charAt2.length);
else if (phoneNumbersInDigits[2].equals('7'))
System.arraycopy(seven, 0, charAt2, 0, charAt2.length);
else if (phoneNumbersInDigits[2].equals('8'))
System.arraycopy(eight, 0, charAt2, 0, charAt2.length);
else if (phoneNumbersInDigits[2].equals('9'))
System.arraycopy(nine, 0, charAt2, 0, charAt2.length);
// No need to compare 4th digits as its the '-'
// Compare the fifth digit of user entered phone number and copy the elements into another array
if (phoneNumbersInDigits[3].equals('2'))
System.arraycopy(two, 0, charAt3, 0, charAt3.length);
else if (phoneNumbersInDigits[3].equals('3'))
System.arraycopy(three, 0, charAt3, 0, charAt3.length);
else if (phoneNumbersInDigits[3].equals('4'))
System.arraycopy(four, 0, charAt3, 0, charAt3.length);
else if (phoneNumbersInDigits[3].equals('5'))
System.arraycopy(five, 0, charAt3, 0, charAt3.length);
else if (phoneNumbersInDigits[3].equals('6'))
System.arraycopy(six, 0, charAt3, 0, charAt3.length);
else if (phoneNumbersInDigits[3].equals('7'))
System.arraycopy(seven, 0, charAt3, 0, charAt3.length);
else if (phoneNumbersInDigits[3].equals('8'))
System.arraycopy(eight, 0, charAt3, 0, charAt3.length);
else if (phoneNumbersInDigits[3].equals('9'))
System.arraycopy(nine, 0, charAt3, 0, charAt3.length);
// Compare the sixth digit of user entered phone number and copy the elements into another array
if (phoneNumbersInDigits[4].equals('2'))
System.arraycopy(two, 0, charAt4, 0, charAt4.length);
else if (phoneNumbersInDigits[4].equals('3'))
System.arraycopy(three, 0, charAt4, 0, charAt4.length);
else if (phoneNumbersInDigits[4].equals('4'))
System.arraycopy(four, 0, charAt4, 0, charAt4.length);
else if (phoneNumbersInDigits[4].equals('5'))
System.arraycopy(five, 0, charAt4, 0, charAt4.length);
else if (phoneNumbersInDigits[4].equals('6'))
System.arraycopy(six, 0, charAt4, 0, charAt4.length);
else if (phoneNumbersInDigits[4].equals('7'))
System.arraycopy(seven, 0, charAt4, 0, charAt4.length);
else if (phoneNumbersInDigits[4].equals('8'))
System.arraycopy(eight, 0, charAt4, 0, charAt4.length);
else if (phoneNumbersInDigits[4].equals('9'))
System.arraycopy(nine, 0, charAt4, 0, charAt4.length);

// Compare the seventh digit of user entered phone number and copy the elements into another array

if (phoneNumbersInDigits[5].equals('2'))
System.arraycopy(two, 0, charAt5, 0, charAt5.length);
else if (phoneNumbersInDigits[5].equals('3'))
System.arraycopy(three, 0, charAt5, 0, charAt5.length);
else if (phoneNumbersInDigits[5].equals('4'))
System.arraycopy(four, 0, charAt5, 0, charAt5.length);
else if (phoneNumbersInDigits[5].equals('5'))
System.arraycopy(five, 0, charAt5, 0, charAt5.length);
else if (phoneNumbersInDigits[5].equals('6'))
System.arraycopy(six, 0, charAt5, 0, charAt5.length);
else if (phoneNumbersInDigits[5].equals('7'))
System.arraycopy(seven, 0, charAt5, 0, charAt5.length);
else if (phoneNumbersInDigits[5].equals('8'))
System.arraycopy(eight, 0, charAt5, 0, charAt5.length);
else if (phoneNumbersInDigits[5].equals('9'))
System.arraycopy(nine, 0, charAt5, 0, charAt5.length);
// Compare the eighth digit of user entered phone number and copy the elements into another array

if (phoneNumbersInDigits[6].equals('2'))
System.arraycopy(two, 0, charAt6, 0, charAt6.length);
else if (phoneNumbersInDigits[6].equals('3'))
System.arraycopy(three, 0, charAt6, 0, charAt6.length);
else if (phoneNumbersInDigits[6].equals('4'))
System.arraycopy(four, 0, charAt6, 0, charAt6.length);
else if (phoneNumbersInDigits[6].equals('5'))
System.arraycopy(five, 0, charAt6, 0, charAt6.length);
else if (phoneNumbersInDigits[6].equals('6'))
System.arraycopy(six, 0, charAt6, 0, charAt6.length);
else if (phoneNumbersInDigits[6].equals('7'))
System.arraycopy(seven, 0, charAt6, 0, charAt6.length);
else if (phoneNumbersInDigits[6].equals('8'))
System.arraycopy(eight, 0, charAt6, 0, charAt6.length);
else if (phoneNumbersInDigits[6].equals('9'))
System.arraycopy(nine, 0, charAt6, 0, charAt6.length);
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
for (int k = 0; k < 3; k++) {
for (int l = 0; l < 3; l++) {
for (int m = 0; m < 3; m++) {

for (int n = 0; n < 3; n++) {

for (int o = 0; o < 3; o++) {
if (count < 10 || count > 2176) {

System.out.print(count + ": ");
System.out.print(charAt0[i]);
System.out.print(charAt1[j]);
System.out.print(charAt2[k]);
System.out.print(charAt3[l]);
System.out.print(charAt4[m]);
System.out.print(charAt5[n]);
System.out.print(charAt6[o]);
System.out.print('\n');
}

if (count == 2174)
System.out.println("...");

count++;

}

}
}

}
}

}

}

}
}

OUTPUT :


Related Solutions

In Java lang Standard telephone keypads contain the digits zero through nine. The numbers two through...
In Java lang 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." Digit Letters 2 ABC 3 DEF 4 GHI 5 JKL 6 MNO...
IN JAVA pls Standard telephone keypads contain the digits zero through nine. The numbers two through...
IN JAVA pls 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." Digit Letters 2 A B C 3 D E F 4 G...
Need a summary of chapter 8 of book ' Good to great' chapter 8
Need a summary of chapter 8 of book ' Good to great' chapter 8
Please turn in the solution for this problem. (No need to solve the LP) The MSS...
Please turn in the solution for this problem. (No need to solve the LP) The MSS Company, which manufacturers a new instant salad machine, has $350,000 to spend on advertising. The product is only to be test marketed initially in the Dallas area. The money is to be spent on a television advertising campaign for the Super Bowl weekend (Friday, Saturday, and Sunday.) The company has three options of advertisement available: daytime advertising, evening news advertising, and the Super Bowl....
Can a complete solution to Problem 34E from Chapter 15 of the Business Statistics Communicating with...
Can a complete solution to Problem 34E from Chapter 15 of the Business Statistics Communicating with Numbers Textbook (2nd edition), please be provided? An automotive workers union, in conjunction with top management, is negotiating a new hourly pay policy for union workers based on three variables: (1) job class, (2) years with the company, and (3) years as a union member at any company. The goal is to develop an equitable model that can objectively specify hourly pay, thereby reducing...
Need a summary of chapter 8 of book ' Good to great'
Need a summary of chapter 8 of book ' Good to great'
Need a summary of chapter 8 of book ' Good to great'
Need a summary of chapter 8 of book ' Good to great'
show some example of few recursive problem in java language and the solution code. Need more...
show some example of few recursive problem in java language and the solution code. Need more practice as a new learner
Can an answer, complete with workings please be provided for Problem 32E, Chapter 15 from the...
Can an answer, complete with workings please be provided for Problem 32E, Chapter 15 from the Business Statistics: Communicating with Numbers (2nd Edition) be made available? The manager of a local Costo store is in the process of making hiring decisions for selling mobile phone contracts. She believes that the sale of mobile phone contracts depends crucially on the number of hours clocked by male and female employees. She collects the weekly data on last year’s sales of mobile phone...
Foe an introduction to Management Science 15 th edition chapter 8 problem 11; need step by...
Foe an introduction to Management Science 15 th edition chapter 8 problem 11; need step by step to do these problem? Assume an investor has $50,000 to invest and wants to minimize the variance of his or her portfolio subject to a constraint that the portfolio returns a minimum of 10%.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT