   import java.util.*;

/**
 * A very simple Document class that can be used to explore
 * issues related to accessibility/visibility
 *
 * @author  Prof. David Bernstein, James Madison University
 * @version 2.0
 */
    public class Document
   {
    // Note that the attributes are protected
   //access is package specific and subclass specific
      protected String       delimiters;
      private String text;
   
   
    /**
     * Explicit Value Constuctor
     *
     * @param text   The text of the document
     */
       public Document(String text)
      {
         this.text = text;
      
         delimiters = " ,.;:!?\t\n\r";
      }
   
   
    /**
     * Append additional text to the end of this Document
     *
     * @param addition   The text to append
     */
       public void append(String addition)
      {
         text = text + addition;
      }
   
   
    /**
     * Get a description of this Document that
     * includes a statistical summary
     *
     * @return  The description
     */
       public String getDescription()
      {
         int          count;
         String       result;
      
      //counts the number of times the .NextToken method is called before an exception
         count  = this.getWordCount();
      
         result = "This document has " + count;
         if (count == 1) result += " word ";
         else            result += " words ";
      
         count = this.getLineCount();
         result += "and " + count;
         if (count == 1) result += " line ";
         else            result += " lines ";
      
         return result;
      }
    
   
   
   
   
    /**
     * Get the text of this Document
     *
     * @return  The text
     */
       public String getText()
      {
         return text;
      }
   
   
   
   
       public int getLineCount()
      {
         char               character;
         int                count, i;
         String             temp;
      //causes a new variable that is local to this method to be created
         temp = this.getText();
      
          // Initialize the line counter
         count = 1;
         System.out.println (" temp.length() is " + temp.length());
         if (temp.length() == 0) count = 0; // No words means no lines
      
          // Count the number of newline characters
         for (i=0; i < temp.length(); i++) 
         {
            character = temp.charAt(i);
            System.out.print (character + " ");
            if (character == '\n') 
            {
              count = count + 1;
              System.out.println (" incremented count to " + count); 
            }  
               
         }
      
         return count;
      }
   
   
   
   
   
   
    /**
     * Get the number of words in this Document
     *
     * @return  The number of words
     */
       public int getWordCount()
      {
         int                count;
         StringTokenizer    tokenizer;
      
      //delimiters are not counted as tokens
         tokenizer = new StringTokenizer(text, delimiters);
      
         count = tokenizer.countTokens();
         System.out.println ("number of times tokenizer can be called " +
            "before it generates an exception " + count);
         return count;
      }
   
   }
