0% found this document useful (0 votes)
6 views3 pages

SRM IST CLA-2 Programming Concepts

The document covers various programming concepts including parallel programming paradigms, NFAs, functional programming in Python, GUI creation using AWT in Java, multithreading, JDBC for database connectivity, client-server communication using sockets, and DFAs. Each section provides definitions, examples, and explanations of the respective topics. The document serves as a comprehensive guide for understanding these programming techniques and their applications.

Uploaded by

neelshreyan2004
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)
6 views3 pages

SRM IST CLA-2 Programming Concepts

The document covers various programming concepts including parallel programming paradigms, NFAs, functional programming in Python, GUI creation using AWT in Java, multithreading, JDBC for database connectivity, client-server communication using sockets, and DFAs. Each section provides definitions, examples, and explanations of the respective topics. The document serves as a comprehensive guide for understanding these programming techniques and their applications.

Uploaded by

neelshreyan2004
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

SRM IST CLA-2 Detailed Notes

1. Parallel Programming Paradigm


Definition: The parallel programming paradigm enables simultaneous execution of multiple processes or threads
to improve speed and efficiency. It divides large problems into smaller subproblems that can run concurrently.
Types: 1. Task Parallelism – Different tasks execute simultaneously. 2. Data Parallelism – The same operation
executes on different data parts. 3. Pipeline Parallelism – Output of one process is input for another. Example
(Python):
import multiprocessing

def square(n):
print(f"Square of {n}: {n*n}")

if __name__ == '__main__':
numbers = [1, 2, 3, 4]
pool = [Link]()
[Link](square, numbers)
Advantages: Faster computation, better CPU utilization, suitable for real-time systems.

2. NFA using Python


Definition: An NFA (Non-deterministic Finite Automata) can move to multiple possible next states for a given input
symbol. It accepts a string if at least one computation path leads to a final state. Formal Definition: M = (Q, Σ, δ,
q■, F) Q: set of states | Σ: alphabet | δ: transition function | q■: start state | F: final states
def nfa_transition(state, symbol):
transitions = {'q0': {'a': ['q0', 'q1']}, 'q1': {'b': ['q2']}, 'q2': {}}
return [Link](state, {}).get(symbol, [])

def accepts_nfa(string):
current_states = ['q0']
final_state = 'q2'
for symbol in string:
next_states = []
for state in current_states:
next_states += nfa_transition(state, symbol)
current_states = next_states
return final_state in current_states

print(accepts_nfa('ab'))
print(accepts_nfa('aab'))
print(accepts_nfa('b'))
Explanation: Accepts strings containing 'a' followed by 'b'.

3. map(), filter(), reduce() in Python


Definition: Functional programming tools for transforming, selecting, and reducing data collections. Functions:
map(function, iterable) → Applies a function to each item. filter(function, iterable) → Filters items based on
condition. reduce(function, iterable) → Reduces iterable into a single value.
from functools import reduce

nums = [1, 2, 3, 4, 5]
squares = list(map(lambda x: x**2, nums))
evens = list(filter(lambda x: x % 2 == 0, nums))
total = reduce(lambda x, y: x + y, nums)

print(squares)
print(evens)
print(total)
Example applications: file filtering, data transformation, and aggregation.
4. GUI using AWT Toolkit
Definition: AWT (Abstract Window Toolkit) provides GUI components such as buttons, text fields, and frames in
Java.
import [Link].*;
import [Link].*;

public class AWTExample {


public static void main(String[] args) {
Frame f = new Frame("AWT Demo");
Label l = new Label("Enter your name:");
TextField t = new TextField();
Button b = new Button("Submit");

[Link](50, 100, 100, 30);


[Link](160, 100, 100, 30);
[Link](100, 150, 80, 30);

[Link](l); [Link](t); [Link](b);


[Link](300, 300);
[Link](null);
[Link](true);
}
}
Explanation: Creates GUI window with label, text field, and button.

5. Multithreading in Java
Definition: Multithreading allows concurrent execution of multiple threads (lightweight processes) in a program.
class MyThread extends Thread {
public void run() {
for (int i = 1; i <= 3; i++) {
[Link](getName() + " running: " + i);
}
}
public static void main(String[] args) {
MyThread t1 = new MyThread();
MyThread t2 = new MyThread();
[Link]();
[Link]();
}
}
Advantages: Faster execution, efficient CPU utilization, suitable for multitasking.

6. Java JDBC Program


Definition: JDBC (Java Database Connectivity) enables Java programs to connect to databases like MySQL.
Steps: Load driver, connect, create statement, execute query, process results, close connection.
import [Link].*;

class JDBCDemo {
public static void main(String[] args) {
try {
[Link]("[Link]");
Connection con = [Link](
"jdbc:mysql://localhost:3306/testdb", "root", "password");
Statement stmt = [Link]();
ResultSet rs = [Link]("SELECT * FROM students");
while ([Link]())
[Link]([Link](1) + " " + [Link](2));
[Link]();
} catch (Exception e) {
[Link](e);
}
}
}
Explanation: Demonstrates database connection and data retrieval.

7. Python Client-Server Communication using Sockets


Definition: Sockets enable bidirectional network communication between client and server applications.
# Server
import socket
s = [Link]()
[Link](('localhost', 9999))
[Link](1)
print("Server waiting...")
conn, addr = [Link]()
print("Connected with", addr)
[Link](b"Hello from server!")
[Link]()

# Client
import socket
c = [Link]()
[Link](('localhost', 9999))
print([Link](1024).decode())
[Link]()
Explanation: The server listens for client connections and sends messages over TCP/IP.

8. DFA using Python


Definition: A Deterministic Finite Automaton (DFA) has only one transition for each input symbol from a given
state.
def dfa_transition(state, symbol):
transitions = {
'q0': {'a': 'q1'},
'q1': {'b': 'q2'},
'q2': {'b': 'q2'}
}
return transitions[state].get(symbol)

def accepts_dfa(string):
state = 'q0'
final_state = 'q2'
for symbol in string:
state = dfa_transition(state, symbol)
if state is None:
return False
return state == final_state

print(accepts_dfa("abb"))
print(accepts_dfa("a"))
Explanation: Accepts strings starting with 'a' followed by one or more 'b's.

You might also like