CSci 101: Introduction to Programming
Lab 3
Carlson Hall, room 120
Some preliminaries.
- There are several things you should know about Linux.
If you haven't yet changed your password, do so now, by clicking
on Mor's Linux Help and following the instructions there. When
you have time, try out some of the other things Mor explains,
like copying, renaming, and deleting files. Those operations
can either be done with the graphic interface KDE or with line
commands. Other operations can only be done with line commands,
like finding your file quota, changing your password, and many
others. We'll explore them in a later lab.
- If you haven't done so yet, read the documentation guide.
(There's a link in the menu on the left.)
- Subscribe to the csci listserv. (Link at the bottom
of the menu.)
- Visit documentation for the Java classes.
Click on Resources in the menu to the left to get a page of resources
for the class.
You'll see a link for on-lin
documentation for Java 1.2 (the kind we're using). Right click on
that link to open
a new window with that page.
That takes you to http://java.sun.com/products/jdk/1.2/docs/api/, the
official specification for Java 1.2. You can find more about any class
you like, for instance, String. Scroll down in the lower left frame until
you see
String. Click on it, and you'll see the whole specification for the
String class, only some of which is included in our textbook in section
2.2. Some of the methods mentioned there may be of use in the program
for this lab.
Program for this lab.
Section 2.2 in our text discusses the String class and its methods. So far the programs you've written use constant strings, those expressions enclosed by double quotation marks, like "Hello there". In this program, you'll work with some of the methods in the String class.
You're program should input two strings (words), then report on the strings as shown. Here's a sample run of the program.
This program compares two words for lexicographic order, and whether
either is contained in the other.
Enter two words, each on a separate line.
green
blue
The two words, "green" and "blue" are not the same.
The word "blue" comes before the word "green" in the dictionary.
Neither word can be found in the other.
Do you want to stop now?
no
Enter two words, each on a separate line.
red
fred
The two words, "red" and "fred" are not the same.
The word "fred" comes before the word "red" in the dictionary.
The word "red" is found in the word "fred".
Do you want to stop now?
no
Enter two words, each on a separate line.
Yellow
yellow
The two words, "yellow" and "yellow" are the same.
Do you want to stop now?
yes
Some suggestions.
- Write and test the program one stage at a time; not all at once.
- Do the explanation first, the part that prints
This program compares two words for lexicographic order, and whether
either is contained in the other.
and the loop that asks the user if he/she wants to stop now. What kind of loop is that? while, do/while, or for?
- After you've compiled and tested that loop, write the part
that prompts the user to enter two words and inputs those two words. You can use the method
SavitchIn.readLineWord(). Compile and run the program again.
- Next include the instructions to see if the two words are equal. You can find the method to use in section
2.6. It would probably be best to ignore case, so that "The" and "the" are considered to be the same word. You
can do this by getting copies of the two words all in the same case, and compare the copies instead of the
original words. Alternatively, you can use the equalsIgnoreCase method. You can find the some String methods to
use in section 2.6, and the rest in the Java 1.2 specification (see preliminaries above). If the two strings are the
same, print a comment to say so, and that's all that has to be done in that case. But if they're not, say they
aren't, and there's more to be done (5 and 6 below). But first, compile and run the program before going on to
make sure everything is working okay.
- In the case that the two words aren't the same, determine which one comes first lexicographically. Compile
and run the program before going on. Note, case should still be ignored, so that both "Cat" and "cat" come before
either "Dog" or "dog".
- Also, in the case the two words aren't the same, see if either one is a substring of the other.
The above are only suggestions as to how to write the program. You may decide to build the program in some other order.
Test your program on the following pairs of words, and
hand in the printed source code and the printed run.
green blue
red fred
yellow yellow
cat Cat
cat Dog
Cat dog
Labs