Com S 362 --- Object-Oriented Analysis and Design HOMEWORK 1: OBJECT BASICS AND JAVA (File $Date: 2003/01/18 11:40:04 $) Due: problem 1, January 22, 2003. The purpose of this homework is to learn the basics of working with objects and classes (object-based programming) in Java, and to have you figure out the mechanics of writing and running Java programs. This is an individual homework, and is not to be done in teams. READINGS Read chapters 1-2 of Ken Arnold, James Gosling, and David Holmes's book The Java Programming Language Third Edition (Addison-Wesley, Reading, Mass., 2000). If you are new to Java you might also want to read the interesting (to you) parts of the "new to Java" section on-line at http://developer.java.sun.com/developer/onlineTraining/new2java/ See also the course "Running Java" web page for help in running Java: http://www.cs.iastate.edu/~cs362/running-java.shtml 1. (40 points) [Sorting] Write a java program (i.e., an application) that takes two file names as command line parameters, and sorts the lines of input found in the first file into ascending order, and prints them to the second file. The lines should be read and sorted as Strings. The file may contain duplicate lines, and these duplicates should be preserved in the output. Your program should create the second file. Your program should have a main method in a class named SortLines in a package named cs362hw. That is, the file SortLines.java should look like: package cs362hw; /* ... */ /** ... @ author */ public class SortLines { /* ... */ } You may, of course, use other classes, which you may put in other files. (Try to put all of the "application logic" in another class.) It can use several methods as well. For each class and for each method, write a javadoc comment (see Arnold, Gosling, and Holmes's book, section 1.3 and also chapter 14 if you wish), for the format of these. Since your main method is in a class named cs362hw.SortLines, after compiling, you would run your program as follows: $ java cs362hw.SortLines inputFile.txt outputFile.txt For example, if inputFile.txt contained the following lines the quality of OOness a thing of simplicity the quality of OOness something clear object-oriented design, rules the quality of OOness OOA&D the previous line was blank #@!- program, hard to modify because not OO then after the run shown above, the file outputFile.txt would contain the following (the first line is the empty one) #@!- program, hard to modify because not OO OOA&D a thing of simplicity object-oriented design, rules something clear the previous line was blank the quality of OOness the quality of OOness the quality of OOness If the input file is empty, the output should be empty also. Your program should give an error message, and exit with a non-zero error code if any problems occur. To start with, it should check that it is given exactly 2 arguments: $ java cs362hw.SortLines SortLines: expecting two filename arguments $ echo $? 2 The "echo $?" command shows the exit code of the program. It works on Unix. Eclipse will also tell you about this directly, without having to use the "echo" command. For example, it should have the following behavior for files that don't exist. (More explanation is given below.) $ java cs362hw.SortLines myNonFile junk.out SortLines: I/O error: myNonFile (No such file or directory) $ echo $? 1 The part of the message that reads "myNonFile (No such file or directory)" comes from the exception's message argument, which you can obtain using e.getMessage(), if e is a java.io.IOException. If the second file already exists, your program should not overwrite it, but instead should complain: $ java cs362hw.SortLines ../../hw1/stanzas.txt `alreadyexists' SortLines: I/O error: Cannot create file named `alreadyexists' $ echo $? 1 If your program encounters other I/O problems, these should also be reported. For example, on Unix (unless you have root permissions) you can't create a file in the directory /, so you should see: $ java cs362hw.SortLines ../../hw1/stanzas.txt /junk SortLines: I/O error: Permission denied $ echo $? 1 You should use exception handling for dealing with possible errors in reading to or writing from files, instead of calling operations to test if these will work in if-statements. See sections 1.13 and chapter 8 of TJPL3e for details on exception handling. HINTS An important part of OO programming is learning your way around the various application programmer interfaces (APIs) in the language. These can be daunting at first, but it's all part of knowing the language. You can also find some of the necessary information in the book The Java Programming Language Third Edition (Addison-Wesley, Reading, Mass., 2000); we'll refer to this below as TJPL3e. Experienced Java programmers rely on the javadocs (HTML documentation produced by the javadoc tool and included in the release). You should try that as a supplement to reading the information in the book to see what their strengths and weaknesses are. Here are some hints and pointers on where to find things in TJPL3e. Command line parameters are described in TJPL3e section 2.9. For Strings, see section 1.10 and chapter 9 of TJPL3e. You may want to store the lines in a collection; for this it may be convenient to use a collection type found in the java.util package (see chapter 16), for example ArrayList, as described in TJPL3e section 16.6.1. You shouldn't have to write the sort algorithm yourself; instead you can use the sort() method provided by the Collections class. For File I/O, you should use the LineNumberReader (see section 15.4.9) and PrintWriter (see section 15.4.8) classes, found in the package java.io. You can create a LineNumberReader on a file by first creating a FileReader object, using the file name as an argument to the constructor. You can create a PrintWriter object by first creating a FileWriter object. To create the output file, use a File object, created using the file name, and call the createFile method on it. To exit with a non-zero error code, use System.exit; to print an error message to the user's console, use System.err.println. The class System is in the java.lang package. Your program can (and probably should) use several classes in Java. Evolutionary design: as a first step, write a program that just copies one file to another. Then use text files. Then work line by line. Then sort the lines. TESTING Your code should compile, and run correctly under Java 1.4.1 (or 1.3.1, which you may have to use if you have a Mac). Test your program on our sample input files. You can find our sample inputs in the directory /home/course/cs362/public/homework/hw1/ on the department Unix machines. These are also available through the web page. Also test it with the wrong number of command line arguments, and on input and output file names that don't exist or have other problems (see above). WHAT TO HAND IN Hand in a printout of all of the code you wrote. By doing this you certify that your code works correctly. If your code does not work correctly you can still hand it in for partial credit, but attach an explanation of what about it does not work. GRADING You will be graded, in part, on how clear your code is. Excessively long code will be penalized: don't repeat code in multiple places, don't write the same expression in multiple places. Be sure your code has javadoc comments for all classes and methods. Your code should be sensibly indented so it is easy to read (Eclipse can do this for you automatically.) It's important to follow all of our directions as exactly as you can, in particular, making sure that the program does the right things on errors. Make all of the fields in the classes you write private. Be sure your name is in the comments in your code.