import javax.swing.*;
/**
* A table of historical population data.
*
* @author Jim Glenn
* @version 0.1 11/20/2003
*/
public class PopulationTable
{
private int[][] pop;
private int numPlaces;
/**
* Creates a population table with all entries initialized to zero.
*
* @param maxPlaces the maximum number of places that the new table can
* hold information for
* @param years the number of years' worth of data to keep for each place
*/
public PopulationTable(int maxPlaces, int years)
{
pop = new int[maxPlaces][years];
numPlaces = 0;
/*
1) envision what it would look like w/o loops
pop[0][0] = 0;
pop[1][0] = 0;
...
pop[maxPlaces - 1][0] = 0;
pop[0][1] = 0;
pop[1][1] = 0;
...
pop[maxPlaces - 1][1] = 0;
...
pop[0][years - 1] = 0;
...
pop[maxPlaces - 1][years - 1] = 0;
2) find statements that can be replaced with loops
for (int pl = 0; pl < maxPlaces; pl++)
pop[pl][0] = 0;
for (int pl = 0; pl < maxPlaces; pl++)
pop[pl][1] = 0;
...
for (int pl = 0; pl < maxPlaces; pl++)
pop[pl][years - 1] = 0;
3) combine loops
for (int pl = 0; pl < maxPlaces; pl++)
{
pop[pl][0] = 0;
pop[p1][1] = 0;
...
pop[pl][years - 1] = 0;
}
4) look again for statements that can be replaced with loops
*/
for (int pl = 0; pl < maxPlaces; pl++)
for (int yr = 0; yr < years; yr++)
pop[pl][yr] = 0;
}
/**
* Adds one place to this table.
*
* @param p an array holding population counts for the new place;
* the size of the array must match the number of years specified when
* this table was created
*/
public void addPlace(int[] p)
{
for (int yr = 0; yr < p.length; yr++)
pop[numPlaces][yr] = p[yr];
numPlaces++;
}
/**
* Returns the maximum value in this table.
*
* @return the maximum value in this table
*/
public int findMax()
{
int max = Integer.MIN_VALUE;
for (int pl = 0; pl < numPlaces; pl++)
for (int yr = 0; yr < pop[pl].length; yr++)
max = Math.max(max, pop[pl][yr]);
return max;
}
/**
* Returns an array containing the percent changes for the specified place.
*
* @param pl the index of a place in this table
* @return an array containing the percent changes for that place
*/
public double[] getPercentChanges(int pl)
{
double[] pct = new double[pop[pl].length - 1];
for (int yr = 0; yr < pct.length; yr++)
pct[yr] = 100 * (double)(pop[pl][yr] - pop[pl][yr + 1]) / pop[pl][yr + 1];
return pct;
}
/**
* Returns a string representation of this table.
*
* @return a string representation of this table
*/
public String toString()
{
String s = "";
for (int pl = 0; pl < numPlaces; pl++)
{
for (int yr = 0; yr < pop[0].length; yr++)
s = s + pop[pl][yr] + '\t';
s = s + '\n';
}
return s;
}
/**
* Calculates the total population for a given year.
*
* @param yr the year to calculate the total for
* @return the population for that year
*/
public int calcTotal(int yr)
{
int tot = 0;
for (int pl = 0; pl < numPlaces; pl++)
tot += pop[pl][yr];
return tot;
}
/**
* Test driver for population table.
*
* @param args ignored
*/
public static void main(String[] args)
{
// Data from US Census Bureau
// there is a link to it here:
// Index of Interesting Tables
int[] alleganyPop = {74946, 80548, 84044, 84169};
int[] aaPop = {427239, 370775, 297539, 206634};
int[] countyPop = {692134, 655615, 621077, 492428};
int[] calvertPop = {51372, 34638, 20863, 15826};
int[] howardPop = {187328, 118572, 61911, 36152};
int[] cityPop = {736014, 786775, 905759, 939024};
PopulationTable mdTable = new PopulationTable(6, 4);
mdTable.addPlace(alleganyPop);
mdTable.addPlace(aaPop);
mdTable.addPlace(countyPop);
mdTable.addPlace(calvertPop);
mdTable.addPlace(howardPop);
mdTable.addPlace(cityPop);
System.out.println(mdTable);
System.out.println(mdTable.calcTotal(0));
System.out.println(mdTable.findMax());
double[] changes = mdTable.getPercentChanges(5);
for (int i = 0; i < changes.length; i++)
System.out.print(changes[i] + "\t");
System.out.println();
}
}
This code can also be downloaded from the file PopulationTable.java.