Assignment #22

Instructions

  1. Write a Java program that does the following:
    1. For output, use either the System.out.println() or JOptionPane.showMessageDialog() methods.
    2. The user should enter INTEGERS as COMMAND LINE ARGUMENTS.
    3. The user can enter any amount of INTEGERS as COMMAND LINE ARGUMENTS.
      For example, the user could enter no command line arguments at all.
      Or just 1 command line argument.
      For example: 12345
      Or they may enter 5 command line arguments.
      For example: 2 2 2 4 6
      Or they might enter non-integer data.
      For example: one 2 three 5
    4. Your main() method should do the following:
      1. Display the number of command line arguments.
      2. Display all of the command line arguments.
      3. Count the number of the command line arguments that are INTEGERS.
      4. Display the count.
      5. Display all of the command line arguments that are INTEGERS.
      6. Calculate the sum of the INTEGERS.
      7. Display the sum.
      8. Calculate the average of the INTEGERS.
      9. Display the average.
    5. Use a try-catch block to determine the difference between integers and non-integer data. If it is not an integer, Integer.parseInt(args[index]) will throw an exception.
    6. Use Integer.parseInt(args[index]) to convert the Strings to Integers.
      You might want to create a second for-loop to add the Integers together.
      A second option is to store the Integers in an Integer array, and then add the Integer array elements together in another for-loop.
    7. Don't forget to include Java documentation (javadoc) comments in your program. See the Java Coding Standard for details.
    8. See the syllabus for the Assignment Grading Guidelines for your program assignments.

Example Input and Output (I/O)

Command line arguments: (none)
Program output:
Number of command line arguments is 0.
Command line arguments: 12345
Program output:
Number of command line arguments is 1.
They are: 12345
The number of integers is 1.
They are: 12345
The sum is: 12345
The average is: 12345
Command line arguments: 2 2 2 4 6
Program output:
Number of command line arguments is 5.
They are: 2 2 2 4 6
The number of integers is 5.
They are: 2 2 2 4 6
The sum is: 16
The average is: 3.2
Command line arguments: one 3 three 4 five
Program output:
Number of command line arguments is 5.
They are: one 3 three 4 five
The number of integers is 2.
They are: 3 4
The sum is: 7
The average is: 3.5

Click to validate the HTML code

Click to validate the CSS code