⌛⌛ Country data (JSON)¶
The submission consists of a Maven project. Place your answer into files pom.xml,
src/main/java/fi/tuni/prog3/round7/jsoncountries/CountryData.java in the directory
Round7/jsoncountries. Remember to pull task material from
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 task is almost indentical with the task about handling country statistics in XML form: the only difference is that the data is now in JSON format instead of XML. The data is in fact exactly the same; it has just 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
CountryData that have at least the following
features. You can decide the rest of the details yourself.
Countrystores the name of the country (string), area (
double), population (
long) and gross domestic product (
Defined in the package
fi.tuni.prog3.round7.jsoncountries(i.e. set the line
package fi.tuni.prog3.round7.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
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.
Doublevalues with the precision of one decimal.
Public member functions
double getGdp()that return the data their names suggest.
CountryDataoffers two public static member functions for reading and writing country data in JSON format.
Defined in the package
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
fieldobjects inside a
fieldobject has under the key “attributes” a key “name” whose value describes which piece of information the
The function returns some kind of a list implementing the interface
List<Country>that contains the
Countryobjects depicting the read data. You must hence combine the data read from the three JSON files into
void writeToJson(List<Country> countries, String countryFile)writes the information depicted by
Countryobjects in the list
countriesin JSON format into the file named by the parameter
A JSON array that contains one JSON object for each country. Each JSON object depicts the data of one country under the keys
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
pom.xml project file:
The value of
The value of
A onejar plugin definition where the value of
CountryTestis the name of the given test class (described below).
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
test files named in the form
gdpX.json, the example
outputs named in the form
outputX.txt, and the example result files named in the form
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 (where the
pom.xml is). Note
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
java -jar target/jsoncountries-1.0.one-jar.jar areaX.json populationX.json gdpX.json countriesX.json.
The test number
X should produce the output depicted in the corresponding file
and create a file
countriesX.json whose contents are identical with the file
A+ presents the exercise submission form here.