⌛⌛ Parameter table

The program is built using Maven. Place the pom.xml file in the round3/parameters directory of your local repository and create to this directory the src/main/java subdirectory. You may name your classes freely, as long as your files are in the round3/parameters/src/main/java directory. If you create your project with NetBeans, it’s recommended to leave the Package: field empty, to ensure the files are placed in the correct directory.

Write a program that first prints the prompt Parameters:, then reads parameters from the user (System.in), each on their own line, and finally prints out all the read 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 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 read parameter. Finding this requires you to, e.g. loop over all the read parameters.

Once both column widths are known, it should be quite simple 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.

Because the number of parameters is not known in advance, some way of detecting when all the parameters have been read must be implemented. In this task the end of the parameters is signalled by inputting an empty line (""). The empty line should not be added to the list of read parameters.

The automatic tests, and the ones given below, assume that you make the following definitions in your pom.xml project file:

  • The value of artifactId is parameters.

  • The value of version is 1.0.

  • The value of the maven.compiler.source and maven.compiler.target elements is 17 or lower. The grader uses Java 17, so any newer versions won’t work.

  • A Onejar plugin definition where the value of mainClass element refers to the main class of your program, which you can name freely in this task. For example, if your main class is named Parameters, the element value is Parameters.

NB: Do NOT create several Scanner instances reading the standard input (System.in). Only create one and use the same one for the whole program. For technical reasons the grader won’t grade your submission correctly if you use more than one Scanner instance to read input from the standard input. The same holds true if you use another class like InputStreamReader to read the user input.

Example

The tests compile your program with the following command:

mvn package

If the compilation is successful the program is run with this command:

java -jar target/parameters-1.0.one-jar.jar

The program outputs the prompt Parameters: after which the parameters are read from the user (System.in), each on their own line. In the first test they are:

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

The expected program output is:

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

Submitting

Make sure before submission that your files are in the /round3/parameters/ directory and that you have pushed the latest changes to your personal remote repository. When ready, enter the URL of your personal remote directory in the field below. The URL is https://course-gitlab.tuni.fi/compcs140-spring2024/------, where ------ denotes your Tampere university username.

A+ presents the exercise submission form here.

Posting submission...