Problems on Class and object

October 9, 2025

1. Define a Basic Class

Problem:
Create a class Car with attributes brand, model, and year.
Add a method start_engine() that prints a message like:
"The 2022 Tesla Model S engine has started."

Explanation:
Introduces the concept of defining a class, using the constructor (__init__), and creating methods that use instance data.

Sample Input:

car1 = Car("Tesla", "Model S", 2022)
car1.start_engine()

Sample Output:

The 2022 Tesla Model S engine has started.


2. Class Representing a Movie

Problem:
Create a class Movie with attributes title, director, and rating.
Add a method describe() that prints details like:
"Inception directed by Christopher Nolan has a rating of 9.0."

Explanation:
Shows how to represent real-world entities and display their information through methods.

Sample Input:

movie1 = Movie("Inception", "Christopher Nolan", 9.0)
movie1.describe()

Sample Output:

Inception directed by Christopher Nolan has a rating of 9.0.


3. Shopping Cart with Total Calculation

Problem:
Create a class ShoppingCart that stores items and prices.
Add methods:

  • add_item(name, price)
  • total() — returns the total price

Explanation:
Teaches how to store multiple data (lists or dictionaries) inside objects and operate on them using methods.

Sample Input:

cart = ShoppingCart()
cart.add_item("Book", 250)
cart.add_item("Pen", 30)
cart.total()

Sample Output:

Total: ₹280


4. Track Employee Bonuses

Problem:
Create a class Employee with name and salary.
Add a method apply_bonus(percent) that increases the salary by a percentage.

Explanation:
Practices modifying attributes and applying formulas within methods.

Sample Input:

emp = Employee("Aman", 50000)
emp.apply_bonus(10)

Sample Output:

Bonus applied. New salary: ₹55000.0


5. Temperature Converter

Problem:
Create a class Temperature with a Celsius value.
Add methods:

  • to_fahrenheit()
  • to_kelvin()

Explanation:
Shows conversion logic and returning computed values via methods.

Sample Input:

t = Temperature(25)
print(t.to_fahrenheit())
print(t.to_kelvin())

Sample Output:

77.0
298.15


6. Simple Library System

Problem:
Create a class Book with title, author, and is_available=True.
Add:

  • borrow() → makes it unavailable
  • return_book() → makes it available again

Explanation:
Demonstrates state changes using boolean attributes.

Sample Input:

book1 = Book("Harry Potter", "J.K. Rowling")
book1.borrow()
book1.return_book()

Sample Output:

Book borrowed successfully.
Book returned successfully.


7. Simple Bank Account with Validation

Problem:
Create a class BankAccount with holder_name and balance=0.
Add:

  • deposit(amount)
  • withdraw(amount) (only if balance ≥ amount)
  • display_balance()

Explanation:
Shows conditional logic and secure data updates within an object.

Sample Input:

acc = BankAccount("Ravi")
acc.deposit(1000)
acc.withdraw(300)
acc.display_balance()

Sample Output:

Deposited ₹1000
Withdrew ₹300
Current balance: ₹700


8. Class for Student Marks

Problem:
Create a class Student that stores name and three subject marks.
Add:

  • average() → returns average marks
  • grade() → returns grade based on average

Explanation:
Combines arithmetic and conditional logic with OOP.

Sample Input:

student1 = Student("Anita", 75, 85, 90)
print(student1.average())
print(student1.grade())

Sample Output:

83.33
A


9. Car Mileage Tracker

Problem:
Create a class MileageTracker with distance and fuel_used.
Add a method calculate_mileage() that returns distance ÷ fuel.
If fuel is 0, handle it gracefully.

Explanation:
Shows how to handle invalid data or prevent runtime errors inside a method.

Sample Input:

trip = MileageTracker(400, 20)
print(trip.calculate_mileage())

Sample Output:

Mileage: 20.0 km/l


10. Product Discount System

Problem:
Create a class Product with name, price, and category.
Add:

  • apply_discount(percent) — reduces price by that percent
  • display_info() — prints product info

Explanation:
Focuses on modifying and displaying data in an organized way.

Sample Input:

p = Product("Laptop", 60000, "Electronics")
p.apply_discount(10)
p.display_info()

Sample Output:

Product: Laptop | Category: Electronics | Price after discount: ₹54000.0

© Copyright 2025 Fessorpro

Terms of Service / Privacy Policy