Your method must be non-static and named \'count\'. Its return type must be \'Ha
ID: 3562789 • Letter: Y
Question
Your method must be non-static and named 'count'. Its return type must be
'HashMap<String,Integer>' and its parameter must be of type 'String'. Your
class must have a no-argument constructor.
A description of the required (public) method:
* @param a String denoting the inputPath the path on the local filesystem
* to the input file
* @returns a HashMap<String,Integer> containing the word->count mappings
*
* This method must read the contents of a file (identified by inputFilePath),
* one char at a time, segment the input into words, and keep track of word
* counts in a java.util.HashMap<String,Integer>.
*
* A word is defined as a contiguous sequence of characters that does not
* contain word separator characters, where word separator characters are:
* ' ' (space), ' ' (tab), ' ' (newline), ',' (comma) and '.' (period).
*
* Think carefully about this definition, and how it applies to an initial
* character sequence not preceded by a word separator, and also a final
* character sequence not followed by a word separator.
*
* You must use only CharacterFromFileReader to read characters from the input
* file. Use it as an Iterator<Character>, keeping in mind the autoboxing &
* unboxing feature of the Java language.
*
* In order to keep your code readable, break your code into several methods.
* Define meaningful private helper methods that you call from the required
* public method.
Explanation / Answer
import java.util.*; import java.lang.Iterable; public class WordGroup { String words; public static void main(String[] args) { WordGroup plato = new WordGroup( "You can discover more about a person in an hour of play than in a year of conversation"); WordGroup roosevelt = new WordGroup( "When you play play hard when you work dont play at all"); String[] platoArray = plato.getWordArray(); String[] rooseveltArray = roosevelt.getWordArray(); plato.printArray(platoArray); roosevelt.printArray(rooseveltArray); System.out.println("____________________________________________________"); HashSet rooseveltWordSet = roosevelt.getWordSet(platoArray); roosevelt.printArray(rooseveltWordSet); } public WordGroup(String word) { words = word.toLowerCase(); } protected String[] getWordArray() { String[] wordArray = words.split(" "); return wordArray; } protected HashSet getWordSet(String[] groupedWords) { HashSet wordSet = new HashSet(); for (String string : groupedWords) { wordSet.add(string); } String[] wordArray = getWordArray(); for (String string : wordArray) { wordSet.add(string); } return wordSet; } protected void printArray(String[] array) { for (String string : array) { System.out.println(string); } } protected void printArray(HashSet hashWords) { for(String hashset: hashWords) { System.out.println(hashset); } } protected void printArray(HashMap printHashMap) { for(String string: printHashMap) { System.out.println(string); } } protected HashMap getWordCounts() { String[] wordArray = getWordArray(); HashMap wordCounts = new HashMap(); for(String string: wordArray) { int wordCounts.put(string, +one); } return null; } }Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.