Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions Question3.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
'''## 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:
def __init__(self, x, y):
self.x = x
self.y = y
def add(self):
return self.x + self.y
def multiply(self, a, multiplier):
return a *multiplier
def subtract(self, b, c):
return b-c
def value(self):
return (self.x, self.y)

number = Numbers(13,4)
print(number.add())
print(number.multiply(13,4))
print(number.subtract(13,4))
print(number.value())
45 changes: 45 additions & 0 deletions Question_1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
'''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:
self.flat = "A Type"
if 25000 >= self.income >= 20000:
self.flat = "B Type"
if 20000 >= self.income >= 15000:
self.flatflat = "C Type"
if self.income < 15000:
self.flat = "D Type"
return self.flat
def show_data(self):
print(f"the society name is :{self.society_name}, the income is : {self.income}, the flat type is :{self.flat} , the house no is :{self.house_no}")

s1 = Society("s1", 1, 3, 30000)
s2 = Society("s2", 2, 2, 22000)
s3 = Society("s3", 3, 3, 18000)
s4 = Society("s4", 4, 4, 14000)
Society.show_data(s1)
46 changes: 46 additions & 0 deletions Question_2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
'''## Question 2:
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 = 0, item = None, price = None, qty = None, discount = None, net_price= None ):
self.item_code = item_code
self.item = item
self.price = price
self.qty = qty
self.discount = discount
self.net_price = net_price
def calculate_discount(self):
if self.qty <= 10:
self.discount = 0
elif 20>=self.qty >= 10:
self.discount = 15
elif self.qty >= 20:
self.discount = 20
return self.discount
def buy(self):
self.item_code=input("item code: ")
self.item = input("item: ")
self.price = int(input("price: "))
self.qty = float(input("qty: "))
self.calculate_discount()
self.net_price = self.price * self.qty - self.discount

def show_all(self):
return print(f"item code:{self.item_code} \n item: {self.item} \n price: {self.price} \n qty: {self.qty } \n discount:{self.discount} \n net price:{self.net_price} ")
shirt = ItemInfo()
shirt.buy()
shirt.show_all()
24 changes: 24 additions & 0 deletions Question_4.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
'''## 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, id):
self.id=id
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()
e1.say_id()
33 changes: 33 additions & 0 deletions Question_5.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
'''## 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:
def __init__(self, name, max_speed, mileage):
self.name = name
self.max_speed = max_speed
self.mileage = mileage
def __str__(self):
return f"Vehicle Model {self.name} has max speed {self.max_speed} and mileage {self.mileage}"
class Bus(Vehicle):
def __init__(self, name, max_speed, mileage, capacity):
super().__init__(self, name, max_speed, mileage)
self.capacity = capacity
def __str__(self):
return f'Bus {self.name} has max speed {self.max_speed} and millage {self.millage} with capacity {self.capacity}'
def update_capacity(self,capacity):
self.capacity = capacity
c1 = Vehicle("mazda",170,200)
print(c1)
b1 = Bus("bmw",130,2000,30)
print(b1)
b1.update_capacity(40)
print(b1)