APP
EXPERIMENTS
[Link] JAVA program using control
structures
public class SimpleProgram
{
public static void main(String[]args)
{
int num = 10;
if (num > 0)
{
[Link]("The number is positive.");
}
else if (num < 0)
{
[Link]("The number is negative.");
}
else
{
[Link]("The number is zero.");
}
for (int i = 1; i <= 5; i++)
{
[Link]("Count: "+i);
}
int[] numbers = {1, 2, 3, 4, 5};
for (int number : numbers)
{
[Link]("Number: "+ number);
}
switch (num){
case 1:
[Link]("The number is 1.");
break;
case 2:
[Link]("The number is 2.");
break;
default:
[Link]("The number is neither 1 nor 2.");
break;
}
}
}
[Link] Sum of series (1 + 2+ 3+
…..n,1+1/2+1/3 +……..1/n,12 + 22+ 32 +
…….n2) using JAVA
public class SeriesSum
{
public static void main(String[]args)
{
int n = 5;
// Sum of series (1 + 2 + 3 + ... + n)
int sum1 = Series1(n);
[Link]("Sum of series 1: " + sum1);
// Sum of series (1 + 1/2 + 1/3 + ... + 1/n)
double sum2 = Series2(n);
[Link]("Sum of series 2: " + sum2);
// Sum of series (12+22+32 + ... + n^2)
int sum3 = Series3(n);
[Link]("Sum of series 3: " + sum3);
}
public static int Series1(int n)
{
int sum = 0;
for (int i = 1; i <= n; i++) {
sum += i;
}
return sum;
}
public static double Series2(double n)
{
double sum = 0;
for (double i = 1; i <= n; i++){
sum += 1/i;
}
return sum;
}
public static int Series3(int n) {
int sum = 0;
for (int i = 1; i <= n; i++)
{
sum += [Link](i, 2);
}
return sum;
}
}
[Link] JAVA Programs to implement
functions
class Function
{
public static void main(String[]args)
{
int x = 25;
int y = function(x);
[Link]("The value of y : " + y);
}
public static int function(int input)
{
int result = input * 2;
return result;
}
}
[Link] JAVA Programs with Class and
Objects
class Add
{
public static void main(String[] args)
{
int num1 = 5;
int num2 = 10;
addition a1= new addition ();
int sum = [Link](num1, num2);
[Link]("Sum: "+sum);
}
}
class addition
{
public int add(int num1, int num2)
{
return num1 + num2;
}
}
[Link] JAVA program - Simple
Calculator using polymorphism
public class ArithmeticOperations
{
public static void main(String[] args)
{
int num1=10;
int num2=20;
int choice= 1;
float result;
switch (choice)
{
case 1:
result = num1 + num2;
[Link]("Addition: "+ result);
break;
case 2:
result = num1 - num2;
[Link]("Subtraction: " + result);
break;
case 3:
result = num1 * num2;
[Link]("Multiplication: " + result);
break;
case 4:
result=num1/num2;
[Link]("division: " + result);
break;
default:
[Link]("Invalid operator!");
break;
}
}
}
[Link] JAVA program - Pay Slip
Generator using Inheritance
import [Link];
class Employee {
private String name;
private int id;
public Employee(String name, int id) {
[Link] = name;
[Link] = id;
}
public String getName() {
return name;
}
public int getId() {
return id;
}
public double calculateSalary() {
return 0;
}
}
class FullTimeEmployee extends Employee {
private double salary;
public FullTimeEmployee(String name, int id, double salary) {
super(name, id);
[Link] = salary;
}
@Override
public double calculateSalary() {
return salary;
}
}
class PartTimeEmployee extends Employee {
private double hourlyRate;
private int hoursWorked;
public PartTimeEmployee(String name, int id, double hourlyRate, int hoursWorked) {
super(name, id);
[Link] = hourlyRate;
[Link] = hoursWorked;
}
@Override
public double calculateSalary() {
return hourlyRate * hoursWorked;
}
}
class PaySlipGenerator {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
[Link]("Enter the employee name: ");
String name = [Link]();
[Link]("Enter the employee ID: ");
int id = [Link]();
[Link]("Enter the employee type (1 - Full Time, 2 - Part Time): ");
int employeeType = [Link]();
double salary = 0;
if (employeeType == 1) {
[Link]("Enter the monthly salary: ");
salary = [Link]();
FullTimeEmployee fullTimeEmployee = new FullTimeEmployee(name, id, salary);
salary = [Link]();
} else if (employeeType == 2) {
[Link]("Enter the hourly rate: ");
double hourlyRate = [Link]();
[Link]("Enter the hours worked: ");
int hoursWorked = [Link]();
PartTimeEmployee partTimeEmployee = new PartTimeEmployee(name, id,
hourlyRate, hoursWorked);
salary = [Link]();
} else {
[Link]("Invalid employee type!");
[Link](0);
}
[Link]("\n--- Pay Slip ---");
[Link]("\n Employee Name: " + name);
[Link]("\n Employee ID: " + id);
[Link]("\n Salary: $" + salary);
}
}
[Link] JAVA Programs to implement
thread
public class MyThread extends Thread
{
private String threadName;
public MyThread(String name)
{
threadName = name;
[Link]("Creating " + threadName);
}
public void run()
{
[Link]("Running " + threadName);
try
{
for (int i = 4; i > 0; i--) {
[Link]("Thread: " + threadName + ", " + i);
[Link](50);
}
}
catch (InterruptedException e)
{
[Link]("Thread " + threadName + " interrupted.");}
[Link]("Thread " + threadName + " exiting.");
}
public static void main(String args[])
{
MyThread thread1 = new MyThread("Thread 1");
MyThread thread2 = new MyThread("Thread 2");
[Link]();
[Link]();
try
{
[Link]();
[Link]();
}
catch (InterruptedException e)
{
[Link]("Main thread interrupted.");
}
[Link]("Main thread exiting.");
}
}
[Link] JAVA Programs with Java Data
Base Connectivity (JDBC)
import [Link];
import [Link];
import [Link];
public class DatabaseConnectivity
{
public static void main(String[] args)
{
String url = "jdbc:mysql://localhost:3306/mydatabase";
String username = "root";
String password = "password";
try
{
[Link](“[Link]”);
Connection connection = [Link](url, username, password);
[Link]("Connected to database");
//perform database operations here
[Link]();
} catch(ClassNotFoundException e) {
[Link](“MySQL JDBC driver not found ");
}catch (SQLException e)
{
[Link]("Database connection error: " + [Link]());
}
} }
[Link] Design with applet and Swing using
JAVA
import [Link].*;
import [Link].*;
import [Link];
import [Link];
public class FormDesign extends JApplet {
private JTextField nameField;
private JRadioButton maleRadioButton;
private JRadioButton femaleRadioButton;
private JComboBox<String> countryComboBox;
private JTextArea addressArea;
private JButton submitButton;
public void init() {
setLayout(new FlowLayout());
JLabel nameLabel = new JLabel("Name:");
nameField = new JTextField(20);
JLabel genderLabel = new JLabel("Gender:");
maleRadioButton = new JRadioButton("Male");
femaleRadioButton = new JRadioButton("Female");
ButtonGroup genderGroup = new ButtonGroup();
[Link](maleRadioButton);
[Link](femaleRadioButton);
JLabel countryLabel = new JLabel("Country:");
String[] countries = {"USA", "Canada", "UK"};
countryComboBox = new JComboBox<>(countries);
JLabel addressLabel = new JLabel("Address:");
addressArea = new JTextArea(5, 20);
submitButton = new JButton("Submit");
add(nameLabel);
add(nameField);
add(genderLabel);
add(maleRadioButton);
add(femaleRadioButton);
add(countryLabel);
add(countryComboBox);
add(addressLabel);
add(addressArea);
add(submitButton);
[Link](new ActionListener() {
public void actionPerformed(ActionEvent e) {
String name = [Link]();
String gender = [Link]() ? "Male" : "Female";
String country = (String) [Link]();
String address = [Link]();
[Link](null,
"Name: " + name + "\n" +
"Gender: " + gender + "\n" +
"Country: " + country + "\n" +
"Address: " + address);
}
});
}
public static void main(String[] args) {
// Create an instance of the FormDesign class
FormDesign formDesign = new FormDesign();
// Initialize the applet
[Link]();
// Create a JFrame to display the applet
JFrame frame = new JFrame();
[Link](JFrame.EXIT_ON_CLOSE);
[Link]().add(formDesign);
[Link](400, 300);
[Link](true);
}
}
[Link] Python Program to implement
functions
def addition(n1, n2):
return n1 + n2
n1 = int(input("Enter the first number: "))
n2 = int(input("Enter the second number: "))
sum_result = addition(n1, n2)
print("Sum:", sum_result)
[Link] program using control structures
and arrays
def find_avg(nums):
total = 0
for num in nums:
total += num
avg = total / len(nums)
return avg
num_ele = int(input("Enter the number of elements in the array: "))
array = []
for i in range(num_ele):
ele = int(input("Enter element {}: ".format(i + 1)))
[Link](ele)
avg = find_avg(array)
print("Average:", avg)
[Link] Python program - TCP/UDP
program using Sockets
import socket
def tcp_server():
tcp_socket = [Link](socket.AF_INET, socket.SOCK_STREAM)
tcp_socket.bind(('localhost', 8888))
tcp_socket.listen(1)
print('TCP Server is listening...')
while True:
conn, addr = tcp_socket.accept()
print('Connected to', addr)
data = [Link](1024).decode()
if not data:
break
print('Received:', data)
[Link]('Hello from TCP Server!'.encode())
[Link]()
def tcp_client():
tcp_socket = [Link](socket.AF_INET, socket.SOCK_STREAM)
tcp_socket.connect(('localhost', 8888))
tcp_socket.sendall('Hello from TCP Client!'.encode())
data = tcp_socket.recv(1024).decode()
print('Received:', data)
tcp_socket.close()
def udp_server():
udp_socket = [Link](socket.AF_INET, socket.SOCK_DGRAM)
udp_socket.bind(('localhost', 9999))
print('UDP Server is listening...')
while True:
data, addr = udp_socket.recvfrom(1024)
if not data:
break
print('Received:', [Link]())
udp_socket.sendto('Hello from UDP Server!'.encode(), addr)
def udp_client():
udp_socket = [Link](socket.AF_INET, socket.SOCK_DGRAM)
udp_socket.sendto('Hello from UDP Client!'.encode(), ('localhost', 9999))
data, addr = udp_socket.recvfrom(1024)
print('Received:', [Link]())
udp_socket.close()
tcp_server()
tcp_client()
udp_server()
udp_client()
[Link] NFA and DFA using Python
class State:
def init (self, name):
[Link] = name
[Link] = {}
def add_transition(self, symbol, state):
if symbol in [Link]:
[Link][symbol].add(state)
else:
[Link][symbol] = {state}
class NFA:
def init (self, start_state, accept_states):
self.start_state = start_state
self.accept_states = accept_states
def accepts(self, input_string):
current_states = {self.start_state}
for symbol in input_string:
next_states = set()
for state in current_states:
if symbol in [Link]:
next_states.update([Link][symbol])
current_states = next_states
return any(state in self.accept_states for state in current_states)
class DFA:
def init (self, start_state, accept_states):
self.start_state = start_state
self.accept_states = accept_states
[Link] = {} # Define transitions as an empty dictionary
def add_transition(self, from_state, symbol, to_state):
if from_state not in [Link]:
[Link][from_state] = {}
if symbol not in [Link][from_state]:
[Link][from_state][symbol] = set()
[Link][from_state][symbol].add(to_state)
def accepts(self, input_string):
current_states = {self.start_state}
for symbol in input_string:
next_states = set()
for state in current_states:
if state in [Link] and symbol in [Link][state]:
next_states.update([Link][state][symbol])
current_states = next_states
return any(state in self.accept_states for state in current_states)
# Create an NFA for the language (a|b)*abb
start_state = State("q0")
accept_state = State("q3")
start_state.add_transition("a", start_state)
start_state.add_transition("b", start_state)
start_state.add_transition("a", accept_state)
accept_state.add_transition("b", accept_state)
nfa = NFA(start_state, {accept_state})
# Test the NFA
print([Link]("abba")) # True
print([Link]("ab")) # False
# Convert the NFA to a DFA
dfa_start_state = frozenset({start_state})
dfa_accept_states = {state for state in nfa.accept_states if state in dfa_start_state}
dfa = DFA(dfa_start_state, dfa_accept_states)
# Test the DFA
print([Link]("abba")) # True
print([Link]("ab")) # False
[Link] a Python program for the
algebraic manipulations using symbolic
paradigm
python
from sympy import symbols, sin, cos, exp, log
# Define the symbolic variables
x = symbols('x')
y = symbols('y')
# Define the expression
expression = sin(x) + cos(y)
# Perform algebraic manipulation
result = exp(log(expression))
# Evaluate the result
value = [Link](x, 1.0).subs(y, 2.0)
# Print the result
print("Result:", value)
[Link] Python programs to implement
event handling
class Event:
def init (self, name):
[Link] = name
[Link] = []
def add_handler(self, handler):
[Link](handler)
def remove_handler(self, handler):
[Link](handler)
def fire(self, *args, **kwargs):
for handler in [Link]:
handler(*args, **kwargs)
class Button:
def init (self, label):
[Link] = label
self.click_event = Event('click')
def click(self):
self.click_event.fire()
def button_click_handler():
print("Button clicked!")
button = Button("Click me")
button.click_event.add_handler(button_click_handler)
[Link]()