【问题标题】:Functional Decomposition Java功能分解Java
【发布时间】:2020-06-05 12:10:51
【问题描述】:

我的教授给我的作业打了分,并说它需要一种叫做功能分解的东西。这是什么,它在我的回文程序中看起来如何。

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

作为一个简短的旁注,他说我需要为此准备 2 个 java 文件。如果我所要做的就是创建一个方法,那么第二个 java 文件会是什么。我使用单独的文件来制作类、构造函数、继承等。当然我不需要为这样的小东西制作两个文件?无论如何,我在电子邮件中问过他,但如果你不知道也没关系。感谢您的时间。

【问题讨论】:

  • 你必须使用课程是作业的一部分吗?如果是,他可能希望您将 main-method 放在一个类中,将 isPalindrome-method 放在另一个类中。
  • 我猜他想让你思考得更抽象一点,以防万一发生了变化,或者需要额外的东西。这是一个简单的例子,他可能想要WordChecker(单词输入和调用验证器)和PalindromValidator,但如果你想要ContractionValidator
  • 功能分解基本上是将程序分解为若干离散步骤,以使程序实现其最终目标。这些步骤中的每一个都可以封装在一个函数中,该函数接受一个输入,然后根据其功能/目的返回一个输出。
  • @vulpini99 他没有在任务描述中说。只是为了在给定单词或短语的情况下创建一个返回 true 或 false 的布尔方法。
  • 这个问题更适合codereview

标签: java


【解决方案1】:

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

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

注意程序的所有逻辑是如何移到主方法之外的。

将算法分解为多个可重用的函数是进行代码面试时的好习惯,因为它向面试官展示了您可以将问题分解为子问题并编写模块化代码。如果您需要在此过程中进行任何更改、替换方法或优化您的解决方案,它也会有所帮助。

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

【讨论】:

    猜你喜欢
    • 2013-12-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多