AP Computer Science --- Haas --- Strings

Objective: Use the required APCS String methods.


/**
 * Below are examples of using each of the required AP String methods
 * 
 * Copy the code below into BlueJ
 * 
 * Before you run it write a comment next to each println 
 * statement stating what you think the output will be.  
 * 
 * Now run the code and fix any incorrect comments.
 **/

public class StringMethods
{
  public static void main(String[] args)
  {
    String str1 = new String("hello");
    String str2 = new String("hello");
    String str3 = "goodbye";
    
    System.out.println ("str1 = " + str1); 
    System.out.println ("str2 = " + str2); 
    System.out.println ("str3 = " + str3);
    
    System.out.println ("str1 equals str2 >>> " + str1.equals(str2)); 
    System.out.println ("str1 equals str3 >>> " + str1.equals(str3)); 

    System.out.println ("str1 compareTo str2 >>> " + str1.compareTo(str2)); 
    System.out.println ("str1 compareTo str3 >>> " + str1.compareTo(str3)); 
    System.out.println ("str3 compareTo str1 >>> " + str3.compareTo(str1)); 

    System.out.println ("length of str1 >>> " + str1.length()); 

    System.out.println ("substring of str1 1,3 >>> " + str1.substring(1,3)); 

    System.out.println ("indexOf \"od\" in str3 >>> " + str3.indexOf("od")); 
    System.out.println ("indexOf \"x\" in str3 >>> " + str3.indexOf("x")); 

    System.out.println ("str1 == str2 >>>" + (str1 == str2));

    str1 = str2;
    System.out.println ("str1 == str2 >>>" + (str1 == str2));
    
    str1 += " world!";
    
    System.out.println ("str1 = " + str1); 
    System.out.println ("str2 = " + str2); 
  }  
}