CS21 Week3 Tuesday In-Class Exercises

  1. Create a week3 subdirectory in your cs21 subdirectory, and from your week3 subdirectory, copy over my week3 files:
    % cd                # remember you can use ls and pwd to figure out where you are in the directory tree
    % cd cs21
    % mkdir week3
    % cd week3
    % pwd                # should list /home/yourusername/cs21/week3
    % cp ~newhall/public/cs21/week3/* .   
    % ls
  2. We will start by opeing up StringTest.java in vim (or emacs), and looking at it together.
Here is a link to the Java API documentation for the String class from the Java class library. We will look at it together.

The entire Java API documentation is available off the CS21 homepage under the "Java API Documentation" link in the On-line References section.

Here are some of the String class methods that may be useful in solving the TODO parts (these are copied from the API documentation):

  String(String original);  Construct a new String initialized to original
  char 	  charAt(int index);  Returns the char value at the specified index.
  String  concat(String str); Concatenates the specified String to the end of this String.
                              (also you can use the + operator with two String arguments to concat them together)

  boolean contains(CharSequence s);  Returns true if and only if this String contains the specified sequence of char values.

  int 	  indexOf(int ch);  Returns the index within this String of the first occurrence of the specified character

  int 	  length();  Returns the length of this String.
  String  substring(int beginIndex, int endIndex); Returns a new String that is a substring of this String.
  String  toUpperCase();  Converts all of the characters in this String to upper case.
  String  toLowerCase();  Converts all of the characters in this String to lower case
  int 	  compareTo(String anotherString); Compares two String lexicographically.
					 a negative return value meane the String is less than anotherString
					 positive: its greater than
					 zero: it is equal to
  int 	  compareToIgnoreCase(String str);

To see the ordering of chars, you can look at the man page for ascii:

% man ascii
The Dec column lists the lexical order of the characters. You can see that the char '1' is less than the char 'A' is less than the char 'a'. So this means that when you compare two strings, a string starting with a numeric char will be less than one starting with an upper case alphabetic char will be less than one starting with a lower case alphabetic char.

The arrow keys can be used to move up and down in the page, and type q to quit out of man.