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

USING JGRASP and g iven driver Assignment3Driver.java (no modifications to drive

ID: 3686948 • Letter: U

Question

USING JGRASP and given driver Assignment3Driver.java (no modifications to driver) :

--- Write a new class named Assignment3.

Requirements:

1. Add two private member variables, one to store the name of the file, and another to store the count of words in the file.

2. Add a public constructor. Its only argument is the name of the file. Use it to initialize one of the variables from requirement #1.

3. Add a public "getter" for the word count, name it "getWordCount".

4. Add a public function named "processFile". It returns nothing and accepts no arguments. This function will:

a) Open the file.

b) Read the file's contents into a buffer.

c) Close the file.

d) Convert the buffer into a string.

e) Tokenize the string and use the tokenizer to initialize your word count variable.

5. Add a private member variable that will store the most frequent word in the file.

6. Add a "getter" for the variable (of #5)

7. Add code to your processFile function that counts the number of times each word appears in the file. A HashMap is a good choice for this.

8. Add code to your processFile function that loops through your HashMap (if that's what you use) to find the most frequent word. After your loop, the variable added (for #5) should contain the value for the most frequent word. Don't worry about ties

Explanation / Answer

Hi I have written full working code. You can test it.

You have to pass a file nane from command line. So make sure that you are passing file name.  

java Assignment3   filename (if you run from command line)

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.StringTokenizer;

public class Assignment3 {
   private String fileName;
   private int wordCount;
   private String frequentWord;
  
   public Assignment3(String fileName){
       this.fileName = fileName;
       this.wordCount = 0;
   }
  
   public int getWordCount(){
       return wordCount;
   }
  
   public String getMostFrequentWord(){
       return frequentWord;
   }
   public void processFile() throws IOException{
       FileInputStream fileInputStream=null;
      
        File file = new File(fileName);
      
        byte[] bFile = new byte[(int) file.length()];
      
            //convert file into array of bytes
        fileInputStream = new FileInputStream(file);
        fileInputStream.read(bFile);
        fileInputStream.close();
      
        String text = new String(bFile);
      
        HashMap<String, Integer> wordsCount = new HashMap<String, Integer>();
      
        StringTokenizer st = new StringTokenizer(text);
      
        while(st.hasMoreTokens()){
          
           String word = st.nextToken();
          
           if(wordsCount.containsKey(word)){
               wordsCount.put(word, wordsCount.get(word)+1);
           }else
               wordsCount.put(word, 1);
        }
      
        String mostFrequent = "";
        int mostCount = 0;
      
        for(Map.Entry<String, Integer> entry : wordsCount.entrySet()){
           if(entry.getValue() > mostCount){
               mostFrequent = entry.getKey();
               mostCount = entry.getValue();
           }
          
           wordCount = wordCount + entry.getValue();
        }
        frequentWord = mostFrequent;
   }
}