Lab: 16 - Population Density of Iowa Counties

Assigned
Friday, 29 September 2023
(1/2 point) Start this lab session by sharing something (a new Java method or eclipse shortcut) you learned this week.
Add the answers to your lab reportat least two ideas per student. 

OBJECTIVE

Today’s objectives are to write a computer program that will identify the most sparsely populated county in the state of Iowa and practice using the ArrayList data structure (one of the Java collections).

The population density of a region is the ratio of its population to its area. In today’s lab, we’ll measure population densities in inhabitants per square mile. We’ll determine which of the ninety-nine counties in the state of Iowa has the least population density.

Start Eclipse and create a new project called demographics, to hold the classes that we’ll define in this lab.

The County Class

First, we’ll need a class of objects that will represent Iowa counties. Each object of this class will need at least three fields: one for the county’s name (name), one for its population (population), and one for its area (area). Declare population and area as integers.

  1. Create a new class called County, containing these three fields. Provide a three-argument constructor in which the caller supplies a value for each of the three fields, an accessor method for each of the three fields (getName, getPopulation, getArea).
  2. Define an additional method in the County class, populationDensity, that returns the population density of the County to which it is sent, in inhabitants per square mile.

Reading the data

Your client program will construct and use a list of County objects—specifically, an ArrayList of them. The file Iowa-counties-2010.dat and Iowa-counties-2022.dat in /home/jimenezp/csc207/Lab-Counties/ contains the actual data that we’ll need. These files contain a four-line entry for each county:

  • the first line gives its name,
  • the second its population (as determined by the 2010 or 2022 census),
  • the third its area in square miles,
  • and the fourth line is blank.

To illustrate the format, here are the first nine lines of one of the files:

Adair
7682
570

Adams
4029
426

Allamakee
  1. (1/2 point) Create a second class, called SparseFinder (the client program). In this class, define a static method readCounty that takes a Scanner as its argument, reads in the four-line entry for a county from that Scanner, constructs a County object from the data that it reads in, and returns the constructed County. What method of Scanner should be used to read each line of an entry?
  2. In the main method for SparseFinder, declare a local variable counties for an ArrayList in which each element is a County. Initialize this variable by creating an empty ArrayList and assigning it to the variable.
  3. In the main method, declare a local variable countyDataSource, of type Scanner, and initialize it by constructing a Scanner that reads from the data files identified above. Specify the file to read using args command-line arguments.
  4. (1/2 point) Write a loop that invokes readCounty to collect the information about each county from countyDataSource and adds each of the resulting County objects to counties. What method should be used to check whether the file has another entry to read?

Processing the list

  1. (1 point) Add a second static method to SparseFinder. This method, called findSparsest, should take a non-empty Collection of County objects as its argument and should traverse that structure, identifying and returning the County that has the least population density. Use an Iterator object for traversing the Collection.
  2. (1/2 point) Update the main method so that, after constructing the list of counties, it invokes the findSparsest method, giving it that newly constructed list, and prints out the name and population density of the County that findSparsest returns. Add in your lab report the output of your program.
  3. (1 point) Modify the program SparseFinder so that contains a method that prints out the names and population densities of the n most sparsely populated counties in the state of Iowa, in order of increasing population density. Remember to pass a Collection of County objects and the n value to a method that finds the sparsely populated counties. The Arrays class has a static method called sort. This method sorts an array of Objects into ascending order according to the natural ordering of its elements. The elements of the list must implement the Comparable interface. Which method should you use to convert an ArrayList to an Array, so you can use the sort method to find the 10 most sparsely populated counties?
  4. Update the main method and add in your lab report the output of your program.

Lab Submission

You will submit your files via Gradescope by the end of the week. Please submit the source files and lab report.

I strongly recommend that each student keep a copy of the lab. Therefore, don’t forget to share the files/folder with your lab partner!

Remember: Write your code anticipating errors and print user-friendly error messages, all your public methods should be well documented (use Javadoc comments). Please include comments or organize your code and report in a way that will help the grader to find the answer to the exercises or posted questions.

For those with extra time

  1. Modify the program so that it determines and prints out

    a. the population density of the state as a whole

    b. the number of counties that are less densely populated than the state as a whole

    c. the name of counties that have increased their population density in those 12 years

    d. the number of counties that have decreased the population density in those 12 years

To compute the state population density, you should divide the total population of counties by the total density of counties.