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
.
Begin this assignment by downloading the file /cs111/download/lab8_programs
.
Below, you are asked 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 class before executing the class again.
Your task is to write and test the class 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.