diff --git a/Week05_Hw_1.py b/Week05_Hw_1.py new file mode 100644 index 0000000..da69815 --- /dev/null +++ b/Week05_Hw_1.py @@ -0,0 +1,69 @@ +'''Question 1: +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`, `house_no`, `no_of_members`, `income` +* `allocate_flat()` to allocate flat according to income using the below table -> according to income, it will decide to flat type +* `show_data()` to display the details of the entire class. +* 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: + + 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): + + if self.income >=25000: + return 'A type' + elif self.income >=20000 and self.income< 25000: + return 'B type' + elif self.income >=15000 and self.income<20000: + return 'C type' + else: + return 'D type' + + def show_data(self): + print("Society Name :", self.society_name) + print("House No :", self.house_no) + print("No of Memmbers :", self.no_of_members) + print("Income :", self.income) + print("Allocate Flat :", self.allocate_flat()) + + + + + + + +Society1=Society("Internet Society",24,234,27000) +Society2=Society("Language Society",48,99,22000) +Society3=Society("Information Technology Society",22,3000,17590) +Society4=Society("Gym Society",9,45,7766) + +Society1.show_data() +Society2.show_data() +Society3.show_data() +Society4.show_data() + + + + + + + diff --git a/Week05_Hw_2.py b/Week05_Hw_2.py new file mode 100644 index 0000000..b2cf470 --- /dev/null +++ b/Week05_Hw_2.py @@ -0,0 +1,80 @@ +''' +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. + +''' +class ItemInfo: + + def __init__(self,item_code, item_name, price, qty, discount, net_price ): + + self.item_code=item_code + self.item_name=item_name + self.price=price + self.qty=qty + self.discount=discount + self.net_price=net_price + + def Buy(self): + + self.item_code=input("Enter Item Code : ") + self.item_name=input("Enter Item Name : ") + self.price=float(input("Enter Price : ")) + self.qty=int(input("Enter Quantity : ")) + + def calculate_discount(self): + + + if self.qty <= 10: + + self.discount=0 + #return self.discount + + elif self.qty>11 and self.qty<20: + + self.discount=15 + #return self.discount + + else: + self.discount=20 + #return self.discount + + self.net_price= (self.price*self.qty - self.discount) + self.discount + + def __init__(self,item_code=None,price=None,qtr=None,discount=None,net_price=None): + + self.item_code=item_code + self.price=price + self.qty=qtr + self.discount=discount + self.net_price=net_price + + def Show_all(self): + + print("Item Code : ", self.item_code) + print("Item Name : ", self.item_name) + print("Item Price : ", self.price) + print("Item Quantity : ", self.qty) + print("Item Discount : ", self.discount) + print("Item Net Price : ", self.net_price) + + + +Item1=ItemInfo() +Item1.Buy() +Item1.calculate_discount() +Item1.Show_all() + diff --git a/Week05_Hw_3.py b/Week05_Hw_3.py new file mode 100644 index 0000000..ae70c2e --- /dev/null +++ b/Week05_Hw_3.py @@ -0,0 +1,44 @@ +## Question 3: +''' +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: #Create a class called `Numbers`, which has a single class attribute called `multiplier`,and a constructor which takes the parameters `x` and `y` + multiplier=1 + def __init__(self,x,y): + self.x=x + self.y=y + + def add(self): #a method called `add` which returns the sum of the attributes `x` and `y`. + return self.x + self.y + + def multiply(self,a): #Write a class method called `multiply`,which takes a single number parameter `a` and returns the product of `a` and `multiplier`. + + self.a=a + return self.a * self.multiplier + + def subtract(self,b,c): #a method called `subtract`, which takes two number parameters, `b` and `c`, and returns `b - c`. + self.b=b + self.c=c + + return self.b-self.c + + def value(self): #a method called `value` which returns a tuple containing the values of `x` and `y`. + tuple=(self.x + self.y) + + return tuple + +object=Numbers(6,10) #created a numbers object and call all the methods you wrote and print the results. + +print("Add: ",object.add()) +print("Multiply:",object.multiply(4)) +print("Subtract: ",object.subtract(5,8)) +print("Numbers: ",object.value()) + diff --git a/Week05_Hw_4.py b/Week05_Hw_4.py new file mode 100644 index 0000000..965a8ec --- /dev/null +++ b/Week05_Hw_4.py @@ -0,0 +1,34 @@ +'''## Question 4: + +* 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.id=Employee.new_id + Employee.new_id +=1 + + def say_id(self): + + print (f"My ID is ", self.id) + +e1=Employee() +e2=Employee() + +e1.say_id() +e2.say_id() + + + diff --git a/Week05_Hw_5.py b/Week05_Hw_5.py new file mode 100644 index 0000000..68eb38e --- /dev/null +++ b/Week05_Hw_5.py @@ -0,0 +1,45 @@ +'''## Question 5: + +* 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: #Create a `Vehicle` class with `name`, `max_speed` and `mileage` instance attributes + def __init__(self,name, max_speed,mileage): + self.name=name + self.max_speed=max_speed + self.mileage=mileage + + def __str__(self): #Created a function `__str__` to vehicle class and print the info about vehicle + + return f" Vehicle Model {self.name} has max speed {self.max_speed} and mileage {self.mileage}" + +class Bus(Vehicle): #Created a child class `Bus` that will inherit all of the variables and methods of the `Vehicle` class, + + def __init__(self, name, max_speed, mileage,capacity): #Added attribute `capacity` to class `Bus` + super().__init__(name, max_speed, mileage) + self.capacity=capacity + + def __str__(self): + + return f" Vehicle Model {self.name} has max speed {self.max_speed} and mileage {self.mileage} capacity {self.capacity}" + + def update_capacity(self,new_capacity): #Added `update_capacity()` method + + self.capacity=new_capacity + #return f"Bus Breng has max speed {self.max_speed()} and mileage {self.mileage()} with capacity {self.new_capacity}" + + +Vehicle1=Vehicle("Mercedes",300,170000) #created a vehicle and bus object and print. +Bus1=Bus("Man",200,450000,40) #created a vehicle and bus object and print. + +print(Vehicle1) +print(Bus1) + +Bus1.update_capacity(55) #updated the capacity with the update_capacity method and print it again. +print(Bus1) \ No newline at end of file