Problem Statement: Library Management System
#Encapsulation #Inheritance (IS-A)
Problem:
You are developing a Library Management System where different types of books need to be
categorized using inheritance. There are borrowable books, and ebooks. Each type of book has
some common properties but also specific behaviors.
Requirements:
1. Create a base class Book with the following attributes:
o title (String)
o author (String)
o ISBN (String)
o displayInfo() method to show book details
2. Create two subclasses that inherit from Book:
o EBook: Has an additional attribute fileSizeMB (double) and add more a method
download().
o BorrowableBook: has an additional attribute isAvailable , and add more
borrow() and returnBook() methods
3. Demonstrate:
- create an Ebook, a borrowableBookook
- display details
- call methods: borrow(), returnBook() C
Problem Statement: Car Dealership System
#Encapsulation #Inheritance (Has-A)
Problem:
You are developing a Car Dealership System where each Car has an Engine. Instead of using
inheritance (IS-A relationship), you will use composition (HAS-A relationship), meaning a Car
has an Engine.
Requirements:
1. Create a class Engine with the following attributes:
o engineType (String) → Example: "V8", "Electric", "Hybrid"
o horsepower (int) → Example: 200, 300, etc.
o startEngine() method → Prints "Engine started: <engineType> with
<horsepower> HP"
o displayInfor(): to print details
2. Create a class Car that contains an Engine object (HAS-A relationship):
o Attributes:
brand (String) → Example: "Toyota", "Ford"
model (String) → Example: "Camry", "Mustang"
year (int) → Example: 2022, 2023
Engine object as a class member
o Methods:
startCar() → Calls startEngine() from Engine
display():to print details
3. Demonstrate:
-create an Engine, a car
- call method startCar(), display()
Problem Statement: School Management System
#Combining IS-A and HAS-A Relationships
Problem:
You are developing a School Management System where different types of User (students and
teachers) exist. A student is a User (IS-A relationship), and a student has a course (HAS-A
relationship).
Requirements:
1. Create a base class User with:
o username (String)
o password (int)
o displayInfo() method to print details
2. Create two subclasses (IS-A relationship):
o Student (inherits from User):
studentID (String)
HAS-A ArrayList<Course> (A student has a course)
enrollCourse(Course c) method
displayInfoStudent() to print details
o Teacher (inherits from User):
teacherID (String)
subject (String)
displayInforTeacher() to print details
3. Create a separate class Course (HAS-A relationship) with:
o courseName (String)
o courseCode (String)
4. Demonstrate:
o Create a student and a teacher
o Enroll the student in a course
o Print details using displayInfo()
------------------------------------------------------------------------------------------------------------------------------------------
Exercise:
Problem Statement: Zoo Management System (Combining IS-A and HAS-A
Relationships)
Problem:
You are designing a Zoo Management System where different types of animals exist, and each
animal has a caretaker. This requires both inheritance (IS-A relationship) and composition
(HAS-A relationship).
Requirements:
1. Create a base class Animal (IS-A relationship) with:
o name (String)
o species (String)
o makeSound() method
o displayInfo() method
2. Create two subclasses (IS-A relationship):
o Mammal (inherits from Animal):
furColor (String)
o Bird (inherits from Animal):
wingSpan (double)
3. Create a Caretaker class (HAS-A relationship):
o caretakerName (String)
o experienceYears (int)
o feedAnimal(Animal a) method → Prints a feeding message
4. Modify Animal class to include a Caretaker object (HAS-A relationship):
o Each animal has a caretaker
o Add a method assignCaretaker(Caretaker c)
Demonstration:
1. Create a Caretaker object.
2. Create a Mammal (e.g., Lion) and assign a caretaker.
3. Create a Bird (e.g., Eagle) and assign the same or a different caretaker.
4. Call displayInfo() on both animals.
5. Call feedAnimal() using the caretaker object.
Expected Output Example:
Animal: Leo
Species: Lion
Fur Color: Golden
Caretaker: Mike (5 years of experience)
Animal: Sky
Species: Eagle
Wing Span: 2.1 meters
Caretaker: Sarah (3 years of experience)
Mike is feeding Leo the Lion.
Sarah is feeding Sky the Eagle.
Bonus Challenge:
Add a Reptile subclass.
Exercise:
Ride-Sharing System (Combining IS-A and HAS-A Relationships)
Problem:
You are designing a Ride-Sharing System like Uber or Grab. In this system, different types of
vehicles exist, and each ride has a driver. This requires both inheritance (IS-A relationship) and
composition (HAS-A relationship).
Requirements:
1. Create a base class Vehicle (IS-A relationship) with:
o brand (String)
o model (String)
o year (int)
o displayInfo() method
2. Create two subclasses (IS-A relationship):
o Car (inherits from Vehicle):
numSeats (int)
o Motorcycle (inherits from Vehicle):
hasSidecar (boolean)
3. Create a Driver class (HAS-A relationship):
o driverName (String)
o licenseNumber (String)
o experienceYears (int)
o driveVehicle(Vehicle v) method → Prints driving details
4. Modify Vehicle class to include a Driver object (HAS-A relationship):
o Each vehicle is assigned a driver
o Add a method assignDriver(Driver d)
Demonstration:
1. Create a Driver object.
2. Create a Car and assign a driver.
3. Create a Motorcycle and assign the same or a different driver.
4. Call displayInfo() on both vehicles.
5. Call driveVehicle() using the driver object.
Expected Output Example:
Vehicle: Toyota Corolla (2022)
Seats: 5
Driver: John Doe (6 years experience)
Vehicle: Harley Davidson (2019)
Has Sidecar: No
Driver: Alice Smith (3 years experience)
John Doe is driving the Toyota Corolla.
Alice Smith is driving the Harley Davidson.