首页 > > 详细

讲解Python、Python辅导、解析Python编程、Python辅导

CS1026: Assignment 3
Due: Friday June 15​th​ at 11:55 pm via OWL
Weight: 16%
This assignment is to be done individually. You may talk to other students on a high level, but all work must be done independently. All assignments will be run through anti-plagiarism software. First
offense penalty will be recorded as an act of academic dishonesty, and a mark of -100% will be awarded.
Learning Outcomes:

By completing this assignment, you will gain skills relating to
• Strings and text files
• Writing your own classes
• Testing code
• Using complex data structures (i.e., sets and dictionaries)

In this assignment part you will create a complete program that uses classes to store, search, remove,
and filter country data. The two major tasks are outlined below.
1.​ ​Implement a class Country
Instance Variables:
i. Name: string
ii. Population: integer
iii.Area: float
iv.Continent: string
Methods:
i. constructor
ii. Getter Methods: getName, getPopulation, getArea, getContinent,
iii.getPopDensity: This calculates and returns the population density for the
country. Population density is the population divided by the area.
iv.Setter Methods: setPopulation
v.​ ​def ​__repr__​(​self​):generate a string representation for the class
Name in Continent
e.g China in Asia class ​Country :
def ​__init__​(​self​, name, pop, area, continent) :
def ​__repr__​(​self​):
def ​setPopulation(​self​, pop):
def ​getName(​self​) :
def ​getArea(​self​) :
def ​getPopulation(​self​) :
def ​getContinent(​self​):
def ​getPopDensity(​self​) :

Test all your classes and methods before moving to the next section. Feel free to create other helper methods if necessary.

2.​ ​Implement a class called ​CountryCatalogue
This class has two instance variables, ​catalogue (​that is a set or dictionary or list of countries​)
and ​cDictionary​ (a dictionary).
This class will have the following methods.
• Constructor:​ this method will open the specified files and then first will create the
cDictionary ​and then create countries and add them to ​catalogue​. The constructor is
given additional parameters, ​continentFileName​ and ​countryFileName​, both strings.
​Major Steps
1. Fill the Dictionary:​ open the file ​continentFileName​ and fill ​cDictionary​.
The key is the country name, while the continent is the value.
2. Fill the Catalogue:​ Open the file ​countryFileName​, then read each line of
the file and from that create a country and add it to ​catalogue​.
A sample data file has been included data.txt ​(Note that both files have headers). ​Do
not forget to close any files that are opened in your methods.
• addCountry:​ given four parameters ​countryName​ (a string), ​countryPopulation​ (an
integer), ​countryArea​ (a float), ​countryContinent​ (a string), first create a country using
this information. If there is already a country with the same name, return ​False​.
Otherwise, there is a new country to add, add the newly created country to ​catalogue​,
make sure you add the continent to the ​cDictionary​. Return ​True​ after successfully
adding the new country. ​Hint: you may find the keys of cDictionary useful​.

• deleteCountry:​ given a parameter ​countryName​ (a string), if there is a country with this
name then it should be removed from the country catalogue entirely (both ​cDictionary
and ​catalogue​). Print a message informing the user whether the country has been
successfully removed from the country catalogue or not. Do not return anything.

• findCountry:​ given a parameter ​countryName​ (a string), if this country exists then
return the country from ​catalogue​. If the country does not exist, return ​None​.

• filterCountriesByContinent:​ given a parameter ​continentName​ (a string), return a list
containing all the countries that are on continents that are ​continentName​.

• printCountryCatalogue:​ print the countries of ​catalogue​ to the screen using the default
print for the Country Class. This method takes no additional parameters.

• setPopulationOfASelectedCountry​: Given two parameters ​countryName​ (a string) and
countryPopulation​ (an integer), set the population of the country named ​countryName
(if it is in the catalogue) to the value ​countryPopulation​. Return ​True​ if the population
is updated, and return ​False​ otherwise.

• findCountryWithLargestPop​: find the country with the largest population, return the
name of this country. This method takes no additional parameters.

• findCountryWithSmallestArea:​ find the country with the smallest area, return the
name of this country. This method takes no additional parameters.

• filterCountriesByPopDensity:​ given two parameters ​lowerBound​ and ​upperBound
(both integers), find all the countries (, i.e. country objects) that have a population
density that falls within these two numeric bounds inclusively. Return a list that
contains all of these countries.

• findMostPopulousContinent​: find the continent with the most number of people living
in it. Return together the name of this continent with its total population. That is, if
mostPopCont​ is the name of the continent found above and ​popMaxCont​ is the total
population of this continent, use ​return​(mostPopCont, popMaxCont).

• saveCountryCatalogue:​ given a parameter ​filename​ (a string), write the country data of
the catalogue as specified below to file ​filename​. Each entry should be formatted as
below, one country per line. Return the number of lines written to ​filename​.
Format:
Name|Continent|Population|PopulationDensity
For example:​ ​China|Asia|1200000000|14.56
​class ​CountryCatalogue:
def ​__init__​(​self​, continentFileName, countryFileName):
def ​filterCountriesByContinent(​self​,continentName):
def ​printCountryCatalogue(​self​):
def ​findCountry(​self​, countryName):
def ​deleteCountry(​self​, countryName):
def ​addCountry(​self​, countryName, countryPopulation, countryArea, countryContinent):
def ​setPopulationOfASelectedCountry(​self​, countryName, countryPopulation):
def ​saveCountryCatalogue(​self​, filename):
def ​findCountryWithLargestPop(​self​):
def ​findCountryWithSmallestArea(​self​):
def ​findMostPopulousContinent(​self​):
def ​filterCountriesByPopDensity(​self​, lowerBound, upperBound):

Test your CountryCatalogue class to make sure all functionality is working.
Use the main.py file provided as the interface to your program. The TA will use a similar main.py to
grade your assignment.​ ​DO NOT EDIT THIS FILE AT ALL
For output to both the console and file you do not need to format numbers to include commas. (e.g
1000 does NOT need to be written as 1,000). In addition, when given some string to query (e.g.
adding a country, finding a country, deleting a country), you do not need to worry about the casing of
the words, e.g. it is OK if findCountry returns None if given “canada” although there is a country
named “Canada”.
You may assume all the data is correct​ and there are no errors relating to the data file ​(so don't worry about Exceptions or validating input).

Non-functional Specifications:

1. Include brief comments in your code identifying yourself, describing the program, and describing
key portions of the code.
2. Assignments are to be done individually and must be your own work. Software may be used to
detect cheating.
3. Use Python coding conventions and good programming techniques, for example:
1. Meaningful variable names
2. Conventions for naming variables and constants
3. Use of constants where appropriate
4. Readability: indentation, white space, consistency

Submit the file in which your classes are defined. The name of the file should be ​countryGalore.py​. It
is important that your filename and class names are exactly as specified, otherwise your program will
not be able to be tested easily, and you will lose marks.

Make sure you attach your Python file to your assignment; DO NOT put the code inline in the textbox.
Make sure that you develop your code with Python 3.5 or 3.6 as the interpreter. We will not evaluate
incompatible code.

What You Will Be Marked On:
• Functional specifications:
• Does the program behave according to specifications?
• Does it run with the main program provided?
• Are your classes created properly?
• Are you using appropriate data structures?
• Is the output according to specifications?
• Non-functional specifications: as described above

Submission
Submit your ​countryGalore.py​ file via OWL. Do not submit any other files, and do not compress (zip,
archive, etc.) your files.

FAQ
• Some of the descriptions are vague.
o I know, and this is intentional. But things will fall into place once you get going.
• How do I do ​X​?
o Google it.
• Do I need to write comments?
o Obv
• I know you told me to do it ​this ​way, but I did it another way, and I think my way is better.
o Your way may be better, but I don’t care. Do it the way I told you.
• Can I work with my friend?
o No
• I know our code looks the same, but we only worked together at a high level.
o No you didn’t. If the anti-plagiarism software thinks your code is the same, you didn’t
just talk on a ​high level​. I can do simple statistics on how similar everyone’s code looks,
and if you’re an outlier, then I know you cheated. I can even very easily obtain a
probability value for you cheating. I look forward to someone trying to cheat, and then
I can say that you have a p-value of 4.5x10​-72​ that you didn’t cheat.
• I know I cheated, I know I know I was cheating, but I’m reeeeaaaaaaaaallllllly sorry ​that I got caught​.
Can we just ignore it this time?
o Lol, no.
• It’s not working, therefore Python is broken!
o Probably not; you’re very likely doing something wrong.
• If I submit it at 11:56pm, you’ll still mark it, right? I mean, commmmon!
o No. 11:55pm and earlier is on time. Anything after 11:55pm is late. Anything late is not
marked. It’s rather simple really.
• OWL was totally broken, it’s not my fault it’s late.
o Then ​try​ to get an accommodation. As far as I’m concerned, it’s late.
• I accidentally submitted the wrong code. Here is the ​right​ code, but it’s late. But you can see
that I submitted the ​wrong​ code on time! You’ll still accept it, right?
o Do you think I was born yesterday? No.
 

联系我们
  • QQ:99515681
  • 邮箱:99515681@qq.com
  • 工作时间:8:00-21:00
  • 微信:codinghelp
热点标签

联系我们 - QQ: 99515681 微信:codinghelp
程序辅导网!