InterviewSolution
Saved Bookmarks
| 1. |
Write a class CITY in Python with following– CName # String Value specifications :– Pop # Numeric value for Population instance attributes-KM # Numeric value– Ccode # Numeric value– Density #Numeric value for Population DensityMethods:– 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”) |
|