Informatics 42 • Winter 2008 • David G. Kay • UC Irvine

Second Homework

Get your work checked and signed off by a classmate, then show it to your TA in lab (preferably by Friday, January 18, since Monday is a holiday).

Part I

Continue with the RPArrayList program you worked on last week, incorporating these changes. Before starting, create a new project in Eclipse called RP2 and import your RPArrayList code into the project, so that you'll have a fresh copy to work on (and will have the ability to roll back to the previous version if you get into trouble).

Part II

A program that takes two command line arguments representing the names of text files and copies the contents of the first file to the second is available at http://www.ics.uci.edu/~kay/java/CopyFile.java.

(a) Install and run the program on your own system. Test it out with a short file; for Eclipse to find the file easily, you should put it into the main folder for your project (so in this case, if you call your project CopyFile, your input file should go into the CopyFile folder in your Eclipse workspace). [To specify command line arguments to a program you're running in Eclipse, follow these steps: (i) Right-click as usual on the .java file containing your main method and choose "Run As"; (ii) Select "Open Run Dialog…"; (iii) In the resulting window, choose the "Arguments" tab; (iv) In the "Program arguments" field, type your command line arguments; (v) Click the "Run" button at the bottom of the window. For more details, check out the "Command-line arguments" section of Alex Thornton's lab writeup, "What's Simple is True" (which we will see again later in the course); it's the source of these instructions.]

Then download the Project Gutenberg version of The Adventures of Sherlock Holmes from http://www.gutenberg.org/dirs/etext99/advsh12.txt (Project Gutenberg is a wonderful resource for non-copyright-protected texts). If you have to work on a slow network connection, you may pick a smaller file; this one is 577K. Run your CopyFile program to make a copy of this file.

Unix systems have the command diff, which compares two files (returning nothing if they're the same), and the commands head and tail, which show the first few lines and the last few lines of a file, respectively. These might be useful in examining these large files. (Windows, unfortunately, does not include analogous commands.)

(b) Modify a copy of your program so that the copied file includes line numbers at the start of each line:

    1: The Project Gutenberg EBook of The Adventures of Sherlock Holmes
    2: by Sir Arthur Conan Doyle
    3: (#15 in our series by Sir Arthur Conan Doyle)
    4:

...

13014: *END THE SMALL PRINT! FOR PUBLIC DOMAIN EBOOKS*Ver.02/11/02*END*

(Note that the line number is formatted and right-justified in a five-character field.)

(c) Make a copy of the original program (without the line numbers); call it Pick. It should take three command line arguments—a string and two file names. The first file name is an existing file, as before, and the second file name is a new file. The new file will contain only those lines in the existing file that start with the specified string (the first command line argument). Check out the methods in the String class for help here.

(d) Write a program called Reverse. It should take two file names as command line arguments and copy the first file to the second, except in reverse order (with the last line of the original file as the first line of the new one). This requires that you store the whole file (as an ArrayList<String>) in your program.

Additional challenges: Like the additional challenges in the lab assignments, these don't yield any actual extra credit, but if you implement some of them, we'll be impressed. (As a general principle, impressing instructors is a good thing because it gives them something special to write in recommendation letters.) Everybody should read these and think about how to code them up, even if you don't actually do it.


Written by David G. Kay, Winter 2005. Modified Winter 2006. Modified by Alex Thornton, Winter 2007, and by David G. Kay, Winter 2008.