My professor graded my assignment and said that it needed something called functional decomposition. What is this and how would it look in this program i have for palindromes.
import java.util.Scanner;
public class Palindrome {
public static void main(String[] args) {
String word;
/**
* create scanner for input and ask user for phrase/word
*/
Scanner kb = new Scanner(System.in);
System.out.println("Enter a word to see if its a palindrome.");
word = kb.nextLine();
/**
* this just removes spaces. For a palindrome like "race car"
*/
String newWord = word.replace(" ", "");
/**
* this removes commas like in given lab examples
*/
String newWord1 = newWord.replace(",", "");
System.out.println(isPalindrome(newWord1));
}
/**
*
* @param word
* @return true or false
*/
public static boolean isPalindrome(String word) {
/**
* if the word is 1 or 2 characters long its automatically a palindrome
*/
if(word.length() == 0 || word.length() == 1) {
return true;
}
/**
* use recursion to keep checking the first and last characters of each substring until result
*/
if (word.charAt(0) == word.charAt(word.length() - 1)) {
return isPalindrome(word.substring(1, word.length() - 1));
}
return false;
}
}
As a quick side note, he said that i needed to have 2 java files for this. What would the second java file be for this if all i had to do was make a method. I use separate files for like making classes, constructors, inheritance, etc. Certainly i didn't need to make two files for something small like this? I asked him in an email anyway but so its fine if you don't know. Thank you for your time.