0% found this document useful (0 votes)
11 views5 pages

TP - Série 1 - 2

The document outlines a series of exercises for a course on Object-Oriented Programming, focusing on concepts such as interfaces, abstract classes, inheritance, and polymorphism in Java. Each exercise includes steps for designing classes and interfaces, implementing them, and discussing the advantages of these concepts. The exercises cover various applications including vehicle systems, library management, employee management, and advanced programming concepts.

Uploaded by

cloudcloud747
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)
11 views5 pages

TP - Série 1 - 2

The document outlines a series of exercises for a course on Object-Oriented Programming, focusing on concepts such as interfaces, abstract classes, inheritance, and polymorphism in Java. Each exercise includes steps for designing classes and interfaces, implementing them, and discussing the advantages of these concepts. The exercises cover various applications including vehicle systems, library management, employee management, and advanced programming concepts.

Uploaded by

cloudcloud747
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

Abdelhamid Mehri University – Constantine 2

NTIC Faculty
Second year common core
2024 - 2025

Object-Oriented Programming 2 (POO2)

Hands-On – Series 01

Exercise 1: Vehicle Interface


In this exercise, we will work on designing a simple system for vehicles using interfaces.
Step 1: Design Interface
1. Design an interface Vehicle with the following methods:
• void start(): a method that starts the vehicle.
• void stop(): a method that stops the vehicle.
• void accelerate(double speed): a method that accelerates the vehicle by a specified speed.
• void brake(): a method that applies the brake to the vehicle.
2. Explain the concept of interfaces in Java and their role in providing a common contract for classes.
3. Why is it beneficial to use interfaces when designing systems with multiple classes that exhibit similar
behaviours?

Step 2: Implement Classes


4. Create two classes, Car and Motorcycle that implement the Vehicle interface. Implement the methods
based on the specific behaviour of each vehicle type.
5. Discuss the process of implementing the Vehicle interface in the Car and Motorcycle classes.
6. How does the use of interfaces facilitate code reuse and polymorphism in this scenario?

Step 3: Main Program


7. In the main program, create instances of Car and Motorcycle, invoke their methods, and observe the
behaviour.
8. Describe the steps you took to create and interact with instances of Car and Motorcycle.
9. Discuss any challenges you faced during the implementation and how you overcame them.

Step 4: Illustrate Polymorphism


10. Create an array or a list of type Vehicle and add instances of both Car and Motorcycle to it.
11. Iterate over the array or list, invoke the common methods (start(), stop(), accelerate(), brake()) on each
element, and observe how polymorphism allows you to treat different objects uniformly.
12. Discuss the advantages of using polymorphism in this context.

1
Exercise 2: Library Management System
In this exercise, we will design a simple library management system using abstract classes and
polymorphism.

Step 1: Design Abstract Class


1. Design an abstract class LibraryItem with the following properties and methods:
• Properties:
– title (String): representing the title of the library item.
– year (int): representing the publication year of the library item.
– isCheckedOut (boolean): indicating whether the library item is checked out or not.
• Methods:
– LibraryItem(String title, int year): a constructor that initializes the title and year.
– void checkOut(): a method that marks the item as checked out.
– void checkIn(): a method that marks the item as checked in.
– void displayInfo(): an abstract method to display information about the library item.
2. Discuss how the use of abstract classes promotes code reuse and polymorphism in this scenario.

Step 2: Implement Concrete Classes


3. Create concrete classes for the following library items, each extending LibraryItem: - Book - Thesis -
Dissertation
- JournalArticle - ProceedingsArticle - Journal - Proceedings
4. Implement the displayInfo() method in each class to display specific information for the corresponding
library item.
5. Discuss the process of implementing the LibraryItem abstract class in each of the concrete classes.

Step 3: Main Program


6. In the main program, create instances of different library items (Book, Thesis, Dissertation, JournalArticle,
ProceedingsArticle, Journal, and Proceedings), invoke their methods, and observe the behaviour.
7. Describe the steps you took to create and interact with instances of various library items.
8. Discuss any challenges you faced during the implementation and how you overcame them.

Step 4: Illustrate Polymorphism


9. Create an array or a list of type LibraryItem and add instances of different library items to it.
10. Iterate over the array or list, invoke the displayInfo() method on each element, and observe how
polymorphism
allows you to treat different objects uniformly.
11. Discuss the advantages of using polymorphism with abstract classes in this context.

