diff --git a/Dynamic Programming/Thread.java b/Dynamic Programming/Thread.java new file mode 100644 index 00000000..1024f4c0 --- /dev/null +++ b/Dynamic Programming/Thread.java @@ -0,0 +1,114 @@ +package multitrycatch; + +class Point { + int first, second; + + public Point(int first, int second) { + this.first = first; + this.second = second; + } +} + +class MaxSumSubMatrix +{ + // function to find maximum sum k x k sub-matrix + public static Point findMaxSumSubMatrix(int mat[][], int k) + { + // M x N matrix + int M = mat.length; + int N = mat[0].length; + + // pre-process the input matrix such that sum[i][j] stores + // sum of elements in matrix from (0, 0) to (i, j) + int[][] sum = new int[M][N]; + sum[0][0] = mat[0][0]; + + // pre-process first row + for (int j = 1; j < N; j++) { + sum[0][j] = mat[0][j] + sum[0][j - 1]; + } + + // pre-process first column + for (int i = 1; i < M; i++) { + sum[i][0] = mat[i][0] + sum[i - 1][0]; + } + + // pre-process rest of the matrix + for (int i = 1; i < M; i++) { + for (int j = 1; j < N; j++) { + sum[i][j] = mat[i][j] + sum[i - 1][j] + sum[i][j - 1] + - sum[i - 1][j - 1]; + } + } + + int total, max = Integer.MIN_VALUE; + Point p = null; + + // find maximum sum sub-matrix + + // start from cell (k - 1, k - 1) and consider each + // submatrix of size k x k + for (int i = k - 1; i < M; i++) + { + for (int j = k - 1; j < N; j++) + { + // Note (i, j) is bottom right corner coordinates of + // square sub-matrix of size k + + total = sum[i][j]; + if (i - k >= 0) { + total = total - sum[i - k][j]; + } + + if (j - k >= 0) { + total = total - sum[i][j - k]; + } + + if (i - k >= 0 && j - k >= 0) { + total = total + sum[i - k][j - k]; + } + + if (total > max) { + max = total; + p = new Point(i, j); + } + } + } + + // returns coordinates of bottom right corner of sub-matrix + return p; + } + + public static void main(String[] args) + { + // 5 x 5 matrix + int[][] mat = + { + { 3, -4, 6, -5, 1 }, + { 1, -2, 8, -4, -2 }, + { 3, -8, 9, 3, 1 }, + { -7, 3, 4, 2, 7 }, + { -3, 7, -5, 7, -6 } + }; + + // sub-matrix size + int k = 3; + + // p contains bottom right corner coordinates of sub-matrix + Point p = findMaxSumSubMatrix(mat, k); + + // print maximum sum sub-matrix + for (int i = 0; i < k; i++) + { + for (int j = 0; j < k; j++) + { + int r = i + p.first - k + 1; + int c = j + p.second - k + 1; + System.out.printf("%3d", mat[r][c]); + } + + System.out.println(); + } + } +} + diff --git a/Networking/Client -Machine.java b/Networking/Client -Machine.java new file mode 100644 index 00000000..ebd9c502 --- /dev/null +++ b/Networking/Client -Machine.java @@ -0,0 +1,32 @@ +import java.io.*; +import java.net.*; +public class ClientMachine +{ + public static void main(String[] args) throws Exception + { + Socket sock = new Socket("localhost", 3000); + //reading from keyboard (keyRead object) + BufferedReader keyRead = new BufferedReader(new InputStreamReader(System.in)); + //sending to client (pwrite object) + OutputStream ostream = sock.getOutputStream(); + PrintWriter pwrite = new PrintWriter(ostream, true); + + // receiving from server ( receiveRead object) + InputStream istream = sock.getInputStream(); + BufferedReader receiveRead = new BufferedReader(new InputStreamReader(istream)); + + System.out.println("Start the chatting, type and press Enter key"); + + String receiveMessage, sendMessage; + while(true) + { + sendMessage = keyRead.readLine(); // keyboard reading + pwrite.println(sendMessage); // sending to server + pwrite.flush(); // flush the data + if((receiveMessage = receiveRead.readLine()) != null) //receive from server + { + System.out.println(receiveMessage); // displaying at DOS prompt + } + } + } +} diff --git a/Networking/Create ServerMachine.java b/Networking/Create ServerMachine.java new file mode 100644 index 00000000..189ada26 --- /dev/null +++ b/Networking/Create ServerMachine.java @@ -0,0 +1,32 @@ +import java.io.*; +import java.net.*; +public class ServerMachine +{ + public static void main(String[] args) throws Exception + { + ServerSocket sersock = new ServerSocket(3000); + System.out.println("Server ready for sending message..."); + Socket sock = sersock.accept( ); + //reading from keyboard (keyRead object) + BufferedReader keyRead = new BufferedReader(new InputStreamReader(System.in)); + //sending to client (pwrite object) + OutputStream ostream = sock.getOutputStream(); + PrintWriter pwrite = new PrintWriter(ostream, true); + + //receiving from server ( receiveRead object) + InputStream istream = sock.getInputStream(); + BufferedReader receiveRead = new BufferedReader(new InputStreamReader(istream)); + + String receiveMessage, sendMessage; + while(true) + { + if((receiveMessage = receiveRead.readLine()) != null) + { + System.out.println(receiveMessage); + } + sendMessage = keyRead.readLine(); + pwrite.println(sendMessage); + pwrite.flush(); + } + } +} diff --git a/Thread_Programming/multiplethread.java b/Thread_Programming/multiplethread.java new file mode 100644 index 00000000..b2498d4f --- /dev/null +++ b/Thread_Programming/multiplethread.java @@ -0,0 +1,38 @@ +class MyThread implements Runnable { + +String name; +Thread t; + MyThread (String thread){ + name = threadname; + t = new Thread(this, name); +System.out.println("New thread: " + t); +t.start(); +} + + +public void run() { + try { + for(int i = 5; i > 0; i--) { + System.out.println(name + ": " + i); + Thread.sleep(1000); +} +}catch (InterruptedException e) { + System.out.println(name + "Interrupted"); +} + System.out.println(name + " exiting."); +} +} + +class MultiThread { +public static void main(String args[]) { + new MyThread("One"); + new MyThread("Two"); + new NewThread("Three"); +try { + Thread.sleep(10000); +} catch (InterruptedException e) { + System.out.println("Main thread Interrupted"); +} + System.out.println("Main thread exiting."); + } +}