Due Date:
Wednesday, 7 April 2021
File(s) to be submitted:
CalculateCountryData.java
Sample Output:
SampleOutput.html
Starter Files:
Summary
Write a program that reads country data from a file and prints a report on the countries found. The file includes countries' names, populations and areas. The report includes all that and also the countries' population densities.
Your program must handle various error conditions:
- If the user fails to enter a valid file (within three tries) then the program simply reports that fact and exits with error code 1.
- If the program encounters invalidly formatted data, it prints a warning and skips that country.
- If the Country object cannot be created with the data read, then the program prints a warning message and skips that country.
- If the file runs out of data while the program is trying to read a country, the program prints a warning and skips that country.
Whenever your program skips data, it reports what the problem was, and includes the name of the country that it was reading data for.
Your report doesn't have to be formatted like mine, but if you're planning on going on in CS it really would be a good idea to learn how formatted printing works.
I have created a brief introduction to printf that explains all you need to know in order to lay out the report the way it is in my sample output.
I have provided a class called Country that holds the data for a country. The client must include the country's name, population and area. Do not modify that class. It is exactly as I want it.
I have provided you with two data files. One has "clean" data (no errors), and the other has (lots of) bad data. Do not modify these files. They are exactly as I want them.
Remember that data files go in the project folder (e.g. A09).
Your job is to write the program (CalculateCountryData). It follows the following steps:
The data consists of
Make good use of methods! Have the file-reading method return a List of Countrys. Have the report-printing method accept that List of Countrys. The report-printing method can use the for-each-style loop to go thru the list of countries.
Things that can go wrong are:
Hint: If you try to read a number and there's no number there, nothing gets read. You'll need to ignore the line that has the bad data on it -- the next country starts on the next line.
Reminder: it's the constructor that throws the exception. You do not check whether the population or area is negative -- the code in Country.java does that for you! Your method just needs to catch it -- which is exactly what you're doing with all the other exceptions.
This can happen even tho' the loop is running until you run out of data. The problem is that there are three pieces of data that need to be there and the loop only checks if the first one (the country's name) is there. If the name is there, then the other information should be, too. But "exceptions" are for dealing with situations where what is true is not the same as what should be.Once again, this country will not be added to the list of countries.
Whenever something goes wrong in the reading process, the program reports what the problem was, and includes the name of the country that the program was reading data for when the error happened.
If NetBeans tells you that the Country's name might not have been initialized, you need to:Remember, whenever you use a
- Figure out why that would be.
- Figure out how to adjust your code to avoid that problem.
try
block, the program may jump out of that block from any location, so Java can't assume that any part of that code has completed successfully!
Notice that everything that can go wrong (other than the file-not-found exception) only happens in the file-reading method. The method for reading the file name deals with the FNFE. Your other methods shouldn't need to deal with exceptions at all.
Notice also that each exception (again, other than the file-not-found exception) results in a country not being added to the list. What's happening in the file-reading method is:
While the file still has data, you try to read a name, population and area, create a country to represent that area, then add that country to the list of countries you're building.Of course if anything goes wrong, then the country doesn't get added to the list.