Exercise #1: Program arguments

To start up a stand-alone Java application, you provide the name of a Java class that contains a method called main. The operating system causes the program to be executed by locating that method and running it.

The main method of such a driver class takes exactly one argument, of type String[]. Under our implementation of Java, the elements of the array that is passed to code are the strings that appear on the command line that invoked the program, after the name of the class, and are called program arguments. For example, if we have an application in which the driver class is called Driver, and we start up the application by typing

java Driver foo bar baz

at the shell prompt, the array passed to Driver.main contains the three program arguments "foo", "bar", and "baz", in that order.

Many applications can be controlled by command-line options, which are program arguments that begin with a designated option character. Conventionally, Unix applications use the hyphen as an option character, and each command-line option (after the option character) begins with a letter and consists entirely of letters, digits, and hyphens.

Options are of three kinds: Boolean, integer, and string. The programmer chooses a default value for each of the options that her program supports. This default value is used unless it is overridden by an explicit command-line option. The value of a Boolean option is indicated simply by the presence or absence of the command-line option; in other words, if the default value of a Boolean option is false, then including the Boolean option as a program argument changes its value to true, and vice versa. The value of an integer option is given by an additional program argument, immediately following the command-line option; for example, to set an integer option called size to 3000, one would include in the command line the program arguments -size 3000. Similarly, the value of a string option can be set by an additional program argument; to set a string option called name to ``wombat'', one would write the program arguments -name wombat.

The assignment is to design, write, and test an OptionValues class in which the values of the command-line options supplied to a Java program can be stored. The constructor for the class should take an argument that indicates which options the program is prepared to support and what the default value of each of these options is.

The class should support a method that takes the array of strings in which the program arguments are stored and recovers the values of the options specified on the command line. This method should throw an exception in any of the following situations:

The OptionValues class should also support a method that returns the value of any option supported by the program -- the value indicated by the command line, if the option was a program argument, and otherwise the default value.

If you like, or if you find it necessary, the OptionValues class may support additional methods of your own design, and you may introduce other classes to help you define the OptionValues class. For instance, you may want to define your own exception classes.

Please submit the source code for your test program, including the OptionValues class, and a transcript of your test runs. Test thoroughly. This exercise is due on Monday, September 13.


created September 3, 1999
last revised September 7, 1999

John David Stone (stone@cs.grinnell.edu)