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

You should implement the following classes according to the UML diagrams. Be eac

ID: 3867210 • Letter: Y

Question

You should implement the following classes according to the UML diagrams. Be each non-trivial method. You will also need to implement the following three new (non-checked) exception classes. When to throw and catch these exceptions are briefly described below, but are left mostly up to you to figure out. 1. DictionaryFormatException 2. Empty WordListException 3. UnsupportedCategory Exception Dictionary - nouns: List verbs: List - adjectives: List - adverbs: List -pronouns: List - interjections: List

Explanation / Answer

Given below is the code for the question. Please do rate the answer if it helped. Thank you.

DictionaryFormatException.java


public class DictionaryFormatException extends RuntimeException {
   public DictionaryFormatException()
   {
       super();
   }
  
   public DictionaryFormatException(String msg)
   {
       super(msg);
   }
}

EmptyWordListException.java


public class EmptyWordListException extends RuntimeException {
   public EmptyWordListException()
   {
       super();
   }
  
   public EmptyWordListException(String msg)
   {
       super(msg);
   }
}

UnsupportedCategoryException.java


public class UnsupportedCategoryException extends RuntimeException {
   public UnsupportedCategoryException()
   {
       super();
   }
  
   public UnsupportedCategoryException(String msg)
   {
       super(msg);
   }
}

Dictionary.java

import java.util.ArrayList;
import java.util.Random;
import java.util.List;
public class Dictionary {
   private List<String> nouns;
   private List<String> verbs;
   private List<String> adjectives;
   private List<String> adverbs;
   private List<String> pronouns;
   private List<String> interjections;
   public Dictionary()
   {
       nouns = new ArrayList<String>();
       verbs = new ArrayList<String>();
       adjectives = new ArrayList<String>();
       adverbs = new ArrayList<String>();
       pronouns = new ArrayList<String>();
       interjections = new ArrayList<String>();
      
   }
   //line expected to be of format pos:word
   public void addWord(String line)
   {
       String[] tokens = line.split(":");
       if(tokens.length != 2)
           throw new DictionaryFormatException(line + " is not in the format pos:word");
       String pos = tokens[0].trim();
       String word = tokens[1].trim();
       if(pos.equalsIgnoreCase("noun"))
           nouns.add(word);
       else if(pos.equalsIgnoreCase("verb"))
           verbs.add(word);
       else if(pos.equalsIgnoreCase("adjective"))
           adjectives.add(word);
       else if(pos.equalsIgnoreCase("adverb"))
           adverbs.add(word);
       else if(pos.equalsIgnoreCase("pronoun"))
           pronouns.add(word);
       else if(pos.equalsIgnoreCase("interjection"))
           interjections.add(word);
       else
           throw new UnsupportedCategoryException("Category " + pos + " not supported");
      
   }
   public String getWord(String partOfSpeech)
   {
         
      
       Random random = new Random(System.currentTimeMillis());
       List<String> list;
       if(partOfSpeech.equalsIgnoreCase("noun"))
           list = nouns;
       else if(partOfSpeech.equalsIgnoreCase("verb"))
           list = verbs;
       else if(partOfSpeech.equalsIgnoreCase("adjective"))
           list = adjectives;
       else if(partOfSpeech.equalsIgnoreCase("adverb"))
           list = adverbs;
       else if(partOfSpeech.equalsIgnoreCase("pronoun"))
           list = pronouns;
       else if(partOfSpeech.equalsIgnoreCase("interjection"))
           list = interjections;
       else
           throw new UnsupportedCategoryException("Category " + partOfSpeech + " not supported");
      
       if(list.size() == 0)
           throw new EmptyWordListException("List of " + partOfSpeech + " is empty");
      
       return list.remove(random.nextInt(list.size()));
         
   }
}

Template.java


public class Template {
   private String template;
   public Template(String template)
   {
       this.template = template;
   }
  
   private String fill(Dictionary dictionary, String sentence, String category)
   {
       int slashIdx, catIdx; //index of / and category
       int start = 0;
       String inbetween, leftout;
      
       while(start < sentence.length())
       {
          
           catIdx = sentence.indexOf(category, start); //get the index of the category word
           if(catIdx == -1) //if category is not found nothing to do
               break;
           slashIdx = sentence.substring(0, catIdx).lastIndexOf("/"); //search for the slash preceding the category word index
           if(slashIdx == -1) //if there is no slash nothing to do
               break;
           inbetween = sentence.substring(slashIdx + 1, catIdx); //get all the characters between the slash and the category word
           int idx = catIdx + category.length();
           if(inbetween.trim().equals("")) //only if the in between characters were whitespaces
           {
               //get the portion of the string after the category word
               if(idx > sentence.length())
                   leftout = "";
               else
                   leftout = sentence.substring(idx);
               try
               {
                   //replace the slash and category word by a dictionary word
                   sentence = sentence.substring(0, slashIdx) + dictionary.getWord(category);
                   start = sentence.length(); //the next index to start searching from
                   sentence += leftout;
               }catch(EmptyWordListException e)
               {
                   System.out.println(e.getMessage());
                   start = idx;
               }
           }
           else
           {
               start = catIdx + category.length();
           }
       }
       return sentence;
   }
   public String fill(Dictionary dictionary)
   {
       String[] categories = {"noun", "verb", "adjective", "adverb", "pronoun", "interjection"};
       String sentence = template;
       for(int i = 0; i < categories.length; i++)
       {
           sentence = fill(dictionary, sentence, categories[i]);
       }
       return sentence;
   }
}

Driver.java

import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.Scanner;

public class Driver {
  
   private static Template loadTemplate(String filename)
   {
       String contents = "";
       Scanner infile;
       try {
           infile = new Scanner(new File(filename));
           while(infile.hasNextLine())
               contents += infile.nextLine();
           infile.close();
       } catch (FileNotFoundException e) {
           System.out.println(e.getMessage());
       }
       return new Template(contents);
   }
  
   private static Dictionary loadDictionary(String filename)
   {
       Dictionary dictionary = new Dictionary();
       Scanner infile;
       try {
           infile = new Scanner(new File(filename));
           while(infile.hasNextLine())
           {
               String line = infile.nextLine().trim();
               if(line.equals("")) continue;
               try
               {
                   dictionary.addWord(line);
               }catch(DictionaryFormatException e)
               {
                   System.out.println("Exception: " + e.getMessage() + " in line " + line);
               }
               catch(UnsupportedCategoryException e)
               {
                   System.out.println("Exception: " + e.getMessage() + " in line " + line);
               }
           }
           infile.close();
       } catch (FileNotFoundException e) {
           System.out.println(e.getMessage());
       }
       return dictionary;
      
   }
   public static void main(String[] args) {
       Scanner keybd = new Scanner(System.in);
       String templateFname, dictionaryFname, outputFname;
       System.out.print("Enter template filename: ");
       templateFname = keybd.next();
       System.out.print("Enter dictionary filename: ");
       dictionaryFname = keybd.next();
       System.out.print("Enter output filename: ");
       outputFname = keybd.next();
       Template template = loadTemplate(templateFname);
       Dictionary dictionary = loadDictionary(dictionaryFname);
      
       try {
           PrintWriter writer = new PrintWriter(new File(outputFname));
           writer.write(template.fill(dictionary));
           writer.close();
           System.out.println("Please check output file " + outputFname );
       } catch (FileNotFoundException e) {
           System.out.println(e.getMessage());
       }
      
   }
}

Input file: sample.txt

The / noun / adverb / verb into the / adjective / noun .

Input file: words.txt

noun:house

noun:cat

noun:dog

adjective:small

verb:eat

verb : bike

adjective:purple

adverb : quickly

  

   adverb : vehemently

output file: sentence.txt

The cat quickly eat into the small house .

output

Enter template filename: sample.txt

Enter dictionary filename: words.txt

Enter output filename: sentence.txt

Please check output file sentence.txt

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote