Skip to content

Commit 4909cc5

Browse files
authored
Merge pull request #2 from batCoder95/branch1
Branch1
2 parents cbc4c30 + c13106b commit 4909cc5

11 files changed

Lines changed: 290 additions & 0 deletions

File tree

FloydWarshall.java

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
public class FloydWarshall {
2+
3+
public static void floydWarshall(int[][] graph){
4+
int N = graph.length;
5+
int dist[][] = new int[N][N];
6+
for(int i=0;i<N;i++){
7+
for(int j=0;j<N;j++){
8+
dist[i][j] = graph[i][j];
9+
}
10+
}
11+
12+
for(int k=0;k<N;k++){
13+
for(int i=0;i<N;i++){
14+
for(int j=0;j<N;j++){
15+
if(dist[i][j]!=-999)
16+
dist[i][j] = Math.max(dist[i][k]+dist[k][j],dist[i][j]);
17+
}
18+
}
19+
}
20+
21+
for(int i=0;i<N;i++){
22+
System.out.println();
23+
for(int j=0;j<N;j++){
24+
System.out.print(" "+dist[i][j]+" ");;
25+
}
26+
}
27+
}
28+
public static void main(String[] args) {
29+
// TODO Auto-generated method stub
30+
int graph[][] = {{-999,-999,-999,-999},
31+
{3,4,5,-999},
32+
{40,8,9,10},
33+
{-999,1,2,3}
34+
};
35+
floydWarshall(graph);
36+
}
37+
38+
}

TravellingSalesman.java

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
import java.util.*;
2+
3+
public class TravellingSalesman
4+
{
5+
private int numberOfNodes;
6+
private Stack<Integer> stack;
7+
8+
public TravellingSalesman()
9+
{
10+
stack = new Stack<Integer>();
11+
}
12+
13+
public void tsp(int adjacencyMatrix[][])
14+
{
15+
numberOfNodes = adjacencyMatrix[1].length - 1;
16+
int[] visited = new int[numberOfNodes];
17+
visited[0] = 1;
18+
stack.push(0);
19+
int element, dst = -1, i;
20+
int min = Integer.MAX_VALUE;
21+
boolean minFlag = false;
22+
System.out.print(0 + "\t");
23+
24+
while (!stack.isEmpty())
25+
{
26+
element = stack.peek();
27+
i = 1;
28+
min = Integer.MAX_VALUE;
29+
while (i <numberOfNodes)
30+
{
31+
if (adjacencyMatrix[element][i] > 1 && visited[i] == 0)
32+
{
33+
if (min > adjacencyMatrix[element][i])
34+
{
35+
min = adjacencyMatrix[element][i];
36+
dst = i;
37+
minFlag = true;
38+
}
39+
}
40+
i++;
41+
}
42+
if (minFlag)
43+
{
44+
visited[dst] = 1;
45+
stack.push(dst);
46+
System.out.print(dst + "\t");
47+
minFlag = false;
48+
continue;
49+
}
50+
stack.pop();
51+
}
52+
}
53+
54+
public static void main(String... arg)
55+
{
56+
int number_of_nodes;
57+
Scanner scanner = null;
58+
try
59+
{
60+
System.out.println("Enter the number of nodes in the graph");
61+
scanner = new Scanner(System.in);
62+
number_of_nodes = scanner.nextInt();
63+
int adjacency_matrix[][] = new int[number_of_nodes][number_of_nodes];
64+
System.out.println("Enter the adjacency matrix");
65+
for (int i = 0; i < number_of_nodes; i++)
66+
{
67+
for (int j = 0; j < number_of_nodes; j++)
68+
{
69+
adjacency_matrix[i][j] = scanner.nextInt();
70+
}
71+
}
72+
for (int i = 0; i < number_of_nodes; i++)
73+
{
74+
for (int j = 0; j < number_of_nodes; j++)
75+
{
76+
if (adjacency_matrix[i][j] == 1 && adjacency_matrix[j][i] == 0)
77+
{
78+
adjacency_matrix[j][i] = 1;
79+
}
80+
}
81+
}
82+
System.out.println("the citys are visited as follows");
83+
TravellingSalesman obj = new TravellingSalesman();
84+
obj.tsp(adjacency_matrix);
85+
} catch (Exception E)
86+
{
87+
System.out.println(E);
88+
System.out.println("Wrong Input format");
89+
}
90+
scanner.close();
91+
}
92+
}

jdbc-tcp/Employee.java

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
public class Employee implements java.io.Serializable{
2+
3+
private static final long serialVersionUID = 1L;
4+
5+
private int Eid;
6+
private String name;
7+
private String post;
8+
private int Did;
9+
private int salary;
10+
11+
Employee(int Eid, String name, String post, int Did, int salary){
12+
this.Eid = Eid;
13+
this.name = name;
14+
this.post = post;
15+
this.Did = Did;
16+
this.salary = salary;
17+
}
18+
19+
public int getEid(){
20+
return this.Eid;
21+
}
22+
public String getName(){
23+
return this.name;
24+
}
25+
public String getPost(){
26+
return this.post;
27+
}
28+
public int getDid(){
29+
return this.Did;
30+
}
31+
public int getSalary(){
32+
return this.salary;
33+
}
34+
}

