3

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.

zOrigin_
  • 59
  • 3
  • Was it part of the assignment that you have to use classes? If yes, he probably wanted you to put the main-method in one class and the isPalindrome-method in another one. –  Jun 05 '20 at 02:56
  • I assume he wanted you to think a little bit more abstractly, in case something changed, or in case something extra was required. This is a simple example for which maybe he wanted `WordChecker` (word input and calls validators) and `PalindromValidator` but what if down the road you wanted `ContractionValidator`. – kendavidson Jun 05 '20 at 02:56
  • Functional decomposition is basically breaking down the program into a number of discrete steps for the program to achieve its endgoal. Each of those steps could be encapsulated in a function, something that takes an input that returns an output according to its functionality/purpose. – de_classified Jun 05 '20 at 02:58
  • @vulpini99 He didnt say in the assignment description. Just to make a boolean method that returns true or false given a word or phrase. – zOrigin_ Jun 05 '20 at 03:07
  • This is a question more suited for [codereview](https://codereview.stackexchange.com/) – smac89 Jun 05 '20 at 03:21
  • Useful reference as a starting point https://en.wikipedia.org/wiki/Decomposition_(computer_science) – Kevin Hooke Jun 05 '20 at 03:31
  • This question has been asked and answered several times already on SO, including [Functional Decomposition](https://stackoverflow.com/questions/20050553/functional-decomposition) and [What is Functional Decomposition?](https://stackoverflow.com/questions/947874/what-is-functional-decomposition) and [Decomposition in java, when is enough enough?](https://stackoverflow.com/questions/3493024/decomposition-in-java-when-is-enough-enough). Did you do any research before posting your question? Did you find the questions I have linked to? If you did, why did they not answer your question? – Abra Jun 05 '20 at 03:38

1 Answers1

0

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.

Evan Wunder
  • 131
  • 3