Formulated by
Duo Zhou
Issued July 11, 2000
Imagine we are requested to write a simple MSWord equivalent application
on Unix/Linux environment using the Java API. For this we need to design and
implement an application that will left and right justify text in a way that
avoids splitting words and leaves paragraphs indented. An attempt should also be
made to distribute the additional blanks as evenly as possible in the justified
line.
To accomplish the above task, we need to write 3 classes namely GUI, inputOutput & justify. The GUI helps the user to open a Jframe, interactively accepts the name of the input text file from the user. The text file should be displayed as JtextArea using JscrollPane. When the user selects "Apply justification" widget wer application should apply wer left – right justify algorithm on this file and display the justified text on the frame. Then the user should have the option of interactively writing into an user defined output file.
Algorithm Development
Books and computer prepared documents are almost always left and right justified. What this means is that all lines have a fixed length with the first word on all lines have a fixed length with the first word on the line always starting in a fixed position (except when there is a new paragraph) and the last word also finishing in another fixed position. As an example, suppose the problem statement above was to be left and right justified. We might have
Design and implement an application
that will left and right justify text in
a way that avoid splitting words and
leaves paragraph indented. An attempt.
fixed starting fixed finishing
position position
The fixed line length is achieved by inserting additional spaces between words. The fixed starting position and finishing position value is accepted from the command line. To simplify the complexity, it is assumed that all unformatted lines in the input file are less than or equal to the fixed length required and the lines in the input file do not have leading blanks except for paragraphs indented.
Sample Input :
This is a test file
This is the continuation of it
This is a test file This is the continuation of it.
Sample Output
This is a test file
This is the continuation of it
This is a test file This is the continuation of it.