Programming Exercises

Exercise 1: Shape Area Calculation

Write a program that calculates the area of different shapes (circle, square, rectangle) using polymorphism. Define a base class Shape with a method calculate_area(). Then create subclasses for each shape (e.g., Circle, Square, Rectangle) and override the calculate_area() method in each subclass to calculate the area according to the shape's specific formula.

 

Exercise 2: Animal Sounds

Write a program that simulates different animals making sounds using polymorphism. Define a base class Animal with a method make_sound(). Then create subclasses for different animals (e.g., dog, cat, bird) and override the make_sound() method in each subclass to produce the appropriate sound.

 

Exercise 3: Employee Payment Calculation

Write a program that calculates payments for employees based on their roles using polymorphism. Define a base class Employee with a method calculate_payment(). Then create subclasses for different roles (e.g., Manager, Developer, Intern) and override the calculate_payment() method in each subclass to calculate payments based on their specific rules (e.g., hourly rate, monthly salary).

 

Exercise 4: Vehicle Simulation

Write a program that simulates different types of vehicles (car, bicycle, motorcycle) using polymorphism. Define a base class Vehicle with methods accelerate() and brake(). Then create subclasses for each type of vehicle and override the accelerate() and brake() methods to simulate the behavior of each vehicle type.

 

Exercise 5: isinstance() Function

Develop a function that behaves differently based on the type of input using isinstance(). Define a function calculate_area() that calculates the area of a circle if the input is a Circle object and the area of a rectangle if the input is a Rectangle object.