Article View: aus.computers.java
Article #612[Fwd: hi i am new to java and i have this assignment can you give me some points on where to start]
From: Craig Dicker
Date: Mon, 10 Jul 2000 00:00
Date: Mon, 10 Jul 2000 00:00
254 lines
11150 bytes
11150 bytes
This is a multi-part message in MIME format.
--------------2E2FBBA0C65BD4DA9338BB55
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit
--------------2E2FBBA0C65BD4DA9338BB55
Content-Type: message/rfc822
Content-Transfer-Encoding: 7bit
Content-Disposition: inline
X-Mozilla-Status2: 00000000
Message-ID: <396470FC.3E13FA79@planet.net.au>
Date: Thu, 06 Jul 2000 21:13:56 +0930
From: Craig Dicker <cdicker@planet.net.au>
X-Mailer: Mozilla 4.72 [en] (Win98; I)
X-Accept-Language: en
MIME-Version: 1.0
Newsgroups: comp.lang.java.programmer
Subject: hi i am new to java and i have this assignment can you give me some
points on where to start
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit
Text Formatting Program
This assignment requires you to write a program, TextFormat.java, that
reads text, structured into paragraphs, and writes out paragraphs
formatted according to the options given to the program. The options
allow the user to output the paragraphs as fully justified (no ragged
edges on either side, as is common with many books), right justified
(ragged left edge), centred (symmetrical about the centre), or simply
left justified (ragged right edge). The user can also specify how wide
the text is to be, and the size of an optional left margin.
The input text can consist of lines of any length, with any number of
spaces or tabs between words. The only way of determining where a
paragraph ends is by the presence of a blank line (perhaps containing
only whitespace characters followed by a newline). In order for the text
formatting to make sense, we'll assume that the text is displayed in a
monospaced font (such as Courier or a terminal font). The task of the
formatting program is to create lines of the correct length to satisfy
the user's width and margin specifications. In the case of justified
text, your program may have to insert space characters in between words
to pad out each line to the same length.
TextFormat must be written to support the following command line usage:
java TextFormat [options] [file]
where options include:
-l
specify left justification (ragged right) of the text
-r
specify right justification (ragged left) of the text
-c
specify centred text
-b
specify full or block justification (straight edges) of the text
-m margin
a margin consisting of margin space characters must appear before each
non-empty line
-w width
the visible output text after the margin must be no longer than width
characters wide (the ending newline is not visible)
If no valid file name, file, is given, the program will read text from
its standard input, allowing users on UNIX to pipe text into the
program.
The program must output the formatted text (and only the formatted text)
to the standard output. The output text should include paragraph breaks
(blank lines) in the same places as paragraph breaks appeared in the
input text. If more than one paragraph break occurs in succession, then
the same number of blank lines should also appear on the output.
Formatting Behaviour
Suppose the input text to TextFormat was the following text:
Proinde cum venabere, licebit, auctore me, ut panarium et
lagunculam sic etiam pugillares feras. Experieris non Dianam
magis montibus quam Minervam inerare. Vale.
Ridebis, et licet rideas. Ego ille quem nosti apros et quidem
pulcherrimos cepi. Ipse? Ipse; non tamen ut omnino ab inertia
mea et quete discederem. Ad retia sedebam: erat in proximo non
venabulum aut lancea, sed stilus et pugilares: meditabar
aliquid enotabamque, ut, si manus vacuas, plenas tamen ceras
reportarem.
Non est quod contemnas hoc studendi genus. Mirum est ut animus
agitatione motuque corporis excitetut.
Note that the above text consists of three paragraphs, and that some
words have more than a single space after them or before them. Your
program should break up the text into words (strings of non-whitespace
characters), and output the words with at least one space between them.
The default formatting is left justified, with a text width of 78
characters, and no margin. For example:
Proinde cum venabere, licebit, auctore me, ut panarium et lagunculam sic
etiam
pugillares feras. Experieris non Dianam magis montibus quam Minervam
inerare.
Vale.
Ridebis, et licet rideas. Ego ille quem nosti apros et quidem
pulcherrimos
cepi. Ipse? Ipse; non tamen ut omnino ab inertia mea et quete
discederem. Ad
retia sedebam: erat in proximo non venabulum aut lancea, sed stilus et
pugilares: meditabar aliquid enotabamque, ut, si manus vacuas, plenas
tamen
ceras reportarem.
Non est quod contemnas hoc studendi genus. Mirum est ut animus
agitatione
motuque corporis excitetut.
There's no blank line after the last paragraph, and a single space
separates every word.
Suppose a width option of 40 characters was given (-w 40), and the
margin was set to 4 characters (-m 4). The output would look like:
Proinde cum venabere, licebit, auctore
me, ut panarium et lagunculam sic etiam
pugillares feras. Experieris non Dianam
magis montibus quam Minervam inerare.
Vale.
Ridebis, et licet rideas. Ego ille quem
nosti apros et quidem pulcherrimos cepi.
Ipse? Ipse; non tamen ut omnino ab
inertia mea et quete discederem. Ad
retia sedebam: erat in proximo non
venabulum aut lancea, sed stilus et
pugilares: meditabar aliquid
enotabamque, ut, si manus vacuas, plenas
tamen ceras reportarem.
Non est quod contemnas hoc studendi
genus. Mirum est ut animus agitatione
motuque corporis excitetut.
The right justified text at 40 characters and no margin would appear as:
Proinde cum venabere, licebit, auctore
me, ut panarium et lagunculam sic etiam
pugillares feras. Experieris non Dianam
magis montibus quam Minervam inerare.
Vale.
Ridebis, et licet rideas. Ego ille quem
nosti apros et quidem pulcherrimos cepi.
Ipse? Ipse; non tamen ut omnino ab
inertia mea et quete discederem. Ad
retia sedebam: erat in proximo non
venabulum aut lancea, sed stilus et
pugilares: meditabar aliquid
enotabamque, ut, si manus vacuas, plenas
tamen ceras reportarem.
Non est quod contemnas hoc studendi
genus. Mirum est ut animus agitatione
motuque corporis excitetut.
Centred text has half in the left side of the total width, and half in
the right side. Where a line length is not an even number of characters,
the text should be started one character left of the true centre.
Centred text at 40 characters and no margin appears as:
Proinde cum venabere, licebit, auctore
me, ut panarium et lagunculam sic etiam
pugillares feras. Experieris non Dianam
magis montibus quam Minervam inerare.
Vale.
Ridebis, et licet rideas. Ego ille quem
nosti apros et quidem pulcherrimos cepi.
Ipse? Ipse; non tamen ut omnino ab
inertia mea et quete discederem. Ad
retia sedebam: erat in proximo non
venabulum aut lancea, sed stilus et
pugilares: meditabar aliquid
enotabamque, ut, si manus vacuas, plenas
tamen ceras reportarem.
Non est quod contemnas hoc studendi
genus. Mirum est ut animus agitatione
motuque corporis excitetut.
Full justification is the most challenging format, since the output may
need to be padded with spaces in a way that makes it appear regularly
spaced and pleasing to read. This is a sample of full justification at
40 characters width and no margin:
Proinde cum venabere, licebit, auctore
me, ut panarium et lagunculam sic etiam
pugillares feras. Experieris non Dianam
magis montibus quam Minervam inerare.
Vale.
Ridebis, et licet rideas. Ego ille quem
nosti apros et quidem pulcherrimos cepi.
Ipse? Ipse; non tamen ut omnino ab
inertia mea et quete discederem. Ad
retia sedebam: erat in proximo non
venabulum aut lancea, sed stilus et
pugilares: meditabar aliquid
enotabamque, ut, si manus vacuas, plenas
tamen ceras reportarem.
Non est quod contemnas hoc studendi
genus. Mirum est ut animus agitatione
motuque corporis excitetut.
Note that every line is exactly the same length as the specified width,
except for the last line of each paragraph, which has only single spaces
between words. If extra (more than one) space is needed between words,
words ending with a punctuation character (comma, semi-colon, colon, and
full-stop) are given an extra space first, then any other words are
padded. Also note that on lines where the number of spaces between words
needs to exceed one, the extra spaces are introduced in alternating
directions: first the leftmost words are padded, then on the next line,
the rightmost words are padded, followed by the leftmost, and so on.
If a line of text includes a word longer than the specified width, your
program should output the long words as they are, even if this means
that long words extend beyond the normal line length; this applies for
all formatting options. Negative, zero, or misformed width or margin
sizes should be quietly ignored.
Implementation Suggestions
It's expected that a single source file, containing the public class
TextFormat, will be manageable for this assignment. You should define
other classes for processing the input (remember: text is normally read
with subclasses of the Reader stream), and creating output lines.
Your main() method is expected to process all command line options, open
the file (if named), and break up the input text using a class/object of
your devising. Please display an error and exit the program is any given
option is unrecognised, or a named file cannot be read.
It's suggested that you exploit polymorphism across different objects to
provide the specialised formatting behaviour in each case. The user
options, then, simply dictate which kind of line formatting object to
create and use at run-time.
You are not required to implement your own collection classes for
holding data. Rather, you should use appropriate collection classes
already provided by the JDK. Please note that to be portable, your Java
program must use the appropriate system property for the local newline
character ('\n' only works on UNIX platforms!).
--------------2E2FBBA0C65BD4DA9338BB55--
Technical Details
Message-ID:
<396991F9.E4A2C309@planet.net.au>Path:
arc1-ca.pugleaf.net!unknown!not-for-mailShow Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 10d6db,ef691971746215ed,start X-Google-Attributes: gid10d6db,public From: Craig Dicker <cdicker@planet.net.au> Subject: [Fwd: hi i am new to java and i have this assignment can you give me some points on where to start] Date: 2000/07/10 Message-ID: <396991F9.E4A2C309@planet.net.au> X-Deja-AN: 644532018 X-Accept-Language: en Content-Type: multipart/mixed; boundary="------------2E2FBBA0C65BD4DA9338BB55" X-Trace: 10 Jul 2000 09:19:34 GMT, ppp47.adel.planet.net.au MIME-Version: 1.0 Newsgroups: aus.computers.java