Q&A CBSE12D-2016-B02C #6468
Question:
Write a class CITY in Python with following specifications
Instance Attributes
- Code # Numeric value
- Name # String value
- Pop # Numeric value for Population
- KM # Numeric value
- Density # Numeric value for Population Density
Methods:
- CalDen() # Method to calculate Density as Pop/KM
- Record() # Method to allow user to enter values
Code,Name,Pop,KM and call CalDen() method
- See() # Method to display all the members also display
a message "Highly Populated Area"
if the Density is more than 12000.
Answer:
class CITY:
def __init__(self):
self.Code = 0
self.Name = ""
self.Pop = 0
self.KM =0
self.Density=0
def CalDen(self):
self.Density = self.Pop / self.KM
def Record(self):
self.Code = input("Enter Code")
self.Name = raw_input("Enter Name")
self.Pop = input("Enter population")
self.KM = input("Enter KM")
CalDen(self) // or self.CalDen()
def See(self):
print Code,Name,Pop, KM, Density
if self.Density > 12000:
print("Highly Populated Area")
# OR print("Highly populated Area")
