   import java.io.*;

/**
 * Writes formatted tables (i.e., 2-dimensional arrays of
 * String objects) to a PrintWriter
 *
 * @author  Prof. David Bernstein, James Madison University
 * @version 1.0
 * @version 1.1 
 * @author Liz Adams - modification
 */
    public class PrintTableWithHeaders
	 {
      private int               fieldWidth;
      private PrintWriter       out;
   
    /**
     * Explicit Value Constructor
     *
     * @param out   The PrintWriter to write to
     */
       public PrintTableWithHeaders(PrintWriter out)
      {
         this.out   = out;
         fieldWidth = 10;
      }// end PrintTableWithHeaders
   
   
    /**
     * Print a table
     *
     * @param table   The table to print
     */
       public void printTable(String[][] table)
      {
         FieldFormat        formatter;
         int                i, j;
      
         formatter = new FieldFormat();
         formatter.setFieldWidth(fieldWidth);
       
	      for (j = 0; j < table[0].length; j++)
			   out.print ("            " + j);
         out.println ();					
         for (i=0; i < table.length; i++)
         {
			   out.print (i + "\t");
            for (j=0; j < table[i].length; j++)
            {
               if (table[i][j] == null)
               {
                  
                  out.print(formatter.fitStringToField(" "));
               }// end if 
               else
               { 
                 out.print
                    (formatter.fitStringToField
                     (table[i][j]));
               } // end else
            }// end for j
            out.println();
         }// end for i
      } // en/d printTable
   
   
   
    /**
     * Set the field width to use for each 
     * entry in the table
     *
     * @param width
     */
       public void setFieldWidth(int width)
      {
         fieldWidth = width;
      }// end setFieldWidth
   		   
   }// end class
