Question: The class must be implemented to provide the following functionality: addWord: The parameter represents a case-sensitive word that has been found in a document. Because

The class must be implemented to provide the following functionality:

  • addWord: The parameter represents a case-sensitive word that has been found in a document. Because an instance must keep track of how many times each word has been added, the count of occurrences for that word must be increased by 1.

    You may assume that all strings passed to this method will consist only of one or more alphabetic characters and there is no need to check that this is the case. In other words, all parameters will be valid words.

  • howMany: The parameter represents a case-sensitive word that has been found in a document. The number of times that the exact word has been passed to the addWord method must be returned.

  • suffixCount: This method takes a single parameter and it must return the number of distinct words added so far to the processor that the parameter is a suffix of. It counts as a suffix if a word in the processor ends with the parameter but does not also exactly match the parameter. For instance: parameter "thing" matches all of: "something", "nothing" and "thing" but only the first two qualify as suffix matches. The count must represent distinct words, so each added word that contains the suffix must only be counted once. For instance, if "something" has been added 3 times, that only counts as 1 match for something.

  • anagramCount: [challenging] This method takes a single parameter and it must return the number distinct case-insensitive anagrams of the given word have been added to the processor. An anagram consists of exactly the same number of letters as the given word with each character occurring the same number of times. However, identical words are not considered to be anagrams. For instance: "and" is an anagram of "dan" but "nan" is neither an anagram of "naan" nor of nan.

    Note that because the match must be case-insensitive, Dan is also an anagram of and. However, because the match is case-insensitive, all of Dan, dan and DAN only count as a single match for any of and, AND and And. Also each anagram is only counted once, regardless of how many times it has been added to the processor via addWord.

public class TextProcessorMain { public static void main(String[] args) throws Exception { // Set up the document to be processed. String document; if(args.length == 1) { File text = new File(args[0]); document = Files.lines(text.toPath()).collect(Collectors.joining(" ")).trim(); } else { document = "Dan saw it was time to take the risk and visit his Nan." + " " + "The thing is, there was nothing to be lost," + " " + "because something was bound to happen."; } // Extract the individual words. String[] words = document.split("[ ,.;:?]+"); System.out.println(words.length + " words to analyse."); TextProcessor processor = new TextProcessor(); for(String word : words) { processor.addWord(word); } // Check some results. for(String word : List.of("nothing", "to", "Friday", "and", "the")) { System.out.println(word); System.out.println(" how many: " + processor.howMany(word)); System.out.println(" suffixes: " + processor.suffixCount(word)); System.out.println(" anagrams: " + processor.anagramCount(word)); } } }

Here is the main code!

public class TextProcessor { public TextProcessor() { } public void addWord(String word) { } public int howMany(String word) { return 0; } public int suffixCount(String str) { return 0; } public int anagramCount(String word) { return 0; } }

I have tried to code this but every time I call the howmany class it always return zero and if I call any other class it return zero please help me solve this

You can use HashMaps when implementing.

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!