Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

It is almost complete, and will compile as-is, but is missing the body for a method called fillMap(). This assignment is to implement and test

It is almost complete, and will compile as-is, but is missing the body for a method called fillMap(). This assignment is to implement and test your own version of fillMap(). Comments throughout the code explain what the program does and what individual parts of the program do. Just above fillMap() is pseudocode explaining not just what it does, but how.
 import java.io.FileInputStream; import java.io.FileNotFoundException; import java.util.HashMap; import java.util.LinkedList; import java.util.Map; import java.util.Scanner; import java.util.TreeMap; // The example input data is Les Miserables by Victor Hugo translated by // Isabel F. Hapgood, and was obtained from Project Gutenburg at // http://www.gutenberg.org/cache/epub/135/pg135.txt public class WordOccurrenceCounter { private LinkedList words; private Map theMap; private Scanner in; private Scanner userInput; // Map above indicates that theMap will be a // structure for looking up integers associated with strings. If you // have used databases, think of theMap as a database table where keys // are strings and the data associated with each key is an integer // count of the number of times each particular string occurs in the // input file. A few HashMap methods that you will see illustrated // below or find useful in the code you write are: // // boolean theMap.containsKey(word) returns true if word is a key in // the map, and false otherwise. // // Integer theMap.get(word) returns the integer count associated with // the word. Note that Java automatically converts between int and // Integer, so we can ignore the distinction between the two types. We // use integer because container classes contain only reference types, // not primitive types. // // void theMap.put(word, count) inserts a word and an associated count // into the map. The keys are treated as a set, so if a key is // inserted into a map multiple times, only the most recent count is // kept--others are discarded. public WordOccurrenceCounter(String fileName) throws FileNotFoundException { words = new LinkedList(); in = new Scanner(new FileInputStream(fileName)); userInput = new Scanner(System.in); } // constructor // Read the words from a file into a linked list. private void read() { while (in.hasNext()) words.add(in.next()); in.close(); } // read() // Fill the desired map, and allow the user to make queries into the // map. private void interact() { fillMap(); String word = queryUser(); while (!word.equals("q")) { if (theMap.containsKey(word)) System.out.println(word + " occurred " + theMap.get(word) + " times."); else System.out.println(word + " occurred 0 times."); // Get the next word. word = queryUser(); } // while() } // interact() // Ask the user for a word to search for. Remind the user that a 'q' // will be interpreted as a request to quit. public String queryUser() { System.out.print("Enter a string ('q' to quit): "); return userInput.next(); } // queryUser() // Transfer words from the linked list to the map. Each map entry // consists of a key and a value. In this application, each key is a // word from the book. The count associated with a key is the number // of times that word has occurred in the list so far. // // Process repeated for each word in the linked list: // // Is the word already in the map? // // No? Then insert the word into the map with a count of 1, since this // is the first time it has been seen. // // Yes? Then get the count already associated with the word. Increment // the count. Place the new count into the map. // private void fillMap() { // Student code goes here. /////////////////// } // fillMap() // Place all the words in the book into a linked list. Then copy that // linked list into a TreeMap. Allow the user to interact with the // words counts in the TreeMap. Then repeat the process with a // HashMap. public void run() { System.out.println("Reading the book..."); read(); theMap = new TreeMap(); System.out.println("Getting counts from a TreeMap..."); interact(); theMap = new HashMap(); System.out.println(" Getting counts from a HashMap..."); interact(); } // run() public static void main(String[] args) throws FileNotFoundException { WordOccurrenceCounter woc = new WordOccurrenceCounter("pg135.txt"); woc.run(); } // main() } // class WordOccurrenceCounter 

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions