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

You will create a main application with static user defined methods that will de

ID: 3665221 • Letter: Y

Question

You will create a main application with static user defined methods that will determine if an inputted phone number (as a string) is a palindrome or not. You will convert the input to different types, dependent upon what you want to do with it. Use string, string tokenizer, string buffer, formatting, and long integer methods whenever possible (and use them as efficiently as possible).

%       The user will be properly prompted and will input a phone number as a string with a string input stream and send the string to user-defined static methods. The phone numbers will be inputted in this exact form:            (999) 999-9999

%       This string will first be sent to a method that will remove all spaces and symbols from its string to form a string of only digits. Use a StringTokenizer to eliminate the spaces & symbols. StringBuffers are more efficient than Strings, so use a StringBuffer to put the digits back together. Change the string buffer back to a string and this new string will be returned to main.

%       You will then send the string of digits (with all the symbols and spaces removed) to a method that will see if it is really a palindrome or not. Again change a copy of the string to a string buffer so you can reverse it all at once. Then change it back to a string and see if they are still the same string of digits as the argument string. Return the boolean result.

%       Back in main, print whether your phone number (as a string of digits) is a palindrome or not.

%       The string of digits will now be sent to a method to convert it to a comma form. The method will convert the string of digits to a long integer (since it is a 10 digit number, make it a long integer). Now either use a NumberFormat or a DecimalFormat to format the long integer with commas. For example: 5,186,294,822. Now return the resulting formatted string of digits and commas back to main.

%       Back in main, print the labeled comma phone number.

Use the following data when you run your program:

(130) 188-1031

(518) 6294822

(565) 677-6565

Make sure you follow directions and use commands and concepts from this chapter. Document your program with at least name, exercise number, and at least 4 lines explaining what the program does. You will also add comments to at least 5 individual lines in the program (comment lines not totally obvious to a novice programmer, especially on objects and method calls). Run your program with the data above. Save (or Copy and Paste) the Console running of your program into a plain output .txt text file. Submit your .java source code main program and the output .txt saved.

Explanation / Answer

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.text.DecimalFormat;
import java.util.StringTokenizer;

/**
* @author Srinivas Palli
*
*/
public class PhonePalindrome {

   /**
   * main method to test static methods
   *
   * @param args
   */
   public static void main(String[] args) {

       try {
           // open up standard input
           BufferedReader br = new BufferedReader(new InputStreamReader(
                   System.in));

           System.out.print("Enter Phone Number:");
           String phoneNumber = br.readLine();
           String stringDigits = removeSpacesSymbols(phoneNumber);
           System.out.println("stringDigits :" + stringDigits);
           boolean stringReverse = reverseString(stringDigits);
           if (stringReverse) {
               System.out.println("Phone Number is Palindrome");
           } else {
               System.out.println("Phone Number is Not Palindrome");
           }
           System.out.println("labeled comma phone number :"
                   + comaForm(stringDigits));

       } catch (Exception e) {
           // TODO: handle exception
           e.printStackTrace();
       }

   }

   /**
   * method to convert it to a comma form form.
   *
   * @param stringDigits
   * @return
   */
   public static String comaForm(String stringDigits) {
       long phoneNumber = Long.parseLong(stringDigits);

       DecimalFormat decimalFormat = new DecimalFormat("#,###,###,###");
       String newPhoneNumber = decimalFormat.format(phoneNumber);

       return newPhoneNumber;

   }

   /**
   * method to check really a palindrome or not
   *
   * @param stringDigits
   * @return
   */
   public static boolean reverseString(String stringDigits) {

       StringBuilder sb = new StringBuilder(stringDigits);

       sb = sb.reverse();

       if (sb.toString().equals(stringDigits)) {
           return true;
       } else {
           return false;
       }

   }

   /**
   * method that will remove all spaces and symbols from its string to form a
   * string of only digits
   *
   * @param phoneNumber
   * @return
   */
   public static String removeSpacesSymbols(String phoneNumber) {

       StringTokenizer st = new StringTokenizer(phoneNumber, " ");
       StringBuffer sb = new StringBuffer();
       while (st.hasMoreElements()) {
           sb.append(st.nextElement());
       }

       String stringDigits = sb.toString().replace("(", "");
       stringDigits = stringDigits.replace(")", "");
       stringDigits = stringDigits.replace("-", "");

       return stringDigits;

   }
}

OUTPUT:

Enter Phone Number:(130) 188-1031
stringDigits :1301881031
Phone Number is Palindrome
labeled comma phone number :1,301,881,031

Enter Phone Number:(518) 629-4822
stringDigits :5186294822
Phone Number is Not Palindrome
labeled comma phone number :5,186,294,822


Enter Phone Number:(565) 677-6565
stringDigits :5656776565
Phone Number is Palindrome
labeled comma phone number :5,656,776,565

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