Question: Continuing on from the previous question, define a class called Country which represents a country and consists of a list of cities. Each Country object

Continuing on from the previous question, define a class called Country which represents a country and consists of a list of cities. Each Country object stores information about the name and a list of cities. The class must have the following functionality:

  1. Create a country with the name in the parameter. For example:
    nz = Country('New Zealand')

    The initializer should also create an empty list of City objects.

  2. The class should also provide functionality to add a city object to the cities list. The method takes name, population and area as parameters. For example:
    nz.add_city('Auckland', 1470100, 607.10) nz.add_city('Christchurch', 383200, 295.15)
  3. The class should also provide functionality to get the total population, the total area and the population density for the country. For example, the code:
    print("{:.2f}".format(nz.get_total_population())) print("{:.2f}".format(nz.get_total_area())) print("{:.2f}".format(nz.get_population_density()))

    would produce the output:

    1853300.00 902.25 2054.09
  4. The class should also provide functionality to return a city object based on the parameter index. For example, the code:
    print(nz.get_city(1))

    would produce the output:

    Christchurch(1298.32)
  5. Return a string representing information about the country. Format the population density to 2 decimal places. For example, the code:
    print(nz) 

    would produce the output:

    New Zealand: Auckland(2421.51) Christchurch(1298.32) Population density = 2054.09

Submit the entire class definition assuming that the City class is given.

Test Result
nz = Country("New Zealand") nz.add_city('Auckland', 1470100,607.10) nz.add_city('Christchurch', 383200, 295.15) print(nz) akl = nz.get_city(0) print(type(akl)) print(akl)
New Zealand: Auckland(2421.51) Christchurch(1298.32) Population density = 2054.09  Auckland(2421.51) 
country1 = Country("New Zealand") country1.add_city('Auckland', 1470100,607.10) country1.add_city('Christchurch', 383200, 295.15) country1.add_city('Wellington',215100,112.29) country1.add_city('Hamilton',176500,110.37) country1.add_city('Tauranga',151300,135.12) print(country1) print("{:.2f}".format(country1.get_total_population())) print("{:.2f}".format(country1.get_total_area()))
New Zealand: Auckland(2421.51) Christchurch(1298.32) Wellington(1915.58) Hamilton(1599.17) Tauranga(1119.75) Population density = 1901.70 2396200.00 1260.03

use py pls

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!