In the realm of software development, there’s no substitute for hands-on experience. In this blog post, we’ll embark on a journey through real-world code examples spanning various programming paradigms. From imperative to declarative, and from procedural to object-oriented, these examples will illuminate the diverse approaches to problem-solving in the world of programming.
Chapter 1: Imperative Programming
1.1 Example 1: Python – Finding the Maximum Element in a List
def find_max(nums):
max_num = float('-inf')
for num in nums:
if num > max_num:
max_num = num
return max_num
numbers = [3, 7, 1, 10, 5]
print("Maximum number:", find_max(numbers))
1.2 Example 2: JavaScript – Filtering Even Numbers from an Array
function filter_even_numbers(nums) {
let result = [];
for (let num of nums) {
if (num % 2 === 0) {
result.push(num);
}
}
return result;
}
let numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
console.log("Even numbers:", filter_even_numbers(numbers));
Chapter 2: Declarative Programming
2.1 Example 1: SQL – Retrieving Employees from a Database Table
SELECT * FROM employees WHERE department = 'IT';
2.1 Example 2: React – Rendering a List of Items
import React from 'react';
function ItemList({ items }) {
return (
<ul>
{items.map(item => (
<li key={item.id}>{item.name}</li>
))}
</ul>
);
}
const items = [
{ id: 1, name: 'Item 1' },
{ id: 2, name: 'Item 2' },
{ id: 3, name: 'Item 3' }
];
function App() {
return <ItemList items={items} />;
}
export default App;
Chapter 3: Object-Oriented Programming
3.1 Example 1: Java – Creating a Simple Bank Account Class
public class BankAccount {
private String accountNumber;
private double balance;
public BankAccount(String accountNumber, double balance) {
this.accountNumber = accountNumber;
this.balance = balance;
}
public void deposit(double amount) {
balance += amount;
}
public void withdraw(double amount) {
if (balance >= amount) {
balance -= amount;
} else {
System.out.println("Insufficient funds");
}
}
public double getBalance() {
return balance;
}
}
3.2 Example 2: Python – Defining a Class for Representing a Car
class Car:
def __init__(self, make, model, year):
self.make = make
self.model = model
self.year = year
self.odometer_reading = 0
def get_descriptive_name(self):
return f"{self.year} {self.make} {self.model}"
def read_odometer(self):
print(f"This car has {self.odometer_reading} miles on it.")
def update_odometer(self, mileage):
if mileage >= self.odometer_reading:
self.odometer_reading = mileage
else:
print("You can't roll back an odometer!")
def increment_odometer(self, miles):
self.odometer_reading += miles
my_car = Car("Toyota", "Camry", 2022)
print(my_car.get_descriptive_name())
my_car.read_odometer()
my_car.update_odometer(100)
my_car.read_odometer()
Through these real-world code examples, we’ve glimpsed into the diverse programming paradigms and languages that underpin modern software development. From imperative to declarative, and from procedural to object-oriented, each paradigm offers unique approaches to problem-solving. By studying and understanding these examples, developers can expand their toolkit and become more proficient in crafting elegant and efficient solutions to a wide array of challenges. Stay curious, keep coding, and let these examples serve as inspiration for your own programming adventures.
Leave a Reply