Question
Copyable Java Text: public class Madlibs { public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.print(Enter template filename:); String templateName = in.nextLine().trim();
Copyable Java Text:
public class Madlibs {
public static void main(String[] args) { Scanner in = new Scanner(System.in);
System.out.print("Enter template filename:"); String templateName = in.nextLine().trim(); System.out.print("Enter dictionary filename:"); String dictionaryName = in.nextLine().trim(); System.out.print("Enter output filename:"); String outputName = in.nextLine().trim();
Template t = loadTemplate(templateName); Dictionary d = loadDictionary(dictionaryName);
try { PrintWriter out = new PrintWriter(outputName); out.println(t.fill(d)); out.close(); } catch(Exception e) { System.out.println("Cannot write to file: '" + outputName + "'"); }
in.close(); }
static private Dictionary loadDictionary(String fileName) { // TODO }
static private Template loadTemplate(String fileName) { Template t;
try { Scanner in = new Scanner(new FileReader(fileName)); t = new Template(in.nextLine()); in.close(); } catch (Exception e) { System.out.println("Cannot open file: '" + fileName + "'"); t = new Template(""); }
return t; } } class DictionaryFormatException extends RuntimeException { DictionaryFormatException(String badEntry) { super("[Error]: Invalid dictionary entry: '" + badEntry + "'"); } } class UnsupportedCategoryException extends RuntimeException { UnsupportedCategoryException(String badCategory) { super("[Error]: Unsupported category: '" + badCategory + "'"); } } class EmptyWordListException extends RuntimeException { EmptyWordListException(String badCategory) { super("[Error]: No remaining words of category: '" + badCategory + "'"); } }
class Template { // TODO } class Dictionary { // TODO }
1 Overview In this assignment, you will be generating MadLibs based on templates and word lists stored in files. This assignment focuses on file input/output and exception handling (throwing and catching) 2 Requirements You should implement the following classes according to the UML diagrams. Be sure to include a small comment for at least 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. DictionaryFormatExceptiorn 2. EmptyWordListException 3. UnsupportedCategoryException Dictionary - nouns: ListStep by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started