java 2D arrays
2019/4/19 Programming Assignment #2
https://foothillcollege.instructure.com/courses/9422/assignments/233969 1/6
Programming Assignment #2
Due Saturday by 7am Points 20 Available Apr 10 at 7am - Jul 7 at 11:59pm 3 months
Objec�ves: Define the new class type(s): EnrollmentData. Use and modify the class TestEnrollmentData to tests the new types. Practice exception handling.
Practice using two dimensional arrays.
Get in the habit of dividing larger projects into smaller parts.
Use git features such as commit and clone to write and read from your repository.
Material from: Program Guidelines, Reference: Lab Homework Requirements from Module. Multi-Dimensional Arrays:
Chapter 7 and 8 from Liang Textbook
Class TestEnrollmentData, example data files (included under the resource folder) in your assigned GitHub repository.
Preliminary We will be working with the data from *.csv input file(s) resources folder in your assigned GitHub repository where the asterisk * in this case means a filename followed by a .csv extension.
Note: The input file(s) are not comprehensives and serve as examples. See below for recommendations of creating your own.
Make sure to read all the way through. Pay close attention the FAQs.
Where to get started?
Always make sure to clone your assigned repository before starting the project. Depending on the project you may have starting code or input data. Once you've downloaded the material in your IDE, continue with the project description...
Applica�on Overview For this and the rest of the programming assignments we will be using data from World Bank Open Data (https://data.worldbank.org/indicator/SE.SEC.NENR?view=chart) . The data will be in Comma-Seperated Value (CSV) (http://(http://en.wikipedia.org/wiki/Comma-separated_values) format. The format of the CSV file is as follows:
Data Source,World Development Indicators,
Indicator,School Enrollment In Primary (% net)
Last Updated Date,[Date in MM/DD/YY format]
Number of countries [N number of countries]
Country Name,[year 1],[year 2]..[year M]
[country name 1],[data for year 1],[data for year 2]...[data for year M]
[country name 2],[data for year 1],[data for year 2]...[data for year M]
...
[country name N],[data for year 1],[data for year 2]...[data for year M]
Let's look at an example of enrollment data for 8 different countries over a 12 year period.
Afghanistan,,,,,,,,,,,,
Canada,,,,,,,99.47058868,99.44992065,99.4045105,99.59809875,99.96224213,
2019/4/19 Programming Assignment #2
https://foothillcollege.instructure.com/courses/9422/assignments/233969 2/6
China,,,,,,,,,,,,
"Egypt, Arab Rep.",90.28288269,95.34037018,,95.98267365,97.1634903,93.17767334,96.87068939,,95.82694244,,97.45082092,
India,,91.30478668,91.02262878,90.76931763,91.03694916,90.4213562,91.58106232,92.25010681,,,,
Nepal,,,,,,98.25801086,99.38890839,,94.13047791,96.53446198,96.62294769,94.69564056
Syrian Arab Republic,89.91343689,91.00061798,91.73821259,92.78222656,92.94094849,,,63.24472046,,,,
United States,93.09568024,94.49015808,95.11688995,93.85852814,92.71695709,91.9122467,92.05748749,91.74311829,92.19660187,92.94197845,,
The data shows the combined net enrollment data for primary and secondary school children.
Why are some cells empty?
Since sharing data in open source is voluntary, if a country does not share its survey results, then the cell for the corresponding year is empty. If a cell is empty then assume data was not submitted for that specific year.
Note: Enrollment indicators are based on annual school surveys, but do not necessarily reflect actual attendance or dropout rates during the year. Also, the length of education differs across countries and can influence enrollment rates, although the International Standard Classification of Education (ISCED) tries to minimize the difference. For example, a shorter duration for primary education tends to increase the rate; a longer one to decrease it (in part because older children are more at risk of dropping out). Moreover, age at enrollment may be inaccurately estimated or misstated, especially in communities where registration of births is not strictly enforced. But we are digressing...
How to analyze the data?
After a couple of steps of manually analyzing the data (via the tedious task of searching, filtering, copying and pasting) I was able to extract an interesting pattern regarding enrollment of primary and secondary school children. For example, can you tell me when which countries have the largest bump in enrollment? in what years? With the data of 8 countries over 12 years, answering the question above is not too difficult. But, imagine if we had to answer similar questions about various countries by manually sifting through the data of 250+ countries over 50+ years. It would not be fun! The goal of this and the following programming assignment is to make this process easier.
The Program Spec Create the class EnrollmentData.
For each class the minimum attributes and methods you need to create are described below each class. These are required. The description works hand in hand with the test file in your assigned project repository. We are using a test driven development (TDD) approach.
Given the input file:
Data Source,World Development Indicators,,,,,,,,,,,
Indicator,School Enrollment In Primary (% net),,,,,,,,,,,
Last Updated Date,7/25/18,,,,,,,,,,,
Number of Countries,8,,,,,,,,,,,
Country Name,2006,2007,2008,2009,2010,2011,2012,2013,2014,2015,2016,2017
Afghanistan,,,,,,,,,,,,
Canada,,,,,,,99.47058868,99.44992065,99.4045105,99.59809875,99.96224213,
China,,,,,,,,,,,,
"Egypt, Arab Rep.",90.28288269,95.34037018,,95.98267365,97.1634903,93.17767334,96.87068939,,95.82694244,,97.45082092,
India,,91.30478668,91.02262878,90.76931763,91.03694916,90.4213562,91.58106232,92.25010681,,,,
Nepal,,,,,,98.25801086,99.38890839,,94.13047791,96.53446198,96.62294769,94.69564056
Syrian Arab Republic,89.91343689,91.00061798,91.73821259,92.78222656,92.94094849,,,63.24472046,,,,
United States,93.09568024,94.49015808,95.11688995,93.85852814,92.71695709,91.9122467,92.05748749,91.74311829,92.19660187,92.94197845,,
In this project we are going to hard-code the data that we want to test in the test file. Typically this is the java file with main() method. In this project the name of the test file is TestEnrollmentData.
When copying data from the CSV file: The test cases provided are samples only. You may assume:
The format of the input files will be the same. The data in the CSV files will be well formatted. There may be no entry for one or more years for a country. For example, in the above sample we have no data for United States for the year 2017, which is indicated by two commas which follow each other with no numerical value. Take a look at the above highlighted text in bold.
2019/4/19 Programming Assignment #2
https://foothillcollege.instructure.com/courses/9422/assignments/233969 3/6
Invalid Assumptions:
Do not: Limit the number of countries (i.e. number of rows), Limit the number of years (i.e. number of columns), Limit the starting year and ending year.
class EnrollmentData Define the class EnrollmentData where one object represents a table containing all data for different countries.
A�ributes: Instance variable called countryNames, a one dimensional array of type String, which holds the names of all countries read from the CSV file. Each object of this class also stores the name of all the countries. Think about how you want to manage this... Recall: If your table holds decimal numbers, then table[x][y] = "USA" is invalid.
Instance variable called yearLabels, a one dimensional array of type int, which holds the year number read from the CSV file. Each object of this class must keep track of the year associated with each column of the table. Is doesn't make sense to have the value in a couple of cells of your table to hold a label (i.e. a value that stands for the year), and the rest to hold the enrollment data. So, you must keep track of the year separate from table.
Hint: The years are consecutive. So, you can just keep track of start or end.
Instance variable called table, a two dimensional array of type double, where the row represents a country, the column represents the net enrollment of primary school (https://data.worldbank.org/indicator/SE.PRM.NENR?view=chart) and secondary school (https://data.worldbank.org/indicator/SE.SEC.NENR?view=chart) . Note: Net enrollment rate is the ratio of children of official school age who are enrolled in school to the population of the corresponding official school age.
Methods The constructor EnrollmentData() which constructs an object and which initializes the instance variables.
Takes in three arguments: an int for the number of countries. This is represented by the rows in table.
an int for the number of years of enrollment for a given country. This is represented by the number of columns in table.
an int for the starting year for the data for all countries.
This method should initialize table and all instance variables. Suggestion: Create an 1D array of type String called countryNames which keeps track of the name of countries. Can you guess the size of the array for the name of the countries? Hint: The number of rows in instance variable table (i.e. table.length) should be the same as the length of countryNames. Suggestion: Create a 1D array of type int called yearLabels values to store the year the value for every year. Hint: Use the startingYear from constructor.
The method addCountry() which adds the country name to instance variable countryNames and adds the one dimensional array of enrollment data to the table.
Takes two arguments: a String for the name of the country and
a one dimensional array of double for the enrollment data of a specific country.
Suggestion: Keep track of which row in the table was last filled in a variable. Then assign* table[last filled row] = one dimensional array of double for the enrollment data of a specific country. *Note: This is an example of pseudo-code.
A toString() method which returns a String representation of the enrollment data stored in table. Include the year labels as the column headers and the country names as the row labels. For a useful toString() representation of a class, you must override the toString() method of the Object class (https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#toString--) . See modules for an example.
Tips On Forma�ng the Output To format the output for a String object and specify the text representation should take 20 spaces use %20s format:
2019/4/19 Programming Assignment #2
https://foothillcollege.instructure.com/courses/9422/assignments/233969 4/6
System.out.printf("%20s ", "United States");
To format the output for a int primitive and specify the number should take 6 spaces use %6d format. The decimal point is counted as a separate space:
System.out.printf("%6d ", 2006);
To format the output for a double primitive and specify the number should take 6 spaces including 2 spaces for the decimal point use %6.2f format. Yes, the f is used for numbers with decimal positions. It is confusing! The decimal point is counted as a separate space:
System.out.printf("%6.2f", 95.123456"); System.out.printf("%6.2f ", requestedEnrollmentPeriod[i]);
Note the above examples are right aligned.
Forma�ng the String object Returned for toString() method
The toString() method should not at any point call System.out.print() or System.out.println() or System.out.printf() methods.
If you want to format the String object returned by the toString() method such that it is more readable use the format() method (https://docs.oracle.com/javase/8/docs/api/java/lang/String.html#format-java.lang.String-java.lang.Object...-) available in the String class. The same rules as formatting using System.out.print* apply. For example:
public String toString() { String representation = ""; representation += String.format("%6.2f ", 95.123456); return representation; }
Note: The above example uses String concatenation which is inefficient as String objects are immutable (https://docs.oracle.com/javase/tutorial/essential/concurrency/immutable.html) ! Consider using the StringBuilder class (https://www.google.com/url? sa=t&rct=j&q=&esrc=s&source=web&cd=1&ved=2ahUKEwiF16bapNrhAhVwIzQIHTBNB6oQFjAAegQICBAC&url=https%3A%2F%2Fdocs.oracle.com%2Fjavas instead.
class TestEnrollment Let's get back to the data. Initially you want to create some test data. To do this, just copy and paste portions of the CSV file you are interested in and create a one-dimensional array out of it. Instead of assigning the array elements individually, initialize it at the time of declaration. For example, we copied data from the CSV file for United States during seven years starting from 2006. So, we initialize the array as follows:
// Indicates that the data is invalid.
public final static int MISSING_DATA = -1;
final double [] partialPrimaryEnrollment03 = {93.09568024,94.49015808,95.11688995,93.85852814,92.71695709,91.9122467,92.05748749,91.74311829,92.19660187,
92.94197845,MISSING_DATA};
However, your test data must be declared as final so that they are not easy to change.
Addi�onal Requirements: In class EnrollmentData, do not hard-code the number of countries (i.e. rows) and the number of subscription years (i.e. columns) that the 2D array table can hold.
Instead, use the arguments passed to the EnrollmentData's constructor to determine the size of the table. Your implementation should be able to handle a flexible range of data.
2019/4/19 Programming Assignment #2
https://foothillcollege.instructure.com/courses/9422/assignments/233969 5/6
Dis�nguishing Test Cases Clearly distinguish the different test cases.
Suggestion: Use the following template for your RUN.txt file:
As always you may create as many additional classes, attributes and methods as you need keeping in mind that the original test file provided in your assigned repository must compile and run successfully. See FAQs below.
Reminder As a reminder...In order to receive full credit on this assignment, your program must:
1. Follow all Program Guidelines, see Modules for the guidelines.
2. Get the correct results when the main() application is run. Here this is in the test class.
3. Include the following in your lab submission: The source code: the listing of your program under the src directory. Do not change the package name of the provided class.
Comment all classes and all methods. Then, generate Javadocs and include it in your submission. Note: For this project we only have one class and one method. Reminder: A brief tutorial on generating Javadocs is included in the Introductions module section Example Program Submission.
Run output sample(s): in a plain-text file called RUN.txt (For now, the output should be only plain-text);
-----------------------------------------------------
-----------------------------------------------------
Test file: resources/inputFile01.txt
NOTES: Testing input file with a subset of countries
and enrollment ranges.
-----------------------------------------------------
[Paste your output here.]
---------------------------------------------------
---------------------------------------------------
Test file: resources/inputFile02.txt
NOTES: Testing input file with all data.
---------------------------------------------------
[Paste your output here.]
2019/4/19 Programming Assignment #2
https://foothillcollege.instructure.com/courses/9422/assignments/233969 6/6
A brief description of all files submitted: in a plain-text file called README.txt.
FAQ: Can I include additional attributes, methods and or classes?
Yes, add as many as you see fit outside of the test class. The exception is if the project description requires that you define a method.
Keep in mind that I run two test files when checking your work:
1) I first run your test file (i.e. the one you submit and perhaps modify).
2) Then, I run the instructor test file, which will be testing for different input.
If your test class creates and depends on classX or methodY(), but the original test class (i.e. one or more test classes that you received as the starting code in your assigned repository) had never created an object of type classX or called methodY(), then the result will be compilation errors in the instructor test class. This is because the interfaces the instructor test class relies on is the same interfaces (i.e. class types and method calls) as the original test class (es).
In general, do *not* change method signatures. This includes:
the package name,
list of file imports,
visibility of methods,
capitalization of method names,
order of arguments,
handling of exceptions, etc.
I will not be grade your project if it results in compilation or runtime errors.
FAQ: Can I change the main() method?
The test class TestEnrollment has mostly already been written for you. Add your test cases. However, do not change method main() in the TestEnrollment class such that it creates dependencies.
The goal of this method is to specify the interfaces that you need to implement. See the previous FAQ.
If you are unsure of what to use, post your question in the discussion forum.