small medium large xlarge

# PracProg2oop

1.

a.
```class Country:

def __init__(self, name, population, area):
""" (Country, str, int, int)

A new Country named name with population people and area area.

34482779
9984670
"""

self.name = name
self.population = population
self.area = area
```

b.

```    def is_larger(self, other):
""" (Country, Country) -> bool

Return whether this country is larger than other.

>>> usa = Country('United States of America', 313914040, 9826675)
True
False
"""

return self.area > other.area
```

c.

```    def population_density(self):
""" (Country) -> float

Return the population density of this country.

3.4535722262227995
"""

return self.population / self.area
```

d.

```    def __str__(self):
""" (Country) -> str

Return a printable representation of this country.

>>> usa = Country('United States of America', 313914040, 9826675)
>>> print(usa)
United States of America has a population of 313914040 and is 9826675 square km.
"""

return '{} has a population of {} and is {} square km.'.format(
self.name, self.population, self.area)
```

e.

``````def repr(self):
""" (Country) -> str``````
``Return a concise representation of this country.``

``````return "Country('{}', {}, {})".format(
self.name, self.population, self.area)``````

2.

a.

```class Continent:

def __init__(self, name, countries):
""" (Continent, str, list of Country) -> NoneType

A continent named name made up of countries.

>>> usa = country.Country('United States of America', 313914040,
...                       9826675)
>>> mexico = country.Country('Mexico', 112336538, 1943950)
>>> countries = [canada, usa, mexico]
>>> north_america = Continent('North America', countries)
>>> north_america.name
'North America'
>>> for country in north_america.countries:
...     print(country)
Canada has a population of 34482779 and is 9984670 square km.
United States of America has a population of 313914040 and is 9826675 square km.
Mexico has a population of 112336538 and is 1943950 square km.
"""

self.name = name
self.countries = countries
```

b.

```    def total_population(self):
""" (Continent) -> int

Return the total population of all the
countries in this continent.

>>> usa = country.Country('United States of America', 313914040,
...                       9826675)
>>> mexico = country.Country('Mexico', 112336538, 1943950)
>>> countries = [canada, usa, mexico]
>>> north_america = Continent('North America', countries)
>>> north_america.total_population()
460733357
"""

total = 0
for country in self.countries:
total = total + country.population

```

c.

```    def __str__(self):
""" (Continent) -> str

Return a printable representation of this Continent.

>>> usa = country.Country('United States of America', 313914040,
...                       9826675)
>>> mexico = country.Country('Mexico', 112336538, 1943950)
>>> countries = [canada, usa, mexico]
>>> north_america = Continent('North America', countries)
>>> print(north_america)
North America
Canada has a population of 34482779 and is 9984670 square km.
United States of America has a population of 313914040 and is 9826675 square km.
Mexico has a population of 112336538 and is 1943950 square km.
"""

res = self.name
for country in self.countries:
res = res + '\n' + str(country)

return res
```
Page History
• V10: Jason Montojo [over 3 years ago]
• V9: Jason Montojo [over 3 years ago]
• V8: Paul Gries [about 4 years ago]
• V7: Paul Gries [about 4 years ago]
• V6: Paul Gries [about 4 years ago]
• V5: Paul Gries [about 4 years ago]
• V4: Paul Gries [about 4 years ago]
• V3: Paul Gries [about 4 years ago]
• V2: Paul Gries [about 4 years ago]
• V1: Sourav Karwa [over 4 years ago]