CS170 - Project 5

CS170, Project 5


Due: See class webpage

1. Purpose

The purpose of this project is to

The problem solving procedure needed for this project is very "simple" for humans to understand - mainly because humans are highly flexible creatures - but quite complicated when you need to translate it into rigid program instructions. This assignment will encourage you to develop "step-wise refinement" skills to design complex algorithms. I am also requiring you to use static methods to re-inforce this concept. You may also need to use static variables (depending on how you develop your solution).

2. Project Description

The formatted text must meet the following formatting criteria:
  1. Each line must be right-adjusted (not "ragged"), except the last line. The last line is not right adjusted.
  2. Each line, except the last line, must contain as many words as possible.
  3. The difference in number of spaces between any two spacing on the same line is at most one space.
  4. The difference in height between the two text columns is at most one line.
  5. The two text columns are to be separated by 5 spaces.
For an example of the result, see below.

3. Other Requirements

The following are "administrative" and/or "didactical" requirements that help us in grade your project and/or requiring you to use some new concepts:
  1. Your program file must be named "pj5.java" and the class containing the main method must be "pj5". (This is necessary for the automated grading system.)

  2. Your program file must contain one class (pj5) only

  3. Your program must contain at least one static method other than main that must be called at least once by main. If you do not use any static methods in your solution, 10 penalty pts will be deducted from your grade.

4. Test Files.

I have provided you with two test inputs, a small one for debugging purposes and a large input text file for real-life testing.

Save a copy of these files into your cs170 project directory.

When you're done writing your program, you can test your program using the small & large input files using the following commands:

   Small test:	java pj5 < pj5.test1
   Large test:	java pj5 < pj5.test2
The output of the small test should look like this:
A pair of giant pandas arrived    called   the  "Panda  Express"
in   Atlanta  aboard  a  plane    today from Beijing, China.

The output of the large test is:

WE  hold  these  Truths  to be    more disposed to suffer, while
self-evident, that all Men are    Evils  are sufferable, than to
created  equal,  that they are    right themselves by abolishing
endowed  by their Creator with    the  Forms  to  which they are
certain   unalienable  Rights,    accustomed.  But  when  a long
that  among  these  are  Life,    Train     of     Abuses    and
Liberty  and  the  Pursuit  of    Usurpations,          pursuing
Happiness  --  That  to secure    invariably  the  same  Object,
these  Rights, Governments are    evinces  a  Design  to  reduce
instituted among Men, deriving    them under absolute Despotism,
their  just  Powers  from  the    it is their Right, it is their
Consent  of the Governed, that    Duty,   to   throw   off  such
whenever     any    Form    of    Government, and to provide new
Government becomes destructive    Guards    for   their   future
of these Ends, it is the Right    Security.  Such  has  been the
of  the  People to alter or to    patient  Sufferance  of  these
abolish  it,  and to institute    Colonies;  and such is now the
new   Government,  laying  its    Necessity   which   constrains
Foundation on such Principles,    them  to  alter  their  former
and  organizing  its Powers in    Systems   of  Government.  The
such  Form,  as  to them shall    History of the present King of
seem  most  likely  to  effect    Great-Britain  is a History of
their  Safety  and  Happiness.    repeated      Injuries     and
Prudence, indeed, will dictate    Usurpations,   all  having  in
that      Governments     long    direct        Object       the
established   should   not  be    Establishment  of  an absolute
changed    for    light    and    Tyranny  over these States. To
transient      Causes;     and    prove   this,   let  Facts  be
accordingly   all   Experience    submitted to a candid World.
hath  shewn,  that Mankind are

5. Algorithm Design

There are many solutions possible and I will give you an idea on how I developed my solution. First, you should recognize that this problem actually consists of 3 independent subproblems:

  1. Read in the whole text to be formatted (discussed in class)

  2. Format the text into right-adjusted lines of 30 characters wide (except for the last line)

  3. Print the formatted lines in 2 columns
Subproblem 3 is very easy and I will only discuss subproblem 2.

You should use an array of String to store the right-adjusted lines. Define a String array with 100 elements should be sufficient for this project. Each element of the String array will contain one line of the text - right adjusted to 30 characters long.

The following algorithm can format the input into lines

   Break the whole input text into individual words
   while ( there are more words in the input text )
   {
      1. get as many words as it will fit on one formatted line
	 - recall the line is 30 characters wide.
         - here, you should separate each word with 1 space.
      2. process the line into a right adjusted line
      3. store processed line in next String array element
   }
Consider an example. If the input text is:
   WE hold these Truths to be self-evident, that all Men are....
We first break the text into single words: "WE", "hold", "these", etc.

Then get get as many words as it will fit in one line:

 char:  123456789012345678901234567890
	WE hold these Truths to be self-evident...
                                   ^^^^^^^^^^^^ do not fit
So line 1 is:
 char:  123456789012345678901234567890
	WE hold these Truths to be
	  ^    ^     ^      ^  ^  ****  4 spaces
	  |    |     |      |  |
	 gap  gap   gap    gap gap
Now we must process the line and make it right adjusted. According to the Criteria 3 in Section 2, the difference in number of spaces between any two spacing on the same line is at most one space. You can see that this can be achieve my distributing the 4 extra spaces that are located at the end of the line, over the gaps in the sentence in a "round robin" manner (like dealing cards in a poker game). The result is:
 char:  123456789012345678901234567890
	WE  hold  these  Truths  to be
	  ^*    ^*     ^*      ^*  ^ 
	  |     |      |       |   |
	 gap   gap    gap     gap gap
You can see that the spaces has been distributed evenly across the gap and the line is right adjusted.

HINT: You can best use static methods to divide the work needed to process the text into formatted lines:

HINT: You will need a static variable to hold the word that did not fit on a line. For example, the word "self-evident" did not fit on the first line and must be stored away for later processing.

6. Turn in