Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a java program called PhoneKeypad that asks the user to enter a phone numb

ID: 3759004 • Letter: W

Question

Write a java program called PhoneKeypad that asks the user to enter a phone number string which can include both letters and numbers (as well as dashes and spaces), and then your program will print out the equivalent phone number using only digits (as well as dashes and spaces). Your program will have a main() method and a getNumber() method which has the following header:

Your getNumber() method should use the international standard letter to number mapping found on the telephone as shown below to translate a character into its numeric value, and your method MUST NOT BE case sensitive.

Design the main() method of your program such that it asks the user to enter a phone number string, and then it passes each character of the phone number string to your getNumber() method, storing the returned number in a string you will print as the output. If a dash '-' or space is found in the input string, you should just add the dash or space to the output string without first sending it to the getNumber() method.

Also, design the main() method of your program such that it allows the user to re-run the program with different inputs (i.e., use a loop structure).

Here is a sample output:

Explanation / Answer

import java.util.Scanner;

public class Phone {

public static int getNumber(char letter) {

       switch (letter) {

       case 'a':

       case 'b':

       case 'c':

           return 2;

          

       case 'd':

       case 'e':

       case 'f':

           return 3;

          

       case 'g':

       case 'h':

       case 'i':

           return 4;

          

       case 'j':

       case 'k':

       case 'l':

           return 5;

          

       case 'm':

       case 'n':

       case 'o':

           return 6;

          

       case 'p':

       case 'q':

       case 'r':

       case 's':

           return 7;

          

       case 't':

       case 'u':

       case 'v':

           return 8;

          

       case 'w':

       case 'x':

       case 'y':

       case 'z':

           return 9;

       default:

           break;

       }

      

       return 0;

   }

   public static void main(String[] args) {

       Scanner sc = new Scanner(System.in);

      

       while (true) {

           System.out.print(" Enter a phone number string: ");

           String str = sc.next();

          

           for (char c : str.toLowerCase().toCharArray()) {

               if (c >= 'a' && c<='z') {

                   System.out.print(getNumber(c));

               }

               else

                   System.out.print(c);

           }

          

          

           System.out.print(" Would you like to go again? ");

           str = sc.next();

          

           if (str.toLowerCase().equals("no")) {

               break;

           }

       }

   }

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote