Waiting for answer This question has not been answered yet. You can hire a professional tutor to get the answer.

QUESTION

Objective: Generate a MadLibs based on templates and word lists stored in files. Focus:

Need some help with basic setup and walk-through.

Objective: Generate a MadLibs based on templates and word lists stored in files.

Focus: file input/output and exception handling (throwing/catching)

Requirements:

Implement the following classes

1. DictionaryFormatException

2. EmptyWordListException

3. UnsupportedCategoryException

class Dictionary{

private List<String> nouns, verbs, adjectives,

adverbs, pronouns, interjections;

public void addWord(String line) {}

public String getWord(String partOfSpeech) {}

}

1. The addWord method takes a line which is expected to be in the for-

mat pos:word, where pos is the part of speech of word. This method

should add word to the appropriate list corresponding to pos. This method

throws an UnsupportedCategoryException if the part of speech does not

correspond to one of the list members, and throws a DictionaryFormatException

if the parameter has an unexpected format. Extra whitespace does not

constitute an illegal format (be sure to account for this).

2. The getWord method will randomly choose a word from the list corre-

sponding to partOfSpeech, remove that word from the list, and return

it. This method throws an UnsupportedCategoryException if the part

of speech does not correspond to one of the list members, and throws an

EmptyWordListException if the list has no words remaining.

Because there is some overlap in these two methods, you are encouraged to

abstract the common parts into a private method (not given in the code skeleton

above).

=============================================================

class Template {

private String template;

public Template(String template) {}

public String fill(Dictionary dictionary) {}

}

1. The Template class has a member which is the unfilled text of the madlib.

The template has placeholders of the form / pos, where pos signifies

the part of speech of the word that replaces it. There may be arbitrary

whitespace between / and pos.

2. The fill method should replace all the placeholders in template with

words pulled from dictionary. This method should not modify template

directly (you should be able to ll the same template with multiple dic-

tionaries).

It's possible for the methods of the dictionary to throw several exceptions.

Placeholders that caused an error (because either the dictionary ran out of

words or the part of speech is unsupported), that placeholder be left alone. If

an error occurs while lling the template, it should be caught and printed to the

console (but should not interrupt lling the template). The fill method itself

should not throw an exception (directly or indirectly) or crash if the template

is formatted poorly.

===============================================================

class Driver {

public static void main(String[] args) {}

private Template loadTemplate(String fileName) {}

private Dictionary loadDictionary(String fileName) {}

1. The loadTemplate method should read the entire file with the given name

and return a Template instance with the contents of the file. If there is

a file error, a message should be printed to the console and a Template

instance with empty contents (") should be returned.

2. The loadDictionary method should be an empty Dictionary, then

read the file with the given name, line-by-line, and add each non-empty

line to the dictionary. If there is a file error or dictionary format error, a message

should be printed to the console. The content of the dictionary should be all of the

non-empty lines that were formatted correctly (regardless if an error occurred).

3. The main method should prompt the user for three filenames (a template

file, a dictionary file, and an output file), then fill the loaded template

with the loaded dictionary, and write the result to the output file. If the

output file cannot be opened for writing, a message should be printed to

the console.

-----------------------------------------------------------------------------------------------

package homework;

import java.io.FileReader;

import java.io.PrintWriter;

import java.util.ArrayList;

import java.util.List;

import java.util.Scanner;

public class Homework {

Show more
LEARN MORE EFFECTIVELY AND GET BETTER GRADES!
Ask a Question