Problem 3 (10 points) Create a class named Palindrome.java then implements the r
ID: 3749864 • Letter: P
Question
Problem 3 (10 points)Create a class named Palindrome.java then implements the recursive method specified below:
public boolean isPalindrome(String s)
The recursive method: isPalindrome takes a string as a parameter and that returns true if the string is a palindrome and false if it is not. A palindrome is a string like "racecar" that has the same sequence of characters when written forwards and backwards. Notice that in a palindrome, the first and last characters match, as do the second and second-to-last, the third and third-to-last, and so on. However, when we test if a string is a palindrome, we only consider alphabetic letters(a-z and ignore cases) and digits (0-9), we simply ignore all other symbols. The table below includes various method calls and the value returned:
Method Call Result ------------------------------------------------------------- isPalindrome("radar") true isPalindrome("Was It A Rat I Saw?") true isPalindrome("pe e p") true isPalindrome("x") true isPalindrome("12321") true isPalindrome("Java") false
Problem 3 (10 points)
Create a class named Palindrome.java then implements the recursive method specified below:
public boolean isPalindrome(String s)
The recursive method: isPalindrome takes a string as a parameter and that returns true if the string is a palindrome and false if it is not. A palindrome is a string like "racecar" that has the same sequence of characters when written forwards and backwards. Notice that in a palindrome, the first and last characters match, as do the second and second-to-last, the third and third-to-last, and so on. However, when we test if a string is a palindrome, we only consider alphabetic letters(a-z and ignore cases) and digits (0-9), we simply ignore all other symbols. The table below includes various method calls and the value returned:
Method Call Result ------------------------------------------------------------- isPalindrome("radar") true isPalindrome("Was It A Rat I Saw?") true isPalindrome("pe e p") true isPalindrome("x") true isPalindrome("12321") true isPalindrome("Java") false
were 10 failures:
Explanation / Answer
-------------------------------------------------------Palindrome.java-----------------------------------------------------------
import java.util.Scanner;
public class Palindrome {
//This is boolean method for checking palindrome of String .
public static boolean isPalindrome(String str)
{
/*
* first change the string to lower case than removing all the characters of String except
* character 'a' to 'z' and digits 0 to 9.
*/
String s=str.toLowerCase().replaceAll("[^a-z0-9]", ""); //
StringBuilder sb=new StringBuilder(s);//saving String to SrtingBuilder because String Builder id mutable
sb.reverse();//reverse the String using reverse() method of String Builder class.
String rev=sb.toString(); //again saving String builder to String Class.
//checking if reverse string and given string if true then return true else false
if(rev.equals(s))
return true;
else
return false;
}
//main method
public static void main(String[] args) {
//for your given input
System.out.println("isPalindrome("radar") : "+isPalindrome("radar"));
System.out.println("isPalindrome("pe e p") : "+isPalindrome("pe e p"));
System.out.println("isPalindrome("Was It A Rat I Saw?") : "+isPalindrome("Was It A Rat I Saw?"));
System.out.println("isPalindrome("12321") : "+isPalindrome("12321"));
System.out.println("isPalindrome("java") :"+isPalindrome("java"));
//to getting String from user
System.out.println("Enter the String : ");
Scanner sc=new Scanner(System.in);
String str=sc.nextLine();
System.out.println("isPalindrome(""+str+"") : "+isPalindrome(str));
sc.close();
}
}
-----------------------------------------------------------Output---------------------------------------------------------------------
isPalindrome("radar") : true
isPalindrome("pe e p") : true
isPalindrome("Was It A Rat I Saw?") : true
isPalindrome("12321") : true
isPalindrome("java") :false
Enter the String :
javaj
isPalindrome("javaj") : true
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.