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

this week you are required to use concepts learned in this week: strings and the

ID: 3584083 • Letter: T

Question

this week you are required to use concepts learned in this week: strings and the Java library method contains. Write a Java class PhoneNumber that will take in a seven-letter word and return the phone number that corresponds to it. In the United States, the digits on a phone pad are mapped to letters by the following table: digit letter 1 2 A B C 3 D E F 4 G H I 5 J K L 6 M N O 7 P Q R S 8 T U V 9 W X Y Z 0 Hence a 7 digit phone number like HAIRCUT would map to 424-7288; NEWCARS would map to 639-2277. Submit a Java program PhoneNumber.java which has just one method decode and behaves as below PhoneNumber pn = new PhoneNumber(); pn.decode(

Explanation / Answer

package extraCredit; // CodeToPhone: map letters and digits on a phone keypad import java.util.Scanner; public class CodeToPhone { // Use an array as a table of the mapping between // letters and the digit of the key on which they appear. // Such an array should be "static final". private static final String keyPad[] = {"aaa","","ABC","DEF","GHI","JKL","MNO","PQRS","TUV","WXYZ"}; // The digits making up the phone number private String phoneNumber; /** Constructor takes a String consisting of letters and digits */ public CodeToPhone( String s ) { this.phoneNumber = toDigit(justDigitsAndLetters(s)); } /** Produce the phone number as a String */ public String toString() { String st = new String(this.phoneNumber); int position = 0; // between 4-7 if (this.phoneNumber.length() 4 ){ //find position after last four position = st.length() - 4; st = new StringBuffer(st).insert(position, "-").toString(); // between 0-3 }else if (this.phoneNumber.length() 7){ int removeNum; //find the number of digits to remove if(st.length() == 8){ removeNum = 1; }else removeNum = 2; st = st.substring(0, st.length() - removeNum); // treat as a 7 digit string position = st.length() - 4; st = new StringBuffer(st).insert(position, "-").toString(); // 10 and 11 }else if (this.phoneNumber.length() == 11 || this.phoneNumber.length() == 10){ if (this.phoneNumber.length() == 10){ st = new StringBuffer(st).insert(0, "1").toString(); } position = st.length() - 4; st = new StringBuffer(st).insert(position, "-").toString(); position = st.length() - 8; st = new StringBuffer(st).insert(position, "-").toString(); position = st.length() - 12; st = new StringBuffer(st).insert(position, "-").toString(); // over 11 }else if (this.phoneNumber.length() > 11){ int removeNum; removeNum = st.length() - 10; st = st.substring(0, st.length() - removeNum); st = new StringBuffer(st).insert(0, "1").toString(); position = st.length() - 4; st = new StringBuffer(st).insert(position, "-").toString(); position = st.length() - 8; st = new StringBuffer(st).insert(position, "-").toString(); position = st.length() - 12; st = new StringBuffer(st).insert(position, "-").toString(); } return st; } // Delete anything that is not a letter or digit private String justDigitsAndLetters(String s) { String newString = new String(s); newString = newString.replaceAll("\W", ""); newString = newString.toUpperCase(); return newString; } // Map letters in a String to their corresponding digit private String toDigit(String s) { String newString = new String(""); for (int i = 0; i