0% found this document useful (0 votes)
3 views4 pages

SCD Lab3

Uploaded by

aizarasool0
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views4 pages

SCD Lab3

Uploaded by

aizarasool0
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Name: Aiza Rasool

Roll no: 2225165083


Dept.: BS SOFTWARE ENGINEERING
Submitted To: Tuba Younas
Assignment

1. Employee Class

class Employee:
def __init__(self, role, department, salary):
self.role = role
self.department = department
self.salary = salary

def show_details(self):
return f"Role: {self.role}, Department: {self.department}, Salary:
{self.salary}"

2. Book Class
class Book:
def __init__(self, title, author, price):
self.title = title
self.author = author
self.price = price

def display_details(self):
return f"Title: {self.title}, Author: {self.author}, Price: $
{self.price:.2f}"

def apply_discount(self, discount):


self.price -= self.price * (discount / 100)
return f"New Price after {discount}% discount: ${self.price:.2f}"

3. Vehicle Class with Subclasses


class Vehicle:
def __init__(self, make, model, year):
self.make = make
self.model = model
self.year = year

def display_details(self):
return f"{self.year} {self.make} {self.model}"

class Car(Vehicle):
def __init__(self, make, model, year, number_of_doors):
super().__init__(make, model, year)
self.number_of_doors = number_of_doors

def display_details(self):
return f"{super().display_details()}, Doors:
{self.number_of_doors}"
class Motorcycle(Vehicle):
def __init__(self, make, model, year, type_of_handlebar):
super().__init__(make, model, year)
self.type_of_handlebar = type_of_handlebar

def display_details(self):
return f"{super().display_details()}, Handlebar:
{self.type_of_handlebar}"

4. Shape with Polymorphism


import math

class Shape:
def area(self):
pass

class Circle(Shape):
def __init__(self, radius):
self.radius = radius

def area(self):
return math.pi * self.radius ** 2

class Rectangle(Shape):
def __init__(self, width, height):
self.width = width
self.height = height

def area(self):
return self.width * self.height

# Demonstrating Polymorphism
shapes = [Circle(5), Rectangle(4, 6)]
for shape in shapes:
print(f"Area: {shape.area():.2f}")

5. BankAccount Class
class BankAccount:
def __init__(self, balance=0):
self.__balance = balance

def deposit(self, amount):


if amount > 0:
self.__balance += amount
return f"Deposited: ${amount:.2f}"

def withdraw(self, amount):


if 0 < amount <= self.__balance:
self.__balance -= amount
return f"Withdrew: ${amount:.2f}"
return "Insufficient balance"

def check_balance(self):
return f"Current Balance: ${self.__balance:.2f}"
6. Abstract Employee Class
from abc import ABC, abstractmethod

class Employee(ABC):
@abstractmethod
def calculate_salary(self):
pass

class FullTimeEmployee(Employee):
def __init__(self, monthly_salary):
self.monthly_salary = monthly_salary

def calculate_salary(self):
return f"Monthly Salary: ${self.monthly_salary:.2f}"

class PartTimeEmployee(Employee):
def __init__(self, hourly_rate, hours_worked):
self.hourly_rate = hourly_rate
self.hours_worked = hours_worked

def calculate_salary(self):
return f"Total Salary: ${self.hourly_rate * self.hours_worked:.2f}"

7. E-Commerce System
class Product:
def __init__(self, name, price):
self.name = name
self.price = price

class Customer:
def __init__(self, name):
self.name = name

class Order:
def __init__(self, customer):
self.customer = customer
self.products = []

def add_product(self, product):


self.products.append(product)

def calculate_total(self):
return sum(product.price for product in self.products)

def display_order_details(self):
details = f"Customer: {self.customer.name}\nOrder Details:\n"
for product in self.products:
details += f"- {product.name}: ${product.price:.2f}\n"
details += f"Total Price: ${self.calculate_total():.2f}"
return details

You might also like