sparkyg5
03-06-2007, 03:14 AM
I am having trouble with this palindrome program. I am trying to write a program that generates palindromes from 100-300
For each number between first and last, my program has to do the following:
1. Convert the number to a string.
2. If the string is a palindrome:
3. Display an adequate message.
4. Find the square of the original number that turned out to be a palindrome.
5. Convert the square to yet another string
6. Test if this second string is a palindrome, too, and if so, display
an adequate message.
7. If the string is not a palindrome, do not display anything.
Here is the code:
/**
* This class tests if a given string is a palindrome.
* Palindromes are words, numbers, and phrases
* that can be read the same backwards as forwards.
*/
public class Palindrome {
/**
* Tests if its string parameter is a palindrome.
*/
public static boolean isPalindrome(String stringToTest) {
String workingCopy = removeJunk(stringToTest);
String reversedCopy = reverse(workingCopy);
return reversedCopy.equalsIgnoreCase(workingCopy);
}
/**
* Returns a reversed copy of a string with all non-alphanumeric
characters stripped.
*/
protected static String removeJunk(String string) {
int i, len = string.length();
StringBuffer dest = new StringBuffer(len);
char c;
for (i = (len - 1); i >= 0; i--) {
c = string.charAt(i);
if (Character.isLetterOrDigit(c)) {
dest.append(c);
}
}
return dest.toString();
}
/**
* Returns a reversed copy of a string.
*/
protected static String reverse(String string) {
StringBuffer sb = new StringBuffer(string);
return sb.reverse().toString();
}
/**
* Provides an illustration of how to test if a string is a palindrome.
*/
public static void main(String[] args) {
String string = "Madam, I'm Adam.";
System.out.println();
System.out.println("Testing whether the following "
+ "string is a palindrome:");
System.out.println(" " + string);
System.out.println();
if (isPalindrome(string)) {
System.out.println("It IS a palindrome!");
} else {
System.out.println("It is NOT a palindrome!");
}
System.out.println();
}
}
Any help or hints would be greatly appreciated!!!
For each number between first and last, my program has to do the following:
1. Convert the number to a string.
2. If the string is a palindrome:
3. Display an adequate message.
4. Find the square of the original number that turned out to be a palindrome.
5. Convert the square to yet another string
6. Test if this second string is a palindrome, too, and if so, display
an adequate message.
7. If the string is not a palindrome, do not display anything.
Here is the code:
/**
* This class tests if a given string is a palindrome.
* Palindromes are words, numbers, and phrases
* that can be read the same backwards as forwards.
*/
public class Palindrome {
/**
* Tests if its string parameter is a palindrome.
*/
public static boolean isPalindrome(String stringToTest) {
String workingCopy = removeJunk(stringToTest);
String reversedCopy = reverse(workingCopy);
return reversedCopy.equalsIgnoreCase(workingCopy);
}
/**
* Returns a reversed copy of a string with all non-alphanumeric
characters stripped.
*/
protected static String removeJunk(String string) {
int i, len = string.length();
StringBuffer dest = new StringBuffer(len);
char c;
for (i = (len - 1); i >= 0; i--) {
c = string.charAt(i);
if (Character.isLetterOrDigit(c)) {
dest.append(c);
}
}
return dest.toString();
}
/**
* Returns a reversed copy of a string.
*/
protected static String reverse(String string) {
StringBuffer sb = new StringBuffer(string);
return sb.reverse().toString();
}
/**
* Provides an illustration of how to test if a string is a palindrome.
*/
public static void main(String[] args) {
String string = "Madam, I'm Adam.";
System.out.println();
System.out.println("Testing whether the following "
+ "string is a palindrome:");
System.out.println(" " + string);
System.out.println();
if (isPalindrome(string)) {
System.out.println("It IS a palindrome!");
} else {
System.out.println("It is NOT a palindrome!");
}
System.out.println();
}
}
Any help or hints would be greatly appreciated!!!