功能分解 Java

3

我的教授评估了我的作业并说它需要进行称为“功能分解”的操作。这是什么意思,以及在我检查回文程序时它应该是什么样子?

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;
    }
}

顺便说一下,他说我需要两个Java文件。如果我只是要创建一个方法,第二个Java文件会是什么呢?像制作类、构造函数、继承等我通常使用单独的文件。对于这样一个小问题,我肯定不需要制作两个文件吧?我无论如何已经在电子邮件中问过他,但如果你不知道也没关系。谢谢你抽出时间。


这个作业要求你使用类吗?如果是的话,他可能希望你将主方法放在一个类中,将isPalindrome方法放在另一个类中。 - user13634030
我认为他希望你能够更抽象地思考,以防出现变化或需要额外的东西。这是一个简单的例子,也许他想要 WordChecker(单词输入和调用验证器)和 PalindromValidator,但如果在未来你需要 ContractionValidator 呢? - kendavidson
功能分解基本上是将程序分解为多个离散步骤,以实现其最终目标。其中每个步骤都可以封装在一个函数中,该函数接受输入并根据其功能/目的返回输出。 - de_classified
作为起点的有用参考资料 https://en.wikipedia.org/wiki/Decomposition_(computer_science) - Kevin Hooke
这个问题已经在SO上被问过并回答了多次,包括功能分解什么是功能分解?Java中的分解,何时足够?。在发帖之前,你有做过任何研究吗?你找到我提供的链接了吗?如果找到了,为什么它们不能回答你的问题? - Abra
显示剩余2条评论
1个回答

0

你的教授只是希望你更好地模块化你的代码,使其更有结构性和可重用性。听起来他还想在另一个文件中创建一个单独的类来处理你的文本。

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));
}

注意程序的所有逻辑已经移出了主方法。

将算法分解为多个可重用函数是一个好的实践,当你参加编程面试时,它可以向面试官展示你可以将问题分解为子问题并编写模块化代码。如果需要进行任何更改、替换方法或优化解决方案,这也会有所帮助。

在大型项目中,这将使您的代码更易于阅读、修改和调试,从而产生更好的代码和更高的效率。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接