StringList
class.
contract for the StringList
class
is a helpful reference.
StringList
methods.
String
To declare
a string, one must use a class name String
. Values of strings are given in double quotes,
for instance:
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 myName = "Elena";
String greeting = "Hi there, it's great to see you!";
String myDateOfBirth = "10/16/66";
String empty = ""; // this string has no characters
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:
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.
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?
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
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
.
main()
, it invokes other methods.
Make sure to quit out of any previous MRJ applications for the program before executing the program again.
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:
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.