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

Re-submit this as JavaScript rather than Java . import java.util.Scanner; public

ID: 3769089 • Letter: R

Question

Re-submit this as JavaScript rather than Java.

import java.util.Scanner;

public class TelephoneNumberTranslator {

public static boolean checkFormat(String number) {

if (number.length() != 12) {

return false;

}

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

if (i == 3 || i == 7) {

if (number.charAt(i) != '-') {

return false; }

}

else if (!(number.charAt(i) >= '0' && number.charAt(i) <= '9')

&& !(number.charAt(i) >= 'a' && number.charAt(i) <= 'z')

&& !(number.charAt(i) >= 'A' && number.charAt(i) <= 'Z')){

return false;

} }

return true;

}

public static char getIntegerValue(char ch)

{

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

ch -= 32;

}

if (ch == 'A' || ch == 'B' || ch == 'C') { return '2'; } else if (ch == 'D' || ch == 'E' || ch == 'F') { return '3'; } else if (ch == 'G' || ch == 'H' || ch == 'I') { return '4'; } else if (ch == 'J' || ch == 'K' || ch == 'L') { return '5'; } else if (ch == 'M' || ch == 'N' || ch == 'O') { return '6'; } else if (ch == 'P' || ch == 'Q' || ch == 'R' || ch == 'S') { return '7'; } else if (ch == 'T' || ch == 'U' || ch == 'V') { return '8'; } else if (ch == 'W' || ch == 'X' || ch == 'Y' || ch == 'Z') { return '9'; } else { return ch; } } public static String getNumber(String number) {

String result = "";

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

result += getIntegerValue(number.charAt(i)); }

return result; }

public static void main(String[] args) {

Scanner in = new Scanner(System.in);

String number = null;

while (true) {

System.out

.print("enter a 10-character telephone number in the format XXX-XXX-XXXX: "); number = in.next(); if(checkFormat(number) == true){ break; } else{ System.out.println("Invalid Format. Try Again!!"); } } System.out.println("Numerical Representation of " + number + " is " + getNumber(number)); } }

Original Problem:

Design a JavaScript program:

Alphabetic Telephone Number Translator. Many companies use telephone numbers like 555-GET-FOOD so the number is easier for their customers to remember. On a standard telephone, the alphabetic letters are mapped to numbers in the following fashion: A, B and C = 2. D, E and F = 3. G, H and I = 4. J, K and L = 5. M, N and O = 6. P, R and S = 7. T, U and V = 8. W, X, Y and Z = 9. Design a program that asks the user to enter a 10-character telephone number in the format XXX-XXX-XXXX. The program should display the telephone number with any alphabetic characters that appeared in the original translated to their numeric equivalent. For example, if the user enters 555-GET-FOOD the program should display 555-438-3663.

(No Java libraries "import java.util.Scanner;") please

Explanation / Answer

import java.util.*;

import java.lang.*;

import java.io.*;

static ImmutableMultimap<String, String> numberAlphaTrans = null;

static
{
numberAlphaTrans = new ImmutableMultimap.Builder<String, String>()
.putAll("2", Lists.<String> newArrayList("A", "B", "C"))
.putAll("3", Lists.<String> newArrayList("D", "E", "F"))
.putAll("4", Lists.<String> newArrayList("G", "H", "I"))
.putAll("5", Lists.<String> newArrayList("J", "K", "L"))
.putAll("6", Lists.<String> newArrayList("M", "N", "O"))
.putAll("7", Lists.<String> newArrayList("P", "Q", "R", "S"))
.putAll("8", Lists.<String> newArrayList("T", "U", "V"))
.putAll("9", Lists.<String> newArrayList("W", "X", "Y", "Z"))
.build();
}

public static void main(String[] args)
{


Scanner keyboard = new Scanner(System.in);


System.out.print("Enter a phone number for translation: ");
String phoneNumber = keyboard.nextLine();


keyboard.close();

List<String> convertedPhone = Splitter
.fixedLength(1)
.splitToList(phoneNumber)
.stream()
.map(p -> {
if (Character.isAlphabetic(p.codePointAt(0))) {
return numberAlphaTrans.inverse().get(p).asList()
.get(0);
} else {
return p;
}
}).collect(Collectors.toList());

System.out.println(String.join("", convertedPhone));
}