CS111 Lab 8 - Strings; StringList Methods

The plan of the lab:

  1. Overview of Java strings and their methods. You may use String contract as a reference.
  2. Introduction to StringList class. contract for the StringList class is a helpful reference.
  3. Introduction to Java applications (a different kind of Java programs). See an overview of Code Warrior applications for details.
  4. Practice with writing StringList methods.

Java class String

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. Values of strings are given in double quotes, for instance:

String myName = "Elena";
String greeting = "Hi there, it's great to see you!";
String myDateOfBirth = "10/16/66";
String empty = ""; // this string has no characters
Note that string values are included in double quotes. What if we need to include a double quote into a string itself? Writing it directly will confuse the compiler, because it will think that the opening quotation before the word Hi! closes the entire string.

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

In order to define a string with the text She said "Hi!" you need to put backslashes before the quotations:

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

Just as you can add two numbers using +, you can add two strings. 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.

We can do the same using string variables:

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

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

  1. public String toUpperCase()

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

    String st1 = "I am HeRe!";
    String st2 = st1.toUpperCase();
    

    The string st2 has the text I AM HERE!.

    As you might guess, there is also a method toLowerCase(), but we will not be using it today.

  2. public boolean equals(String str)

    This method compares two strings: the one it is applied to 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 the method is applied to, 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 Problem Set 7. 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.

CodeWarrior applications

Click here to read about Jave applications and about running them in CodeWarrior. The first method in an application that gets executed is called main(), it invokes other methods.

Make sure to quit out of any previous MRJ applications for the program before executing the program again.

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 need to be careful to quit out of any previous MRJ applications for the program before executing the program again.

Your task is to write and test the methods described below. Write your methods one-by-one. When you have written a method, write several test statements in main to make sure that your method works in all cases. Don't forget to test your methods on empty lists (if applicable).

Here are the methods that you need to write: