DS Assigment 4
DS Assigment 4
2.Create a class called Rectangle with methods to calculate the area and perimeter of the rectangle.
x=94
######
print("welcome to area calculator\n press 1 for circle\n press 2 for rectangle
\npress 3 for sqare\n press 4 for triangle \n press 5 for cube \n press 6 for
parallelogram \n press 7 for cylinder ")
choice=int(input('enter a no >>>> '))
if choice==1: #circle
if choice==2: #rectangle
if choice==3: #sqare
if choice==4: #triangle
if choice==5: #cube
if choice==6: #parallelogram
if choice==7: #cylinder
3.Create a class called BankAccount with methods to deposit, withdraw and check the balance.
class Bank_Account:
def __init__(self):
self.balance=0
print("Hello!!! Welcome to the Deposit & Withdrawal Machine")
def deposit(self):
amount=float(input("Enter amount to be Deposited: "))
self.balance += amount
print("\n Amount Deposited:",amount)
def withdraw(self):
amount = float(input("Enter amount to be Withdrawn: "))
if self.balance>=amount:
self.balance-=amount
print("\n You Withdrew:", amount)
else:
print("\n Insufficient balance ")
def display(self):
print("\n Net Available Balance=",self.balance)
# Driver code
4.Create a class called Student with methods to calculate the average grade and display the student
information.
5.Create a class called Employee with methods to calculate the salary and display the employee
information.
6. Create a class called Animal with methods to make a sound and move. Then create subclasses for
different types of animals (e.g. Dog, Cat, Bird) and override the methods to customize their behavior.
class Animal:
def __init__(self, name):
self.name = name
def make_sound(self):
pass
def move(self):
pass
class Dog(Animal):
def make_sound(self):
return f"{self.name} barks"
def move(self):
return f"{self.name} runs on four legs"
class Cat(Animal):
def make_sound(self):
return f"{self.name} meows"
def move(self):
return f"{self.name} walks on four legs"
class Bird(Animal):
def make_sound(self):
return f"{self.name} chirps"
def move(self):
return f"{self.name} flies"
# Usage
if __name__ == "__main__":
dog = Dog("Buddy")
cat = Cat("Whiskers")
bird = Bird("Polly")
7.Create a class called Car with methods to start the engine, accelerate, and brake. Then create
subclasses for different types of cars (e.g. SportsCar, SUV, Sedan) and override the methods to
customize their performance.
class Car:
def __init__(self, make, model):
self.make = make
self.model = model
self.speed = 0
self.engine_started = False
def start_engine(self):
self.engine_started = True
print(f"The {self.make} {self.model}'s engine is now running.")
class SportsCar(Car):
def __init__(self, make, model):
super().__init__(make, model)
self.sport_mode = False
def enable_sport_mode(self):
self.sport_mode = True
print(f"Sport mode enabled for the {self.make} {self.model}. It's
ready for high performance!")
class SUV(Car):
def __init__(self, make, model):
super().__init__(make, model)
self.off_road_mode = False
def enable_off_road_mode(self):
self.off_road_mode = True
print(f"Off-road mode enabled for the {self.make} {self.model}. It can
handle rough terrain with ease!")
class Sedan(Car):
def __init__(self, make, model):
super().__init__(make, model)
# usage:
if __name__ == "__main__":
sports_car = SportsCar("Ferrari", "488 GTB")
suv = SUV("Jeep", "Grand Cherokee")
sedan = Sedan("Toyota", "Camry")
sports_car.start_engine()
suv.start_engine()
sedan.start_engine()
sports_car.enable_sport_mode()
suv.enable_off_road_mode()
sports_car.accelerate(50)
suv.accelerate(40)
sedan.accelerate(30)
sports_car.brake(20)
suv.brake(15)
sedan.brake(10)
8.Create a class called Person with methods to speak and walk. Then create subclasses for different
types of people (e.g. Teacher, Student, Athlete) and override the methods to reflect their roles and
personalities.
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
def speak(self):
print(f"{self.name} is speaking.")
def walk(self):
print(f"{self.name} is walking.")
class Teacher(Person):
def __init__(self, name, age, subject):
super().__init__(name, age)
self.subject = subject
def speak(self):
print(f"{self.name} is teaching {self.subject}.")
class Student(Person):
def __init__(self, name, age, grade):
super().__init__(name, age)
self.grade = grade
def speak(self):
print(f"{self.name} is studying in grade {self.grade}.")
class Athlete(Person):
def __init__(self, name, age, sport):
super().__init__(name, age)
self.sport = sport
def walk(self):
print(f"{self.name} is walking to practice for {self.sport}.")
def speak(self):
print(f"{self.name} is an athlete who plays {self.sport}.")
# Example usage:
teacher = Teacher("Mr. Smith", 35, "Math")
student = Student("Alice", 16, 10)
athlete = Athlete("John", 25, "Basketball")
import math
class Shape:
def area(self):
pass
def perimeter(self):
pass
class Rectangle(Shape):
def __init__(self, length, width):
self.length = length
self.width = width
def area(self):
return self.length * self.width
def perimeter(self):
return 2 * (self.length + self.width)
class Triangle(Shape):
def __init__(self, base, height, side1, side2, side3):
self.base = base
self.height = height
self.side1 = side1
self.side2 = side2
self.side3 = side3
def area(self):
return 0.5 * self.base * self.height
def perimeter(self):
return self.side1 + self.side2 + self.side3
class Circle(Shape):
def __init__(self, radius):
self.radius = radius
def area(self):
return math.pi * self.radius**2
def perimeter(self):
return 2 * math.pi * self.radius
# Example usage:
rectangle = Rectangle(5, 3)
print("Rectangle Area:", rectangle.area())
print("Rectangle Perimeter:", rectangle.perimeter())
triangle = Triangle(4, 3, 5, 6, 7)
print("Triangle Area:", triangle.area())
print("Triangle Perimeter:", triangle.perimeter())
circle = Circle(4)
print("Circle Area:", circle.area())
print("Circle Circumference:", circle.perimeter())
10.Create a class called BankAccount with methods to deposit and withdraw money. Then create
subclasses for different types of accounts (e.g. Checking Account, Savings Account,
MoneyMarketAccount) and override the methods to reflect their features and restrictions.
class BankAccount:
def __init__(self, account_number, balance=0):
self.account_number = account_number
self.balance = balance
class CheckingAccount(BankAccount):
def __init__(self, account_number, balance=0, overdraft_limit=100):
super().__init__(account_number, balance)
self.overdraft_limit = overdraft_limit
class SavingsAccount(BankAccount):
def __init__(self, account_number, balance=0, interest_rate=0.02):
super().__init__(account_number, balance)
self.interest_rate = interest_rate
def add_interest(self):
interest = self.balance * self.interest_rate
self.balance += interest
return f"Added ${interest} in interest. New balance: ${self.balance}"
class MoneyMarketAccount(BankAccount):
def __init__(self, account_number, balance=0, monthly_withdraw_limit=6):
super().__init__(account_number, balance)
self.monthly_withdraw_limit = monthly_withdraw_limit
self.monthly_withdraw_count = 0
# usage:
checking_account = CheckingAccount("C12345", 1000, overdraft_limit=200)
print(checking_account.deposit(500))
print(checking_account.withdraw(1500))