This program determines whether the string the user has input is a palindrome or not.
import acm.program.ConsoleProgram;
public class PurePalindrome extends ConsoleProgram {
public void run() {
String originalString;
String reversedString;
boolean isPalindrome;
originalString = readLine("? ");
reversedString = reverseString(originalString);
isPalindrome = checkPalindrome(originalString, reversedString);
println("The word you entered " + determineWord(isPalindrome)
+ " a palindrome. " + originalString + " reversed is: "
+ reversedString + ".");
}
private boolean checkPalindrome(String word, String revWord) {
if (revWord.equals(word)) {
return true;
} else {
return false;
}
}
private String reverseString(String wordToReverse) {
String reversedWord = "";
for (int i = 0; i < wordToReverse.length(); i++) {
reversedWord = wordToReverse.charAt(i) + reversedWord;
}
return reversedWord;
}
private String determineWord(boolean palindrome) {
if (palindrome) {
return "is";
} else {
return "is not";
}
}
}
Would all these methods be considered pure functions? If not, why not? I'm having a bit of trouble determining whether a method is a pure function or not.