This course has already ended.

⌛⌛ Parameter table

Place your code into a file named Parameters.java.

Write a program that prints out all its command line parameters in ascending alphabetical order in a “nicely” formatted table. The table itself is drawn by printing characters in a suitable manner. Compose the table as follows:

  • The table has two columns.

    • The first column shows row numbers. The first row has number 1, the second number 2, and so on.

    • The second column contains the command line parameters in ascending alphabetical order. The first row contains the alphabetically smallest parameter, the second row the alphabetically second smallest parameter, and so on.

  • Both columns will be just wide enough to fit the widest value in that column plus one padding space character on its both sides.

    • Note that the two column widths are independent of each other.

    • The values in the first column are aligned to the right, and the values in the second column are aligned to the left.

      • Extra space is filled with spaces.

  • The table cells are surrounded by borders that are expressed by characters as follows:

    • The outer border of the table consists of hash characters ‘#’.

    • The two columns are separated by a vertical border consisting of vertical bar characters ‘|’.

    • Two rows are separated by a horizontal border consisting of minus characters ‘-‘.

    • Exception: inner crossings of vertical and horizontal borders consist of plus characters ‘+’.

Tips

You first need to find out the widths of both columns, which essentially requires you to determine the widest values in them.

The widest value in the first column is the last row number. You may e.g. simply convert the last row number into a String and then check the length of the result.

The widest value in the second column is the longest command line parameter. Finding this requires you to e.g. loop over all command line parameters.

Once both column widths are known, it should be quite simple to to print the actual table. It might be a good idea to use e.g. the function System.out.format that allows you to specify a width and alignment to use when printing a value.

Example

The first test runs your program as follows:

java Parameters Chad Benin Angola Algeria Finland Romania "Democratic Republic of the Congo"
Bolivia Uzbekistan Lesotho

Note how the parameter “Democratic Republic of the Congo” was enclosed in quotes in order to inlude spaces within it. The expected program output is:

#########################################
#  1 | Algeria                          #
#----+----------------------------------#
#  2 | Angola                           #
#----+----------------------------------#
#  3 | Benin                            #
#----+----------------------------------#
#  4 | Bolivia                          #
#----+----------------------------------#
#  5 | Chad                             #
#----+----------------------------------#
#  6 | Democratic Republic of the Congo #
#----+----------------------------------#
#  7 | Finland                          #
#----+----------------------------------#
#  8 | Lesotho                          #
#----+----------------------------------#
#  9 | Romania                          #
#----+----------------------------------#
# 10 | Uzbekistan                       #
#########################################

A+ presents the exercise submission form here.

Posting submission...