/**
 * TestString.java -- this is an application/main class that tests the String
 * class.
 * It has been created to explain the various operations that we could do with
 * a string.
 * 
 * CSCE 155 Fall 2005
 * @author Leen-Kiat Soh
 * @version 1.0
 */
 
class TestString {

   public static void processWord( String word ) {

      word = "The clone war has begun!";
      System.out.println("Inside the method: " + word);

   }

   /**
    * This main method tests seven (7) operations/tasks that one could do 
    * with a string.  
    * (1) Access and print out each character individually
    * (2) Convert all characters to uppercase (and lowercase too!)
    * (3) Find the number of tokens
    * (4) Compare strings
    * (5) Concatenate and extract parts of strings
    * (6) Passing a string to a method and changing the string in the method.
    * (7) Use StringBuffer to insert strings.
    */

   public static void main (String[] args)  {

      // Basic idea: what is a String?
      String str1 = "abc";

      char data[] = {'a', 'b', 'c'};
      String str2 = new String(data);

      String str3 = new String("abc");

      if (str1.equals(str2) && str2.equals(str3))
         System.out.println("They are all equal.");
      else
         System.out.println("They are not equal.");

      String yoda = "Do. Or Do Not.  Try Not!";
      char BLANK = ' ';
      int size = yoda.length();

      // what does the following do?
      //
      for (int i = 0; i < size; i++)  {   // zero indexing!
         System.out.println(yoda.charAt(i));  // what is 'charAt()'?
      }

      // what does the following do?
      //
      System.out.println(yoda.toUpperCase());  // what is 'toUpperCase()'?

      // what does the following do?
      int index = 0; int wordCount = 0;
      while (index < yoda.length())  {

         // ignore all the blacnk spaces until a character that is not a 
	 // blank
         while (index < yoda.length() && yoda.charAt(index) == BLANK)  
            index++;

         // now locate the end of the word, process until we hit the next 
	 // blank
         while (index < yoda.length() && yoda.charAt(index) != BLANK) {
            index++;
         }

         wordCount++;

      }  // end while loop

      System.out.println("wordCount = " + wordCount);

      // what does the following do?
      String yodaAgain = "Be afraid.  Very afraid, you should be.";
      String yodaAgain2 = "Too weak he is.";
      String yodaAgain3 = "Do. Or Do Not.  Try Not!";
      String yodaAgain4 = yodaAgain3.toLowerCase();

      System.out.println( yoda.compareTo(yodaAgain) ); 
      System.out.println( yoda.compareTo(yodaAgain2) );
      System.out.println( yoda.compareTo(yodaAgain3) );
      System.out.println( yoda.compareTo(yodaAgain4) );
      // System.out.println( yoda.equals(yodaAgain4) );

      String strA = "java";
      String strB = "javan";
      System.out.println( strA.compareTo(strB) );



      // what does the following do?
      
      String word1 = "Anakin Skywalker";
      String word2 = "Darth Vader";
      String newWord = word1 + word2.substring(5,10);
      System.out.println( newWord);

      // what does the following do?
      String word3 = "Clone Wars";
      processWord(word3);
      System.out.println( word3 );

      // what does the following do?
      word1 = "Hello, are you?";
      word2 = "how";
      StringBuffer tempBuffer = new StringBuffer(word1);
      tempBuffer.insert(7, "'" + word2 + "' ");
      tempBuffer.append("! You still have good in you!");
      newWord = tempBuffer.toString();

      System.out.println( newWord);

   }


}  // end TestString class


