Question: Analyze the following java class and determine the runtime complexity using Big O notation: I have written a java program that takes a text file,

Analyze the following java class and determine the runtime complexity using Big O notation:

I have written a java program that takes a text file, converts the words to tokens, and stores them in a hashmap. The program then adds the words to a Priority Queue which is used to sort the words lexically and by frequency. I have been asked to compute the complexity of my program, but it has proved harder than I thought. If someone could help me analyze the entire class and determine it's Big O, I would be very grateful. The code is as follows:

public class TextAnalyzer { //Global data structures  private ArrayList wordList; private Map wordMap; private PriorityQueue> wordQueue; //Constructor to read in file and parse string tokens into hashmap  public TextAnalyzer(File inputFile) { //try with resources  try (FileReader fileIn = new FileReader(inputFile); Scanner in = new Scanner(fileIn)) { in.useDelimiter("[^A-Za-z]+"); //use scanner to delimit and save string tokens into temp arrayList  wordList = new ArrayList<>(); while (in.hasNext()) { String token = in.next().toLowerCase(); wordList.add(token); } } catch (IOException e) { e.printStackTrace(); System.out.println("There was an error opening the file. FileNotFound exception."); } //add the words to the hashmap  addToMap(); }//end constructor  //utility method to add words from temp arrayList to hashmap  public void addToMap() { //create instance of hashmap using map interface  wordMap = new HashMap<>(); for (String words : wordList) { wordMap.merge(words, 1, (a, b) -> a + b); } } //utility method used to print frequency of words in descending order  public void freqSort() { wordQueue = new PriorityQueue<>(uniqueWordCount(), new CompareWordFreq()); wordQueue.addAll(wordMap.entrySet()); while (!wordQueue.isEmpty()) { System.out.println(wordQueue.poll()); } } //utility method used to print words in lexicographic order  public void lexSort() { wordQueue = new PriorityQueue<>(uniqueWordCount(), new compareWordLex()); wordQueue.addAll(wordMap.entrySet()); while (!wordQueue.isEmpty()) { System.out.println(wordQueue.poll()); } } //returns total non unique words in file  public int wordCount() { return wordList.size(); } //returns total unique words in hashmap  public int uniqueWordCount() { return wordMap.size(); } //inner custom comparitor class to compare map elements by frequency  private class CompareWordFreq implements Comparator> { @Override public int compare(Map.Entry o1, Map.Entry o2) { return o2.getValue().compareTo(o1.getValue()); } } //inner custom comparitor to compare map elements by lexicographical ordering  private class compareWordLex implements Comparator> { @Override public int compare(Map.Entry o1, Map.Entry o2) { return o1.getKey().compareTo(o2.getKey()); } } //main method for testing  public static void main(String[] args) { String filepath = args[0]; File fileToAnalyze = new File(filepath); TextAnalyzer analyzer = new TextAnalyzer(fileToAnalyze); System.out.println("Number of non-unique words found: " + analyzer.wordCount() + " "); System.out.println("Number of unique words found: " + analyzer.uniqueWordCount() + " "); analyzer.printMap(); System.out.println (" " + "Words sorted by word frequency: " + " "); analyzer.freqSort(); System.out.println (" " + "Words sorted in lexical order: " + " "); analyzer.lexSort(); }//end main }//end TextAnalyzer 

The main data structures used here are an ArrayList, a Hashmap, and a Priority Queue. I have included the main method to show how the program is being used. Thanks in advance.

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!