Computer Science II, Lab 02
Tuesday, August 28th
Objective: Play with command line arguments. Get more practice with Strings.
This week's lab will ask you to complete a series of "small" activities designed to use some of the command line things we talked about in Session 3 and Session 4.
In addition to working with the command line, there are two "hidden" goals in this week's lab.
If you get stuck, you should ask a lab assistant for some direction.
Write a simple class called "EchoAll" which contains only a main method. This method should echo all of the command-line arguments to System.out with a label for where the argument was in the argument list. For example,
% java EchoAll Greetings Dr. Schafer, you excellent teacher!
Should produce:
args[0] is Greetings args[1] is Dr. args[2] is Schafer, args[3] is you args[4] is excellent args[5] is teacher!
[Q1] You should make a list of any "special cases" and how this code should handle them. As you write your code, you should then make sure that you handle them all.
[SIG1] When you feel that your code is working properly, have a lab assistant test and sign off on your Echo class.
Write a simple class called "Calculate" which contains only a main method. This method expects exactly three arguments - two numbers separated by either a plus sign or a minus sign.
If a valid set of arguments is provided, the program should calculate the sum or difference (depending on the symbol) between the two numbers and display the result. If an invalid set of arguments is provided then a "usage" message is displayed. A sample set of executions is as follows:
% java Calculate 3 + 5 8 % java Calculate 12 - 7 5 % java Calculate 4 + 5 + 6 Usage: <number> [+|-] <number> % java Calculate 12 * 42 Usage: <number> [+|-] <number>
[Q2] You should make a list of as many "wrong cases" as you can think of and consider which ones you are able to handle. You may not have the knowledge to handle all of the wrong cases, but see how many you can come up with. Talk to Mike or Dr. Schafer if you have questions about whether or not you should handle a case.
[SIG2] When you feel that your code is working properly, have a lab assistant test and sign off on your Calculate class.
A sample set of executions is as follows:
% java Analyze -max 3 5 24 7 -2 1 3 The max value is 24 % java Analyze -min 3 5 24 7 -2 1 3 The min value is -2 % java Analyze -max 3 The max value is 3 % java Analyze 3 5 24 7 -2 1 3 Usage: [-max|-min] <number>* % java Analyze Usage: [-max|-min] <number>*
[Q3] You should make a list of as many "wrong cases" as you can think of and consider which ones you are able to handle. You may not have the knowledge to handle all of the wrong cases, but see how many you can come up with. Talk to Mike or Dr. Schafer if you have questions about whether or not you should handle a case.
[SIG3] When you feel that your code is working properly, have a lab assistant test and sign off on your Analyze class.