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

Java Problem. I need it in 2 hours. Overview These exercises will allow you to h

ID: 674522 • Letter: J

Question

Java Problem. I need it in 2 hours.

Overview

These exercises will allow you to have some practice with the ideas behind the set data structure, as well as the idea of an iterator. They will also let you have some more practice with the idea of Object-oriented programming.

Objectives

Practice with programming fundamentals

Review of various Java fundamentals (branching, loops, variables, methods, etc.)

Practice with Java classes and Object Oriented Programming

Practice with the map data structure

Exercise 1 Description

In this exercise you will be examining the behavior of the Map data structure.  FOR ALL EXERCISES: Make sure you place a comment block at the top of the code with your name and your partner's name.

MapTest.java

words.txt

wizardOfOz.txt

warAndPeace.txt

Create a ClosedLab08 project folder. Then import the above files into that ClosedLab08 project folder. As a reminder, you can do this by selecting File -> Import, then expanding the General folder and selecting "File System". Then browse to the directory where you saved the two files above. (More detailed instructions can be found in the CSE 1223 Closed Lab 01 assignment). MapTest.java should be imported into your src folder, while the three text files should be imported into your main ClosedLab08 folder.

Make sure that you have imported MapTest properly. Look over the code in MapTest - trace through it and see if you can tell what it does. Run the code and provide the file "words.txt" to it and see what it does. Do the same for wizardOfOz.txt and warAndPeace.txt.

This code stores the words from a file into a single List, and is the same as SetTest2.java from last week. Note the stub method getWordCounts. Fill in the code for this method so that it builds a Map that contains word/count pairs - when the code is finished, each word should be a key in the map associated with a count of the number of times that word occurs in the file. Modify the main loop so that it iterates over the keys in the Map and displays the counts of each word in the file.

The stub in getWordCounts uses a HashMap.   Notice the ordering of the elements displayed to the screen. Now modify getWordCounts to use a TreeMap. How are the results from using a HashMap and TreeMap different? Write your answer in the comments at the top of the program.

MapTest.java:

words.txt

wizardOfOz.txt:

http://web.cse.ohio-state.edu/cse2123/currentsem/closedlabs/ClosedLab07.code/wizardOfOz.txt

warAndPeace.txt

http://web.cse.ohio-state.edu/cse2123/currentsem/closedlabs/ClosedLab07.code/warAndPeace.txt

Explanation / Answer

I have answered this question under three points. Please find them below, & let me know in case of any queries.

(1). /* Function with HashMap */

   public static Map<String,Integer> getWordCounts(String fname) {
           Scanner inFile = new Scanner(new File(fname));
inFile.useDelimiter("[^A-Za-z]");
   Map<String,Integer> tokenCounts = new HashMap<String,Integer>();
  
while (inFile.hasNext()) {
String token = inFile.next();
               if(tokenCounts.get(token)!=NULL && !token.equals(""))
               {
                   tokenCounts.put(token, tokenCounts.get(token) + 1);
               }
else if (!token.equals("")) {
tokenCounts.put(token,1);
}
}
inFile.close();

           for (String name: tokenCounts.keySet()){
               String key =name;
               int value = tokenCounts.get(name);
               System.out.println(key + " " + value);
           }

return tokenCounts;
}

(2). /* Function with TreeMap */

   public static Map<String,Integer> getWordCounts(String fname) {
           Scanner inFile = new Scanner(new File(fname));
inFile.useDelimiter("[^A-Za-z]");
   TreeMap<String,Integer> tokenCounts = new TreeMap<String,Integer>();
  
while (inFile.hasNext()) {
String token = inFile.next();
               if(tokenCounts.get(token)!=NULL && !token.equals(""))
               {
                   tokenCounts.put(token, tokenCounts.get(token) + 1);
               }
else if (!token.equals("")) {
tokenCounts.put(token,1);
}
}
inFile.close();

           for (String name: tokenCounts.keySet()){
               String key =name;
               int value = tokenCounts.get(name);
               System.out.println(key + " " + value);
           }

return tokenCounts;
}

(3). Result observations

The output from the TreeMap were displayed in Ascending Order of `Key`.
While the output from the HashMap were displayed in the order in which `key` are stored.

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