


ICT-4361 Homework 5a
Purpose
This exercise will familiarize you with using
polymorphic containers and text processing in Java.
We will use the idea of “form letter
processing” to convey these ideas.
Form letters are a way of combining a form
letter template (text and placeholders for substitution) with a data
collection (say, names and addresses) and some computed variables
(such as the date) to create a useful result (say, a personalized
business letter).
As you can see from the class diagram, a
The FormLetter is a concrete class derived from
the abstract class FormLetterTemplate. Each instance of FormLetter
can be used to create one new form letter per Properties object.
FormLetterTemplate class has four public methods (other than constructors).
Two build the form letter, and two are used for producing output.
addDataItemEntry is used to add one DataItemEntry to the form letter
addTextEntry is used to add one TextEntry to the form letter
printFormLetterTemplate outputs the form letter template itself to the provided output stream. This method
will show the placeholders.
doFormLetter creates and outputs a form letter, using the Properties object to provide values for all the placeholders.
In this exercise we will complete the classes for the form letter framework, and process a form letter to produce the appropriate results.
In the followup exercise for next week, the data collections and output will use files rather than internal storage.
What to Hand In
Ensure your name, and the name of the file is available in a comment at the top of
the file.
You do not need to submit files from the homework starter files that are unchanged.
Also, ensure that you have a sample of the output from the program.
If your program fails to compile, hand in your error listing as your output.
For electronic submission, “zip” your submission together into a single file, to ensure
nothing is missing;
For each question asked, provide one or two sentences summarizing your answer. Please
be both complete and succinct.
Problems
- Create and Test a Form Letter
-
Start with the homework starter files. These will give you a good framework for creating your FormLetterTemplate. -
Examine the FormLetterEntryabstract class, and create the two derived classesTextEntryandDataItemEntry. Be sure to begin to implement (or let your IDE do it) all the abstract methods in each derived class. -
Add fields and methods to the FormLetterclass. These should include a container forFormLetterEntrys, appropriate constructors forFormLetter, and the methods outlined in the class starter. -
Compile and run the FormLetterHelloprogram. When you have implemented the above, it should compile and run. -
Capture the output of your test run for your submission. -
Create a new class called MyFormLetter, which represents another example of a form letter. It must contain at least oneTextEntryand at least oneDataItemEntry. This letter should be different thanFormLetterHello, and use differentPropertyentries. Compile it, run it, and same the output of your test run for your submission. -
Optional: Run the JUnit tests and make them all pass (this might take minor changes to your classes).
You will need to ensure the JUnit libraries are on your classpath.
If you submit this optional part, be sure to capture the output showing the JUnit tests passed and include with your submission.
-
- Question: Describe why these files will not compile as they are. Be specific, and display some confirmation for your description. The answer may be stored in a text file, or incorporated as comments in your code.
Notes
-
You can put the sample classes into your NetBeans environment by putting all the files except FormLetterTest.javainto the source directory, and this remaining file into the test directory. Similar instructions for Eclipse. Or, you can also simply use ant with the providedbuild.xmlfile. -
While you could create the TextEntryandDataItemEntryat the bottom of theFormLetterEntry.java, please instead create them as separate, public classes. -
Note that while doing input and output in the DataItemEntry,TextEntry, andFormLettercode, you should always use theoutinstance passed in as a parameter, rather than codingSystem.outinto these methods. This will ensure that all your output happens properly, and jUnit tests will succeed.
In a production scenario, output fromdoFormLetterwould go to a mass mailer, or printer, or such device. -
Note that the FormLetterclass must store an ordered list ofFormLetterEntrys. This is best handled by using one of the Collections classes in thejava.utilpackage. Various methods in yourFormLetterclass will iterate through the collection. For example, aListofFormLetterEntrywould be a reasonable collection, instantiated by, say, aLinkedListorArrayListofFormLetterEntry. Because your ordered list storesFormLetterEntryinstances, it will be able to storeTextEntryobjects andDataItemEntryobjects (through inheritance). -
Note that the Propertiesobject can contain things that might change for each letter (e.g., name) as well as computable things (e.g., date). -
Your addTextEntrymethod creates a newTextEntryusing the provided text and adds it to your list -
Your addDataItemEntrymethod creates a newDataItemEntryusing the provided name and adds it to your list -
Your doFormLettermethod should first go through each item on your list, and invoke itsdoProcessmethod, providing thePropertiesobject it needs for substitution. Afterward, it should invokeformLetterString()to place the result on the provided output stream. -
FormLetterHellois the simplest test program using aFormLetterand this set of classes, so try running it first. -
You must also be sure not to add extra spaces when printing the templates or FormLetter, and finally when printing out the template, you must print outDataItemEntrys as <name>. That is, a less-than, the name of the data item, and then a greater-than.
Evaluation
| Criteria | Weight |
|---|---|
| Answer for the question, including justification | 10% |
| TextEntry derived class and test | 20% |
| DataItemEntry derived class and test | 20% |
| FormLetter class completion | 20% |
| FormLetterHello test program and output | 15% |
| MyFormLetter test program and output | 15% |
| JUnit tests completed and output | +10% |