IN JAVA Question 3 (15 points) - complete program Write a method named isPalindr
ID: 3761586 • Letter: I
Question
IN JAVA
Question 3 (15 points) - complete program Write a method named isPalindrome that: - takes as argument a string, S, - returns true if the string is a palindrome and false otherwise. - does NOT read any user input and does NOT print anything. Write the main method as well. In the main method do the following: - read a string from the user, - call the isPalindrorne method on that string, - after calling isPalindrome. based on the answer, print ''It is a palindrome.'' or ''It is NOT a palindrome.''.Explanation / Answer
/* package whatever; // don't place package name! */
import java.util.*;
import java.lang.*;
import java.io.*;
/* Name of the class has to be "Main" only if the class is public. */
class Palindrome
{
public static boolean isPalindrome(String s)
{
int len = s.length();
int i=0,j=len-1;
while(i<j)
{
if(s.charAt(i)!=s.charAt(j))
return false;
i++;
j--;
}
return true;
}
public static void main (String[] args) throws java.lang.Exception
{
// your code goes here
Scanner input = new Scanner(System.in);
System.out.println("Input a string : ");
String line = input.nextLine();
if(isPalindrome(line)==true)
System.out.println("It is a palindrome");
else
System.out.println("It is not a palindrome");
}
}