0% found this document useful (0 votes)
65 views

Practiceset 2

The document contains 5 questions asking to write Python classes to model different scenarios. For question 1, a BankAccount class is defined with attributes like account number, balance, and customer name. Methods like deposit, withdraw, and check_balance are added. For question 2, a StringManipulator class with methods get_string and print_string_uppercase is written. Question 3 creates a Circle class with methods to calculate area and perimeter. Question 4 defines a Car class with attributes make, model, and year, and displays an object's details. Finally, question 5 creates a MathUtils class with methods to calculate factorial and check if a number is even.

Uploaded by

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

Practiceset 2

The document contains 5 questions asking to write Python classes to model different scenarios. For question 1, a BankAccount class is defined with attributes like account number, balance, and customer name. Methods like deposit, withdraw, and check_balance are added. For question 2, a StringManipulator class with methods get_string and print_string_uppercase is written. Question 3 creates a Circle class with methods to calculate area and perimeter. Question 4 defines a Car class with attributes make, model, and year, and displays an object's details. Finally, question 5 creates a MathUtils class with methods to calculate factorial and check if a number is even.

Uploaded by

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

Practice Set 2

Nikhil Rai
22BCE11442

Q1) Write a Python class Bank Account with attributes like


account_number, balance, date_of_opening and customer_name,
and methods like deposit, withdraw, and check_balance.
Code
import datetime
class BankAccount:
def __init__(self, account_number, customer_name, initial_balance=0):
self.account_number=account_number
self.customer_name=customer_name
self.balance=initial_balance
self.date_of_opening=datetime.datetime.now()
def deposit(self, amount):
if(amount>0):
self.balance+=amount
print(f"Deposited ${amount}. New balance: ${self.balance}")
else:
print("Invalid deposit amount. Please enter a positive
value.")
def withdraw(self, amount):
if(0<amount<=self.balance):
self.balance-=amount
print(f"Withdrew ${amount}. New balance: ${self.balance}")
else:
print("Invalid withdrawal amount or insufficient funds.")
def check_balance(self):
print(f"Account balance for {self.customer_name} (Account
#{self.account_number}): ${self.balance}")
if(__name__ == "__main__"):
# Creating a bank account
account1 = BankAccount(account_number="123456789", customer_name="John
Doe", initial_balance=1000)
# Depositing and withdrawing
account1.deposit(500)
account1.withdraw(200)
# Checking balance
account1.check_balance()

oUTPUT
Deposited $500. New balance: $1500
Withdrew $200. New balance: $1300
Account balance for John Doe (Account #123456789): $1300

Q2) Write a Python class that has two methods: get_String and
print_String , get_String accept a string from the user and
print_String prints the string in upper case.
Code
class StringManipulator:
def __init__(self):
self.user_string = ""
def get_string(self):
self.user_string = input("Enter a string: ")
def print_string_uppercase(self):
print("Uppercase String:", self.user_string.upper())
if(__name__ == "__main__"):
# Creating an instance of StringManipulator
string_manipulator = StringManipulator()
# Getting a string from the user
string_manipulator.get_string()
# Printing the entered string in uppercase
string_manipulator.print_string_uppercase()

oUTPUT
Enter a string: hello everyone
Uppercase String: HELLO EVERYONE

Q3) Write a Python program to create a class representing a Circle.


Include methods to calculate its area and perimeter.
Code
import math
class Circle:
def __init__(self, radius):
self.radius=radius
def calculate_area(self):
area=math.pi*self.radius**2
return area
def calculate_perimeter(self):
perimeter=2*math.pi*self.radius
return perimeter
if(__name__ == "__main__"):
# Creating an instance of Circle with a radius of 5
circle1=Circle(radius=5)
# Calculating and printing the area
area=circle1.calculate_area()
print(f"Area of the circle with radius {circle1.radius}: {area:.2f}")
# Calculating and printing the perimeter
perimeter=circle1.calculate_perimeter()
print(f"Perimeter of the circle with radius {circle1.radius}:
{perimeter:.2f}")

oUTPUT
Area of the circle with radius 5: 78.54
Perimeter of the circle with radius 5: 31.42
Q4) Create a Python class called “Car” with attributes like make,
model, and year. Then, create an object of the “Car” class and
print its details.
Code
class Car:
def __init__(self, make, model, year):
self.make=make
self.model=model
self.year=year
def display_details(self):
print(f"Car Details:\nMake: {self.make}\nModel:{self.model}\nYear:
{self.year}")
if(__name__ == "__main__"):
# Creating an instance of the Car class
car1=Car(make="Toyota", model="Camry", year=2022)
# Displaying the details of the car
car1.display_details()

oUTPUT
Car Details:
Make: Toyota
Model: Camry
Year: 2022

Q5) Create a class called “MathUtils” with class methods for


calculating the factorial of a number and a static method to check
if a number is even.
Code
class MathUtils:
@classmethod
def factorial(cls, n):
if(n==0 or n==1):
return 1
else:
return n*cls.factorial(n-1)
@staticmethod
def is_even(number):
return number%2==0
if(__name__ == "__main__"):
# Using the class method to calculate the factorial of 5
result_factorial=MathUtils.factorial(5)
print(f"The factorial of 5 is: {result_factorial}")
# Using the static method to check if a number is even
number_to_check=8
if MathUtils.is_even(number_to_check):
print(f"{number_to_check} is an even number.")
else:
print(f"{number_to_check} is an odd number.")

oUTPUT
The factorial of 5 is: 120
8 is an even number.

You might also like