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

please use JAVA Programming Businesses want phone numbers that are easy to remem

ID: 3785884 • Letter: P

Question

please use JAVA Programming

Businesses want phone numbers that are easy to remember, and one that can be tied to a phone number are even better.

Given the “standard international keypad”, write a program to determine the phone number based on a user-supplied 7-letter phrase (for example:BadDogs,glasses )

1

2 ABC

3 DEF

4 GHI

5 JKL

6 MNO

7 PQRS

8 TUV

9 WXYZ

*

0

#

------------------------------------------------------------------------------------------------

This is the design for code, please make it with these 3 functions

1. Main function

Get a word from the user and store it in a string variable “word”.

If the word cannot be changed into a number à “canChanged” function

                    Exit the program.

Change “word” variable into a phone number à “wordToNumber” function

Print the number.

2. 'canChanged' function

---> check if the word can be changed into a phone number.

If the number of letters in “word” is not 7,

                    Return false.

If there is any character which is not alphabet

                    Return false.

Return true.

3. 'wordToNumber' function

---> change a word into a phone number.

a string variable “word”, which will be changed into a phone number.

a string literal which is the result of change.

Declare a string variable “number”

Loop until accessing all letters in “word” is finished

                    Switch (word)

                                        If a,b,c,A,B,C: stack 2 into “number”

                                        If d,e,f,D,E,F: stack 3 into “number”

                                        If g,h,i,G,H,I: stack 4 into “number”

                                        If j,k,l,J,K,L: stack 5 into “number”

                                        If m,n,o,M,N,O: stack 6 into “number”

                                        If p,q,r,s,P,Q,R,S: stack 7 into “number”

                                        If t,u,v,T,U,V: stack 8 into “number”                   

                                        If w,x,y,z,W,X,Y,Z: stack 9 into “number”

Return “number.”

1

2 ABC

3 DEF

4 GHI

5 JKL

6 MNO

7 PQRS

8 TUV

9 WXYZ

*

0

#

Explanation / Answer

Hi, Please find my implementation.

Please let me know in case of any issue.

import java.util.Scanner;

public class PhoneNumber {

  

  

   public static boolean canChanged(String word){

      

       if(word == null || word.length() != 7)

           return false;

      

       for(int i=0; i<word.length(); i++)

           if(!Character.isAlphabetic(word.charAt(i)))

               return false;

      

       return true;

   }

  

   public static String wordToNumber(String word){

      

       String number = "";

      

       for(int i=0; i<word.length(); i++){

          

           char c = Character.toLowerCase(word.charAt(i));

           switch (c) {

           case 'a':

           case 'b':

           case 'c':

               number = number + "2";

               break;

           case 'd':

           case 'e':

           case 'f':

               number = number + "3";

               break;

           case 'g':

           case 'h':

           case 'i':

               number = number + "4";

               break;

           case 'j':

           case 'k':

           case 'l':

               number = number + "5";

               break;

           case 'm':

           case 'n':

           case 'o':

               number = number + "6";

               break;

           case 'p':

           case 'q':

           case 'r':

           case 's':

               number = number + "7";

               break;

           case 't':

           case 'u':

           case 'v':

               number = number + "8";

               break;

           case 'w':

           case 'x':

           case 'y':

           case 'z':

               number = number + "9";

               break;

           }

       }

      

       return number;

   }

   public static void main(String[] args) {

      

       Scanner sc = new Scanner(System.in);

      

       System.out.print("Enter a word: ");

       String word = sc.next();

      

       System.out.println("Phone number: "+wordToNumber(word));

   }

}

/*

Sample run:

Enter a word: pravesh

Phone number: 7728374

Enter a word: adgpmkx

Phone number: 2347659

*/