0% found this document useful (0 votes)
12 views3 pages

Problem Sheet 7 (Classes and Inheritance)

The document outlines a series of programming tasks involving the creation of various classes and their functionalities in Python. It includes tasks such as creating a contact class, implementing a shopping cart with discounts, designing a password manager, and establishing class hierarchies for students, vehicles, and banking systems. Each task emphasizes concepts like inheritance, method overriding, and encapsulation in object-oriented programming.
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)
12 views3 pages

Problem Sheet 7 (Classes and Inheritance)

The document outlines a series of programming tasks involving the creation of various classes and their functionalities in Python. It includes tasks such as creating a contact class, implementing a shopping cart with discounts, designing a password manager, and establishing class hierarchies for students, vehicles, and banking systems. Each task emphasizes concepts like inheritance, method overriding, and encapsulation in object-oriented programming.
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

Problem Sheet 7

1. Create a class Contact with attributes: name, email, and phone. Include a method
display() that prints the contact details.

2. Create a class representing a shopping cart. Include methods for adding and removing
items, and calculating the total price. The price is based on the number of items
purchased where the regular price is charged for orders of less than 10 items, a 10%
discount is applied for orders of between 10 and 99 items, and a 20% discount is
applied for orders of 100 or more items. Test the class using suitable objects of the
class.

3. Create a base class Appliance with a constructor that initializes the attribute
power_rating. Then, create two derived classes WashingMachine and Microwave that
inherit from Appliance. Both derived classes should override a method named start()
to provide their own specific implementation. Demonstrate the use of constructors and
method overriding in your code.

4. Create a class called Password_manager. The class should have a list called old
passwords that holds all of the user's past passwords. The last item of the list is the
user's current password. Have a method get_password that returns the current
password and set_password that sets the user's password. The set_password method
should only change the password if the attempted password is different from all the
user's past passwords. Also, create a method called retype_password that receives a
string and returns a boolean True or False depending on whether the string is equal to
the set_password or not. set_password should be invoked only if retype_password
returns true. Otherwise, prompt the user "retype password is different from set
password".

5. Create a class hierarchy to represent students and their grades using constructors and
inheritance.
Base Class: Person
Attributes:
name
roll_number
Constructor: Initializes the name and roll_number.
Derived Class: Student (inherits from Person)
Attributes:
marks – a list of integers representing marks from different subjects.
Constructor: Initializes name, roll_number, and marks using the base class constructor
for the first two.
Methods:
calculate_average() – Returns the average of the marks.
assign_grade() – Assigns and returns a grade based on the average:

A: 90–100

B: 75–89
C: 60–74

D: 40–59

F: Below 40

6. Create class Laptop with attributes brand="Dell", ram="8GB", processor="i5". Allow


overriding via constructor. Add show_specs() method.

7. Create a base class Vehicle with a method display_type().


 Create two derived classes Car and Bike that inherit from Vehicle.
 Both Car and Bike should override the display_type() method to provide their
specific implementation.
 Additionally, each derived class should define one unique method of its own.
 Demonstrate the use of inheritance and method overriding by creating objects
of Car and Bike and calling their respective methods.

8. Create a multilevel inheritance structure with the classes Organism, Animal, and
Human.
 Each class should have a constructor that prints a message indicating which
class constructor is being called.
 Also, each class should define one method: Organism has exist(), Animal has
move(), and Human has think().
 Create an object of the Human class and demonstrate how constructors and
methods from all parent classes are invoked using that object.
9. Create a base class Publication with common attributes like title and author.
 Inherit a class Book from Publication and add an additional attribute isbn.
 Create a list of 5 Book objects with different titles, authors, and ISBNs.
 Write a method search_by_author(name) to return all books written by the
specified [Link] the use of inheritance in your implementation.

10. Consider a Banking System. Design a class called Bank that has data members
representing account_number, name, account_type, branch, balance_amount etc.
Derive a Deposit class from Bank for depositing the amount into that account and to
maintain the balance. Also, derive a Withdrawal class from Bank for withdrawing the
amount from that account and to maintain the balance. Derive a Statement class from
both ‘Deposit’ and ‘Withdrawal’ to print the complete bank statement of the specific
account holder with all transactions. Use constructors in all the class.

11. Consider ‘Shape' is the superclass, and 'Circle' and 'Square' are subclasses. The
'Circle' and 'Square' classes inherit the 'draw' method from the 'Shape' superclass,
Constructor and they provide their own implementations, enhancing or customizing
the behavior as needed. The program demonstrates creating instances of the derived
classes, showcasing how the common functionality defined in the 'Shape' superclass is
reused by the subclasses.

12. Create a class Clock with hour, minute, second. Use constructor to initialize and a
method to display time in hh:mm:ss format.
13. Define a class TemperatureConverter with method to_fahrenheit(celsius). Return
Fahrenheitequivalent. (F = C × 9/5 + 32)
14. A programmer is designing a project to catalog all books in a library. He plans to have
a Book class that stores the features of each Book: title, author, price and so on with
suitable methods to initialize. Another class Library will store an array of Book
objects and includes methods to list the books, to add a book, to remove a book and
search for a book. Model the above problem and solve it.

15. Consider an application for displaying the examination result. Design three classes:
Student, Exam and Result. The Student class has data members such as those
representing roll number, name, etc. Create the class Exam by inheriting the Student
class. The Exam class adds data members representing the marks scored in six
subjects. Derive the Result from the exam class and it has its own data members such
as total marks. Write an interactive program to model this relationship.

You might also like