![]() |
Lab 9
|
StringList class (
StringList class contract)
StringList methods.
String 
.!,?), 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
\ (backslash)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! :-)
+ 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:
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.
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?
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 classStringList 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.
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.
main to make sure that your method
works in all cases.
Here are the methods that you need to write (5 have red check marks):
public static StringList mapPluralize (StringList L)L is a list of nouns,
returns a new list with the plurals of each of the nouns. The
nouns are pluralized by adding the suffix "s". For
instance, the plural of "cat" is "cats"
and the plural of "mouse" is "mouses"
(this simple-minded pluralizer doesn't know about special cases
like "mice").
Examples:
mapPluralize(fromString("[dog,cat,mouse]")) returns the string list
[dogs,cats,mouses].
mapPluralize(fromString("[]")) returns the string list [].
public static StringList mapUpperCase (StringList L)Examples:
mapUpperCase(fromString("[dog,cat,mouse]")) returns
the string list [DOG,CAT,MOUSE].
mapUpperCase(fromString("[]")) returns
the string list [].
Note:
Use the toUpperCase() instance method from the String
class to convert individual strings.
public static boolean isMember (String s, StringList L)true if s is an element of L,
and returns false otherwise.
Examples:
isMember("dog", fromString("[dog,cat,goat]")) returns true.
isMember("cat", fromString("[dog,cat,goat]")) returns true.
isMember("goat", fromString("[dog,cat,goat]")) returns true.
isMember("tiger", fromString("[dog,cat,goat]")) returns false.
isMember("at", fromString("[dog,cat,goat]")) returns false.
isMember("go", fromString("[dog,cat,goat]")) returns false.
isMember("dog", fromString("[]")) returns false.
Note: Make sure that the method handles the empty list!
public static StringList explode (String s)s.
Examples:
explode("computer") returns the string list [c,o,m,p,u,t,e,r].
explode("") returns the string list [].
Notes: The LabOps class contains two helper
methods that are useful for defining explode:
public static String first (String s)s.
For example, first("computer") returns the string "c".
public static String butFirst (String s)s.
For example, butFirst("computer") returns the string "omputer".
public static String implode (StringList L)L in order.
Examples:
implode(fromString("[dog,cat,mouse]")) returns the string "dogcatmouse".
implode(fromString("[lists, are, fun]")) returns the string "lists are fun".
implode(fromString("[]")) returns the string "".
public static StringList filterMatches (String s, StringList L)L that contain s as a substring.
Examples:
filterMatches("com", fromString("[computer,program,incomparable,intercom,Java]"))
returns the string list [computer,incomparable,intercom]
filterMatches("com", fromString("[cat,dog,mouse]"))
returns the string list []
filterMatches("com", fromString("[]"))
returns the string list []
Note: To test if one string is a substring of another, you should
use the indexOf() instance method from the String class.
It works as follows. Let s1 and s2 be two strings.
Then s1.indexOf(s2) returns the index of the first occurrence of
s2 in s1, or -1 if it does not occur at all.
For example, if s1 is the string "Hi there", then:
s1.indexOf("Hi") returns 0
s1.indexOf("there") returns 3
s1.indexOf("e") returns 5
s1.indexOf("bye") returns -1
public static StringList insert (String s, StringList L)L is an n-element list of strings
sorted in alphabetical order.
Returns a new (n+1)-element list of alphabetically sorted
strings that contains s in addition to all the elements
of L.
Examples:
insert("dog", fromString("[cat,goat,lion]"))
returns the string list [cat,dog,goat,lion].
insert("ant", fromString("[cat,goat,lion]"))
returns the string list [ant,cat,goat,lion].
insert("tiger", fromString("[cat,goat,lion]"))
returns the string list [cat,goat,lion,tiger].
insert("goat", fromString("[cat,goat,lion]"))
returns the string list [cat,goat,goat,lion].
Note: In Java, two strings are compared via the
compareTo() instance method of the String class.
It works as follows. Let s1 and s2 be two strings.
Then s1.compareTo(s2)
s1 comes before s2 in alphabetical order;
s1 and s2 are equal strings;
s1 comes after s2 in alphabetical order.
"cat".compareTo("dog") returns -1;
"cat".compareTo("cat") returns 0;
"cat".compareTo("ant") returns 1.
public static StringList insertionSort (StringList L)L in
alphabetical order.
Examples:
insertionSort(fromString("[lion,dog,cat,ant,goat]"))
returns the string list [ant,cat,dog,goat,lion].
insertionSort(fromString("[Time,flies,like,a,banana]"))
returns the string list [Time,a,banana,flies,like].
insertionSort(fromString("[]"))
returns the string list [].
Notes:
"Time") are considered
to alphabetically precede uncapitalized words (such as "banana").
insertionSort method should work
by using the insert method you defined above
to insert the head of the list into the result of
recursively sorting the tail of the list.