Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Write the solution in JAVA Part B: Palindromes A palindrome is a String value that is spelled the same way forward and backward. Some values
Write the solution in JAVA
Part B: Palindromes A palindrome is a String value that is spelled the same way forward and backward. Some values of palindromes are: - "radar" - "a" (or any String of length 1) - "" (a String of length 0) - "Able was I ere I saw Elba" (if capitalization is ignored) - "A man, a plan, a canal, Panama!" (if only the letters are considered) Write a method with the signature: public static boolean isPalindrome (String S) This method returns true if s is a palindrome, false otherwise. This method: 1. Makes a cleaned-up copy of s such that only the letters are retained and all the letters are lowercase (that is, no capitalization, spaces, or punctuation remain in the string); 2. Passes this cleaned-up string to a recursive method (which you also are to write) of the same name and same return type; and then 3. Returns whatever it gets back from the recursive method. Getting rid of capitalization in a String is easy; the String class includes a toLowerCase() method. To retain only letters, loop through s to extract each character with the charAt() method and build another string that only includes the letters. You can determine if a char value is a letter in a variety of ways. For instance, you might use the isLetter() method provided by the Character class. Or you can ask if a character is greater than or equal to ' a ' and also less than or equal to ' z ' . (Remember: The Java comparison operators like ==,= are valid with char values.) The second isPalindrome() method (the recursive one) is private instead of public, and has the following signature: private static boolean ispalindrome (String s, int index) The index parameter is used to designate a smaller and smaller portion of the string each time the recursive method is called. Initially, your public isPalindrome() method calls the recursive method with a string and an index value of 0 (zero). As an example, consider the string "abccba" where index is 0. Using the charAt() method from the String class, we can extract the character at index 0 , which is: 'a' For this string to be a palindrome, that first character must be equal to the last character in the string. Since the last character is also a, then this string might indeed be a palindrome ... but it depends on checking the rest of the string. We can do that by a recursive call to isPalindrome() with index+1. When index is 1 , we want to compare the two blue characters shown here: "abccba" Similarly, when we get to the recursive call where index is 2 , we want to compare the two blue characters shown here: "abccba" If at any point the recursive method finds two corresponding characters that are not equal, then the method immediately returns false. The base case is when index gets close enough to the middle of the string that there are no more pairs to check. At this point the method returns true. (The only way the value of index can make it to that point is if all the corresponding pairs of characters are equal along the way to getting there, so the string must be a palindrome.) Hint: It might help to use a couple of examples like "abccba" (an even number of characters) and "radar" (an odd number of characters) to manually trace (with pen and paper) the value of index and figure out how to test for the base case. Provide in the same class a main() method that tests your public isPalindrome method. Include your test values in an array of Strings. Loop through this array to produce output like the following: Yes! "radar" Yes! "a" Yes! "" Yes! "Able was I ere I saw Elba" Yes! "A man, a plan, a canal, Panama!" No... "Hello" No...." "abcaba" Include the first five strings shown above, and also the following strings of your own choosing: - Two other strings that are palindromes (these don't have to be actual words); and - Four other strings that are not palindromes. Do not include "Hello" or "abcdba". Hint: Use the two-character sequence \" to include double quotation marks in your output as shown aboveStep by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started