public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
String x = input.next();
/* ------------------------------------------
Algorithm to check if x is a palindrome
------------------------------------------ */
boolean isPalindrome = true;
int i, last;
last = x.length() - 1;
for ( i = 0; i < x.length()/2; i++ )
{
if ( x.charAt( i ) != x.charAt( last-i ) )
{
isPalindrome = false;
break; // We can exit the loop now
}
}
System.out.println(isPalindrome);
}
|
public class Tools
{
public static boolean isPalindrome(String s)
{
...
}
}
|
In another class (e.g.: MyProg), write a main( ) method that reads in a word and calls the isPalindrome( ) in the Tools class to prints out whether the input word is a palindrome.
public class Tools
{
public static void main(String[] args)
{
// Reads in a word and check if it is a palindrome
// Must use the isPalindrome( ) methid in the Tools class
}
}
|