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

Hello, need some java help on adding 1 thing to my current code. I need this to

ID: 3822988 • Letter: H

Question

Hello, need some java help on adding 1 thing to my current code. I need this to have a graphical interface that deplays my output after click a button.

Here is my code thanks for the help.

import java.io.File;

import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;

public class WordCount {
  
   static String[] invalidWords={"the","and","it","a","on","in"};//list of words not to be considered
   public static void main(String[] args) {
       List<String> wordsInFirst=new ArrayList<String>();
       List<String> wordsInSecond=new ArrayList<String>();
       File firstFile = new File("D:\workspace\chegg\src\wordcount\How the Old Man Made People.txt");
       File secondFile = new File("D:\workspace\chegg\src\wordcount\Mistakes of Old Man.txt");
       wordsInFirst= inputFromFile(firstFile);
       wordsInSecond=inputFromFile(secondFile);
       List<Word> uniqueWords1 = setFrequencies(wordsInFirst);
       List<Word> uniqueWords2 = setFrequencies(wordsInSecond);
       System.out.println("Top 10 words in first File: ");
       System.out.println("Word Frequency Percentage");
       System.out.println("---- --------- ----------");
       List<String> topTen1=new ArrayList<String>();
       for (int i=0;i<=10;i++) {
           double size=uniqueWords1.size();
           double frequency=uniqueWords1.get(i).getFrequency();
           double percent=(frequency/size)*100;
           System.out.println(uniqueWords1.get(i).toString()+" "+Math.round(percent)+"%");
           topTen1.add(uniqueWords1.get(i).getValue());
       }
       System.out.println("Top 10 words from Second File: ");
       System.out.println("Word Frequency Percentage");
       System.out.println("---- --------- ----------");
       List<String> topTen2=new ArrayList<String>();
       for (int i=0;i<=10;i++) {
           double size=uniqueWords2.size();
           double frequency=uniqueWords2.get(i).getFrequency();
           double percent=(frequency/size)*100;
           System.out.println(uniqueWords2.get(i).toString()+" "+Math.round(percent)+"%");
           topTen2.add(uniqueWords2.get(i).getValue());
       }
       listOfCommonWords(topTen1,topTen2);
   }
   /*
   * Method to read words from the file and set to the list
   */
   public static List<String> inputFromFile(File myFile) {
       List<String> words=new ArrayList<String>();
       Scanner in;
       try {
           // read the details from the given file
           in = new Scanner(myFile);
           while(in.hasNext()) {
               String nextWord=in.next(); //read the next word through scanner
               nextWord=nextWord.replace(",", ""); //replace commas"," appended at the end of the file
               nextWord=nextWord.replace(".", ""); //replace ."," appended at the end of the file
               nextWord=nextWord.replace(""", ""); //replace double quotes"," appended at the end of the file
               if(!Arrays.asList(invalidWords).contains(nextWord)){ //check if the word is not the one from the list of words to be removed
                   words.add(nextWord);
               }
           }
           in.close();
       } catch (FileNotFoundException e) {
           System.out.println("Input file not found : " + myFile);
           System.exit(1);
       }
       // Read from input file and get the list of words
       return words;
   }
   public static List<Word> setFrequencies(List<String> words){
       List<Word> newList=new ArrayList<Word>();//List of words with their frequencies
       List<String> addedList=new ArrayList<String>();
       for (String word : words) {
           Word w=new Word();
           if(!addedList.contains(word)){//Condition to make sure the added word is not added.
               w.setValue(word);
               w.setFrequency(Collections.frequency(words,word));
               newList.add(w);
               addedList.add(word);
           }
       }
       Collections.sort(newList,new Word());
       return newList;
   }
   /*
   * returns common Words from top 10 words of both files
   */
   public static List<String> listOfCommonWords(List<String> wordsList1, List<String> wordsList2){
       List<String> commonWords=new ArrayList<String>();
       for (String word : wordsList1) {
           if(wordsList2.contains(word)){//Condition to make sure the added word is not added.
               commonWords.add(word);
           }
       }
       System.out.println("Common Words among top 10 from both files: "+commonWords);
       return commonWords;
   }


}

Word.java this is used to set frequency and implement compareTo method to sort using frequency

import java.util.Comparator;

public class Word implements Comparator<Word>{
   private String value;
   private int frequency;
   public String getValue() {
       return value;
   }
   public void setValue(String value) {
       this.value = value;
   }
   public int getFrequency() {
       return frequency;
   }
   public void setFrequency(int frequency) {
       this.frequency = frequency;
   }
   //Override to String to print the required value and frequency
   public String toString(){
      
       return " "+value+" "+frequency;
   }

//To sort based on frequencies

   @Override
   public int compare(Word w1, Word w2) {
       return w2.frequency-w1.frequency;
   }
}

Explanation / Answer

Button b;

b=new Button("Graphical Interface");

add(b);

b.addActionListener(this);

public void actionPerformed(ActionEvent e)

{

if(e.getSource()==b)

{

setVisible(true);

}

}