Functional Decomposition Java

Viewed 398

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.

1 Answers

Your professor just wanted you to better modularize your code to make it more structured and reusable. It sounds like he also wanted a separate class in another file to process your text.

Palindrome.java

import java.util.Scanner;

public class Palindrome {

    public static String readWord() {
        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();

        return word;

    }

    public static String parseWord (String word) {
        /**
         * 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(",", "");

        return newWord1;
    }

    /**
     * 
     * @param String word
     * @return true or false
     */
    public static boolean testPalindrome(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 testPalindrome(word.substring(1, word.length() - 1));
        }
        return false;
    }
}

Main.java

public class Main {

public static void main(String[] args) {
    String word = Palindrome.readWord();
    newWord = Palindrome.parseWord(word);
    System.out.println(Palindrome.testPalindrome(newWord));
}

Notice how all of the logic of the program has been moved outside of the main method.

Decomposing an algorithm into multiple reusable functions is good practice for when you have code interviews because it shows the interviewer that you can break problems down into subproblems and write modular code. It will also help if you need to make any changes, replace methods, or optimize your solution along the way.

In large projects, it will make your code easier to read, easier to modify, and easier to debug resulting in better code and better overall efficiency.

Related