Introduction to Programming (CPSC 124)
—Hobart & William Smith Colleges, Spring 2015
Thursday Lab #8
Home | Syllabus | Calendar | Class Notes | Labs and Projects | General Notes

Due by the start of class on Friday, 04/10/2015

In this assignment, you will gain some practice with file input and output, plus some additional experience in working with String data.

Supporting Code

The following class contains methods for producing formatted versions of a paragraph, both guaranteed to fit within a specified width, both with support for left and right margins. Both methods are complete. Your job will be modifying this file with a main() method that supports reading from and writing to text files.

public class Format {
    
    /**
     * Returns a formatted version of the text in paragraph: each line consists
     * of at most maxwidth characters, counting the left and right margins,
     * whose size is given in number of spaces by margin.  Each line is aligned
     * on the left edge, plus a number of spaces equal to margin.  There is 
     * exactly one space between words.
     */ 
    public static String leftJustify(String paragraph, int maxwidth, int margin) {
        String[] words = paragraph.split("\\s+");
        
        String result = indent(margin) + words[0];
        int linewidth = margin + words[0].length();  // begin a new line
        
        int w = 1;
        while (w < words.length) {
            if ((linewidth + words[w].length() + 1) <= (maxwidth - margin) ){
                result = result + " " + words[w];
                linewidth = linewidth + 1 + words[w].length();
            } else {
                result = result + "\n";
                result = result + indent(margin) + words[w]; // new line
                linewidth = margin + words[w].length();
            }
            w = w + 1;
        } // while
        
        return result;
    }
    
    /**
     * Returns a formatted version of the text in paragraph: each line consists
     * of at most maxwidth characters, counting the left and right margins,
     * whose size is given in number of spaces by margin.  Each line is aligned
     * on the _right_ edge, plus a number of spaces equal to margin.  There is 
     * exactly one space between words.
     */
    public static String rightJustify(String paragraph, int maxwidth, int margin) {
        String[] lines = leftJustify(paragraph,maxwidth,margin).split("\n");
        
        String result = "";
        int i = 0;
        while (i < lines.length) {
            int padding = maxwidth - (lines[i].length() + margin);
            String leftspaces = indent(padding);
            result = result + leftspaces + lines[i] + "\n";
            
            i = i + 1;
        }
        
        return result;
    }
    
    private static String indent(int n) {
        String ind = "";
        int i = 0;
        while (i < n) {
            ind = ind + " ";
            i = i+1;
        }
        return ind;
    }
} // Format

Your Job

Add a main() method that

  1. opens a text file, whose name is given as a command-line argument
  2. reads each line in the text file, which will either be a formatting command or else a line of text in a paragraph.
  3. writes the text in each paragraph into an output file, according to the formatting command given for the paragraph:
    • .left: The paragraph text is formatted with left justification (every line aligned along the left margin). Each line begins with a margin of five spaces, followed by text that is no more than 70 characters in width. This reflects a five-space margin on the right side, as well. In other words, each line is a maximum of 80 characters wide, counting the five-space margin on either side.
    • .right: The paragraph text is formatted with right justification (every line aligned along the right margin). The same requirement of 80-character width with 5-space left and right margins applies.
  4. writes a blank line between each paragraph
  5. closes both the input and output files when everything is finished

Each paragraph begins with a formatting command—.left or .right—, and you may assume that the .left and .right commands are always given on their own lines.

Sample input file

.left
Sorabji's counterpoint stems from Busoni's and Reger's; writers have described
it as more successful than that of the latter. The influence of these
composers led Sorabji to employ various baroque contrapuntal forms (chorale
prelude, passacaglia, fugue and others), but he rejected the symmetry and forms
that characterise the music of composers such as Mozart and Brahms. Sorabji
was dismissive of the Classical style, mainly because he saw it as restricting
the musical material to conform to a "ready-made mould", and his musical
thinking is closer to that of the Baroque era than to the Classical.
.right
Ornamentation assumes a preeminent role in much of Sorabji's music. His harmonic
language, which frequently combines tonal and atonal elements, is thus freer
than in the music of many other composers and less amenable to
analysis. Like many other 20th-century composers, Sorabji displays a
fondness for tritone and semitone relationships. The opening gesture of his
Fourth Piano Sonata, for example, emphasises these two intervals, and the two
long pedal points in its third movement are a tritone apart. However, some
people have remarked that his music rarely contains the tension that is commonly
associated with very dissonant music.

Corresponding output file

     Sorabji's counterpoint stems from Busoni's and Reger's; writers have
     described it as more successful than that of the latter. The influence
     of these composers led Sorabji to employ various baroque contrapuntal
     forms (chorale prelude, passacaglia, fugue and others), but he
     rejected the symmetry and forms that characterise the music of
     composers such as Mozart and Brahms. Sorabji was dismissive of the
     Classical style, mainly because he saw it as restricting the musical
     material to conform to a "ready-made mould", and his musical thinking
     is closer to that of the Baroque era than to the Classical.
     
       Sorabji's counterpoint stems from Busoni's and Reger's; writers have
     described it as more successful than that of the latter. The influence
      of these composers led Sorabji to employ various baroque contrapuntal
             forms (chorale prelude, passacaglia, fugue and others), but he
             rejected the symmetry and forms that characterise the music of
         composers such as Mozart and Brahms. Sorabji was dismissive of the
       Classical style, mainly because he saw it as restricting the musical
      material to conform to a "ready-made mould", and his musical thinking
                is closer to that of the Baroque era than to the Classical.

Turn In

The source code for your completed program, Format.java, in a subfolder of your turn-in directory called "lab8". Please also provide a paper copy of your work.

Standards (READ ME)

Formatting Requirements

See the Style Guide, available from the General Notes section of our course web site. All elements of beautiful, clear code style described there will be expected.

General


John H. E. Lasseter