Question: Python, question 2 please Define a class called City which represents a city. Each City object stores information about the city's name, population and area


Python, question 2 please
Define a class called City which represents a city. Each City object stores information about the city's name, population and area (km2). The class must have the following functionality: 1. Create a city with the parameter name, population and area. For example: city1 = City("Auckland", 1470100, 607.10) Note: The default value of population is 1 and default value of area is 1. 2. The class should also provide functionality to set the population and area of a city. The function should only modify the population or area if the parameter value is > 0. For example, the code city2 = City('Auckland') city2.set_population (-1) #no change city2.set_area(607.10) print(city2.name, city2.population, city2.area) would produce the output: Auckland 1 607.1 3. The class should also provide functionality to return the population density of a city (i.e. population / area). For example, the code: print("{:.2f}".format(city1.get_population_density) would produce the output: 2421.51 4. Return a string representing the city (name and population density). For example, the code: print(city1) would produce the output: Auckland(2421.51) Submit the entire class definition in the answer box below. For example: Test Result city1 = City("Auckland", 1470100, 607.1) Auckland 1470100 607.1 print(city1.name, city1.population, city1.area) Christchurch(1298.32) city2 = City('Christchurch', 383200, 295.15) print(city2) Wellington (1915.58) Hamilton(1599.17) city1 = City("Wellington", 215100, 112.29) print(city) city2 = City( 'Hamilton', 176500,110.37) print(city2) Auckland(1.00) 1 1 Auckland 1 607.1 city2 = City('Auckland') print(city2, city2.population, city2.area) city2.set_population (-1) city2.set_area(607.10) print(city2.name, city2.population, city2.area) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 class City: def __init__(self, name, population = 1, area = 1): self.name=name self.population=population self. area-area def set_population(self, population): if population>0: self.population=population def set_area(self, area): if area>0: self. area-area def get_population_density(self): return self.population/self.area def __str_(self): return "{:}({:.2f})".format(self.name, self.get_population_density) 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', 147e100, 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: 18533ee.ee 9e2.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.89 Submit the entire class definition assuming that the City class is given. For example: Test Result nz = Country("New Zealand") New Zealand: nz.add_city("Auckland', 1470108,607.10) Auckland(2421.51) nz.add_city('Christchurch', 383200, 295.15) Christchurch(1298.32) print(nz) Population density = 2054.89 akl = nz.get_city(@)
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
