⌛⌛ Country data (JSON)

A note about AI

You are encouraged to use an AI tool to solve this task. Please, find more information about the task at the previous Chapter, in you have not covered that part of the material yet.

The exercise is returned as a Maven project. Place the pom.xml file in the round7/jsoncountries directory of your local repository and create to this directory the src/main/java subdirectory. Create class files Country.java and CountryData.java and attach them to the fi.tuni.prog3.jsoncountries package. Your files should be in the round7/jsoncountries/src/main/java/fi/tuni/prog3/jsoncountries directory. NetBeans creates the pom.xml file and the directory structure for this task automatically, provided that you enter correct values, while creating a Maven project.

There are test material available in the round7/jsoncountries directory of the remote material repository.

In this task you will try reading and writing JSON data using Google’s GSON library: https://github.com/google/gson/blob/master/UserGuide.md. Information about what kind of Maven dependency is needed to use the library can be found behind the link.

The base data for the task consists of a sample from open data published by the world bank containing the area, population and gross domestic product for a handful of countries. The data is has been converted from XML to JSON with an automatic tool. Due to this the structure of the data is somewhat clumsy: the conversion tool has aimed to follow the form of the original XML verbatim.

You should implement the classes Country and CountryData that have at least the following features. You can decide the rest of the details yourself.

  • Class Country stores the name of the country (string), area (double), population (long) and gross domestic product (double).

    • Defined in the package fi.tuni.prog3.jsoncountries. Set the statement package fi.tuni.prog3.jsoncountries; into the very beginning of the class.

    • Implements the interface Comparable<Country> so that the comparison is based on the name of the country according to the natural order of the String class.

    • A public member function String toString() that returns a String representation of the country in the form shown in the example outputs: first the name of the country, and after it the area, population and gross national product, each on separate lines and indented with two spaces. Double values with the precision of one decimal. Please, note that point is used as the decimal separator. The format member function of the String class is a convenient helper in this task.

    • Public member functions String getName(), double getArea(), long getPopulation() and double getGdp() that return the data their names suggest.

  • Class CountryData offers two public static member functions for reading and writing country data in JSON format.

    • Defined in the package fi.tuni.prog3.jsoncountries. Set the statement package fi.tuni.prog3.jsoncountries; into the very beginning of the class.

    • List<Country> readFromJsons(String areaFile, String populationFile, String gdpFile) reads country data from the JSON files named by the parameters.

      • The files contain information about the area, population and gross domestic product of the countries as suggested by the file names. Note how the data is in three separate files.

      • Deduct the structure of the files by investigating the example input files. The structure of each file is identical: information for one country is in field objects inside a record object. Each field object has under the key “attributes” a key “name” whose value describes which piece of information the field object contains.

      • The function returns some kind of a list implementing the interface List<Country> that contains the Country objects depicting the read data. You must hence combine the data read from the three JSON files into Country objects.

    • void writeToJson(List<Country> countries, String countryFile) writes the information depicted by Country objects in the list countries in JSON format into the file named by the parameter countryFile.

      • A JSON array that contains one JSON object for each country. Each JSON object depicts the data of one country under the keys name, area, population and gdp.

      • Check the exact output format from the example outputs. They have been created using the setPrettyPrinting() feature of the GSON library.

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 jsoncountries.

  • The value of version is 1.0.

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

  • A Onejar plugin definition where the value of mainClass is CountryTest which is the name of the given test class (see below).

Testing

There are three test sets available for the task. Below, the character X refers to the number of the test and is 1, 2 or 3.

You can test your classes with the test program given in the file CountryTest.java, the test files named in the form areaX.json, populationX.json and gdpX.json, the example outputs named in the form outputX.txt, and the example result files named in the form resultX.json.

Set CountryTest.java into the root of the src/main/java subdirectory of your Maven project, and the other files into the root directory of your Maven project, that is, where the pom.xml is. Note that CountryTest.java does not include a package definition and therefore is not placed into a deeper subdirectory.

After this you can compile the program with mvn package and run the test X as java -jar target/jsoncountries-1.0.one-jar.jar areaX.json populationX.json gdpX.json countriesX.json in the root directory of the project. The test number X should produce the output depicted in the corresponding file outputX.txt and create a file countriesX.json whose contents are identical with the file resultX.json.

A+ presents the exercise submission form here.