jdbc-tcp/TCPClient.java

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import java.io.*;
2+
import java.net.*;
3+
4+
public class TCPClient {
5+
6+
public static void main(String[] args) {
7+
String serverName = "localhost";
8+
int port = 6666;
9+
try{
10+
System.out.println("\n\nConnecting to " + serverName + " on port " + port);
11+
Socket client = new Socket(serverName, port);
12+
System.out.println("\n\nJust connected to " + client.getRemoteSocketAddress());
13+
14+
System.out.print("\n\n\nEnter Employee ID : ");
15+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
16+
int idInput = Integer.parseInt(br.readLine());
17+
18+
OutputStream outToServer = client.getOutputStream();
19+
DataOutputStream out = new DataOutputStream(outToServer);
20+
out.writeInt(idInput);
21+
22+
InputStream inFromServer = client.getInputStream();
23+
DataInputStream in = new DataInputStream(inFromServer);
24+
System.out.print("\n\n\n\n\nBelow details were returned by the server : "+in.readUTF());
25+
26+
client.close();
27+
}
28+
catch(Exception e){
29+
System.out.println("Client Side problem : "+e);
30+
}
31+
32+
}
33+
34+
}

jdbc-tcp/TCPServer.java

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
import java.util.*;
2+
import java.io.*;
3+
import java.net.*;
4+
import java.sql.*;
5+
6+
public class TCPServer {
7+
8+
private ServerSocket TCPSocket;
9+
10+
public TCPServer(int port)throws IOException{
11+
TCPSocket = new ServerSocket(port);
12+
TCPSocket.setSoTimeout(2147483647);
13+
}
14+
15+
public void retrieveEmployee(){
16+
while(true)
17+
{
18+
try
19+
{
20+
System.out.println("\n\nWaiting for client on port " + TCPSocket.getLocalPort() + "...");
21+
Socket server = TCPSocket.accept();
22+
System.out.println("\n\nJust connected to "+ server.getRemoteSocketAddress());
23+
DataInputStream in = new DataInputStream(server.getInputStream());
24+
int x = in.readInt();
25+
System.out.println("\n\nClient Requested for employee "+x);
26+
27+
Class.forName("com.mysql.jdbc.Driver");
28+
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/EmployeeDB","root", "pass");
29+
PreparedStatement stmt=con.prepareStatement("SELECT *FROM EMPLOYEE WHERE Eid = ?");
30+
stmt.setInt(1,x);
31+
List<Employee> details = new ArrayList<Employee>();
32+
ResultSet rs = stmt.executeQuery();
33+
while(rs.next()){
34+
Employee E = new Employee(rs.getInt(1),rs.getString(2),rs.getString(3),rs.getInt(4),rs.getInt(5));
35+
details.add(E);
36+
}
37+
String str = null;
38+
for(Employee E : details){
39+
str = "\n\n\nEMPLOYEE ID : "+Integer.toString(E.getEid())+"\nEMPLOYEE NAME : "+E.getName()+"\nPOST : "+E.getPost()+"\nDEPT ID : "+Integer.toString(E.getDid())
40+
+"\nSALARY : "+Integer.toString(E.getSalary())+"\n\n\n";
41+
}
42+
43+
DataOutputStream out = new DataOutputStream(server.getOutputStream());
44+
out.writeUTF(str);
45+
46+
server.close();
47+
48+
49+
}
50+
catch(Exception e){
51+
System.out.println("Server side Retrieval problem : ");
52+
e.printStackTrace();
53+
}
54+
}
55+
}
56+
57+
public static void main(String[] args) {
58+
int port = 6666;
59+
try{
60+
TCPServer t = new TCPServer(port);
61+
t.retrieveEmployee();
62+
63+
}catch(Exception e){
64+
System.out.println("Server main method problem : "+e);
65+
}
66+
67+
}
68+
69+
}

jdbc-tcp/employeedb/EmployeeDB.sql

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
CREATE TABLE Employee(
2+
Eid INT NOT NULL,
3+
name VARCHAR(20) NOT NULL,
4+
post VARCHAR(20) NOT NULL,
5+
Did INT NOT NULL,
6+
salary INT NOT NULL
7+
);
8+
9+
INSERT INTO Employee (Eid, name, post, Did, salary) VALUES (1, 'ABC', 'Software Engineer', 101, 40000);
10+
INSERT INTO Employee (Eid, name, post, Did, salary) VALUES (2, 'DEF', 'Systems Engineer', 201, 25000);
11+
INSERT INTO Employee (Eid, name, post, Did, salary) VALUES (3, 'MNO', 'Data Scientist', 302, 30000);
12+
INSERT INTO Employee (Eid, name, post, Did, salary) VALUES (4, 'PQR', 'Technical Analyst', 401, 30000);
13+
INSERT INTO Employee (Eid, name, post, Did, salary) VALUES (5, 'XYZ', 'Java Developer', 501, 50000);

jdbc-tcp/employeedb/db.opt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
default-character-set=utf8
2+
default-collation=utf8_general_ci

jdbc-tcp/employeedb/employee.frm

8.48 KB
Binary file not shown.

jdbc-tcp/employeedb/employee.ibd

96 KB
Binary file not shown.

jdbc-tcp/employeedb/info

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Employee database files.

0 commit comments

Comments
 (0)