CS100, Spring 2000, Project 6 Tips

This is the third of 4 documents for Project 6:

    1. Project 6 Write-Up
    2. Project 6 Milestones
--> 3. Project 6 Tips
    4. Project 6 What To Hand In

Here are some tips and reminders to help you with Project 6.  This document is
structured in a fashion parallel to the Milestones write-up.

===============================================================================
Overall Tips
===============================================================================

+ You can use $DecimalFormat$ to print fewer floating point digits.

+ Do NOT instantiate arrays or objects unless actually needed.  For example,
  this is legal code:

    Parthon a = new Parthon();
    Parthon b = new Parthon();
    // swap a and b
        Parthon tmp; tmp = a; a = b; b = tmp;

  Observe that we did NOT need $tmp = new Parthon()$.

+ Read the tip above: We really, really mean it!!!  In the example above, if
  $a$, $b$, and $tmp$ were arrays, we would NOT need to instantiate/create an
  array for $tmp$.

+ Do not hard-code "magic numbers", e.g. used named constants and instance
  variable or method $length$ as appropriate.

===============================================================================
Tips for Milestone 1: Decimal Format, Initializer Lists, Anonymous Arrays
===============================================================================

+ The example from section when we first saw nested loops is relevant:

  /* print

    11 12 13 14
    21 22 23 23
    31 32 33 34
    41 42 43 44

  */

  for (int i = 1; i <= 4; i++) {
    for (int j = 1; j<= 4; j++) System.out.print(i + "" + j + " ");
    System.out.println();
  }

+ You can use $DecimalFormat$ to make columns line up; refer to Online examples
  and Lewis and Loftus.

+ An initializer list can contain (references to) objects, e.g. (references to)
  arrays.

If a loop is no help for initializing an array, recall 3 other techniques.
For example, consider creating the one dimensional array

    -20 37 48 -8.

+ Using only assignment statements is rather verbose:

    int[] x; x = new int[4]; x[0] = -20; x[1] = 37; x[2] = 48; x[3] = -8;
    
+ Using an initializer list is nice and concise:

    int[] x = { -20, 37, 48, -8 };

  However, note that an initializer list is a special abbreviation -- the
  following is *not* legal Java code:

    ILLEGAL! -->   int[] x; x = { -20, 37, 48, -8 };  <-- ILLEGAL!

+ Using an "anonymous array" is almost as concise as an initializer list:

    int[] x = new int[] { -20, 37, 48, -8 };

  Furthermore, anonymous arrays can be used more generally, e.g. the following
  *is* legal Java code:

    int[] z; z = new int[] { -20, 37, 48, -8 };

===============================================================================
Tips for Milestone 2: Basic $char$ and $String$ operations
===============================================================================
  
You will find some of the following tips helpful, but not necessarily all, and
are not required to use all of them.


$char$ tips:

+ Use $(int)$ or arithmetic on $char$s to convert a $char$ to its integer
  representation, e.g. $(int) 'a' - (int) 'A'$ or $'a' - 'A'$.

+ Use $(char)$ to convert an integer to the character it represents, e.g.
  $(char) 65$.

+ Integer representations for lower-case letters occur in sorted order.
  Integer representations for upper-case letters occur in sorted order.
  There is a constant integer "shift" between lower- and upper-case letters.

+ Characters can be compared, e.g. $'a'<'b'$ is $true$, $'b'<'a'$ is $false$.


$String$ tips:

+ Use method $int length()$ to find the length of a String.

+ Use method $char charAt(int i)$ to find the character at position $i$ 
  in a String.

+ Use constructor $String(char[] s)$ to create a String whose contents
  correspond to those of $char$ array $s$.

+ Use operator $+$ to concatenate Strings.

+ Use method $int indexOf(char c)$ to get the position of the first
  occurrence (from left-to-right, if it exists) of $c$ in a String.  Note that
  $-1$ is reported ($return$ed) as the position for non-occurring characters.
  
+ Method $String toLowerCase()$ returns a string with all upper-case letters
  converted to lower-case.


Here is an example that illustrates many, but not all, of these tips:

    // return result of transforming lower-case letters to upper-case
    String toUpper(String s) {
        char[] t = new char[s.length()];
        for (int i = 0; i<s.length(); i++) {
            char c = s.charAt(i);
            if ('a' <= c && c <= 'z') c = (char) (c + 'A' - 'a');
            t[i] = c;
        }
        return new String(t);
    }

===============================================================================
Tips for Milestone 3: Tally Reminders and Tips and TokenReader help
===============================================================================

+ Tips for Milestone 2 will also be helpful here.

+ Don't forget to instantiate any needed elements in an array of objects.

+ An array is an object, so a 2-dimensional array is an array of arrays,
  i.e. an array of objects.

+ Do *not* confuse *row-major* and *column-major*.

+ Tip: convert non-letters to spaces and upper-case letters to lower-case.

+ Tip: go ahead and count double spaces, but at the end, set the bigram tally
  for double spaces to 0.

+ You can compute unigram tallies by summing the rows (or the columns) of the
  table for bigram tallies.  This neatly gives the correct unigram tally for
  spaces -- otherwise, you have to deal with double spaces, etc.

+ You can compute the total number of unigrams from the unigram tallies.

+ Use this template to read input from a file using TokenReader:

    // name of a file in the same folder as the project
    String filename = "sample.txt"; 
    TokenReader in = new TokenReader(filename);
    for (String s = in.readLine(); !in.eof(); s = in.readLine()) {
        // process $s$, e.g. print it
            System.out.println(s);
    }

+ Note: $readLine()$ throws away the old/current line of text and reads the
  next.  Thus, you cannot call $readInt()$ to read an integer and then call
  $readLine()$ to read the rest of the line.  (Try it!)

+ Note: TokenReader has an odd notion of $eof()$ (_E_nd _O_f _F_ile). That
  is, suppose in a file there are two lines of text with no newline at the end:

    This is
    not good

  The 1st call to $readLine()$ returns "This is" and $eof()$ is $false$.
  The 2nd call to $readLine()$ returns "not good" and $eof()$ is $false$.
  The 3rd call to $readLine()$ returns an error message and $eof()$ is $true$.
  
  Most systems would have $eof()$ return $true$ after the 2nd call of
  $readLine()$, not after the 3rd.

===============================================================================
Tips for Milestone 4: Review Nested Loop Example from Section
===============================================================================

+ The method for 1-dimensional arrays might or might not be helpful for the
  method for 2-dimensional arrays, but it is probably a good idea to try both
  ways: (1) using it and (2) not using it.

+ The loop structure for the 2-dimensional array is very similar to that of
  Milestone 1. 

===============================================================================
Tips for Milestone 5: Arrays of Arrays and Incremental Development
===============================================================================

+ For the method for 2-dimensional arrays, work separately on swapping the rows
  and swapping the columns:

  - Write "swap the rows" and test it out
    (print the results and visually confirm that it worked).

  - Write "swap the columns" and test it out
    (print the results and visually confirm that it worked).

  - Combine "swap the rows" and "swap the columns" and test it out.

+ The method for 1-dimensional arrays might or might not be helpful for the
  method for 2-dimensional arrays, but it is probably a good idea to try both
  ways: (1) using it and (2) not using it.

+ Since 2-dimensional arrays are arrays of arrays, there is a very fast way to
  swap rows; it does not require a loop.  However, don't worry if you can't
  think of it.