private static void populateWorld(World pWorld) {
Country australia = new Country("Australia", 5);
Country malaysia = new Country("Malaysia", 2);
Country southAfrica = new Country("South Africa", 3);
City aliceSprings = new City("Alice Springs", 0.026, 608, false);
City canberra = new City("Canberra", 0.37, 580, true);
City melbourne = new City("Melbourne", 4.1, 31, false);
City adelaide = new City("Adelaide", 1.2, 40, false);
City sydney = new City("Sydney", 4.6, 9, false);
City kualaLumpur = new City("Kuala Lumpur", 1.6, 22, true);
City johorBahru = new City("Johor Bahru", 1.5, 37, false);
City johannesburg = new City("Johannesburg", 1.0, 1753, false);
// Johannesburg is misspelled in the sample output trace.
City capeTown = new City("Cape Town", 0.83, 0, true);
// Cape Town is one of the three capital cities of South Africa.
City pretoria = new City("Pretoria", 0.52, 1271, true);
// Pretoria is another of the three capitals of South Africa
australia.addCity(aliceSprings);
australia.addCity(canberra);
australia.addCity(melbourne);
australia.addCity(adelaide);
australia.addCity(sydney);
malaysia.addCity(kualaLumpur);
malaysia.addCity(johorBahru);
southAfrica.addCity(johannesburg);
southAfrica.addCity(capeTown);
southAfrica.addCity(pretoria);
pWorld.addCountry(australia);
pWorld.addCountry(malaysia);
pWorld.addCountry(southAfrica);
}
public Country(String initName, int capacity) {
name = initName;
cities = new City[capacity];
numCities = 0;
arrayCapacity = capacity;
}
public City(String initName, double initPopulation, int initElevation,
boolean initIsCapital) {
name = initName;
population = initPopulation;
elevation = initElevation;
isCapital = initIsCapital;
}
public Boolean addCity(City newCity) {
if (newCity != null) {
cities[numCities] = newCity;
numCities++;
return true;
} else {
return false;
}
}
public boolean addCountry(Country newCountry) {
if (newCountry == null) {
return false;
} else {
countries[numCountries] = newCountry;
numCountries++;
return true;
}
}
So there are the relevant methods; populateWorld, the City constructor, the Country constructor, addCity (to a country) and addCountry (to a world) The issue, so nobody has to search for my last post, is that when I add the countries I've instantiated to pWorld, the cities inside those countries are not copied over; it claims that the are no cities in any of the countries.
I have tried manually adding the cities from the pre-added to country to the copy of that country inside a world by editing the addCountry method, but it gives me an array out of bounds exception, which is incredibly confusing; there is space for all the cities in each of the countries; this would imply that there are, during addCountry's runtime, cities in the copied country.