Question: Could use some more help on this example program. The instructions in the example include if the phrase is not valid in the isValidPhrase(), the
Could use some more help on this example program. The instructions in the example include if the phrase is not valid in the isValidPhrase(), the code should terminate itself. A valid phrase can only include letters/words, whereas an invalid phrase would include be numbers and punctuation marks besides punctuation at the end of the sentence. Can someone write the code so that it matches the example's instructions?
Below is my code
| import java.util.*; | |
| /** | |
| * The PhraseAnalyzer class breaks down phrases and gives data about them. | |
| */ | |
| public class PhraseAnalyzer { | |
| private String phrase; | |
| /** | |
| * Builds a PhraseAnalyzer object with the given expression. | |
| * | |
| * @param phrase the expression to be broke down | |
| */ | |
| public PhraseAnalyzer(String phrase) { | |
| this.phrase = phrase; | |
| } | |
| /** | |
| * Checks whether the expression is legitimate. | |
| * | |
| * @return true if the expression is valid, false otherwise | |
| */ | |
| public boolean isValidPhrase() { | |
| return phrase != null && !phrase.isEmpty(); | |
| } | |
| /** | |
| * Returns an array of words in the expression. | |
| * | |
| * @return an array of words in the expression | |
| */ | |
| public String[] getWords() { | |
| return phrase.split("\\s+"); | |
| } | |
| /** | |
| * Returns the smallest words in the expression. | |
| * | |
| * @return the smallest words in the expression | |
| */ | |
| public String[] getSmallestWords() { | |
| List | |
| int smallestLength = Collections.min(words, Comparator.comparing(String::length)).length(); | |
| return words.stream().filter(word -> word.length() == smallestLength).toArray(String[]::new); | |
| } | |
| /** | |
| * Returns the largest words in the expression. | |
| * | |
| * @return the largest words in the expression | |
| */ | |
| public String[] getLargestWords() { | |
| List | |
| int largestLength = Collections.max(words, Comparator.comparing(String::length)).length(); | |
| return words.stream().filter(word -> word.length() == largestLength).toArray(String[]::new); | |
| } | |
| /** | |
| * Returns the average word length in the expression. | |
| * | |
| * @return the average word length in the expression | |
| */ | |
| public double getAverageWordLength() { | |
| String[] words = getWords(); | |
| int totalLength = Arrays.stream(words).mapToInt(String::length).sum(); | |
| return (double) totalLength / words.length; | |
| } | |
| /** | |
| * Returns a map of letter counts in the expression. | |
| * | |
| * @return a map of letter counts in the expression | |
| */ | |
| public Map | |
| Map | |
| for (char c : phrase.toCharArray()) { | |
| if (Character.isLetter(c)) { | |
| letterCounts.put(c, letterCounts.getOrDefault(c, 0) + 1); | |
| } | |
| } | |
| return letterCounts; | |
| } | |
| public static void main(String[] args) { | |
| Scanner scanner = new Scanner(System.in); | |
| System.out.print("Enter an expression: "); | |
| String phrase = scanner.nextLine(); | |
| PhraseAnalyzer analyzer = new PhraseAnalyzer(phrase); | |
| if (analyzer.isValidPhrase()) { | |
| String[] words = analyzer.getWords(); | |
| String[] smallestWords = analyzer.getSmallestWords(); | |
| String[] largestWords = analyzer.getLargestWords(); | |
| double averageWordLength = analyzer.getAverageWordLength(); | |
| Map | |
| System.out.println("Words: " + Arrays.toString(words)); | |
| System.out.println("Smallest words: " + Arrays.toString(smallestWords)); | |
| System.out.println("Largest words: " + Arrays.toString(largestWords)); | |
| System.out.println("Average word length: " + averageWordLength); | |
| System.out.println("Letter count: " + letterTally); | |
| } else { | |
| System.out.println("Invalid expression"); | |
| } | |
| } | |
| } |
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
