![]() |
Lab 9
|
String class
and some of its methods.
StringList
class (
StringList
class contract)
StringList
methods.
String
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:
Take a look at the Java documentation for the String class.
How many constructors do you see there?
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
\
(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; //value of "wish" is: "Happy birthday!"
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):
public String toUpperCase()
When applied to a string, toUpperCase()
converts all of the characters
in this String to upper case. See the following example:
String str1 = "We are HeRe!";
String str2 = str1.toUpperCase(); //str2 has the value of: WE ARE HERE!
There is also toLowerCase()
, but we will not be
using it today.
public boolean equals(String str)
This method compares this string (the receiver of the
equals
message) to the one passed as parameter.
It returns true
iff the two strings represent
the same sequence of characters. 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
. (Notice that the index
of the first character in the string is 0
.)
Here are some examples:
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
public int compareTo(String str)
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 a negative value;
"cat".compareTo("cat")
returns 0;
"cat".compareTo("ant")
returns a positive value.
StringList
classIntList
, 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!
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.
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):
mapPluralize ()
"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 []
.
mapUpperCase ()
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.
isMember (String s, StringList L)
Examples:
isMember("cat", fromString("[dog,cat,goat]"))
returns true
.
isMember("tiger", fromString("[dog,cat,goat]"))
returns false
.
isMember("at", fromString("[dog,cat,goat]"))
returns false
.
isMember("dog", fromString("[]"))
returns false
.
isMember("", fromString("[dog,cat,goat]"))
returns false
.
Note: Make sure that the method handles the empty list!
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"
.
String implode (StringList L)
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 ""
.
filterMatches ()
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.
Here are a few more excersises, if you have time:
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: The
compareTo()
instance method of the String
class should be
useful here.
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.