TreeMap Accumulating Tutorial
From Progzoo
TreeMap Tutorials
Accumulating
We process the world TreeMap in order to answer important questions.
Old Europe.
Prior to 2004 the members of the European Union were
Austria Belgium Denmark Finland France Germany Greece Ireland Italy Luxembourg The Netherlands Portugal Spain Sweden United Kingdom
Use an accumulating parameter to calculate the total area
of old Europe
We can represent these strings in an array:
String [] oldEU = {"Austria","Belgium" ,"Denmark" ,"Finland"
,"France" ,"Germany" ,"Greece" ,"Ireland"
,"Italy" ,"Luxembourg" ,"The Netherlands" ,"Portugal"
,"Spain" ,"Sweden" ,"United Kingdom"};
int totArea = 0;
for (String s:oldEU)
totArea += world.get(s).area;
System.out.println(totArea);
test text
New Europe.
In 2004 the following ten countries joined the EU
Cyprus, Czech Republic, Estonia, Hungary, Latvia, Lithuania, Malta, Poland, Slovakia, Slovenia
Calculate the total area of Europe including new and old.
String [] newEU = { "Cyprus",
"Czech Republic", "Estonia", "Malta",
"Hungary", "Latvia", "Lithuania",
"Poland", "Slovakia", "Slovenia"};
.
static void doStuff(TreeMap<String,Country> world)
{
String [] newEU = {"Cyprus", "Czech Republic", "Estonia",
"Hungary", "Latvia", "Lithuania", "Malta",
"Poland", "Slovakia", "Slovenia"};
for (String s:newEU)
System.out.println(world.get(s).name);
}
[Font ]
[Default ]
[Show ]
[Resize ]
[History ]
[Profile ]
test text
Old and New Europe.
Calculate the percentage of Europe that is old or new.
Do this for area, population and GDP.
This is a debugging exercise. Most of the program is correct but there
a couple of mistakes in the given answer.
There is a line missing in the oldEU loop.
The calculation for newGDP is incorrect.
.
static void doStuff(TreeMap<String,Country> world)
{
String [] oldEU = {"Austria","Belgium" ,"Denmark" ,"Finland"
,"France" ,"Germany" ,"Greece" ,"Ireland"
,"Italy" ,"Luxembourg" ,"The Netherlands" ,"Portugal"
,"Spain" ,"Sweden" ,"United Kingdom"};
String [] newEU = {"Cyprus", "Czech Republic", "Estonia",
"Hungary", "Latvia", "Lithuania", "Malta",
"Poland", "Slovakia", "Slovenia"};
//Initialise all accumulating parameters.
int oldArea = 0;
long oldPop = 0;
long oldGDP = 0;
int newArea = 0;
long newPop = 0;
long newGDP = 0;
for (String s:oldEU)
{
Country c = world.get(s);
oldArea += c.area;
oldGDP += c.gdp;
}
for (String s:newEU)
{
Country c = world.get(s);
newArea += c.area;
newPop += c.pop;
newGDP += c.pop;
}
System.out.printf("Area\told%3d\tnew%3d\n",
100*oldArea/(oldArea+newArea),
100*newArea/(oldArea+newArea));
System.out.printf("Pop\told%3d\tnew%3d\n",
100*oldPop/(oldPop+newPop),
100*newPop/(oldPop+newPop));
System.out.printf("GDP\told%3d\tnew%3d\n",
100*oldGDP/(oldGDP+newGDP),
100*newGDP/(oldGDP+newGDP));
}
[Font ]
[Default ]
[Show ]
[Resize ]
[History ]
[Profile ]
test text
G8
The G8 summit in 2005 included Canada, France, Germany, Italy, Japan,
Russia, United Kingdom and USA.
Calculate the total GDP for these countries and the total GDP for the planet.
Print the GDP of the G8 as a percentage of World GDP.
Declare g8GDP and worldGDP as long and calculate them.
System.out.println(100*g8GDP/worldGDP);
You don't need to do it this way but to save you typing you may
use:
String [] g8 = new String[]{"Canada","France","Germany","Italy",
"Japan","Russia","United Kingdom","USA"};
test text
Guessing the median population.
We speculate that Mauritania has the median population. If that is
the case then the number of countries with a population higher is
exactly the same as the number of countries with a smaller population.
Print the number of countries smaller and the number of
countries bigger than Mauritania. Do not count countries with exactly the
same population.
test text