• Java 2 Platform Standard Edition 5.0 API specification
• OpenJDK source code repository for library classes
• Source code from Data structures and problem-solving using Java, third edition
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.
demographics, to
hold the classes that we'll define in this lab.County classFirst, 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).
demographics package, called County, containing these three fields.County in which the
caller supplies a value for each of the three fields.getName, getPopulation, getArea).populationDensity, that returns the population
density of the County to which it is sent, in inhabitants per square
mile.
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
demographics package, called
SparseFinder, for the application program.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.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.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.readCounty to collect the
information about each county from countyData and adds each of the
resulting County objects to counties.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.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.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.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.SparseFinder?