This is Java language: Part II: Can You Hear Me Now? (5 points) Filename(s): Hom
ID: 3923681 • Letter: T
Question
This is Java language:
Part II: Can You Hear Me Now? (5 points) Filename(s): Homework5Methods. java Write a method public static String getDigits (String phoneNumber) that takes an 11-character phone number written with any combination of 11 uppercase letters, lowercase letters and/or digits, and zero or more hyphens, and which returns the digits that a person would have to press on a cell phone to dial that phone number. We will use the mapping of letters to digits given at en . wikipedia. org/wiki /Telephone-keypad# /media/File:Telephone-keypad2.svg You may assume that the String parameter phoneNumber contains some combination of exactly 11 uppercase letters, lowercase letters and/or digits, but that any number of hyphens may be present. You can assume that a hyphen will not appear as the first or last character of the string. Do not assume that the first character is always a 1. Examples: Return Value Method Call getDigits ("1-800-Flowers")"18003569377" getDigits ("1800FLOWERS"18003569377" getDigits ("1-CSE-114-JAVA")"12731145282" getDigits ("Seawolf-SBU-1") 73296537281"Explanation / Answer
Tested on Eclipse
/****************HomeWork5Methods.java***************/
public class HomeWork5Methods {
/*
* GetDigits function methods which takes phoneNumber of String type 1)
* First we convert phoneNumber to upper case 2) and getting character from
* specific position using charAt() function 3) If Character is matched with
* specific condition then we are setting value according to condition
*/
public static String getDigits(String phoneNumber) {
String result = "";// result variable for storing result
/* Converting into upper case */
String upperCaseLetter = phoneNumber.toUpperCase();
/* Running Loop up to number of character */
for (int i = 0; i < upperCaseLetter.length(); i++) {
/* Getting Character from specific position */
String s = Character.toString(upperCaseLetter.charAt(i));
if (s.equals("A") || s.equals("B") || s.equals("C")) {
result += 2;
}
else if (s.equals("D") || s.equals("E") || s.equals("F")) {
result += 3;
}
else if (s.equals("G") || s.equals("H") || s.equals("I")) {
result += 4;
}
else if (s.equals("J") || s.equals("K") || s.equals("L")) {
result += 5;
}
else if (s.equals("M") || s.equals("N") || s.equals("O")) {
result += 6;
}
else if (s.equals("P") || s.equals("Q") || s.equals("R") || s.equals("S")) {
result += 7;
} else if (s.equals("T") || s.equals("U") || s.equals("V")) {
result += 8;
}
else if (s.equals("W") || s.equals("X") || s.equals("Y") || s.equals("Z")) {
result += 9;
} else if (s.equals("0") || s.equals("1") || s.equals("2") || s.equals("3") || s.equals("4")
|| s.equals("5") || s.equals("6") || s.equals("7") || s.equals("8") || s.equals("9")) {
result += s;
}
}
/* returning Result */
return result;
}
}
/**********************Homework5Driver.java****************************/
public class Homework5Driver {
public static void main(String[] args) {
System.out.println("Part 2:");
String result = "";
/* Calling GetDigits method */
result = HomeWork5Methods.getDigits("1-800-Flowers");
System.out.println("getDigits("1-800-Flowers") = "+result);
result = HomeWork5Methods.getDigits("1800FLOWERS");
System.out.println("getDigits("1800FLOWERS") = "+result);
result = HomeWork5Methods.getDigits("1-CSE-114-JAVA");
System.out.println("getDigits("1-CSE-114-JAVA") = "+result);
result = HomeWork5Methods.getDigits("Seawolf-SBU-1");
System.out.println("getDigits("Seawolf-SBU-1") = "+result);
}
}
/***********************output************************/
Part 2:
getDigits("1-800-Flowers") = 18003569377
getDigits("1800FLOWERS") = 18003569377
getDigits("1-CSE-114-JAVA") = 12731145282
getDigits("Seawolf-SBU-1") = 73296537281
If you have any query please feel free to ask
Thanks a lot
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.