Lab #9: Width-limited Printing

The goal of this lab is to write a class MaxWidthPrinting that receives from the user a positive integer called width and a file name and prints the tokens from the file so that each line in the print out has no more than width characters while minimizing the total number of lines by maximizing the consective number of tokens appearing in each line. Two rules apply. First, if more than one work appears in one line, neighboring words should be separated by exactly one white space character. Second, a word whose length is greater than or equal to width should appear as a line containing just that word.

Suppose that the value of width is 15 and the first five lines of the input text file are as follows:

But, in a larger sense, we can not dedicate -- we can not consecrate -- we can not hallow -- this ground. The brave men, living and dead, who struggled here, have consecrated it, far above our poor power to add or detract.

Then the program produces the following lines:

But, in a larger sense, we can not dedicate -- we can not consecrate -- we can not hallow -- this ground. The brave men, living and dead, who struggled here, have consecrated it, far above our poor power to add or detract.

The first line “But, in a” has 9 characters. Adding the next token “larger” to this would increase the length to 9 + 1 + 6 = 16. Here the additive term of 1 is for the required one whitespace character. So additing “larger” would create an overflow, and so, we move on to a fresh new line, with “larger” as the first word, leaving behind “But, in a” as a printed line. After “larger” the second line acquires the next token “sense,” thereby turning the length to 13. Since the ensuing token “we” has length 2 and there is an additional whitespace character between the words, adding “we” would change the length to 16, and so the word cannot be added. There are only three lines that fully utilize the 15 character limit:

struggled here,

consecrated it,

add or detract.

Here is an example in which we are forced to place just one word in a line because the word has length greater than or equal to width.

It's
Supercalifragilisticexpialidocious!
Even though the
sound of it Is
something quite
atrocious If
you say it loud
enough You'll
always sound
precocious
Supercalifragilisticexpialidocious!
Um-dittle-ittl-um-dittle-I
Um-dittle-ittl-um-dittle-I
Um-dittle-ittl-um-dittle-I
Um-dittle-ittl-um-dittle-I

Here width is set to 15.

The code goas as follows. First, we will receive the width value and the file name and create a Scanner object out of the file name. To generate a print-out with maximum width we use a while loop that takes action as long as the scanner has the next token. We record the number of characters that have been used in the line currently being generate using an int variable currentLength. The initial value of this variable is 0. In the loop body, the first thing we do it to read the next token from the file and store it in a String variable word. By the end of the loop body, we will have printed the word on the screen. Let wordLength be the length of this token. There are five possible cases:

The sufficiency appearing in Case 4 can be checked by comparing width with currentLength + 1 + wordLength.

After the conclusion of the while-loop, if the currentLength is positive, then an additional new line character is needed.

Here is an another example, with a part of Moby Dick as an input:

% java MaxWidthPrinting
Enter width: 60
Enter file name: moby.txt
"Lad, lad, I tell thee thou must not follow Ahab now. The
hour is coming when Ahab would not scare thee from him, yet
would not have thee by him. There is that in thee, poor lad,
which I feel too curing to my malady. Like cures like; and
for this hunt, my malady becomes my most desired health. Do
thou abide below here, where they shall serve thee, as if
thou wert the captain. Aye, lad, thou shalt sit here in my
own screwed chair; another screw to it, thou must be." "No,
no, no! ye have not a whole body, sir; do ye but use poor me
for your one lost leg; only tread upon me, sir; I ask no
more, so I remain a part of ye." "Oh! spite of million
villains, this makes me a bigot in the fadeless fidelity of
man!-and a black! and crazy!-but methinks like-cures-like
applies to him too; he grows so sane again." "They tell me,
sir, that Stubb did once desert poor little Pip, whose
drowned bones now show white, for all the blackness of his
living skin. But I will never desert ye, sir, as Stubb did
him. Sir, I must go with ye." "If thou speakest thus to me
much more, Ahab's purpose keels up in him. I tell thee no;
it cannot be." "Oh good master, master, master! "Weep so,
and I will murder thee! have a care, for Ahab too is mad.
Listen, and thou wilt often hear my ivory foot upon the
deck, and still know that I am there. And now I quit thee.
Thy hand!-Met! True art thou, lad, as the circumference to
its centre. So: God for ever bless thee; and if it come to
that,-God for ever save thee, let what will befall."

Here are the two text files super.txt and moby.txt

Here is a template you can use: MaxWidthPrinting.java

Go back to the lab main page

Go back to the home page