Answer:
See explaination
Explanation:
Make use of Python programming language.
File: Country.py
class Country:
def __init__(self, n="", p=0, a=0):
""" Constructor """
self.name = n
self.population = p
self.area = a
self.pDensity = self.population / self.area
def getCountryName(self):
""" Getter method """
return self.name
def getPopulation(self):
""" Getter Method """
return self.population
def getArea(self):
""" Getter Method """
return self.area
def getPDensity(self):
""" Getter Method """
return self.pDensity
File: CountryInfo.py
from Country import *
# Opening file for reading
with open("d:\\Python\\data.txt", "r") as fp:
# List that hold countries
countryInfo = []
# Processing file line by line
for line in fp:
# Stripping spaces and new lines
line = line.strip()
# Splitting and creating object
cols = line.split('\t')
cols[1] = cols[1].replace(',', '')
cols[2] = cols[2].replace(',', '')
c = Country(cols[0], int(cols[1]), float(cols[2]))
countryInfo.append(c)
# Processing each countryInfo
largestPop = 0
largestArea = 0
largestPDensity = 0
# Iterating over list
for i in range(len(countryInfo)):
# Checking population
if countryInfo[i].getPopulation() > countryInfo[largestPop].getPopulation():
largestPop = i;
# Checking area
if countryInfo[i].getArea() > countryInfo[largestPop].getArea():
largestArea = i;
# Checking population density
if countryInfo[i].getPDensity() > countryInfo[largestPop].getPDensity():
largestPDensity = i;
print("\nLargest population: " + countryInfo[largestPop].getCountryName())
print("\nLargest area: " + countryInfo[largestArea].getCountryName())
print("\nLargest population density: " + countryInfo[largestPDensity].getCountryName() + "\n")
Please go to attachment for sample program run