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

I need some help with this in java.... ( should be a hashmap) thanks! 1. A funct

ID: 664012 • Letter: I

Question

I need some help with this in java.... ( should be a hashmap)

thanks!

1. A function called freqDistribution with two parameters: infile and distfile. The first parameter, infile, is the name of the file for which we want to compute the frequency distribution. The second parameter, distfile is the name of the file into which the frequency distribution is to be written in sorted order of the characters. That is, the characters should be listed in alphabetically sorted order in the output file. Each line of the file should contain the character and its frequency. Use string formatting to make sure the output is neatly formatted. In order to carry out the above task, you must first create a dictionary to store the frequency distribution. You will do this by calling the helper function freqDictionary described in #(3) below. Remember to close distfile when you are done writing into it.

2. A function called orderedFreqDistribution with two parameters: infile and orderedDistfile. The first parameter, infile, is the name of the file for which we want to compute the frequency distribution. The second parameter, orderedDistfile is the name of the file into which the frequency distribution is to be written in sorted order of the frequency. That is, the characters should be printed into the file in decreasing order of frequency. If several characters have the same frequency, they should be printed in alphabetical order. Use string formatting to make sure that the output in orderedDistfile is neatly formatted. Once again, in order to carry out this task, you must first create a dictionary to store the frequency distribution by calling the helper function freqDictionary 2 described in #(3) below. Remember to close ordered distfile when you are done writing into it.

3. A function called freqDictionary with a single parameter infile, the name of the text file for which we want to compute the frequency distribution. The function should first open infile for reading. It should then create a dictionary with key:value pairs, where the key is a character occurring in infile and its associated value is the frequency of that character in the file (i.e., the number of times it occurs in the file). Note that every non-whitespace character (characters other than newline, space, and tab) occurring in the file should be included in the dictionary. Upper and lower case letters should be kept distinct. Characters that do not occur in the file should not be included in the dictionary. The function must return the dictionary. Remember to close the file prior to the return statement.

Explanation / Answer

import java.io.*;
import java.util.*;

class run{
   public static Map<Character, Integer> sortByComparator(Map<Character, Integer> unsortMap) {
        List<Map.Entry<Character, Integer>> list =
           new LinkedList<Map.Entry<Character, Integer>>(unsortMap.entrySet());

       Collections.sort(list, new Comparator<Map.Entry<Character, Integer>>() {
           public int compare(Map.Entry<Character, Integer> o1,
Map.Entry<Character, Integer> o2) {
               return (o1.getValue()).compareTo(o2.getValue());
           }
       });
       Map<Character, Integer> sortedMap = new LinkedHashMap<Character, Integer>();
       for (Iterator<Map.Entry<Character, Integer>> it = list.iterator(); it.hasNext();) {
           Map.Entry<Character, Integer> entry = it.next();
           sortedMap.put(entry.getKey(), entry.getValue());
       }
       return sortedMap;
   }
   public static Map<Character,Integer> freqDictionary(BufferedReader read) throws IOException{
       String line;
       Map<Character,Integer> hm = new HashMap<Character,Integer>();
       while ((line = read.readLine()) != null){
           String[] l = line.split("\s+");
           for (String s : l){
               for (int i = 0; i < s.length(); i++){
                   char ch = s.charAt(i);
                   if (hm.containsKey(ch) == true){
                       hm.put(ch,hm.get(ch)+1);
                   }
                   else{
                       hm.put(ch,1);  
                   }
               }
           }
       }
       return hm;
   }
   public static void orderedFreqDistribution(BufferedReader read,BufferedWriter wr) throws IOException{
       Scanner sc = new Scanner(System.in);
       System.out.print("Enter the file to read : ");
       String file = sc.nextLine();
       read = new BufferedReader(new FileReader(file));
       System.out.print("Enter the file to write : ");
       file = sc.nextLine();
       wr = new BufferedWriter(new FileWriter(file));
       Map<Character,Integer> hm = freqDictionary(read);
       Map<Character, Integer> map = sortByComparator(hm);
       for (Map.Entry<Character, Integer> entry : map.entrySet()) {
           wr.write("Key : " + entry.getKey() + " Value : " + entry.getValue());
       }
       wr.close();
   }
   public static void freqDistribution(BufferedReader read,BufferedWriter wr) throws IOException{
       Scanner sc = new Scanner(System.in);
       System.out.print("Enter the file to read : ");
       String file = sc.nextLine();
       read = new BufferedReader(new FileReader(file));
       System.out.print("Enter the file to write : ");
       file = sc.nextLine();
       wr = new BufferedWriter(new FileWriter(file));
       Map<Character,Integer> hm = freqDictionary(read);
       Map<Character, Integer> map = new TreeMap<Character, Integer>(hm);
       for (Map.Entry<Character, Integer> entry : map.entrySet()) {
           wr.write("Key : " + entry.getKey() + " Value : " + entry.getValue());
       }
       wr.close();
   }
   public static void main(String[] args) throws IOException{
       BufferedReader read = null;
       BufferedWriter write = null;
       freqDistribution(read,write);
   }
}