CCSU Fall 1998 Project 1 Posting Date: September 9, 1998 Due Date: September 21, 1998 Relevant Demos: Demo 2 (fh.java). This project comes in only one part. It covers the material on file IO. Background: The famous UNIX od (octal dump) utility is one of the great utilities provided by UNIX. Here is what od does: Typing od -b file1 at the UNIX command prompt prints the contents of file1 to standard output as a series of octal numbers, one octal number per byte. If the file is a simple ASCII text file, the octal numbers correspond to the ASCII codes of the characters in file1, INCLUDING end of line characters. The od utility comes with a series of options. Thus od -b file1 prints bytes as octal numbers, but od -c file1 prints all printable characters as actual ASCII characters, all CONTROL characters (like CTRL C or CTRL N) as the corresponding letter preceded by a backslash, and all other bytes as octal numbers. Here is the first part of of the result of the command od -c fh.java on the CS Department's UNIX system: / / D e m o 2 : f h . j a v a \n \n / / A F i l e D e m o - - A F i l e H a n d l e r U t i l i t y \n / / A J a v a S c r e e n (Some line numbers produced by od have been omitted.) Here is the first part of the result of the command od -b fh.java on the same system: 057 057 040 104 145 155 157 040 062 072 040 146 150 056 152 141 166 141 012 012 057 057 040 040 101 040 106 151 154 145 040 104 145 155 157 040 055 055 040 101 040 106 151 154 145 040 110 141 156 144 154 145 162 040 125 164 151 154 151 164 171 012 057 057 040 040 101 040 112 141 166 141 040 123 143 162 145 145 156 040 (Again, the line numbers have been omitted.) The octal numbers 057 057 represent the // at the beginning remark of demo 2. The 040 is the space, and the 104 is the capital D. 057, 040, and 104 are the ASCII codes in octal for /, for a space, and for a capital D. Description: Write od (octal dump) as a Java screen based application. The program od.java should know what to do from its command line arguments, as UNIX od does, and as fh.java does. The program should send its output to standard out, as UNIX od does. (Standard out is the terminal screen, what Java's System.out.print prints to. Implement both the -b and -c options. Don't bother to implement any of the other UNIX od options, and don't bother to have the output come in a nice square, just print it all on one line and let the monitor break the lines. But do print out octal numbers in the range 000 to 077 (octal) with leading zeroes, so that, for example 007 comes out as 007 rather than 7 and 012 comes out as 012 rather than 12. And do command line argument checking as demo 3 (fh.java) does. You will find the following code useful. To convert an int b to its octal representation as a String, use the following code: Integer.toString(b, 8) Use System.out.print to print the octal representation of each byte you read from the file. Unfortunately, this representation does not include leading zeroes, so you will have to test the range of b. You will also find it useful to know a quirk Java inherits from its predecessor C. An int constant with a leading zero in a program is interpreted as an octal number, not a decimal number. Thus 077 is interpreted as an int equal to 63 decimal, not 77 decimal. Coincidentally, 077 is the largest int with a two digit octal representation, not counting the leading zero of course. Test your od program on the source text of the program (the .java file) and the executable image of the program (the .class file). Also, test your od program on the same text file from an ftp site on a UNIX system (README files are always good choices) gotten two ways: by setting the transfer type to ascii, and then setting it to binary. (If you use UNIX as your platform, the ftp both ways from an NT or Mac machine.) See if you can see the difference in the two downloads when you run od -c on both files. WARNING: The od program produces LOTS of screen output. One way to get one screenful at a time is to invoke it from the MS DOS command prompt like this: java od -b fh.java | more When I did this, it took a very long time for the first screenfull to appear, and the remaining screenfulls were lost. These are problems with MS DOS's and NT's implementation of pipes and the more command, and have nothing to do with the functioning of your program. Another way might be to type java od -b fh.java > fh.od more < fh.od This is clumsy, but may give you better and quicker results. Just typing java od fh.java worked just fine, but most of the screen output scrolled off the top of the screen. General Instructions: (1) Hand in a FOLDER--the kind with a pocket, you can buy them in the bookstore--with YOUR NAME on it, containing in the pocket (1) printed copies of the text of the od class, and (2) a 3.5" floppy disk with the .java and .class files for your class. (2) Your files MUST be named od.java and od.class, so I can easily find them. Your class MUST be named od. (No period at the end.) (3) Each of your programs must begin with a remark box containing the following information, formatted as indicated: PROGRAMMER: Your name. DATE: Current date. COURSE: Course number and semester (CS 152, Fall 1998). PROJECT: Project number (Project 1 in this case). PROGRAM: Program name (use the class name described above). DESCRIPTION: A brief description of what the program does. FILES: The files your program uses (user specified file1 and standard out). LANGUAGE: Java JDK version you used (4) Each method in your program must begin with a remark box as illustrated in the demos. Your program MAY NOT use a depreciated api. (If you do, a compiler warning message comes up. Usually, this means you used Java 1.0 rather than Java 1.1 event handling.)