Today, you'll continue with some more word games. You'll also be introduced to Mercurial, a tool for managing the different versions that you make of files. Take a few minutes to read the page about Mercurial; what is there will make more sense as you work through the lab.
For a starting point, clone the repository I have provided. The cloning process will create a new directory, so change into the directory where you want it to be created. Then
$ hg clone /cslab/class/csci235/labs/lab2
That will make a lab2
directory, which will include both
a working copy and a directory. Change into that directory.
The presence of the repository is normally invisible. If you do
ls
, you won't see anything but one Java source file.
However, if you give the -a
(for all) option to
ls
, it will show you the filenames that start with dots.
You should see a couple that start with .hg
.
You can check the status of your working copy with
$ hg status
That won't tell you much right now, because you haven't done
anything. But fire up your editor and make an awful mess of
Palindrome.java
, being sure you save the results.
If you try the status command again, the file will be flagged as
"modified". Now try
$ hg revert Palindrome.javaThat will cause the file
Palindrome.java
to revert to the
last version committed to the repository. You'll need to re-load it
in the editor to see that, but it should look better than your mess.
The code you have right now is what you started with last time; you
probably want to start instead with the (working) version you wrote
last time; so you need to copy Palindrome.java
from your
account to this directory. (Do you remember how?)
Then, since you want that to be in the repository,
commit it with
$ hg commit -m "Palindrome.java from my lab 1"The message on the end is what you'll be able to see in repository logs. So try
$ hg logto see the revisions in your repository.
Your program from last time may not have printed a very clear message. Fix it so that it prints the input string, in quotes, and says whether it is a palindrome.
Right now, the program distinguishes based on case, so that, for example, "Pop" is not correctly recognized as a palindrome. Fix it so that it ignores case. Test it; make sure that it prints the input string as the user typed it.
This should be very simple. Pages 38-41 of the textbook list some of the methods available for strings. (You could look at the online documentation forclass String
,
but that is a lot longer and contains a lot of clutter that we don't
yet understand; there is also an abbreviated version of this
reachaable from "Simplified documentation" on the class
page.)
Once you have it working, commit it to your repository with
$ hg commit -m "Palindrome.java ignores case"
Now it is time to make your program ignore any spaces and punctuation in the input. Once you've done this, you should get something like
'A man, a plan, a canal: Panama!' is a palindrome.
Note: there are methods in class Character to help you classify
characters. Look for methods with names that
start with 'is
' that take a parameter that is of type
char
.
You'll notice that these methods have the word
static
in front of them, as does the digit()
method that you used in the last lab. When a method is not
static, you name it relative to an object, as when
substring()
is called in the second line of
String s = "1234"; String t = s.substring(2,3);When you call a static method, though, you name it relative to the class, as in the use of
digit()
here:
int i = Character.digit('3', 10);
Your previous program tests whether a string is a palindrome. Now you'll tackle another word game.
A sentence is a pangram if it contains every letter of the alphabet at least once. Examples include
Write a program which prompts the user for a string and determines whether or not that string is a pangram. Ignore spaces, punctuation, and capitalization. Your program should also include an opening documentation block (following the style we've seen in class) and a comment on every variable you declare.
Note: a good way to start would be to copy your palindrome program. You can do that with
$ hg copy Palindrome.java Pangram.java
You use the "hg copy
" command (instead of plain
cp
) so that the repository will know that your new file
is derived from the old one.
If you create a new file (Silly.java
, say) that you want the repository to track, you
use the command
$ hg add Silly.javato tell the repository about it. The contents of the file won't actually go into the repository, though, until you do a commit.
Hint: Think about how you would do this using pencil and paper. You can do this using the methods listed in the textbook, which are also in the simplified online documentation. Need a hint? Another?
Feel free to commit your changes at any time that you have something you might want to go back to. To commit, think of a brief description/label for the state you are about to commit. Then the command
$ hg commit -m "message"with your message substituted.
You can revert the state of a file to its last committed state with
$ hg revert filename
(It is also possible to revert to an earlier committed state, but that is a bit more complicated.)
When you are satsified that you have completed part III, commit with
the message "finished part III
".
Two words or phrases are anagrams of each other if they contain all the same letters (with the same frequency). Examples include
Write a program Anagrams.java
that prompts the user for two
strings.
Test whether the two strings are anagrams of each other.
Ignore spaces, punctuation, and capitalization.
Your program should also include an opening documentation block
(following the style we've seen in class)
and a comment on every variable you declare.
Again, you might want to copy one of your other programs to get a starting point. Don't forget to add your file to the repository and commit (at least) the final version.
Make a script file that shows you compiling your programs and running them with inputs that make it clear that they work correctly. (See how to script.)
Print your two .java
files and the script. (See how to print.)
Use handin
to hand in the files as lab2
, too. (See
how to do that, too.)
There are special ways to copy a directory with a Mercurial repository; those are described on the Mercurial page.