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
```

3.

a.

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

Return a string representation of this Student.

>>> student = Student('Paul', 'Ajax', 'pgries@cs.toronto.edu', '1234')
>>> student.__str__()
'Paul\\nAjax\\npgries@cs.toronto.edu\\n1234\\nPrevious courses: \\nCurrent courses: '
"""

member_string = super().__str__()

return '''{}\n{}\nPrevious courses: {}\nCurrent courses: {}'''.format(
member_string,
self.student_number,
' '.join(self.courses_taken),
' '.join(self.courses_taking))
```

b.

```# Member repr:

def __repr__(self):
""" (Member) -> str

Return a concise string representation of this Member.

>>> member = Member('Paul', 'Ajax', 'pgries@cs.toronto.edu')
>>> member.__repr__()
"Member('Paul', 'Ajax', 'pgries@cs.toronto.edu')"
"""

return "Member('{}', '{}', '{}')".format(

# Faculty repr:

def __repr__(self):
""" (Faculty) -> str

Return a concise string representation of this Faculty.

>>> faculty = Faculty('Paul', 'Ajax', 'pgries@cs.toronto.edu', '1234')
>>> faculty.__repr__()
"Faculty('Paul', 'Ajax', 'pgries@cs.toronto.edu', 1234, [])"
"""

return "Faculty('{}', '{}', '{}', {}, [{}])".format(
self.name, self.address, self.email, self.faculty_number,
','.join(self.courses_teaching))

# Student repr:

def __repr__(self):
""" (Faculty) -> str

Return a concise string representation of this Faculty.

>>> student = Student('Paul', 'Ajax', 'pgries@cs.toronto.edu', '1234')
>>> student.__repr__()
"Student('Paul', 'Ajax', 'pgries@cs.toronto.edu', 1234, [], [])"
"""

return "Student('{}', '{}', '{}', {}, [{}], [{}])".format(
self.name, self.address, self.email, self.student_number,
','.join(self.courses_taken), ','.join(self.courses_taking))
```

c.

Page History
• V10: Jason Montojo [almost 4 years ago]
• V9: Jason Montojo [almost 4 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]