Wednesday, February 24, 2016

PL/SQL block that computes the sum of the salary of all the employees working in the particular department

PL/SQL block

SET SERVEROUTPUT ON

DECLARE
sumSalary number; 
BEGIN
select sum(salary) into sumSalary
from employees
where department_id=&department_id;  
dbms_output.put_line('The sum of salary ->'||sumSalary); 
END;
/

Output:

TCP Socket Programming in Java


Write a server-client TCP socket program where the client takes inputs, sends it to the server to perform a certain operation, and gets back the output of the operation. Run the programs in local host and in different systems in LAN.

/**
*
* @author anuz
*/
public class ClientSide {

/**
* @param args the command line arguments
* @throws java.io.IOException
*/
public static void main(String[] args) throws IOException {
// TODO code application logic here

try (
//name of the computer and the port number to which you want to connect.
Socket socket = new Socket("localhost", 8888);
//readers and writers to write characters over the socket.
PrintWriter out = new PrintWriter(socket.getOutputStream(), true);//to send data to server
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));//for response from the server
BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in));//standard input
) {
System.out.println(in.readLine());
String userInput;
//the loop reads a line at a time from the standard input stream and immediately sends it to the server
while ((userInput = stdIn.readLine()) != null) {
if(userInput.equalsIgnoreCase("exit")){
break;
}
out.println(userInput);
System.out.println(in.readLine());
}

} catch (IOException ex) {
System.out.println(ex.getMessage());
}

}

}





/**
*
* @author anuz
*/
public class ServerSide {

/**
* @param args the command line arguments
* @throws javax.script.ScriptException
*/
public static void main(String[] args) throws ScriptException {
// TODO code application logic here
try {

//server socket port number
ServerSocket serverSocket = new ServerSocket(8888);
while(true){
Socket clientSocket = serverSocket.accept();
System.out.println("connection from :" + clientSocket.getInetAddress()); //display client IP connected to server

PrintStream out = new PrintStream(clientSocket.getOutputStream());
out.println("welcome to the calculator server");

BufferedReader reader = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
String line;

while ((line = reader.readLine()) != null) {
try {
System.out.println(line);
if (line.equalsIgnoreCase("exit")) {
break;
}

//code for operation of user input.

//------------------------------------------------
ScriptEngineManager mgr = new ScriptEngineManager();
ScriptEngine engine = mgr.getEngineByName("JavaScript");
System.out.println("=>" + engine.eval(line));
out.println("=>" + engine.eval(line));
//-------------------------------------------------

/*
 Security Note:
 You should never use ScriptEngine in a server context with user input. The executed
JavaScript can access all Java classes and thus hijack your application without limit
It executes a script, not evaluates an expression.
*/


}catch (ScriptException ex) {
System.out.println(ex.getMessage());
}
}

}catch (IOException ex) {
System.out.println(ex.getMessage());
}
}

}
}



Output:


Illustration 1: ServerSide

Illustration 2: ClientSide













 

Java Program to calculate factorial by passing object




public class FindFactorial {
int a=0;
  FindFactorial(int i){
    a=i;
  }
  int displayFact(FindFactorial f){
    if(f.a<1)
      return 1;
    else{
         f.a--;
         return (f.a+1)*displayFact(f);
     }
  }

}

 

public class Main {
public static void main(String[] args){
      FindFactorial f1 = new FindFactorial(3);
      System.out.println("factorial ="+ f1.displayFact(f1));
}
}


Output:



Java program with matrix


import java.util.Scanner;

class Sum
{
public static void main(String[] args)
{
int m, n, c, d;
Scanner in = new Scanner(System.in);
System.out.println("Enterin the number of rows and columns of matrix");
m = in.nextInt();
n = in.nextInt();
int first[][] = new int[m][n];
int second[][] = new int[m][n];
int sum[][] = new int[m][n];
System.out.println("Enter the elements of first matrix");
for ( c = 0 ; c < m ; c++ )
for ( d = 0 ; d < n ; d++ )
first[c][d] = in.nextInt();
System.out.println("Enter the elements of second matrix");
for ( c = 0 ; c < m ; c++ )
for ( d = 0 ; d < n ; d++ )
second[c][d] = in.nextInt();
in.close();
for ( c = 0 ; c < m ; c++ )
for ( d = 0 ; d < n ; d++ )
sum[c][d] = first[c][d] + second[c][d]; //replace '+' with '-' to subtract matrices
System.out.println("Sum of entered matrices:-");
for ( c = 0 ; c < m ; c++ )
{
for ( d = 0 ; d < n ; d++ )
System.out.print(sum[c][d]+"\t");
System.out.println();
}
}
}

Output: