CS21 Lab 9: Country Class Documentation
These are the method functions of the Country Class:
the Constructor
The constructor creates a new Country
object.
Country(name, co2_vals, pop_1960, pop_2020, gdp_1960, gdp_2020):
"""
Creates a new Country object with passed values:
name: the name of the country (str)
CO2_vals: list of 5 CO2 emission values for 1960, 1980, 2000, 2020, 2022
(list of float)
pop_1960: 1960 population in Millions (float)
pop_2020: 2020 population in Millions (float)
gdp_1960: 1960 GDP in Billions of US dollars (float)
gdp_2020: 2020 GDP in Billions of US dollars (float)
"""
-
An example call:
co2_list = [13.0868, 31.777, 42.11, 41.231, 40.808] nor = Country("Norway", co2_list, 3.581239, 5.379475, 5.163271598, 362.1983184)
getCO2(year)
Gets the country’s CO2 emissions level for a given year.
getCO2(year):
"""
Get the country's CO2 emission level for the passed year
year: an int value (should be one of 1960, 1980, 2000, 2020, or 2022)
returns: returns the countries CO2 emission level for the passed year
returns -1 if the country has no reported level for that year
returns -2 if the passed year is invalid
"""
-
An example call:
val = nor.getCO2(1980)
getCO2Vals()
Returns a list of CO2 values for the years 1960, 1980, 2000, 2020, 2022. The list may be modified by the caller.
def getCO2Vals():
"""
Returns a list of CO2 values for the Country for the years
(1960, 1980, 2000, 2020, and 2022) in that order.
The caller can modify the returned list.
"""
-
An example call:
vals = nor.getCO2Vals()
getGDP(year)
Gets the country’s GDP for a given year.
getGDP(year):
"""
Get the country's GDP in the passed year
year: one of 1960 or 2020 (int)
returns: the GDP value of the passed year (float)
or returns -2 if the passed year in invalid
"""
-
An example call:
gdp = nor.getGDP(2020)
getName()
Get the country’s name.
getName():
"""
Get the country's Name
returns: the name of the country (str)
"""
-
An example call:
name = nor.getName()
getPopulation(year)
Get the country’s Population for a given year.
getPopulation(year):
"""
Get the country's population in the passed year
year: one of 1960 or 2020 (int)
returns: the population value of the passed year (float)
or returns -2 if the passed year in invalid
"""
-
An example call:
pop = nor.getPopulation(1960)
print a Country object
To print out information about a Country object call the print
function passing it a Country object.
-
An example call:
print(nor)
This call would result in the following output:
Norway 1960 pop: 3.581239 2020 pop: 5.379475 1960 GDP: 5.163272 2020 GDP: 362.198318 1960 1980 2000 2020 2022 13.086800 31.777000 42.110000 41.231000 40.808000