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

PART 1 Design and implement a program called “WordHistogram.java” that creates a

ID: 3820102 • Letter: P

Question

PART 1

Design and implement a program called “WordHistogram.java” that creates a histogram that allows you to visually inspect the frequency distribution of a set of word in a given file. The program should read the input filename and output filename as command line arguments. A word is defined as collection of letters a-z and A-Z

For example if the input file is:

How much wood would a woodchuck chuck

If a woodchuck could chuck wood?

He would chuck, he would, as much as he could,

And chuck as much wood as a woodchuck would

If a woodchuck could chuck wood.

The output file will contain:

              a :    4
           and :      1
             as :      4
        chuck :    5
         could :    3
             he :      3
           how :     1
               if :     2
         much :    3
         wood :    4
woodchuck :    4
        would :    4

Hint:

While(inputFile.hasNext())

{

              Read a line

              add “ ” to end of line

            create a StringBuffer

              append the line to the buffer replacing all non alphabetical characters with “ ”
       // String s1 = s.replaceAll("[^a-zA-Z]+"," ").toLowerCase();

}

Create an array of String by splitting the buffer

sort the array

Add up unique words

print the result in the output file

PART 2

test your program using the infile.txt

infile.txt is:

PART 3

2 files are needed, WorldHistogram.java and outfile.txt

Explanation / Answer

// Note: Please provide thumbs up if you like the solution

// File: Histogram.java

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

public class Histogram
{
public static void main(String args[]) throws Exception
{
  
BufferedReader infile = new BufferedReader(new FileReader("input"));
HashMap<String,Integer> histogram = new HashMap<String,Integer>();
String word;
while ((infile.ready()))
{
word = infile.readLine();
  
//System.out.println("word is: "+word);
String word1 = word.replaceAll("[^a-zA-Z]+"," ").toLowerCase();
//System.out.println("word1 is : "+word1);
String[] line = word1.split(" ");
int len = 0;
while(len<line.length)
{
   //System.out.println("line word is: "+line[len]);
  
   if(histogram.get(line[len])== null) //if the word your currently on is not duplicated
   {
   histogram.put(line[len],1);
   }
   else
   {
   histogram.put(line[len], histogram.get(line[len])+1);
   }
   len++;
}
}
  
infile.close();
printHistogram( histogram );
outputHistogram(histogram);
}

private static void printHistogram( HashMap<String,Integer> hm )
{
List <String> keys = new ArrayList<String> (hm.keySet());
  
// sort the collection by keys so sorted elements comes up
Collections.sort(keys);
for (String key: keys)
{
System.out.println(key +":" +" " + hm.get(key));
}
}

private static void outputHistogram( HashMap<String,Integer> hm )
{
List <String> keys = new ArrayList<String> (hm.keySet());   
Collections.sort(keys);
PrintWriter out = null;
try {
       out = new PrintWriter(new FileWriter("E:\output.txt"));
   } catch (IOException e) {
       // TODO Auto-generated catch block
       e.printStackTrace();
   }
for (String key: keys)
{
out.println(key +":" +" " + hm.get(key));
//out.write(key +":" +" " + hm.get(key));
}
out.close();
}

}

// output file:

a:   4
and:   1
as:   4
chuck:   5
could:   3
he:   3
how:   1
if:   2
much:   3
wood:   4
woodchuck:   4
would:   4