Task 1: Simple Algorithms

Your task is to implement two small tools:

  1. Diamond: Ask the user for a number and print a diamond with the given line-height.

  2. CSV Parsing: Read a CSV table from a file and print it nicely formatted.

The minimum requirement for passing the functional grading of this task is a correct output and correct error handling of the first tool, “Diamond”.

Use the given template.

Shell

The two tools should be callable from a single shell:

Welcome! Choose one of the following: DIAMOND, TABLE, DIRECTORIES
(PEEGS)# DIAMOND 5
  *
 * *
* * *
 * *
  *
(PEEGS)# TABLE table-test.csv
|                                              FIRST NAME|LAST NAME|AGE|FAVORITE PET|
|========================================================|=========|===|============|
|                                                   Name1|    Name2| 22|         Dog|
|LOOOOOOOOOOOOOOOOOOOOOOOOOOOOoooooooooooooooooooong Name|        n|  1|      Animal|

The shell can be exited on the command-line by pressing Ctrl+C.

1. Diamond

Note

Implement this tool in class DiamondAlgorithm.

The below behavior is expected.

When the user enters command DIAMOND $num and presses enter, the shell checks whether $num is an odd integer >= 3. If it is, a diamond with the specified line numbers is printed:

(PEEGS)# DIAMOND 9
    *
   * *
  * * *
 * * * *
* * * * *
 * * * *
  * * *
   * *
    *
(PEEGS)#

The diamond must be equilateral. There should be no whitespaces after the last ‘*’ of each row.

If no valid integer is entered, the message “Error! Integer expected!” is displayed.

(PEEGS)# DIAMOND n
Error! Integer expected!
(PEEGS)#

If the method DiamondAlgorithm#String getPrintableDiamond(int) receives an invalid integer, it throws an IllegalArgumentException.

1. CSV Parsing

Note

Implement this tool in class TableAlgorithm. The file table-test.csv can be used for testing.

CSV format

The CSV format is a common format for storing data in tables. The first line of the CSV file contains the table headers. Each consecutive line is one row of the table. Columns are separated by ;.

Example file content:

FIRST NAME;LAST NAME;AGE;FAVORITE PET
Name1;Name2;22;Dog
LOOOOOOOOOOOOOOOOOOOOOOOOOOOOoooooooooooooooooooong Name;n;1;Animal

Expected behavior

The below behavior is expected.

When the user enters command TABLE $path and presses enter, the shell checks whether $path is a valid path to a file.

If it is, the file is read and parsed as a CSV according to the above description. The resulting table is printed according to this format:

  1. Each table line starts and ends with character |.

  2. All columns are separated from each other with character |.

  3. The width of each column is equal to the maximum width of all its values.

  4. Field values are right-aligned.

  5. Each column of the table header is separated from the other table rows with a line of ==== the length of the column.

Example:

(PEEGS)# TABLE table-test.csv
|                                              FIRST NAME|LAST NAME|AGE|FAVORITE PET|
|========================================================|=========|===|============|
|                                                   Name1|    Name2| 22|         Dog|
|LOOOOOOOOOOOOOOOOOOOOOOOOOOOOoooooooooooooooooooong Name|        n|  1|      Animal|
(PEEGS)#

If the user enters an invalid file path or a directory path, the message “Error! Data file does not exist” is displayed:

(PEEGS)# TABLE ../f
Error! Data file does not exist
(PEEGS)#

If the method TableAlgorithm#String getPrintableTable(Path) gets an invalid file path or a directory path, it throws an IOException.

General Requirements

  • The output and return values of implemented methods must exactly match the output described here. Otherwise, tests will fail.

  • Do not add any publicly visible methods or classes to the program. You can add and change all non-public methods and classes.

  • Do not modify any public method signatures or interfaces. You are not allowed to add method parameters to existing public methods.

  • Documentation of the source code is part of the exercise and will be graded. The Google Java Style Guide must be fulfilled.

  • Your solution must be anonymous. Your name may not appear in the solution.

  • Your solution may not contain compiled data (no build directory or .class files). Please check the content of your ZIP file before uploading.

Helpful Resources

Some helpful resources:

While reading JavaDoc, you may encounter the following types that could be confusing:

  • Stream is an advanced data structure similar to lists. It can be consumed only once and has lots of methods for transformation and filtering. Often uses method references and other advanced topics. You can turn any Stream into an immutable List like this:

    Stream<Integer> numberStream = // .. snip ..;
    List<Integer> numberList = numberStream.collect(Collectors.toList());
    
  • Iterable is an interface that is often used by methods to allow lots of data structures. Almost any collection in Java is an Iterable: lists, maps, etc.

  • CharSequence is an interface that is often used by methods to not only allow Strings, but also other, similar types. String, StringBuilder etc. are all CharSequences.

Good luck!