WorldPopulation.java.html

  1  /**
  2     This program prints a table showing the world population growth over 300 years.
  3  */
  4  public class WorldPopulation
  5  {
  6     public static void main(String[] args)
  7     {
  8        final int ROWS = 6;
  9        final int COLUMNS = 7;
 10  
 11        int[][] populations = 
 12           { 
 13              { 106, 107, 111, 133, 221, 767, 1766 },
 14              { 502, 635, 809, 947, 1402, 3634, 5268 }, 
 15              { 2, 2, 2, 6, 13, 30, 46 }, 
 16              { 163, 203, 276, 408, 547, 729, 628 },
 17              { 2, 7, 26, 82, 172, 307, 392 },
 18              { 16, 24, 38, 74, 167, 511, 809 }
 19           };
 20  
 21        String[] continents = 
 22           { 
 23              "Africa", 
 24              "Asia", 
 25              "Australia",
 26              "Europe", 
 27              "North America", 
 28              "South America" 
 29           };
 30  
 31        System.out.println("                Year 1750 1800 1850 1900 1950 2000 2050");
 32        
 33        // Print population data
 34        
 35        for (int i = 0; i < ROWS; i++)
 36        {      
 37           // Print the ith row
 38           System.out.printf("%20s", continents[i]);
 39           for (int j = 0; j < COLUMNS; j++)
 40           {
 41              System.out.printf("%5d", populations[i][j]);
 42           }
 43           System.out.println(); // Start a new line at the end of the row
 44        }
 45  
 46        // Print column totals
 47  
 48        System.out.print("               World");
 49        for (int j = 0; j < COLUMNS; j++) 
 50        {
 51           int total = 0; 
 52           for (int i = 0; i < ROWS; i++)
 53           {
 54              total = total + populations[i][j];
 55           }
 56           System.out.printf("%5d", total);         
 57        }
 58        System.out.println(); 
 59     }
 60  }