CS151 -- Lab 4

Command Line Arguements

In this lab we will be working with "command line arguements" to Java programs. By "command line arguements", I mean things that appear on the command line after "java Xxx". For example suppose you have a class names CLA:
        javac CLA.java
        java CLA a s this 1
the command line arguements would be "a s this 1"

Step 1

Actually create a class called CLA (acronym for Command Line Arguements). This class should only have a main method. Recall that the "signature" of a main method looks linked
        public static void main(String[] args)
the "args" variable is there to get the command line arguements.

Inside you main mathod write a loop to print all of the command line arguements.For instance, in the example above, your program might should output

        0 a 
        1 s 
        2 this 
        3 1
Make sure the output of your program agrees with this sample. Also try your program with other command line arguements.

Default Arguements

Frequently, during developemnt, you want to use the same set of arguements every time; and do not want to bothered with entering them. This is expecially true within VSC. If you use the run command it is difficult to set command line arguements (I do not even know if it is possible). What you can do, is to set default arguements within your code.

The idea is to create an array of strings that the program will use if there are no command line arguements. That array would look exactly like the command line args. For instance (keeping witht he example above),

        String[] myDefaults = {"a", "s", "this", "1"};
Then use this array of default arguements when the program starts with no arguements from the command line.

Extend your main method to have a set of default arguements and use them when no command line arguements are provided.

In the future many of the assignments will use command line arguements. The technique implemented in this lab should make your debugging and development easier.

On Completion

Send your code to gtowell151@cs.brynmawr.edu .