Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

An Introduction to Programming, Using Python. By David Schneider Chapter 7.1 Program 1 I need assistance with a program. I created the class but struggling

An Introduction to Programming, Using Python. By David Schneider

Chapter 7.1 Program 1

I need assistance with a program. I created the class but struggling on best way to create the data dictionary. I am leaning towards a key and the value as list or can go for a more complex approach of create dictionary within a dictionary object. I have not found good example of code to assist. me. Could use guidance quickly. Here is my class code and the actual programming problem. Thanks for your help.

class Nation: def __init__(self, country="", continent="", population=0.0, area=0): self._country = country self._continent = continent self._population = population self._area = area

def setCountry(self, country): self._country = country

def setContinent(self, continent): self._continent = continent

def setPopulation(self, population): self._population = population

def setArea(self, area): self._area = area

def getCountry(self): return self._country

def getContinent(self): return self._continent

def getPopulation(self): return self._population

def getArea(self): return self._area

def popDensity(self): self._popDensity = (1000000 * self._population) / self._area return self._popDensity

Must use UN.txt file with following format

country,continent,area,population

Canada,North America, 34.8,3855000

France, Europe,66.3,211209

Pakistan,Asia,196.2,310403

1. United Nations. The file UN.txt gives data about 193 members of the United Nations. Each Line of the file contains four pieces of data about of country - name, continent, population(in millions) and land area (square miles).

a) Create a class names Nations with four instances variables to hold the data for a country and a method names popDensity that calculates the population density of a country. Write a program that uses the class to create a dictionary of 193 items, where each item of the dictionary gas the form:

country: Nation object for that country

use the file UN.txt to create the dictionary and save the dictionary in a pickled binary file names nationsDict.dat. Also save the class Nations in a file named nation.py.

b) Write a program that requests the name of a UN member county as input and then displays information about the country as input and then displays information about the country as shown below. Use the pickled binary file nationsDict.dat and the file nation.py created in part a)

Enter a Country: Canada

Continent: North America

Population: 34,800,000

Area: 3,855,000.00 square miles

c) Write a program that requests the name of a continent as input, and the displays the names in descending order of the 5 most densely populated UN member countries in the continent as per the below. Use the pickled binary file nationsDict.dat and the file nation.py created in part a)

Enter a continent: South America

Ecuador

Columbia

Venezuala

Brazil

Peru

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Systems For Advanced Applications 9th International Conference Dasfaa 2004 Jeju Island Korea March 2004 Proceedings Lncs 2973

Authors: YoonJoon Lee ,Jianzhong Li ,Kyu-Young Whang

2004th Edition

3540210474, 978-3540210474

More Books

Students also viewed these Databases questions

Question

What are the parts of an egg?

Answered: 1 week ago

Question

sharing of non-material benefits such as time and affection;

Answered: 1 week ago