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

esponses to a survey in a file so you can analyze the survey yourself I\'ve give

ID: 3598979 • Letter: E

Question

esponses to a survey in a file so you can analyze the survey yourself I've given a survey as follows: Computer science is the greatest discipline on earth. Choose a response from below: Neutral Disagree Not applicable: I've stored the responses in a file. Write a program that calculates the number of Agree, Neutral, and Disagree responses and lets a user explore which Not applicable: responses contain a given word or words until the user wishes to quit. Th expected interaction of the program is shown below on the left. The sample file contents are in the box on the right. The bold, underlined, italicized words were entered by the user Agree: 6 Neutral: 1 Disagree: 5 Enter a keyword to search not applicable or quit Uncertain That keyword was listed 2 times Enter a keyword to search not applicable or quit not sure That keyword was listed 0 times Enter a keyword to search not applicable or quit quit Agzee Dásagree Disagree Seutral Not applicable: Uncertain Not applicable: Who knows? Sot applicable: Uncertain Bot applicable: Don't care Not applicable: Sot appiicable: bisagree You will write the two methods below and part of the main method. publie static int countOtherKeywords( Stringl] data, int dataSize, String answer) public static int readFile(Stringl data, String fileName) There also is another method that you may use without writing: public static int countResponses(Stringl] data, int dataSize, String answer) This method returns the number of times that answer appears in the data array at indices between 0 and dataSize-1 (inclusive).

Explanation / Answer

package comm;

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.StringTokenizer;
import java.util.Map.Entry;

public class SameWordCount {

public Map<String, Integer> getWordCount(String fileName){

FileInputStream fis = null;
DataInputStream dis = null;
BufferedReader br = null;
Map<String, Integer> wordMap = new HashMap<String, Integer>();
try {
fis = new FileInputStream(fileName);
dis = new DataInputStream(fis);
br = new BufferedReader(new InputStreamReader(dis));
String line = null;
while((line = br.readLine()) != null){
StringTokenizer st = new StringTokenizer(line, " ");
while(st.hasMoreTokens()){
String tmp = st.nextToken().toLowerCase();
if(wordMap.containsKey(tmp)){
wordMap.put(tmp, wordMap.get(tmp)+1);
} else {
wordMap.put(tmp, 1);
}
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally{
try{if(br != null) br.close();}catch(Exception ex){}
}
return wordMap;
}

public List<Entry<String, Integer>> sortByValue(Map<String, Integer> wordMap){

Set<Entry<String, Integer>> set = wordMap.entrySet();
List<Entry<String, Integer>> list = new ArrayList<Entry<String, Integer>>(set);
Collections.sort( list, new Comparator<Map.Entry<String, Integer>>()
{
public int compare( Map.Entry<String, Integer> o1, Map.Entry<String, Integer> o2 )
{
return (o2.getValue()).compareTo( o1.getValue() );
}
} );
return list;
}

public static void main(String a[]){
MaxDuplicateWordCount mdc = new MaxDuplicateWordCount();
Map<String, Integer> wordMap = mdc.getWordCount("C:/Users/Tejuu/Desktop/kmtomiles/MyTestFile.txt");
List<Entry<String, Integer>> list = mdc.sortByValue(wordMap);
for(Map.Entry<String, Integer> entry:list){
System.out.println(entry.getKey()+" ==== "+entry.getValue());
}
}
}

input:

SampleText.txt

hai hello hai hello hai the hai hello the the hai

out put:

hai ====5

hello===3

the ====3