App Assignment 2023
App Assignment 2023
App Assignment 2023
MOGESHWAR
RA2211030050038
B.tech CSE-CS
2nd year
1.Simple JAVA program using control structures
public class SimpleProgram
{
public static void main(String[]args)
{
int num = 10;
if (num > 0)
{
System.out.println("The number is positive.");
}
else if (num < 0)
{
System.out.println("The number is negative.");
}
else
{
System.out.println("The number is zero.");
}
for (int i = 1; i <= 5; i++)
{
System.out.println("Count: "+i);
}
int[] numbers = {1, 2, 3, 4, 5};
for (int number : numbers)
{
System.out.println("Number: "+ number);
}
switch (num){
case 1:
System.out.println("The number is 1.");
break;
case 2:
System.out.println("The number is 2.");
break;
default:
System.out.println("The number is neither 1 nor 2.");
break;
}
}
}
2.Implement Sum of series (1 + 2+ 3+…..n,1+1/2+1/3 +
……..1/n,12 + 22+ 32 +…….n2) using JAVA
import java.util.Scanner;
class Employee {
private String name;
private int id;
@Override
public double calculateSalary() {
return salary;
}
}
@Override
public double calculateSalary() {
return hourlyRate * hoursWorked;
}
}
class PaySlipGenerator {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
double salary = 0;
if (employeeType == 1) {
System.out.print("Enter the monthly salary: ");
salary = scanner.nextDouble();
FullTimeEmployee fullTimeEmployee = new FullTimeEmployee(name, id, salary);
salary = fullTimeEmployee.calculateSalary();
} else if (employeeType == 2) {
System.out.print("Enter the hourly rate: ");
double hourlyRate = scanner.nextDouble();
add(nameLabel);
add(nameField);
add(genderLabel);
add(maleRadioButton);
add(femaleRadioButton);
add(countryLabel);
add(countryComboBox);
add(addressLabel);
add(addressArea);
add(submitButton);
submitButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String name = nameField.getText();
String gender = maleRadioButton.isSelected() ? "Male" : "Female";
String country = (String) countryComboBox.getSelectedItem();
String address = addressArea.getText();
JOptionPane.showMessageDialog(null,
"Name: " + name + "\n" +
"Gender: " + gender + "\n" +
"Country: " + country + "\n" +
"Address: " + address);
}
});
}
def find_avg(nums):
total = 0
for num in nums:
total += num
avg = total / len(nums)
return avg
avg = find_avg(array)
print("Average:", avg)
12.Implement Python program - TCP/UDP program using
Sockets
import socket
def tcp_server():
tcp_socket = socket.socket(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 = conn.recv(1024).decode()
if not data:
break
print('Received:', data)
conn.sendall('Hello from TCP Server!'.encode())
conn.close()
def tcp_client():
tcp_socket = socket.socket(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 = socket.socket(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:', data.decode())
udp_socket.sendto('Hello from UDP Server!'.encode(), addr)
def udp_client():
udp_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
udp_socket.sendto('Hello from UDP Client!'.encode(), ('localhost', 9999))
data, addr = udp_socket.recvfrom(1024)
print('Received:', data.decode())
udp_socket.close()
tcp_server()
tcp_client()
udp_server()
udp_client()
13.Construct NFA and DFA using Python
class State:
def init (self, name):
self.name = name
self.transitions = {}
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}
current_states = next_states
class DFA:
def init (self, start_state, accept_states):
self.start_state = start_state
self.accept_states = accept_states
self.transitions = {} # Define transitions as an empty dictionary
def add_transition(self, from_state, symbol, to_state):
if from_state not in self.transitions:
self.transitions[from_state] = {}
if symbol not in self.transitions[from_state]:
self.transitions[from_state][symbol] = set()
self.transitions[from_state][symbol].add(to_state)
current_states = next_states
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)
python
from sympy import symbols, sin, cos, exp, log
class Event:
def init (self, name):
self.name = name
self.handlers = []
class Button:
def init (self, label):
self.label = label
self.click_event = Event('click')
def click(self):
self.click_event.fire()
def button_click_handler():
print("Button clicked!")
button.click()