CS111, Wellesley College

Lab 9

Wedn, Oct 31

Strings & StringList Methods

Today's agenda:

  1. Overview of Java 's String class and some of its methods.
  2. Introduction to StringList class ( StringList class contract)
  3. Practice writing StringList methods.

Java class String

Please bring up the on-line Java documentation page on your browser, and find the String class, under "All Classes", in the lower-left frame. Part of our work today, is to start getting familiar with the Java documentation, and locating in there information we need.

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 (as expected):
String myName;
String greeting;

String constants are written in double quotes. We can assigne a string literal to a String variable, as in:

    myName = "Elmo"; //"myName" has been declaired above
    greeting = "Hi there, it's great to see you!"; //"greeting" has been declaired above
    String myDateOfBirth = "4/1/88";
    String empty = ""; // this string has no characters
Take a look at the Java documentation for the String class. How many constructors do you see there?

Using the \ (backslash)

A backslash in a string tells the Java compiler to interpret the symbol that follows it 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; //value of "wish" is: "Happy birthday!"

Some String Methods

Here are some of the String methods, relevant to today's lab. (For each one of them, make sure to check the Java on-line docmentation):


StringList class

In lecture, you were introduced to the class IntList, wich is an "in- house" developed Java class. Today, rather than using that class, we will be using an other "in-house" developed class, called StringList. (You will use it also in your next pset.) Here is the contarct to the StringList class. Notice how similar the contracts for those two classes are!

Lab exercises

Begin this assignment by downloading the folder lab9_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.

Helpful hints:

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