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

LabWork 5

The document outlines lab exercises for a 2nd Year IT Engineering course focused on Object Oriented Programming. It includes tasks on implementing inheritance, overloading and overriding methods, and utilizing polymorphism with classes such as Engine, Vehicle, Car, Truck, MotorCycle, and Person. Additionally, it provides specific coding examples and expected outcomes for students to analyze and implement in their programming practice.

Uploaded by

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

LabWork 5

The document outlines lab exercises for a 2nd Year IT Engineering course focused on Object Oriented Programming. It includes tasks on implementing inheritance, overloading and overriding methods, and utilizing polymorphism with classes such as Engine, Vehicle, Car, Truck, MotorCycle, and Person. Additionally, it provides specific coding examples and expected outcomes for students to analyze and implement in their programming practice.

Uploaded by

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

Object Oriented Programming 1 2nd Year IT Engineering 2024/2025

Lab Work 5

Exercise 1: Implementation of Inheritance

- Create classes: Engine, Vehicle, Car, Truck , MotorCycle and ClassTest.

- In the ClassTest create objects of all classes.

- To use polymorphism properties, create an ArrayList of Vehicle class, and add all objects of its
derived classes; Iterate through the elements of the list and, for each iteration, call the displayInfo
method from the current object.

Exercise 2: Overloading and Overriding Properties


What results does the following program produce?
public class A {
public void f(double x) {System.out.print ("A.f(double=" + x +")");}}
public class B extends A{}
public class C extends A {
public void f(long q){System.out.print("C.f(long="+q+")");}}
public class D extends C {
public void f(int n) {System.out.print ("D.f(int=" + n + ") ");}}
public class E extends B{}
class F extends C {
public void f(float x) {System.out.print ("F.f(float=" + x + ")");}
public void f(int n) {System.out.print ("F.f(int=" + n + ") ");}}
public class TestClass {
public static void main (String arg[]) {
byte bb=1; short p=2; int n=3; long q=4; float x=5.f; double y=6. ;
A a=new A(); a.f(bb); a.f(x);
B b=new B(); b.f(bb); b.f(x);
C c=new C(); c.f(bb); c.f(q); c.f(x);
D d=new D(); d.f(bb); c.f(q); c.f(y);
E e=new E(); e.f(bb); e.f(q); e.f(y);
F f=new F(); f.f(bb); f.f(n); f.f(x); f.f(y); f.f(p);}}
Object Oriented Programming 1 2nd Year IT Engineering 2024/2025

Exercise 3: Polymorphism Properties

Write a Person class with attributes (name, birthDate, address), and a Student class that inherits from the
Person class. A student is characterized by (specialty, level). Write an Employee class that also inherits from
the Person class and is characterized by (position, baseSalary, bonus). All classes must contain an overridden
displayInfo() method to display the object's information. The Employee class contains a calculateSalary()
method to calculate the net salary using the formula: Salary = baseSalary + bonus – (baseSalary * 0.09).
Similarly, the Student class contains a calculateAge(int currentYear) method to return the student's age, with
the current year as a parameter.
Here are the following declarations:

Person p1=new Person ("Ahmed","Alger",2000);

Employee emp1=new Employee("Mohammed","Oran",1985, ‘’Mécanicien’’, 75000, 25000);

Person p2=new Employee("Ali","Setif",1990, ‘’Informaticien’’, 65000, 15000);

Student e1=new Student("Djamel","Setif",2005,"Informatique","L2");

Person e2=new Student("Lamia","Constantine",2002,"Chimie","M2");

What methods are accessible for each object?

What are the results produced by the following statements?

p1=e1 ; p1.displayInfo () ; System.out.println(‘’Age = ‘’+e1.calculAge(2023) );

p2=emp1 ;p2. displayInfo () ; System.out.println(‘Salaire = ‘’+p2. calculateSalary () );

Person [] tp={p1,p2,emp1 ,e1,e2} ;

for (Personne p : tp)

p.displayInfo() ;

You might also like