Design and implement a Java program for programming exercise 6.21, page 239 (nam
ID: 3603328 • Letter: D
Question
Design and implement a Java program for programming exercise 6.21, page 239 (name it PhoneKeypad), to display a given phone number with letters in all number format. Sample output is shown in the textbook. Follow the instructions in the problem statement. Design the test program main method to handle all input and output and to allow the user to re-run the program with different inputs (i.e., use a loop structure). Document your code and follow the model outputs in the textbook. (Note: Various formal and informal national and international formats for displaying telephone numbers include a plus sign for indicating the presence of a country code, spaces, parentheses, dashes, periods, etc. and the length of the telephone number can vary widely. The program must be able to process phone numbers of any length with any character which can be entered using a standard keyboard – if the entered number contains a character that is neither a letter nor a digit, treat it is a legitimate phone number display character.)
*6.21 (Phone keypads) The international standard letter/number mapping for telephones is shown in Programming Exercise 4.15. Write a method that returns a number, given an uppercase letter, as follows int getNumber(char uppercaseLetter) Write a test program that prompts the user to enter a phone number as a string. The input number may contain letters. The program translates a letter (uppercase or lowercase) to a digit and leaves all other characters intact. Here is a sample run of the program: Enter a string: 1-800-Flowers 1-800-3569377 Enter a string: 1800flowers 18003569377Explanation / Answer
import java.util.Scanner;
public class PhoneKeypad {
public char getDigitForkey(char letter) {
if (letter == 'A' || letter == 'B' || letter == 'C') {
return '2';
}
else if (letter == 'D' || letter == 'E' || letter == 'F') {
return '3';
}
else if (letter == 'G' || letter == 'H' || letter == 'I') {
return '4';
} else if (letter == 'J' || letter == 'K' || letter == 'L') {
return '5';
} else if (letter == 'M' || letter == 'N' || letter == 'O') {
return '6';
}
else if (letter == 'P' || letter == 'Q' || letter == 'R' || letter == 'S') {
return '7';
}
else if (letter == 'T' || letter == 'U' || letter == 'V') {
return '8';
}
else if (letter == 'W' || letter == 'X' || letter == 'Y' || letter == 'Z') {
return '9';
}
return letter;
}
public String getDigitPhoneFromCharPhone(String phone) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < phone.length(); i++) {
sb.append(getDigitForkey(Character.toUpperCase(phone.charAt(i))));
}
return sb.toString();
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
PhoneKeypad pk = new PhoneKeypad();
while(true) {
System.out.print("Enter a string: ");
String phone = sc.nextLine();
System.out.println(pk.getDigitPhoneFromCharPhone(phone));
}
}
}
Sampe run
Enter a string: 1-800-Flowers
1-800-3569377
Enter a string: 1800flowers
18003569377
Enter a string:
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.