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

1. Use a TreeMap to sort the Words that are to be displayed, as usual, in the ri

ID: 3836516 • Letter: 1

Question

1. Use a TreeMap to sort the Words that are to be displayed, as usual, in the right column of the GridLayout. All the code you need is in the PowerPoint on TreeMaps. You will need to create a Comparator for the class Word. 2. Use a Regular Expression to verify that the passed to the constructor of a Word is three letters. The format of the input file will be the same as Project 1. Submitting the Project. You should now have at least the following files to submit for this project: Project 4 java Word java Word GUI. java WordNode java. Word List .java Un sortedWordList java SortedWordList ava FileMenuHandler java Word Comparator.java Illegal java Submit a jar file Create a jar file called Project4Jar and submit and upload it to Blackboard by the due date for full credit.

Explanation / Answer

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.LinkedList;
import java.util.Map.Entry;
import java.util.TreeMap;

public class Project4 {

   public static void main(String[] args) {
       LinkedList<String> list = new LinkedList<String>();
       LinkedList<String> list2 = new LinkedList<String>();

       BufferedReader br = null;
       FileReader fr = null;

       try {

           fr = new FileReader("filename.txt");
           br = new BufferedReader(fr);

           String sCurrentLine;

           br = new BufferedReader(new FileReader("filename.txt"));

           while ((sCurrentLine = br.readLine()) != null) {
               String[] words = sCurrentLine.split(" ");
               for (int i = 0; i < words.length; i++)
                   list.add(words[i]);
           }
           UnsortedWordList unsortedList = new UnsortedWordList(list);
           TreeMap<String, Object> sortedMap = sortWords(list);
           for (Entry<String, Object> ent : sortedMap.entrySet()) {
               list2.add(ent.getKey());
           }

           SortedWordList sortedList = new SortedWordList(list2);

       } catch (IOException e) {

           e.printStackTrace();

       } finally {

           try {

               if (br != null)
                   br.close();

               if (fr != null)
                   fr.close();

           } catch (IOException ex) {

               ex.printStackTrace();

           }

       }

   }

   private static TreeMap sortWords(LinkedList<String> list) {
       TreeMap<String, Object> words = new TreeMap<String, Object>(new WordComparator());
       for (int i = 0; i < list.size(); i++) {
           words.put(list.get(i), null);
       }
       return words;
   }

}

import java.util.LinkedList;
import java.util.List;

public class UnsortedWordList {
   List<Word> words = new LinkedList<Word>();

   UnsortedWordList(List list) {
       this.words = list;
   }

}

import java.util.LinkedList;
import java.util.List;

public class SortedWordList {

   List<Word> words = new LinkedList<Word>();

   SortedWordList(List list) {
       this.words = list;
   }

}

import java.util.Comparator;

public class WordComparator implements Comparator {

   @Override
   public int compare(Object o1, Object o2) {
       String w1 = (String) o1;
       String w2 = (String) o2;
       if (w1.equals(w2))
           return 0;
       else if (w1.compareTo(w2) > 0)
           return 1;
       else
           return -1;
   }

}


public class Word {
   String word;

   Word(String str) {
       this.word = str;
   }

}