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

(8 points) In this question, we are going to write a Scheme program that calcula

ID: 3863109 • Letter: #

Question

(8 points) In this question, we are going to write a Scheme program that calculates word counts. The input is a list of words and the output should be counts for words that appear in the input list. For example, if the input is time is long but life is short) Then one output can be CC Short 1) Cis 2) Clife 1) ut 1) Clong 1) time 1) Note that you are not asked to sort the words in the output. Therefore, the output is correct as long as the counts are correct. Do the following steps to implement the word-count program. In our discussion, we call a word with a count a word-count pair, for example, (short 1) and Cis 2) are word-count pairs. We call a list of word- count pairs a word-count list. (a) (2 points) Write a function initia List that takes a list of words and creates a word-count list. The resulting word-count list should have the word count 1 for every word. Use the map function we discussed in class to implement initia WCList. For instance,

Explanation / Answer

//save file as Lab.java, then compile javac Lab.java then run java Lab

import java.util.*;

class Lab {
   public static void main(String[] args) {
       Scanner sc = new Scanner(System.in);
       System.out.println("Enter a string: ");
       String str3 = sc.nextLine();
       String str = str3 + " ";
       Map<String, Integer> map = new LinkedHashMap<>();
       String str1 = "";
       char ch;

       for (int i = 0; i < str.length(); i++) {

           ch = str.charAt(i);
           if (((str.charAt(i) == ' ') || str.charAt(i) == 10 || str.charAt(i) == 13) != true) {
               str1 = str1 + ch;

           }

           else {
               if (map.containsKey(str1)) {
                   map.put(str1, map.get(str1) + 1);
               } else {
                   map.put(str1, 1);
               }
               str1 = "";
           }

       }

       Set data=map.entrySet();
       Iterator it=data.iterator();
       while(it.hasNext()){
           Object obj=it.next();
           Map.Entry entry=(Map.Entry)obj;
           Object key=entry.getKey();
           Object val=entry.getValue();
           System.out.print(" ( " +key+ " " + val + " ) " );
       }

   }
}

/* your output is like

D:>javac Lab.java

D:>java Lab
Enter a string:
hi kk j kk jj kk hh
(hi 1)(kk 3)(j 1)(jj 1)(hh 1)
D:>

*/