CS111, Wellesley College 2006

Lab 9

Wedn/Thur, November 1/2, 2006

Strings & StringList Methods

Today's agenda:

  1. Overview of Java strings and their methods. You may use String contract as a reference.
  2. Introduction to StringList class ( StringList class contract)
  3. Introduction to Java applications (a different kind of Java program).
  4. Practice writing StringList methods.

Java class String

Think of strings as letter beads on a cord (see picture above). Java strings are used to store words, sentences, and any other text. Strings consists of characters, such as letters (upper case and lower case letters are considered different characters), digits, punctuation marks (examples: .!,?), other symbols (+.-, etc.), spaces, tabs, and such.

To declare a string, one must use a class name String. String constants are written in double quotes. For example:

    String myName = "Elmo";
    String greeting = "Hi there, it's great to see you!";
    String myDateOfBirth = "4/1/88";
    String empty = ""; // this string has no characters

Using the \ (backslash)

A backslash in a string tells the Java compiler to interpret the symbol that follows the backslash literally. What does that mean? Let's take an example. Note that string values are included in double quotes. What if we need to include a double quote in a string? Writing it directly will confuse the compiler: In the example below, the compiler interprets the double quote before the word Hi! as the closing quote for the string that begins with She said .

    String whatIsIt = "She said "Hi!""; //Compiler error!!! :-(

In order to define a string with the text She said "Hi!", you need to escape the embedded quotation marks by putting backslashes before them:

    String muchBetter = "She said \"Hi!\""; //Compiler is happy now! :-)

String Concatenation

You combine two strings with the + operator. The result is a string that puts the text of the second string right after the text of the first one (it's called string concatenation). For instance:

    String wish = "Happy " + "birthday!"; 

The string wish has the text Happy birthday! in it. Note the space after Happy in the first string.

Concatenation isn't only for constants:

    String word1 = "Happy ";
    String word2 = "birthday!";
    String wish = word1 + word2; 

Strings are objects in Java (even though you don't need to use new, to create a new string). The String contract describes methods that can be applied to strings. The ones relevant to today's lab are:

  1. public String toUpperCase()

    When applied to a string, toUpperCase() returns another string in which all lower-case letters of the first string are replaced by their upper-case versions. For instance, in the example below

          String st1 = "We are HeRe!";
          String st2 = st1.toUpperCase();
          

    The string st2 has the text WE ARE HERE!.
    (So, if we did something like: String st3 = str2 + str2 + str2; then we get
    )

    There is also toLowerCase(), but we will not be using it today.

  2. public boolean equals(String str)

    This method compares two strings: the one that receives the equals message and the one passed as a parameter. It returns true if the two strings contain the same text, false otherwise. Examples:

         String june = "June";
         String july = "July";
         boolean b1 = june.equals(july); // b1 is false
         boolean b2 = june.equals("June"); // b2 is true
         boolean b3 = july.equals("July"); // b3 is true
         boolean b4 = july.equals("july"); // b4 is false.  Why?
         

  3. public int indexOf(String str)

    If the string given as a parameter occurs within the string object that receives the indexOf() message, then the result is the index (i.e. the position) of the first occurrence of the parameter string. If the parameter does not occur in the given the string, the method returns -1. The index of the first character in the string is 0.

    Examples explain it better:

         String str1 = "coconut";
         String str2 = "nut";
         int n1 = str1.indexOf("co"); // n1 is 0
         int n2 = str1.indexOf(str2); // n2 is 4
         int n3 = str2.indexOf("co"); // n3 is -1
         

StringList class

Rather than using the lists of integers introduced in lecture, in this lab we will be using lists of strings since these will be used in your Problem Set. The contract for the StringList class is almost identical to the contract for the IntList class the only difference is that the StringList methods use the types String and StringList whereever the IntList methods use the types int and IntList.

Lab exercises

Begin this assignment by downloading the folder lab8_programs from the cs111d account. Your task is to write several class methods that manipulate string lists. You should write the definitions of each of these class methods in the class LabOps within the file LabOps.java. To test your class methods, you should add testing statements to the main method in the LabOps class. The LabOps class is executed as an application, not as an applet. You can view the output of your program in the Console window in the bottom half of DrJava.

Helpful hints:

Here are the methods that you need to write (5 have red check marks):