Top Banner

of 28

54240326

Apr 03, 2018

Download

Documents

bansal_aditi03
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
  • 7/28/2019 54240326

    1/28

    Downloaded from www.pencilji.com

    Downloaded from www.pencilji.com

    1. Program for implementing a stack & to perform Push & Popoperations

    import java.io.*;

    class st

    {int top=0;int max=0,i=0,n=0;int stk[];

    BufferedReader input=new BufferedReader(new InputStreamReader(System.in));st()

    {try{

    System.out.println("Enter the size of the stack");

    max=Integer.parseInt(input.readLine());}

    catch(IOException e){}stk=new int[max];

    }

    public void add(){

    try{if(top0)System.out.println("Deleted element is"+stk[--top]);

    else

    System.out.println("Stack underflow");

    }public void display(){ if(top==0)

    System.out.println("Stack is empty");

    else{

    for(int i=0;i

  • 7/28/2019 54240326

    2/28

    Downloaded from www.pencilji.com

    Downloaded from www.pencilji.com

    }}

    }class Stack

    {

    public static void main(String args[])throws IOException{

    String data;int ch=0;

    st a=new st();BufferedReader input=new BufferedReader(new InputStreamReader(System.in));while(true)

    {System.out.println("1:PUSH 2:POP 3:DISPLAY 4:EXIT");

    System.out.println("Enter ur choice");try{

    ch=Integer.parseInt(input.readLine());}catch(IOException e){}

    switch(ch){

    case 1:a.add();

    break;case 2:a.delete();

    break;case 3 :a.display();

    break;

    case 4 :System.exit(0);}

    }}

    }

  • 7/28/2019 54240326

    3/28

    Downloaded from www.pencilji.com

    Downloaded from www.pencilji.com

    Output:

    C:\cd jdk1.3\bin>javac Stack.java

    C:\JDK1.3\BIN>java Stack

    Enter the size of the stack2

    1:PUSH 2:POP 3:DISPLAY 4:EXITEnter ur choice

    1Enter the element of the stack23

    1:PUSH 2:POP 3:DISPLAY 4:EXITEnter ur choice

    145

    1:PUSH 2:POP 3:DISPLAY 4:EXITEnter ur choice3

    elements are 23elements are 45

    1:PUSH 2:POP 3:DISPLAY 4:EXIT

    Enter ur choice2

    Deleted element is 451:PUSH 2:POP 3:DISPLAY 4:EXITEnter ur choice

    4C:\JDK1.3\BIN>

  • 7/28/2019 54240326

    4/28

    Downloaded from www.pencilji.com

    Downloaded from www.pencilji.com

    2. Program to implement the following operations on a QueueAdd an element

    Delete an elementDisplay the elements

    import java.io.*;class Queue1{public static void main(String[] args)throws IOException

    {int a[]=new int [10];

    int pos=0,n=0,j;BufferedReader b=new BufferedReader(new InputStreamReader(System.in));while(true)

    {System.out.println("1.INSERTION");

    System.out.println("2.DELETION");System.out.println("3.DISPLAY");System.out.println("4.EXIT");

    System.out.println("enter your choice");String choice = b.readLine();

    if(choice.equals("1")){System.out.println("enter the element");

    String ma = b.readLine();int d = Integer.parseInt(ma);

    a[pos]=d;

    pos++;n++;

    }else

    if(choice.equals("2")){for(j=0;j

  • 7/28/2019 54240326

    5/28

    Downloaded from www.pencilji.com

    Downloaded from www.pencilji.com

    }else

    if(choice.equals("4")){

    System.exit(0);

    }else

    {System.out.println("Enter proper choice");

    }}

    }

    }

  • 7/28/2019 54240326

    6/28

    Downloaded from www.pencilji.com

    Downloaded from www.pencilji.com

    Output:

    C:\JDK1.3\BIN>java Queue11.INSERTION

    2.DELETION

    3.DISPLAY4.EXIT

    Enter your choice1

    Enter the element21.INSERTION

    2.DELETION3.DISPLAY

    4.EXITEnter your choice

    1Enter the element5

    1.INSERTION2.DELETION3.DISPLAY

    4.EXITEnter your choice

    3Elements are: 2Elements are:5

    1.INSERTION2.DELETION

    3.DISPLAY4.EXITEnter your choice

    21.INSERTION

    2.DELETION3.DISPLAY4.EXIT

    Enter your choice

    3Elements are: 51.INSERTION2.DELETION

    3.DISPLAY4.EXIT

    Enter your choice4

  • 7/28/2019 54240326

    7/28

    Downloaded from www.pencilji.com

    Downloaded from www.pencilji.com

    3. Program to implement the following operations on a singly linked listCreate a List

    Add a Node to the Front of the ListAdd a Node to the Back of the List

    Delete a Specified NodeDisplay a List

    import java.io.*;

    import java.util.*;class Link1{

    LinkedList l1=new LinkedList();BufferedReader br=new BufferedReader(new InputStreamReader(System.in));

    String input;

    public void create(int size)throws IOException{for(int i=0;i

  • 7/28/2019 54240326

    8/28

    Downloaded from www.pencilji.com

    Downloaded from www.pencilji.com

    l1.remove(pos);}

    public void display()

    {

    System.out.println("Elements in the list:");System.out.println(l1);

    }}

    class Link14{

    public static void main(String arg[])throws IOException{

    int choice;BufferedReader br=new BufferedReader(new InputStreamReader(System.in));

    Link1 l=new Link1();

    while(true)

    {System.out.println("1.CREATE LINKED LIST");System.out.println("2.ADD FIRST");

    System.out.println("3.ADD LAST");System.out.println("4.REMOVE");

    System.out.println("5.DISPLAY");System.out.println("6.EXIT");System.out.println("Enter your choice:");

    choice=Integer.parseInt(br.readLine());

    switch(choice){

    case 1:System.out.println("Enter the size:");

    int s=Integer.parseInt(br.readLine());l.create(s);

    break;case 2:l.begin();

    break;

    case 3:l.last();break;

    case 4:l.delete();break;

    case 5:l.display();

    break;case 6:System.exit(0);

    default:System.out.println("Enter Proper C hoice");}

  • 7/28/2019 54240326

    9/28

    Downloaded from www.pencilji.com

    Downloaded from www.pencilji.com

    }}

    }

    Output:

  • 7/28/2019 54240326

    10/28

    Downloaded from www.pencilji.com

    Downloaded from www.pencilji.com

    C:\JDK1.3\BIN>java Link14

    1.CREATE LINKED LIST2.ADD FIRST

    3.ADD LAST

    4.REMOVE5.DISPLAY

    6.EXITEnter your choice:

    1Enter the size:2

    Enter the node item:4

    Enter the node item:-2

    1.CREATE LINKED LIST2.ADD FIRST3.ADD LAST

    4.REMOVE5.DISPLAY6.EXIT

    Enter your choice:2

    Enter the item to be inserted at the beginning:901.CREATE LINKED LIST

    2.ADD FIRST3.ADD LAST

    4.REMOVE5.DISPLAY6.EXIT

    Enter your choice:5

    Elements in the list:[90, 4, -2]1.CREATE LINKED LIST

    2.ADD FIRST3.ADD LAST

    4.REMOVE5.DISPLAY6.EXIT

    Enter your choice:3

    Enter the item to be inserted at the last:10

  • 7/28/2019 54240326

    11/28

    Downloaded from www.pencilji.com

    Downloaded from www.pencilji.com

    1.CREATE LINKED LIST2.ADD FIRST

    3.ADD LAST4.REMOVE

    5.DISPLAY

    6.EXITEnter your choice:

    5Elements in the list:

    [90, 4, -2, 10]1.CREATE LINKED LIST2.ADD FIRST

    3.ADD LAST4.REMOVE

    5.DISPLAY6.EXIT

    Enter your choice:4Enter the position:

    21.CREATE LINKED LIST2.ADD FIRST

    3.ADD LAST4.REMOVE

    5.DISPLAY6.EXITEnter your choice:

    5Elements in the list:

    [90, 4, 10]1.CREATE LINKED LIST2.ADD FIRST

    3 ADD LAST4.REMOVE

    5.DISPLAY6.EXITEnter your choice:

    6

    C:\JDK1.3\BIN>

  • 7/28/2019 54240326

    12/28

    Downloaded from www.pencilji.com

    Downloaded from www.pencilji.com

    4. Program to implement a Producer and Consumer Problem usingThreads

    import java.io.*;

    class Q

    {int n;boolean valueset=false;synchronized int get()

    {if(!valueset)

    try{

    wait();

    }catch (InterruptedException e1)

    {System.out.println("Thread Interrupted");

    }

    System.out.println("get" +n);valueset=false;

    notify();return n;

    }

    synchronized void put(int n){

    if(valueset)

    try{

    wait();}catch (InterruptedException e2)

    {System.out.println("thread interrupted");

    }this.n=n;valueset=true;

    System.out.println("put " +n);

    notify();}

    }

    class Producer implements Runnable{

    Q q; Thread t;Producer (Q q)

  • 7/28/2019 54240326

    13/28

    Downloaded from www.pencilji.com

    Downloaded from www.pencilji.com

    {this.q=q;

    t=new Thread(this, "Producer");t.start();

    }

    public void run(){

    int i=0;while (i

  • 7/28/2019 54240326

    14/28

  • 7/28/2019 54240326

    15/28

    Downloaded from www.pencilji.com

    Downloaded from www.pencilji.com

    5. Program to create an applet to scroll a text message

    import java.applet.*;import java.awt.*;

    public class AB extends Applet implements Runnable

    {String str;

    int x,y;public void init()

    {str="WELCOME TO RNSIT";x=300;

    y=100;new Thread(this).start();

    }public void run()

    {try{

    while(true){x=x-10;

    if(x

  • 7/28/2019 54240326

    16/28

    Downloaded from www.pencilji.com

    Downloaded from www.pencilji.com

    Output:

    C:\JDK1.3\BIN>javac AB.java

    C:\JDK1.3\BIN>appletviewer AB.html

    Applet Viewer:AB

    Applet

    WELCOME TO RNSIT

    Applet started.

  • 7/28/2019 54240326

    17/28

    Downloaded from www.pencilji.com

    Downloaded from www.pencilji.com

    6. Develop a Java program for a Client and Server Program to do thefollowing:

    1.The client requests for a file.2.The server sends the contents of the file requested.

    3.The client receives the file and displays it.

    // code for client program

    import java.net.*;import java.util.*;import java.io.*;

    public class Client{

    public static void main(String args[]){Socket client=null;

    BufferedReader br=null;try

    {System.out.println(args[0]+" "+ args[1]);client=new Socket(args[0],Integer.parseInt(args[1]));

    } catch(Exception e){}BufferedReader input=null;PrintStream output=null;

    try

    {input=new BufferedReader(new InputStreamReader(client.getInputStream()));output=new PrintStream(client.getOutputStream());

    br=new BufferedReader(new InputStreamReader(System.in));String str=input.readLine();//get the prompt from the server

    System.out.println(str);//display the prompt on the client machineString filename=br.readLine();if(filename!=null)

    output.println(filename);String data;

    while((data=input.readLine())!=null)

    System.out.println(data);

    client.close();

    }catch(Exception e){

    System.out.println(e);

  • 7/28/2019 54240326

    18/28

    Downloaded from www.pencilji.com

    Downloaded from www.pencilji.com

    }}

    }

    /* Code for Server program*/

    import java.net.*;

    import java.util.*;import java.io.*;

    public class Server

    {public static void main(String args[])

    {

    ServerSocket server=null;

    try

    {server=new ServerSocket(Integer.parseInt(args[0]));

    } catch(Exception e){}

    while(true)

    {

    Socket client=null;

    PrintStream output=null;BufferedReader input=null;

    try{

    client=server.accept();} catch(Exception e){System.out.println(e);}

    try{

    output=new PrintStream(client.getOutputStream());input=new BufferedReader(new InputStreamReader(client.getInputStream())) ;

    }catch(Exception e){System.out.println(e);}

    //send the command Prompt to the client

    output.println("ENTER THE FILE NAME>");try

  • 7/28/2019 54240326

    19/28

    Downloaded from www.pencilji.com

    Downloaded from www.pencilji.com

    {

    //get the file name from the clientString filename=input.readLine();

    System.out.println("Client requested file:" + filename);

    try

    {File f=new File(filename);

    BufferedReader br=new BufferedReader(new FileReader(f));String data;

    while((data=br.readLine())!=null)

    {output.println(data);

    }}catch(FileNotFoundException e)

    { output.println("FILE NOT FOUND");}

    client.close();

    }catch(Exception e){System.out.println(e);

    }}

    }

    }

  • 7/28/2019 54240326

    20/28

    Downloaded from www.pencilji.com

    Downloaded from www.pencilji.com

    Output:

    C:\JDK1.3\BIN>javac C lient.javaC:\JDK1.3\BIN>javac Server.java

    C:\JDK1.3\BIN>java Server 80

    /* In a new prompt*/C:\JDK1.3\BIN>java Client localhost 80

    Local host 80ENTER THE FILE NAME>

    Server.java /*File Serever.java is displayed *//*In the server prompt*/Client requested file: Server.java

  • 7/28/2019 54240326

    21/28

    Downloaded from www.pencilji.com

    Downloaded from www.pencilji.com

    7. Program to implement the Simple Client/Server Application usingRM

    /*Interface Program */

    import java.rmi.*;public interface TimeIntf extends Remote

    {public String getTime() throws RemoteException;

    public void display() throws RemoteException;}

    /*Server Program */

    import java.rmi.server.*;

    import java.rmi.*;import java.net.*;import java.util.*;

    import java.text.*;

    public class TimeServer extends UnicastRemoteObject implements TimeIntf{TimeServer() throws Exception {}

    public String getTime() throws RemoteException{Date dd=new Date();

    SimpleDateFormat sdf;sdf=new SimpleDateFormat("hh:mm:ss");

    System.out.println("date&time"+sdf.format(dd));String tt=sdf.format(dd);return tt;

    }public void display() throws RemoteException

    {System.out.println("Hello This Is To Demonstrate RMI ");}

    public static void main (String args[] ) throws Exception{TimeServer tobj=new TimeServer();Naming.rebind("Time",tobj);

    }}

  • 7/28/2019 54240326

    22/28

    Downloaded from www.pencilji.com

    Downloaded from www.pencilji.com

    /*Client Program */

    import java.rmi.*;public class TimeClient

    {

    public static void main(String args[]) throws Exception{

    TimeIntf t;t=(TimeIntf)Naming.lookup("Time");

    System.out.println(t.getTime());t.display();}

    }

  • 7/28/2019 54240326

    23/28

    Downloaded from www.pencilji.com

    Downloaded from www.pencilji.com

    Output:

    C:\JDK1.3\BIN>javac TimeIntf.javaC:\JDK1.3\BIN>javac TimeServer.java

    C:\JDK1.3\BIN>javac TimeClient.java

    C:\JDK1.3\BIN>rmic TimeServerC:\JDK1.3\BIN>start rmiregistry

    /*minimize the rmiregistry window & the prompt window*//*Open a new prompt window*/

    C:\JDK1.3\BIN>java TimeClient/*Using ALT+TAB switch to the other prompt window*/

    C:\JDK1.3\BIN>java TimeServerJava TimeSerever

    date & time 02:19:09Hello this is to demonstrate RMI

    /*Open the client prompt using ALT+TAB */

    date & time 02:19:12

  • 7/28/2019 54240326

    24/28

    Downloaded from www.pencilji.com

    Downloaded from www.pencilji.com

    8. Create a Java Servlet program to do the following:1. To receive employee name & telephone number from the client

    browser2. To generate a response & send it to the client browser.

    Create a HTML document to do the following:1. To accept employee name & telephone number2. To send them to Servlet

    /* Servlet Program*/import javax.servlet.*;

    import java.io.*;

    public class TestServlet extends GenericServlet

    {public void service(ServletRequest request, ServletResponse response)

    {

    try{PrintWriter out=response.getWriter();

    String name=request.getParameter("name");

    String phone=request.getParameter("phone");out.println("");

    out.pr intln("");out.pr intln("Hello" +name);out.println("

    ");

    out.pr intln("Your phone number is:" +phone);out.println("");

    }catch(Exception e){}

    }

    }

    /*html Program*/

    Enter your name:

    Enter your Telphone no:

  • 7/28/2019 54240326

    25/28

    Downloaded from www.pencilji.com

    Downloaded from www.pencilji.com

    Output:

    C:\JDK1.3\BIN>javac Test Servlet.java

    /*Then a TestServlet.class file is created. Move this to the path below*/

    C:\Apache group\Tomcat 4.1\web apps\Web Inf \ classes \TestServlet.class/*similarly move the prg11.html file to the path below*/

    C:\Apache group\Tomcat4.1 \ web apps \ root \ prg11.html/* Now open the web browser*/

    http://127.0.0.1:8080/prg11.html

    Enter your name:

    Enter your Telephone no:

    submit clear

    http://127.0.0.1:8080/prg11.htmlhttp://127.0.0.1:8080/prg11.htmlhttp://127.0.0.1:8080/prg11.html
  • 7/28/2019 54240326

    26/28

    Downloaded from www.pencilji.com

    Downloaded from www.pencilji.com

    9. /*Create a HTML page to accept one of the three colors red,green, lue& to send this o a Servlet. Write a Java program to create Servlet to

    accept the color information sent by the client s HTML page & togenerate a response.*/

    /*Servlet Program*/import java.io.*;import javax.servlet.*;

    public class pgm12 extends GenericServlet{

    public void service(ServletRequest request,ServletResponse response) throws

    ServletException,IOException{

    PrintWriter out=response.getWriter();

    String color=request.getParameter("r1");out.println("");

    out.println("");

    out.println("The color you have choosen is : " +color);

    out.println("");

    }

    }

    Select any one color BLUE

    Green RED

  • 7/28/2019 54240326

    27/28

    Downloaded from www.pencilji.com

    Downloaded from www.pencilji.com

    Output:

    Select any one color

    BLUE Green RED

    Submit

    The color you have chosen is: BLUE

  • 7/28/2019 54240326

    28/28

    Downloaded from www.pencilji.com