2
Exercise 3: Employee Management System
In this exercise, we will design a simple employee management system using classes and inheritance.

Step 1: Design Class Hierarchy


1. Design a base class Employee with the following properties and methods:
• Properties:
– name (String): representing the name of the employee.
– employeeId (int): representing the unique identifier for each employee.
– salary (double): representing the monthly salary of the employee.
• Methods:
– Employee(String name, int employeeId, double salary): a constructor that initializes the name,
employeeId, and salary.
– void displayInfo(): a method to display information about the employee.
– void calculateSalary(): an abstract method to calculate the salary (implementation specific to each
type of employee).
2. Discuss the benefits of using a base class in a class hierarchy.

Step 2: Implement Derived Classes


3. Create two derived classes, Manager and Developer, that extend the Employee class.
4. Implement the calculateSalary() method in each derived class with different logic based on the employee
type:
• For a Manager, calculate the salary as the base salary plus a bonus.
• For a Developer, calculate the salary as the base salary plus overtime pay.
5. Discuss the process of implementing the Employee base class and the derived classes.

Step 3: Main Program


6. In the main program, create instances of Manager and Developer, invoke their methods, and observe the
behaviour.
7. Describe the steps you took to create and interact with instances of Manager and Developer.
8. Discuss any challenges you faced during the implementation and how you overcame them.

Step 4: Illustrate Polymorphism


9. Create an array or a list of type Employee and add instances of both Manager and Developer to it.
10. Iterate over the array or list, invoke the displayInfo() and calculateSalary() methods on each element, and
observe how polymorphism allows you to treat different objects uniformly.
11. Discuss the advantages of using polymorphism in this context.

3
Exercise 4: Multiple Inheritance with Interfaces
In this exercise, we explore multiple inheritance through interfaces in Java.

Step 1: Design Interfaces


1. Design an interface Person with methods void setName(String name) and void setAge(int age).
2. Design another interface Employee with methods void setEmployeeId(int id) and void setSalary(double
salary).
3. Design a class Manager that implements both Person and Employee interfaces.

Step 2: Main Program


4. In the main program, create instances of Manager, invoke methods from both interfaces, and observe the
behaviour.
5. Discuss the advantages and challenges of achieving multiple inheritance through interfaces in Java.

Exercise 5: Multiple Inheritance with Abstract Class and Interfaces


In this exercise, we will explore multiple inheritance by creating a class that inherits from an abstract class
and implements two interfaces.

Step 1: Design Abstract Class and Interfaces


1. Design an abstract class Animal with abstract methods void makeSound() and void move().
2. Design two interfaces: Swim with method void swim() and Fly with method void fly().

Step 2: Implement Class


3. Create a class Bird that inherits from Animal and implements both Swim and Fly interfaces.
4. Implement the methods in the Bird class based on the behaviour of birds.

Step 3: Main Program


5. In the main program, create instances of Bird, invoke methods from the abstract class and interfaces, and
observe the behaviour.
6. Discuss the design considerations and advantages of using multiple inheritance with an abstract class and
interfaces in Java.

4
Exercise 6: Advanced Concepts with Abstract Classes and Interfaces
In this exercise, we delve into advanced details about abstract methods, access modifiers, and interfaces in
Java.

Step 1: Design Abstract Class with Advanced Abstract Methods


1. Design an abstract class Shape with an abstract method double calculateArea().
2. Create a subclass Circle that extends Shape and implements the calculateArea() method considering the
formula for the area of a circle.
3. Design another subclass Rectangle that extends Shape and implements the calculateArea() method based
on the formula for the area of a rectangle.

Step 2: Explore Access Modifiers


4. Introduce access modifiers to the Shape class. Modify the calculateArea() method and discuss how access
modifiers affect method visibility in subclasses.

Step 3: Implement Interfaces


5. Design an interface Resizable with a method void resize(double factor).
6. Make the Circle class implement the Resizable interface, providing a specific implementation for resizing
circles.

Step 4: Main Program


7. In the main program, create instances of Circle and Rectangle, invoke methods from both the abstract
class and the interface, and observe the behaviour.
8. Experiment with access modifiers on the abstract class and discuss the impact on subclass
implementations and interface usage.
9. Summarize the advanced concepts learned in this exercise, emphasizing the importance of abstract
methods, access modifiers, and interfaces in Java programming.

You might also like