Question: Received help on this example java program but answerer did not include a main program. Can someone create the main program using the answerer's program?

Received help on this example java program but answerer did not include a main program. Can someone create the main program using the answerer's program?

Example instructions are below, as well as the answerer's code.

Ex) Create a PhraseAnalyzer program using isValidPhrase(), getWords(), getSmallestWords(), getLargestWords(), getAverageWordLength(), and getLetterTally() methods. The program should prompt the user to insert a phrase and the program should produce a list including the smallest word used, largest word used, average word length, least frequently used letters, and most frequently used letter. The phrase must also be valid, meaning characters that are not letters are not valid but letters that do not form a word can still count as a word. Ex: asdhj is considered valid. 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.

import java.util.*;

public class PhraseAnalyzer {

private String phrase;

private String[] words;

private boolean isValid;

public PhraseAnalyzer(String phrase) {

this.phrase = phrase;

this.words = getWords();

this.isValid = isValidPhrase();

}

private String[] getWords() {

// split the phrase into words using whitespace as a delimiter

return phrase.split("\\s+");

}

private boolean isValidPhrase() {

for (String word : words) {

// check if the word contains only letters and ends with a punctuation mark

if (!word.matches("[a-zA-Z]+[.,;:?!]*$")) {

System.out.println("Phrase is not valid. Terminating.");

System.exit(1);

}

}

return words.length > 0;

}

public String[] getWordsArray() {

return words;

}

public String[] getSmallestWords() {

if (!isValid) {

System.out.println("Phrase is not valid. Terminating.");

System.exit(1);

}

Arrays.sort(words, Comparator.comparing(String::length));

return Arrays.copyOfRange(words, 0, 5);

}

public String[] getLargestWords() {

if (!isValid) {

System.out.println("Phrase is not valid. Terminating.");

System.exit(1);

}

Arrays.sort(words, Comparator.comparing(String::length).reversed());

return Arrays.copyOfRange(words, 0, 5);

}

public double getAverageWordLength() {

if (!isValid) {

System.out.println("Phrase is not valid. Terminating.");

System.exit(1);

}

int totalLength = 0;

for (String word : words) {

totalLength += word.length();

}

return (double) totalLength / words.length;

}

public Map getLetterCounts() {

if (!isValid) {

System.out.println("Phrase is not valid. Terminating.");

System.exit(1);

}

Map letterCounts = new HashMap<>();

for (String word : words) {

for (char c : word.toCharArray()) {

if (Character.isLetter(c)) {

letterCounts.put(c, letterCounts.getOrDefault(c, 0) + 1);

}

}

}

return letterCounts;

}

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!