please use JAVA Programming Businesses want phone numbers that are easy to remem
ID: 3785886 • 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 (example: 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
Answer)
import java.util.Scanner;
public class StringToNumbers
{
private static Scanner sc;
public static void main(String[] args)
{
sc = new Scanner(System.in);
System.out.println(" Please Enter The Phone Number (With Letters): ");
String phone_number = sc.nextLine(); //input word
phone_number = phone_number.toUpperCase(); //convert to upper case letter
int h=canChanged(phone_number); //check whether the input word length is 7 or not
if(h==1){
System.out.printf("%nOutput phone number for '%s' is '%s'",
phone_number, wordToNumber(phone_number) //function to change the word to number
);
}
else
System.out.println("Can not be changed");
}
public static int canChanged(String phone_number) //check the word length in 7 or not
{
int len=phone_number.length();
if(len==7)
return 1;
else
return 0;
}
public static long wordToNumber(String phone_number) //change word to number
{
long num = 0;
int strLen = phone_number.length();
for (int currCharacter = 0; currCharacter < strLen; currCharacter++)
{
char ch = phone_number.charAt(currCharacter);
if (Character.isLetter(ch))
{
switch(ch)
{
case 'A' : case 'B' : case 'C' : num *= 10; num += 2; break; //rules to change the number
case 'D' : case 'E' : case 'F' : num *= 10; num += 3; break;
case 'G' : case 'H' : case 'I' : num *= 10; num += 4; break;
case 'J' : case 'K' : case 'L' : num *= 10; num += 5; break;
case 'M' : case 'N' : case 'O' : num *= 10; num += 6; break;
case 'P' : case 'Q' : case 'R' : case 'S' : num*= 10; num += 7; break;
case 'T' : case 'U' : case 'V' : num *= 10; num += 8; break;
case 'W' : case 'X' : case 'Y' : case 'Z' : num*= 10; num += 9; break;
}
}
else if (Character.isDigit(ch))
{
num *= 10; num += Character.getNumericValue(ch);
}
else
{
System.out.println("Invalid character!");
}
} // End of for loop
// Return actual number only at the end of the function
return num;
}// End of full_number function
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.