diff --git a/Q1_society_allocation.py b/Q1_society_allocation.py new file mode 100644 index 0000000..d64aec5 --- /dev/null +++ b/Q1_society_allocation.py @@ -0,0 +1,49 @@ +"""Create the class Society with following information: +society_name, house_no, no_of_members, flat, income +Methods : +1)An __init__ method to assign initial values of society_name, house_no, no_of_members, income +2)allocate_flat() to allocate flat according to income using the below table -> according to income, +it will decide to flat type +3)show_data() to display the details of the entire class. +4)Create one object for each flat type, for each object call allocate_flat() and show_data() +Income Flat +>=25000 A Type +>=20000 and <25000 B Type +>=15000 and <20000 C Type +<15000 D Type""" + +class Society: #We define initial values of society_name, house_no, no_of_members, income + def __init__(self,society_name, house_no, no_of_members, income): + self.society_name = society_name + self.house_no=house_no + self.no_of_members=no_of_members + self.income=income + + def allocate_flat(self): #We define allocation types,according to income + if self.income>=25000: + self.flat='A Type' + elif 20000<=self.income <25000: + self.flat='B Type' + elif 15000<=self.income<20000: + self.flat='C Type' + elif self.income<15000 : + self.flat='D Type' + return self.flat + + def show_data(self): #We display details of entire class + print(f""" +Society Name:{self.society_name} +House_No : {self.house_no} +Number of Members: {self.no_of_members} +Income : {self.income} +Allocation : {self.allocate_flat()}""",end="\n ---->") + +Ex_1=Society("Red",4,6,15000) +Ex_2=Society("Teal",3,5,40000) +Ex_3=Society("Black",1,1,8000) +Ex_4=Society("White",11,2,23000) +Ex_1.show_data() +Ex_2.show_data() +Ex_3.show_data() +Ex_4.show_data() + diff --git a/Q2_shopping.py b/Q2_shopping.py new file mode 100644 index 0000000..82d7b7d --- /dev/null +++ b/Q2_shopping.py @@ -0,0 +1,62 @@ +"""Define a class named ItemInfo with the following description: +item_code(Item Code), item(item name), price(Price of each item), + qty(quantity in stock), discount(Discount percentage on the item), + net_price(Price after discount) + +Methods : +A member method calculate_discount() to calculate discount as per the following rules: +-If qty <= 10 —> discount is 0 +-If qty (11 to 20 inclusive) —> discount is 15 +-If qty >= 20 —> discount is 20 +-A constructor init method to assign the initial values for item_code to 0 and price, +qty, net_price and discount to null +-A function called buy() to allow user to enter values for item_code, item, price, qty. +-Then call function calculate_discount() to calculate the discount and net_price(price * qty - discount). +-A function show_all() or similar name to allow user to view the content of all the data members.""" + + + + + + +#We assign startup values to call after to calculate +class ItemInfo: + def __init__(self,item_code=0,item=None,price=None,qty=None,net_price=None,discount=None) : + self.item_code=item_code #Item Code + self.item=item #item name + self.price=price #Price of each item + self.qty=qty #Quantity in Stock + self.net_price=net_price #Discount percentage on the item + self.discount=discount #Price After Discount + + def calculate_discount(self): #This func calculate amount of discount + if self.qty<=10: + self.discount=0 + elif 11<=self.qty<=20: + self.discount=15 + elif self.qty>=20: + self.discount=20 + return self.discount + + def buy(self): #This func generates net price of items + self.item_code=int(input("Enter item code :")) + self.item=input("Enter Item Name :") + self.price=int(input("Enter price of item :")) + self.qty=int(input("Enter quantity of items :")) + self.calculate_discount() + self.net_price=(self.price * self.qty-self.discount) + return self.net_price + + def show_all(self): #This will display the content of all the data members to user . + print(f"""Item Code: {self.item_code} +Item Name: {self.item} +Price : {self.price} +Quantity : {self.qty} +Discount : {self.calculate_discount()} +Net Price : {self.net_price}""") + + +Ex=ItemInfo() +Ex.buy() +Ex.calculate_discount() +Ex.show_all() \ No newline at end of file diff --git a/Q3_numbers.py b/Q3_numbers.py new file mode 100644 index 0000000..dbf00e2 --- /dev/null +++ b/Q3_numbers.py @@ -0,0 +1,36 @@ +"""Create a class called Numbers, which has a single class attribute called multiplier, and a constructor which takes the parameters x and y (these should all be numbers). + +Write a method called add which returns the sum of the attributes x and y. +Write a class method called multiply, which takes a single number parameter a and returns the product of a and multiplier. +Write a method called subtract, which takes two number parameters, b and c, and returns b - c. +Write a method called value which returns a tuple containing the values of x and y. +Create a numbers object and call all the methods you wrote and print the results.""" + + +class Numbers: + multiplier = 1.5 #we assign class attribute as 1.5 + + def __init__(self, x, y): + self.x = x + self.y = y + + def add(self): #returns the sum of the attributes x and y. + return self.x + self.y + + def multiply(self, m): #returns multiply m by class attribute(1.5) + return self.multiplier* m + + def value(self): #converts x,y to tuple(x,y) + return (self.x,self.y) + + def subtract(self,b, c): #we assign new parameters,these take another values than x,y. + self.b=b + self.c=c + return b - c +#m,b,c are not assigned at __init_ intentionally. +numbers=Numbers(5,10) +print("The sum of the attributes x and y: %d"%(numbers.add())) +print("The substract : %d"%numbers.subtract(2,3)) +print("The multiplied by (1.5): %d"%numbers.multiply(2)) +print(numbers.value()) + diff --git a/Q4_emp_ID.py b/Q4_emp_ID.py new file mode 100644 index 0000000..ac7dc95 --- /dev/null +++ b/Q4_emp_ID.py @@ -0,0 +1,25 @@ +"""Define the Employee class with an __init__() method +--Define a class variable new_id and set it equal to 1 +--Each Employee instance will need its own unique ID. Thus, inside __init__(), +define self.id and set it equal to the class variable new_id +--Lastly, increment new_id by 1 +--Define a say_id() method +--Inside say_id(), output the string "My id is " and then the instance id. +Define the variable e1 and set it to an instance of Employee +Define the variable e2 and set it to an instance of Employee +Have both e1 and e2 output their ids""" + + +class Employee: + new_id=1 + def __init__(self): + self.employee_id = Employee.new_id + Employee.new_id+=1 + + def say_id(self): + return "My ID is %d."%(self.employee_id) # %d will display as integer. +e1=Employee() +e2=Employee() +print(e1.say_id(),e2.say_id(),sep="\n") + + diff --git a/Q5_vehicle.py b/Q5_vehicle.py new file mode 100644 index 0000000..18e71c5 --- /dev/null +++ b/Q5_vehicle.py @@ -0,0 +1,41 @@ +"""Create a Vehicle class with name, max_speed and mileage instance attributes +--Add function __str__ to vehicle class and print the info about vehicle as: + "Vehicle Model X has max speed 180 and mileage 12" +--Create a child class Bus that will inherit all of the variables and methods +of the Vehicle class +--Add attribute capacity to class Bus +--Update Bus class such that print message will be: Bus Breng has max speed +180 and mileage 50 with capacity 100 (Hint: Override _str_ method) +--Add update_capacity() method to the class Bus +--Create a Vehicle and a Bus object and print both of them +call update_capacity() method for the earlier created Bus object and print it, +see the difference""" + + + +class Vehicle: + def __init__(self,name,max_speed,mileage): + self.name=name + self.max_speed=max_speed + self.mileage=mileage + def __str__(self): + return f"Vehicle:{self.name} has max speed {self.max_speed}km and mileage {self.mileage}." + + +class Bus(Vehicle): #inherits Vehicle and add capacity in Bus additionally + def __init__(self, name, max_speed, mileage,capacity): + Vehicle.__init__(self,name, max_speed, mileage) + self.capacity=capacity + def __str__(self): # we override ___str__ for Bus class,capacity will be added + return f"Bus:{self.name} has max speed {self.max_speed}km and mileage {self.mileage} with capacity {self.capacity}." + + def updated_capacity(self,updated_capacity): #to upgrade capacity + self.capacity=updated_capacity + +V=Vehicle("Ford",200,11) +B=Bus("Man",200,20,40) + +print(V) +print(B) +B.updated_capacity(50) #upgraded capacity as 50 for B +print(B)