ICT-4361 Homework 5a
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
As you can see from the class diagram, a
FormLetter is a concrete class derived from
the abstract class
FormLetterTemplate. Each instance of
can be used to create one new form letter per
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.
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
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.
- Create and Test a Form Letter
Start with the homework starter files. These will give you a good framework for creating your
FormLetterEntryabstract class, and create the two derived classes
DataItemEntry. 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 for
FormLetterEntrys, appropriate constructors for
FormLetter, 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 one
TextEntryand at least one
DataItemEntry. This letter should be different than
FormLetterHello, and use different
Propertyentries. 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.
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 provided
While you could create the
DataItemEntryat the bottom of the
FormLetterEntry.java, please instead create them as separate, public classes.
Note that while doing input and output in the
FormLettercode, you should always use the
outinstance passed in as a parameter, rather than coding
System.outinto these methods. This will ensure that all your output happens properly, and jUnit tests will succeed.
In a production scenario, output from
doFormLetterwould go to a mass mailer, or printer, or such device.
Note that the
FormLetterclass must store an ordered list of
FormLetterEntrys. This is best handled by using one of the Collections classes in the
java.utilpackage. Various methods in your
FormLetterclass will iterate through the collection. For example, a
FormLetterEntrywould be a reasonable collection, instantiated by, say, a
FormLetterEntry. Because your ordered list stores
FormLetterEntryinstances, it will be able to store
DataItemEntryobjects (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).
addTextEntrymethod creates a new
TextEntryusing the provided text and adds it to your list
addDataItemEntrymethod creates a new
DataItemEntryusing the provided name and adds it to your list
doFormLettermethod should first go through each item on your list, and invoke its
doProcessmethod, providing the
Propertiesobject it needs for substitution. Afterward, it should invoke
formLetterString()to place the result on the provided output stream.
FormLetterHellois the simplest test program using a
FormLetterand 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 out
DataItemEntrys as <name>. That is, a less-than, the name of the data item, and then a greater-than.
|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%|