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

In this lab you format a string of 10 characters as a telephone number. It is a

ID: 672495 • Letter: I

Question

In this lab you format a string of 10 characters as a telephone number. It is a Java program.

Your program prompts the user to enter a telephone number as a string of 10 characters representing letters or numbers: {a – z, A - Z, 1 – 9} .

The characters in the input string can be a mix of digits, lower-case and upper-case characters.

Your program should convert UPPER-CASE letters to LOWER-CASE letters, and leave the digits unchanged.

Then your program should format your new string of 10 characters into a string of 13 characters that follow the standard format: (xxx)xxx-xxxx

Here is a sample run of the program:

The user enters FLOwer4YOU

Your program displays (flo)wer-4you

1) Writing the pseudo-code for this exercise will help you greatly.

The part of your program that processes the input string has less than 10 lines.

2) In order to convert selectively the letters in UPPER-CASE to LOWER-CASE, you can test the ASCII code of each characters in you string.

Here are the Java methods

            a) that convert a character to its decimal ASCII value:            int myASCII = (int)myChar;

            b) that convert a decimal ASCII code to a character:               char myChar = (char)myASCII;

You must use the ASCII codes for the conversion, or no credit for the assignment.

3) In Java a character cannot be “concatenated” to another character to make a string, but a character can be concatenated to a string (in this case, the compiler makes an automatic “cast” i.e., a type conversion), and the result is a string.

Provide pseudo-code of professional quality (sequence of instructions in plain English allowing the immediate translation into Java statements)

Use a loop for processing each character in the input string

Processing each character using its decimal ASCII-code

Display the output formatted as (xxx)xxx-xxxx

Follow requirements A and B

Explanation / Answer

The comments here can be called as an algorithm, for the program.

1. Read the input string into raw_Number.

2. Initialize the string final_Number to an empty string, and the integer variable i to 0.

3. Read the ith character from raw_Number into the variable myChar.

4. if its first character, append '(' to final_Number.

5. if its fourth character, append ')' to final_Number.

6. if its seventh character, append '-' to final_Number.

7. Convert myChar to equivalent ASCII value and store it into myASCII.

8. If myASCII is between 65 and 90, add 32 to itself.

9. Convert myASCII to equivalent character value and store it into myChar.

10. Append myChar to final_Number.

11. Increment i.

12. If i less than 10, go to step 3.

13. Print the string final_Number.

import java.io.*;

public class PhoneNumberTranslator
{
public static void main(String[] args) throws IOException
{
String raw_Number;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter the 10-character telephone number {a – z, A - Z, 1 – 9}: ");   //Prompting the user for input.
raw_Number = br.readLine();   //Reading the input string.
char myChar;
int myASCII;
String final_Number = "";   //Initializing the final_Number to an empty string.
for(int i = 0; i < 10; i++)   //For each of the 10 characters in an input string.
{
if(i == 0)                   //Before the first character is processed.
final_Number += '(';       //Append an open brace '(' to the final String.
else if(i == 3)           //Before the fourth character is processed.
final_Number += ')';       //Append a closed brace ')' to the final String.
else if(i == 6)           //Before the seventh character is processed.
final_Number += '-';     //Append a hyphen '-' to the final String.
myChar = raw_Number.charAt(i);   //Read the character from string raw_Number into myChar.
myASCII = (int)myChar;       //Convert that character into its equivalent ASCII value.
if(myASCII >= 65 && myASCII < 91)   //If that ASCII value is between 65 and 90 (if its a capital letter).
myASCII += 32;           //Add 32 to that variable myASCII (make it a small letter).
myChar = (char)myASCII;   //Convert the ASCII value back into a character.
final_Number += myChar;    //Append the character to the final String.
}
System.out.println("The telephone number is: "+final_Number);   //Print the final string.
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote