CMSC 206 (Data Structures)

Assignment#1
Due:  September 12, 2012 by 11:59pm

Due:  September 14, 2012 by 11:59pm


Introduction

The purpose of this assignment is to get you started in writing programs in Java.

Write a program that reads a list of integers from the command line and prints out the min, max, and mean of the list.

Here are some example traces of running this program:

> java Assignment1 2 4 6
   Min: 2
   Max: 6
   Mean: 4

> java Assignment1 -4 0 -9 2
   Min: -9
   Max: 2
   Mean: -2.75

> java Assignment1 3.14 2 4 90
   Error:  input "3.14" is not a valid integer
   Usage:  java Assignment1 <list of integers>

> java Assignment1 CMSC206
   Error:  input "CMSC206" is not a valid  integer
   Usage:  java Assignment1 <list of integers>

> java Assignment1
   Error:  no input given
   Usage:  java Assignment1 <list of integers>

Notice that much of the program will consist of error checking the input.

Getting Started

The first thing you should try to run is the basic HelloWorld program, given below:

public class HelloWorld {
    public static void main(String[] args) {
        System.out.println("Hello World!");
    }
}

Copy this code into a file called HelloWorld.java and the compile it using the following command:

> javac HelloWorld.java

This command should produce the file HelloWorld.class, which is the compiled Java program.  You can then run the HelloWorld program by:

> java HelloWorld
Hello World!

Implementation Hints

Here is a rough structure you can use for your program:

Once you have HelloWorld working, here are some suggested steps to develop your assignment.  Make sure you have a working program (debugged and tested) at the end of each step before moving to the next.  Document your code as you go with comments.

  1. Copy HelloWorld.java into Assignment1.java and rename the class.  Compile and run it to be certain it works.

  2. Modify the program to print out all of the input arguments.

    The input arguments are stored as strings in the array args, as shown in the HelloWorld program above.  You can test the number of input arguments based on the length of this array.  Just print each element of the args array to System.out.

  3. Add the error message that occurs whenever the user does not specify any input arguments.

    Java contains multiple output streams, each with different purposes.  System.out should be used for normal output, and System.err should be used for errors:
    System.out.println("This is a normal output message.");
    System.err.println("This is an error message.");

    To exit your program before reaching the end of main(), you can use the following code:
    System.exit(1);  // the number (in this case 1) denotes a status code.  By convention, exiting with a code of 0 indicates normal termination, and anything else denotes some error.  Different errors can use different status codes. 

  4. Define an int[] array within the program and initialize it within your code to contain a bunch of integers.  Add methods to compute and output the min of that array.

  5. Modify your code to also compute and output the max (simultaneously while iterating through the array to compute the min).

  6. Modify your code to also compute and output the mean (again, simultaneously while iterating to compute the min and max).

  7. Modify your code to initialize the int[] array from the String[] command line arguments.

    To convert a String variable str that contains an integer in text form to an int variable, you can use the following code:
    int n;   // why do we need to declare n outside of the try-catch statement?
    try {
        n = Integer.parseInt(str);  // This line extracts the integer from the String variable str and stores it in n. 
    } catch (Exception e) {
        // code that is here is executed if the conversion fails, which is useful for error checking
    }


  8. Add in error checking for the conversion to integers.

  9. Make sure that your program meets all of the requirements.  Test it thoroughly, making certain that the output matches the output trace given above in the Introduction.