MyCat
¶
MyCat (Choose Your Own Adventure)¶
MyCat (Choose Your Own Adventure)
Prerequisite Information
This practice activity should be done on Odin, so make to complete all of the tutorials in the Unix chapter before attempting it and that you followed the steps in that chapter to activate the CSCI 1302 shell profile on your Odin account. This practice activity also assumes that readers have worked through the material in the Exceptions chapter prior to starting the activity.
Prepare For an Adventure
Step 0: Quick introduction to the cat command
The Unix cat command reads files sequentially, writing them
to the standard output. The file operands are processed in command-line
order. If file is a single dash (-
) or absent, cat
reads from standard input.
After completing this activity, you will have your own version of cat called MyCat that supports reading multiple files.
Step 1: Get and inspect the starter code for MyCat
The starter code for this practice activity is a modified version of a sample solution to the MyCat program that your instructor presented earlier.
Execute the command below to download and extract the files:
sh -c "$(curl -fsSl https://cs1302uga.github.io/cs1302-book/_bundle/cs1302-cat-starter.sh)"
- downloading cs1302-cat-starter bundle...
- verifying integrity of downloaded files using sha256sum...
- extracting downloaded archive...
- removing intermediate files...
subdirectory cs1302-cat-starter successfully created
Change to the cs1302-cat-starter
directory that was created using
cd, then look at the files that were bundled as part of the
starter code using tree.
cd cs1302-cat-starter
tree
.
|-- etc
| |-- ABC.txt
| `-- hello.txt
`-- src
`-- cs1302
`-- cat
|-- MyCat.java
`-- Printer.java
5 directories, 4 files
Step 2: Compile and run the starter code
Here is a quick summary of the classes in the starter code:
Fully-Qualified Name |
Dependencies |
Description |
---|---|---|
|
Utility class for printing files. |
|
|
|
Main class for the MyCat program. |
Compile the starter code. Based on this information, use javac to compile each
.java
file into a directory namedbin
, adjusting the class path as needed to take into consideration each file’s dependencies. The starter code should compile without any modifications; however, it may intentionally include code style issues so that you have an opportunity to fix them later.Sample Solution
javac -d bin src/cs1302/cat/Printer.java
javac -d bin -cp bin src/cs1302/cat/MyCat.java
Use java to run cs1302.cat.MyCat three times, once for each of the two files in the
etc
and once more for standard input. After that, use the cat command that comes with Unix to display the same files (and standard input). Your MyCat program should produce the same output as cat in all three scenarios.Sample Solution
java -cp bin cs1302.cat.MyCat etc/ABC.txt
_ ____ ____ / \ | __ ) / ___| / _ \ | _ \| | / ___ \| |_) | |___ /_/ \_\____/ \____|
java -cp bin cs1302.cat.MyCat etc/hello.txt
hhhhhhh lllllll lllllll !!! h:::::h l:::::l l:::::l !!:!! h:::::h l:::::l l:::::l !:::! h:::::h l:::::l l:::::l !:::! h::::h hhhhh eeeeeeeeeeee l::::l l::::l ooooooooooo !:::! h::::hh:::::hhh ee::::::::::::ee l::::l l::::l oo:::::::::::oo !:::! h::::::::::::::hh e::::::eeeee:::::eel::::l l::::l o:::::::::::::::o!:::! h:::::::hhh::::::h e::::::e e:::::el::::l l::::l o:::::ooooo:::::o!:::! h::::::h h::::::he:::::::eeeee::::::el::::l l::::l o::::o o::::o!:::! h:::::h h:::::he:::::::::::::::::e l::::l l::::l o::::o o::::o!:::! h:::::h h:::::he::::::eeeeeeeeeee l::::l l::::l o::::o o::::o!!:!! h:::::h h:::::he:::::::e l::::l l::::l o::::o o::::o !!! h:::::h h:::::he::::::::e l::::::ll::::::lo:::::ooooo:::::o h:::::h h:::::h e::::::::eeeeeeee l::::::ll::::::lo:::::::::::::::o !!! h:::::h h:::::h ee:::::::::::::e l::::::ll::::::l oo:::::::::::oo !!:!! hhhhhhh hhhhhhh eeeeeeeeeeeeee llllllllllllllll ooooooooooo !!!
java -cp bin cs1302.cat.MyCat -
#### hello hello #### world world #### C-d
cat etc/ABC.txt
_ ____ ____ / \ | __ ) / ___| / _ \ | _ \| | / ___ \| |_) | |___ /_/ \_\____/ \____|
cat etc/hello.txt
hhhhhhh lllllll lllllll !!! h:::::h l:::::l l:::::l !!:!! h:::::h l:::::l l:::::l !:::! h:::::h l:::::l l:::::l !:::! h::::h hhhhh eeeeeeeeeeee l::::l l::::l ooooooooooo !:::! h::::hh:::::hhh ee::::::::::::ee l::::l l::::l oo:::::::::::oo !:::! h::::::::::::::hh e::::::eeeee:::::eel::::l l::::l o:::::::::::::::o!:::! h:::::::hhh::::::h e::::::e e:::::el::::l l::::l o:::::ooooo:::::o!:::! h::::::h h::::::he:::::::eeeee::::::el::::l l::::l o::::o o::::o!:::! h:::::h h:::::he:::::::::::::::::e l::::l l::::l o::::o o::::o!:::! h:::::h h:::::he::::::eeeeeeeeeee l::::l l::::l o::::o o::::o!!:!! h:::::h h:::::he:::::::e l::::l l::::l o::::o o::::o !!! h:::::h h:::::he::::::::e l::::::ll::::::lo:::::ooooo:::::o h:::::h h:::::h e::::::::eeeeeeee l::::::ll::::::lo:::::::::::::::o !!! h:::::h h:::::h ee:::::::::::::e l::::::ll::::::l oo:::::::::::oo !!:!! hhhhhhh hhhhhhh eeeeeeeeeeeeee llllllllllllllll ooooooooooo !!!
cat -
#### hello hello #### world world #### C-d
In the last example, the user entered two lines into standard input, each of which is printed when RET is pressed. To end the program, the user typed C-d to send a special end of file (
EOF
) character to the program so that the program’s internalScanner
object forSystem.in
does not wait for more lines to be typed. You can see the loop for this inPrinter.java
.
Choose Your Own Adventure
Adventure 1: Enhance MyCat
During this adventure, you are tasked with implementing and documenting a new feature to enhance MyCat. The version of MyCat that is included in the starter code only accepts exactly zero or one command-line arguments; however, the version of cat that comes with Unix supports zero or more command-line arguments.
Example Outputs
java -cp bin cs1302.cat.MyCat -
#### hello hello #### world world #### C-d
java -cp bin cs1302.cat.MyCat
#### hello hello #### world world #### C-d
java -cp bin cs1302.cat.MyCat etc/ABC.txt
_ ____ ____ / \ | __ ) / ___| / _ \ | _ \| | / ___ \| |_) | |___ /_/ \_\____/ \____|
java -cp bin cs1302.cat.MyCat etc/ABC.txt etc/ABC.txt
_ ____ ____ / \ | __ ) / ___| / _ \ | _ \| | / ___ \| |_) | |___ /_/ \_\____/ \____| _ ____ ____ / \ | __ ) / ___| / _ \ | _ \| | / ___ \| |_) | |___ /_/ \_\____/ \____|
java -cp bin cs1302.cat.MyCat etc/hello.txt etc/ABC.txt
hhhhhhh lllllll lllllll !!! h:::::h l:::::l l:::::l !!:!! h:::::h l:::::l l:::::l !:::! h:::::h l:::::l l:::::l !:::! h::::h hhhhh eeeeeeeeeeee l::::l l::::l ooooooooooo !:::! h::::hh:::::hhh ee::::::::::::ee l::::l l::::l oo:::::::::::oo !:::! h::::::::::::::hh e::::::eeeee:::::eel::::l l::::l o:::::::::::::::o!:::! h:::::::hhh::::::h e::::::e e:::::el::::l l::::l o:::::ooooo:::::o!:::! h::::::h h::::::he:::::::eeeee::::::el::::l l::::l o::::o o::::o!:::! h:::::h h:::::he:::::::::::::::::e l::::l l::::l o::::o o::::o!:::! h:::::h h:::::he::::::eeeeeeeeeee l::::l l::::l o::::o o::::o!!:!! h:::::h h:::::he:::::::e l::::l l::::l o::::o o::::o !!! h:::::h h:::::he::::::::e l::::::ll::::::lo:::::ooooo:::::o h:::::h h:::::h e::::::::eeeeeeee l::::::ll::::::lo:::::::::::::::o !!! h:::::h h:::::h ee:::::::::::::e l::::::ll::::::l oo:::::::::::oo !!:!! hhhhhhh hhhhhhh eeeeeeeeeeeeee llllllllllllllll ooooooooooo !!! _ ____ ____ / \ | __ ) / ___| / _ \ | _ \| | / ___ \| |_) | |___ /_/ \_\____/ \____|
java -cp bin cs1302.cat.MyCat etc/ABC.txt - etc/ABC.txt
_ ____ ____ / \ | __ ) / ___| / _ \ | _ \| | / ___ \| |_) | |___ /_/ \_\____/ \____| #### hello, world! hello, world! #### bye! bye! #### C-d _ ____ ____ / \ | __ ) / ___| / _ \ | _ \| | / ___ \| |_) | |___ /_/ \_\____/ \____|
In the last example, the user entered two lines into standard input, each of which is printed when RET is pressed. To end the program, the user typed C-d to send a special end of file (
EOF
) character to the program so that the program’s internalScanner
object forSystem.in
does not wait for more lines to be typed. You can see the loop for this inPrinter.java
.To progress with this adventure, modify the
main
method inMyCat.java
so that it works as expected when zero or more command-line arguments are provided by the user. To test your program, compare what it to what cat does when run with the same command-line arguments. Remember to also update the Javadoc comments in your program so that they correctly communicate any new behavior (e.g., the Javadoc comment for themain
method).Sample Solution
/** * Entry point for the application. Zero or more command-line arguments are expected. * If a filename is given as an argument, then the program should print the contents of that * file to standard output. If a single dash (i.e., "-") is given as an argument, then * the program should print the contents of standard input. If no command-line arguments * are provided, then the program should print the contents of standard input. * * @param args The command-line arguments. */ public static void main(String[] args) { if (args.length == 0) { args = new String[] { "-" }; } // if for (int i = 0; i < args.length; i++) { String filename = args[i]; try { if (filename.equals("-")) { Printer.printStdInLines(); } else { File file = new File(filename); Printer.printFileLines(file); } // if } catch (FileNotFoundException fileNotFound) { System.err.printf("Unable to print file %d: %s\n", i, filename); System.err.println(fileNotFound.getMessage()); } // try } // for } // main
To complete this adventure, make sure that you can compile each
.java
file individually using javac and that you can run cs1302.cat.MyCat using java.
Adventure 2: Package Practice
Here is a quick summary of the classes in the starter code:
Fully-Qualified Name |
Dependencies |
Description |
---|---|---|
|
Utility class for printing files. |
|
|
|
Main class for the MyCat program. |
During this adventure, you are tasked with adjusting the file and code to realize the FQN change(s) described below:
Before
After
cs1302.cat.Printer
cs1302.io.Printer
cs1302.cat.MyCat
cs1302.cat.MyCat
To complete this adventure, make sure that you can compile each
.java
file individually under the new FQN assumptions and that you can run MyCat using java to view a couple providing multiple files (and test it with standard input). We recommend that you delete yourbin
directory so that you do not accidentally use old versions when compiling and running.
Adventure 3: Code Style Practice
During this adventure, you are tasked with making sure the code passes all of the course’s code style guidelines.
To complete this adventure, make sure that you can compile each
.java
file individually, then use check1302 to discover code style issues and emacs to resolve those issues without changing the program’s behavior. After you edit the code, be sure to double check that the code still compiles and runs as expected Even minor edits can introduce errors, so it’s always best to do this double checking.
Adventure 4: API Documentation Practice
During this adventure, you are tasked with generating and hosting the API documentation website for MyCat so that users connected to the VPN can access it by accessing your Webwork URL with their web browser (at
/~myid/cs1302-cat-api
wheremyid
is your Odin username).To complete this adventure, generate the files for the API documentation website using the javadoc command, then use the :command`ln` command to create a symbolic link to those files under your
~/public_html
directory so that you can access them using your web browser.If you do not remember how to generate and/or host a Javadoc-based API documentation website, then please refer to following sections in the Javadoc and API Documentation chapter before continuing:
Adventure 5: Interpreter Script Practice
Coming Soon!