Computer Science II, Lab 02

Tuesday, August 28th

 Objective: Play with command line arguments.  Get more practice with Strings.

 


Introduction

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.

  1. Part of the goal with this week's lab is to challenge you to think about a problem and start solving it given minimal direction.  If you are challenged by a problem, remember the concept of "incremental development" - think about the first small piece that you need and get that working.  Sometimes this means writing code that isn't part of the finished program, but helps you convince yourself that some other part of code is correct.
  2. This week we have (will) started talking about the concept of testing.  The activities today ask you to identify the special cases and test cases that you should use when writing this code.  Pay particular attention to these details as a portion of your grade will be based on the quality of your cases.

If you get stuck, you should ask a lab assistant for some direction.


Activity 1:  Writing an "Echo"

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.


Activity 2:  Writing a simple calculator

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.


Activity 3:  Writing a simple analyzer

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.