Advertisements
Advertisements
प्रश्न
Create the class SOCIETY with following information:
society_name
house_no
no_of_members
flat
income
Methods
→ An __init__ method to assign initial values of society_name as "Surya Apartments", flat as "A Type", house_no as 20, no_of_members as 3, income as 25000.
→ Inputdata( ) - to read data members(society,house_no,no_of_members&income) and call allocate_flat().
→ allocate_flat( ) - To allocate flat according to income
| Income | Flat |
| >=25000 | A Type |
| >=20000 and <25000 | B Type |
| <15000 | C Type |
→ Showdata( ) - to display the details of the entire class.
Advertisements
उत्तर
Logic: The constructor assigns the given default values. input_data() accepts the details, calls allocate_flat(), and show_data() displays the object's details.
Assumption: The table omits the range from 15000 to below 20000. It is treated as C Type, i.e. income below 20000.
class Society:
def __init__(self):
# Assign initial values
self.society_name = "Surya Apartments"
self.house_no = 20
self.no_of_members = 3
self.flat = "A Type"
self.income = 25000
def input_data(self):
# Read details from the user
self.society_name = input("Enter society name: ")
self.house_no = int(input("Enter house number: "))
self.no_of_members = int(input("Enter number of members: "))
self.income = float(input("Enter monthly income: "))
self.allocate_flat()
def allocate_flat(self):
# Allocate flat according to income
if self.income >= 25000:
self.flat = "A Type"
elif self.income >= 20000:
self.flat = "B Type"
else:
self.flat = "C Type"
def show_data(self):
# Display all data members
print("Society Name:", self.society_name)
print("House Number:", self.house_no)
print("Number of Members:", self.no_of_members)
print("Income:", self.income)
print("Flat Allocated:", self.flat)
s1 = Society()
s1.input_data()
s1.show_data()
Society is written in PascalCase as per Python naming convention; it represents the class asked as SOCIETY.
