Q&A CBSE12A-2016-B02C #6290
Question:
Write a class CITY in Python with following specifications
Instance Attributes
-Ccode # Numeric value
-CName # String value
-Pop # Numeric value for Population
-KM # Numeric value
-Density # Numeric value for Population Density
Methods:
-DenCal() # Method to calculate Density as Pop/KM
-Record() # Method to allow user to enter values
Ccode,CName,Pop,KM and call DenCal() method
-View() # Method to display all the members
also display a message ”Highly Populated City”
if the Density is more than 10000
Answer:
class CITY:
def __init__(self):
self.Ccode = 0
self.CName = ""
self.Pop = 0
self.KM =0
self.Density=0
def DenCal(self):
self.Density = self.Pop / self.KM
def Record(self):
self.Ccode = input("Enter CCode")
self.CName = raw_input("Enter CName")
self.Pop = input("Enter population")
self.KM = input("Enter KM")
DenCal(self) // or self.DenCal()
def View(self):
print CCode,CName,Pop, KM, Density
if self.Density > 10000:
print("Highly populated city")
# OR print("Highly populated city")
