Polymorphism Exercise Problems, Exercises of Object Oriented Programming

This document includes polymorphism exercises on bith types: funtion over-loading and function over-riding.

Typology: Exercises

2023/2024

Uploaded on 04/18/2024

ushba-fatima
ushba-fatima 🇵🇰

2 documents

1 / 8

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CS 212: Object Oriented Programming Page 1
Department of Computing
CS 212: Object Oriented Programming
Lab 10: Polymorphism
Name: Ushba Fatina
CMS: 467212
pf3
pf4
pf5
pf8

Partial preview of the text

Download Polymorphism Exercise Problems and more Exercises Object Oriented Programming in PDF only on Docsity!

Department of Computing

CS 212: Object Oriented Programming

Lab 10: Polymorphism

Name: Ushba Fatina

CMS: 467212

Lab 10: Polymorphism

Introduction:

Polymorphism is one of the fundamental concepts in object-oriented programming (OOP) that

allows objects of different classes to be treated as objects of a common superclass. Method

overloading is a form of polymorphism where multiple methods with the same name but different

parameters are defined within a class. This enables the same method name to perform different

actions based on the type and number of parameters passed to it.

Objective:

The objective of this demonstration is to illustrate how method overloading enables polymorphism

in Java. By defining multiple versions of a method with the same name but different parameters,

we can create flexible and versatile code that adapts to various data types and scenarios. Through

this demonstration, we aim to show case the power and flexibility of polymorphism in object-

oriented programming.

Tools:

VS-code, NetBeans, Eclipse etc.

Description:

In this demonstration, we will create a Java program that show cases polymorphism using method

overloading. We will define a class with multiple overloaded methods, each performing a different

operation but sharing the same name. By varying the number or type of parameters passed to these

methods, we will observe how the appropriate version of the method is invoked based on the

context of the method call.

We will create a class named Math Operations that contains overloaded methods for basic

arithmetic operations such as addition, subtraction, multiplication, and division. Each method will

have multiple signatures to handle different data types (e.g., integers and floating-point numbers).

By testing these methods with various data types and parameter combinations, we will demonstrate

how method overloading enables polymorphism by providing different behaviors based on the

method signature.

Through this demonstration, we aim to emphasize the flexibility and versatility of polymorphism

using method overloading, allowing developers to write more concise and adaptable code that can

handle diverse scenarios efficiently.

Lab Tasks

Task 1:

Shape Hierarchy :

  • Create a hierarchy of shapes such as Circle, Square, and Triangle.
  • Each shape class should implement a method calculateArea() to compute its area.
  • Method overloading should be used so that each shape class has its own version of

calculateArea().

  • Demonstrate polymorphism by calling calculateArea() on objects of different shape

classes.

Task 2:

Employee Management:

  • Design an Employee class with attributes like name, salary, and job title.
  • Implement a method calculateBonus() in the Employee class to calculate the bonus.
  • Extend the Employee class to create subclasses like Manager and Engineer.
  • Override the calculateBonus() method in each subclass to calculate the bonus differently,

thus demonstrating method overriding.

Code:

class Employee { private String name; private double salary; private String jobTitle; public Employee(String name, double salary, String jobTitle) { this.name = name; this.salary = salary; this.jobTitle = jobTitle; } public String getName() { return name; } public double getSalary() { return salary; } public String getJobTitle() { return jobTitle; } public double calculateBonus() { // Default implementation of bonus calculation return 0.05 * salary; // 5% of salary } } class Manager extends Employee { public Manager(String name, double salary, String jobTitle) { super(name, salary, jobTitle); } @Override public double calculateBonus() { // Override bonus calculation for managers return 0.1 * getSalary(); // 10% of salary

class Engineer extends Employee { public Engineer(String name, double salary, String jobTitle) { super(name, salary, jobTitle); } @Override public double calculateBonus() { // Override bonus calculation for engineers return 0.08 * getSalary(); // 8% of salary } } public class Lab10 { public static void main(String[] args) { Employee employee 1 = new Employee("John", 50000, "Sales Associate"); Manager manager1 = new Manager("Alice", 80000, "Sales Manager"); Engineer engineer1 = new Engineer("Bob", 60000, "Software Engineer"); // Calculate and display bonuses System.out.println("Employee " + employee 1 .getName() + "'s bonus: $"

  • employee1.calculateBonus()); System.out.println("Manager " + manager1.getName() + "'s bonus: $" + manager1.calculateBonus()); System.out.println("Engineer " + engineer1.getName() + "'s bonus: $"
  • engineer1.calculateBonus()); } }

Code:

class Animal1 { public void eat() { System.out.println("Animal is eating."); } public void sound() { System.out.println("Animal makes a sound."); } } class Dog1 extends Animal1 { @Override public void sound() { System.out.println("Dog barks."); } } class Cat extends Animal1 { @Override public void sound() { System.out.println("Cat meows."); } } class Bird extends Animal1 { @Override public void sound() { System.out.println("Bird chirps."); }

public class Lab10 { public static void main(String[] args) { Animal1 animal1 = new Animal1(); Dog1 dog1 = new Dog1(); Cat cat1 = new Cat(); Bird bird1 = new Bird(); // Call eat and sound methods for each animal animal1.eat(); animal1.sound(); System.out.println(); dog1.eat(); dog1.sound(); System.out.println(); cat1.eat(); cat1.sound(); System.out.println(); bird1.eat(); bird1.sound(); } }

Output:

Deliverables:

Complete your labs tasks and upload on LMS. The marking will be based on

Quiz/viva/lab task submitted.