CSE 142 - Summer 2003 - Homework 7 Assigned: MONDAY, August 11, 2003 Written Assignment Due: MONDAY, August 18, 2003 @ 9:30pm Programming Assignment Due: MONDAY, August 18, 2003 @ 9:30pm A link to the turn-in pages will appear on the course calendar webpage. The hand-out that accompanies this assignment is an implementation of a simple command prompt (shell), analogous to the Windows Command Prompt, Macintosh Terminal and Unix *sh programs. This is not a pairs programming assignment - please work individually. The assignment is very open-ended and you need to think creatively to complete a good solution. It is a good idea to start early. ------------------ Written Assignment ------------------ To gain full credit on each question, your answer must have no significant flaws, must be clear to the reader, must be complete, and must not be verbose. Please be concise and to the point. As always, you should record your answers to the questions in a plain-text file (for example, "answers.txt") and then submit that text file along with the rest of your electronic turn-in. 1. File I/O. a) Explain the purpose of the java.io.File class. b) Explain the purpose of the java.io.FileInputStream class. c) Explain the purpose of the java.io.FileOutputStream class. d) Explain the purpose of the java.io.BufferedReader class. Please write no more than 10-12 lines about each class. 2. Console I/O. a) Explain the control flow of the Shell class. You only need to read the Shell class and Command interface. b) Explain the control flow of the ListFile class. c) What is the java.util.StringTokenizer class useful for? Please write no more than half a page about each class. 3. Sorting and searching. a) Implement a comparator class for java.io.File objects. Your class must implement the java.util.Comparator interface. All other decisions are left to you. b) Explain in 10-12 lines of prose how your comparator can be used to sort a collection of file objects. Read the documentation for the class java.util.Arrays first. 4. Find and explain three bugs or poorly designed aspects of the shell and completed commands in the hand-out code. Please write no more than 10-12 lines about each of the three entries. This will be easier if you complete, or at least begin, the programming assignment first. 5. Write half a page about your solution to the programming assignment. Please explain what shell extensions you completed successfully vs. what you could not complete, and what you found simple to implement vs. what was difficult. Bullet format will be more appreciated than long paragraphs of prose - be very brief. ---------------------- Hand-out Code ---------------------- The core of the hand-out code are the files Shell.java and Command.java. The Shell class implements a simple command prompt and the Command interface defines the behavior of the classes that implement various shell commands. The hand-out also contains the following example shell commands: - CreateFile creates a new file (as per the Unix touch command), - RenameDirectory renames a directory (Unix mv, DOS move), - DeleteDirectory deletes an empty directory (Unix/DOS rmdir), - ListFile lists (or prints) the content of a file (Unix cat, DOS type), - Help prints instructions to standard output, and - Exit terminates the program. To run the shell, unzip the code into a directory of your choice. Then run a command prompt. On Windows you select Start Menu -> Program Files -> Accessories -> Command Prompt Change to the directory where you put your code, e.g. cd "c:\documents and settings\joeuser\my documents\cse142\hw7" or cd c:\temp\hw7 (The quotation marks are only necessary when the path contains white space, for example "documents and settings".) You can compile the code with the command javac -classpath . *.java The -classpath switch, followed by the dot, simply indicates to the compiler that the files to be compiled are all in the current directory. If you get tired of typing this command, then use the command prompt history; just press the 'up' arrow to access commands you typed in before. When you have compiled everything you can run the shell with the command java -classpath . Shell You won't see anything much happen - but you will be running in your own shell, on top of the command prompt you started up earlier. Try the help and exit commands first. Then try the command type Exit.java and figure out what happens when you hit enter. Now you are ready to tackle the programming assignment. Note that you can also use DrJava to write and compile code, as before. The only thing DrJava won't do for you is properly execute the shell. The reason is that the version of DrJava we're using doesn't have very good support for console I/O. ---------------------- Programming Assignment ---------------------- Your assignment is to augment the shell with additional commands. You should implement the following commands: - CreateDirectory creates a new directory (Unix/DOS mkdir), - DeleteFile deletes a file (Unix rm, DOS del), - RenameFile renames a file (Unix mv, DOS move), - ListDirectory lists the content of a directory (Unix ls, DOS dir), - CopyFile copies a file (Unix cp, DOS copy), - FindString reads a file and prints the lines containing a search string (Unix grep, DOS find), and - ListDirectoryBySize lists the content of a directory in order of decreasing file size, with directories appearing first. Finally select an appropriate shell command of your own choice and extend it by adding a parameter to control sorting of the output. You should use your comparator class (see the written part) to do this. The CreateDirectory class should be in CreateDirectory.java, etc. Please don't change the class names, as the turn-in server will be configured to expect the names given here. Each command has to implement the Command interface and you also have to integrate the commands into the shell by modifying the Shell.parse method. It is a good idea to use the DOS or Unix commands to figure out how the various shell commands should behave. Using a Windows command prompt, type dir to see what the Windows list directory command does, etc. You have to make independent choices to complete this assignment - make sure your code is carefully documented. Also indent everything correctly and write clean code, as the grading will be more concerned with programming style than before. This programming assignment has a number of independent parts. However, if you are clever you will be able to use some of the code you wrote for one command to help you with the implementation of another. This will reduce your total work but requires a little thinking up front. ---------------------- One Final Trick! ---------------------- You don't have to do this part and you won't get any credit even if you do! But it's rather neat - so you might take a shot if you finish everything else early and are still enjoying yourself. Your TA will certainly be impressed. It's not that hard to extend your shell so that you can use it for all your programming work. It just takes one more command. That command is typically called exec (short for execute) and it lets you execute any other program from the shell. For example, you could type Notepad Shell.java if you wanted to edit the shell code using Windows Notepad, and then you could type javac -classpath . *.java to compile a new version of your shell (but then you'd have to exit and re-run to get the new functionality). All you need to do to implement the exec command is to read the Javadoc for java.lang.Runtime.exec(String[] cmdarray) and then use that method in a new class (say, Exec) that implements the Command interface. To change the shell so you don't have to type a command identifier but can instead type the name of whatever program you want to run, all you have to do is change the final 'else' block in the parse method of the Shell class. It should try your Exec class and only print "not found" if the execution doesn't work. Have fun!