Lab exercise #8: Population density of Iowa counties

Course links

External links

The goal

Our objective for today is to write a computer program that will identify the most sparsely populated county in the state of Iowa.

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 smallest population density.

  1. Start Eclipse and create a new package 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, one for its population, and one for its area (in square miles).

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

Reading the data

Our application program will construct and use a list of County objects -- specifically, a LinkedList of them.

The file /home/stone/datasets/Iowa-counties-2000.dat contains the actual data that we'll need. This file contains a four-line entry for each county: The first line gives its name, the second its population (as determined by the 2000 census), and the third its area in square miles; the fourth line is blank. To illustrate the format, here are the first nine lines of the file:

Adair
8243
570

Adams
4482
426

Allamakee
  1. Create a second class in the demographics package, called SparseFinder, for the application program.
  2. Create a static method readCounty in this class that takes a BufferedReader as its argument, reads in the four-line entry for a county from that BufferedReader, constructs a County object from the data that it reads in, and returns the constructed County.
  3. In the main method for SparseFinder, declare a local variable counties that can hold a LinkedList in which each element is a County. Initialize this variable by creating an empty LinkedList and assigning it to the variable.
  4. In the main method, declare a local variable countyData, of type BufferedReader, and initialize it by constructing a BufferedReader that reads from /home/stone/datasets/Iowa-counties-2000.dat.
  5. Write a loop that invokes readCounty to collect the information about each county from countyData and adds each of the resulting County objects to counties.

Processing the list

  1. Add a second static method to SparseFinder. This method, called findSparsest, should take a non-empty LinkedList of County objects as its argument and should traverse that list, identifying and returning the County that has the least population density.
  2. Extend the main method so that, after constructing the list of counties, it invokes findSparsest on that list and prints out the name and population density of the County that findSparsest returns.

Extras

  1. Modify the program (SparseFinder) so that it prints out the names and population densities of the ten most sparsely populated counties in the state of Iowa, in order of increasing population density.
  2. Modify the program so that it determines and prints out (a) the population density of the state as a whole and (b) the number of counties that are less densely populated than the state as a whole.
  3. Add a static method to SparseFinder that takes a LinkedList of County objects as its argument and constructs and returns a TreeMap (defined in the java.util package) in which the keys are the names of counties and the corresponding values are their population densities.
  4. Use the Web to find more recent estimates of the populations of Iowa counties. How would you go about extracting those estimates from the Web document and making them available to SparseFinder?