Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Appropriately coded for a second quarter Java Course: There is some debate on in

ID: 3869192 • Letter: A

Question

Appropriately coded for a second quarter Java Course:

There is some debate on influence of Jane Austen on Charlotte Bronte work as a writer (and in general on all three Bronte sisters'). If you are interested in finding more, feel free to google for their works and the debate. For this exercise, using the publicly available books on Project Gutenberg (http: //www.gutenberg.org), you are asked to find the top 10 words and number of times they occur in books by Charlotte Bronte but not used by Jane Austen. To simply this exercise, we will only use the following books: HashMap (or HashTree) Java Collection will come handy for finding and keeping the count of words. You are only allowed to use Java Collections as described in Chapter 11 of our class textbook. To receive full credit, submit the following: Java source code file(s) Nicely formatted report containing a table of top 10 words found in Charlotte Bronte's books and not in Jane Austen's and their counts, and a summary of your insights from this exercise including how this type of analysis can be useful and any suggestions for improvements. Don't submit the text files for the books!

Explanation / Answer

import java.io.BufferedReader; import java.io.FileReader; import java.util.ArrayList; import java.util.Collections; import java.util.HashMap; public class HashMapEx { public static void main(String[] args) { // Creating new HashMap objects // keys are String, values are Integer HashMap wordcount = new HashMap(); try { // Opening file // change "/Users/anyexample/input.txt" to path to your test file BufferedReader in = new BufferedReader(new FileReader( "/Users/andrian/input.txt")); // string buffer for file reading String str; // reading line by line from file while ((str = in.readLine()) != null) { str = str.toLowerCase(); // convert to lower case // starting index, we'll use this to copy words from string int idx1 = -1; // process each characters for (int i = 0; i 1) { // copy word from input string buffer to new variable // from previous non-letter symbol // to current symbol which is also non-letter // if this is a letter(than it is last character in the line // and we should copy it to word) if (Character.isLetter(str.charAt(i))) i++; // copying... String word = str.substring(idx1 + 1, i); // Check if word is in HashMap if (wordcount.containsKey(word)) { // get number of occurrences for this word // increment it // and put back again wordcount.put(word, wordcount.get(word) + 1); } else { // this is first time we see this word, set value '1' wordcount.put(word, 1); } } // remember current position as last non-letter symbol idx1 = i; } } } // Close buffered reader in.close(); } catch (Exception e) { // If something unexpected happened // print exception information and quit e.printStackTrace(); System.exit(1); } // This code sorts outputs HashMap sorting it by values // First we're getting values array ArrayList values = new ArrayList(); values.addAll(wordcount.values()); // and sorting it (in reverse order) Collections.sort(values, Collections.reverseOrder()); int last_i = -1; // Now, for each value for (Integer i : values) { if (last_i == i) // without dublicates continue; last_i = i; // we print all hash keys for (String s : wordcount.keySet()) { if (wordcount.get(s) == i) // which have this value System.out.println(s + ":" + i); } // pretty inefficient, but works } } }
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote