SlideShare a Scribd company logo
Downloaded from www.pencilji.com
Downloaded from www.pencilji.com
1. Program for implementing a stack & to perform Push & Pop
operations
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(top<max)
{
System.out.println("Enter the element of the stack");
stk[top++]=Integer.parseInt(input.readLine());
}
else
System.out.println("Stack overflow");
}
catch(IOException e){}
}
public void delete()
{
if(top>0)
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<top;i++)
System.out.println(" elements are"+stk[i]);
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);
}
}
}
}
Downloaded from www.pencilji.com
Downloaded from www.pencilji.com
Output:
C:cd jdk1.3bin>javac Stack.java
C:JDK1.3BIN>java Stack
Enter the size of the stack
2
1:PUSH 2:POP 3:DISPLAY 4:EXIT
Enter ur choice
1
Enter the element of the stack
23
1:PUSH 2:POP 3:DISPLAY 4:EXIT
Enter ur choice
1
45
1:PUSH 2:POP 3:DISPLAY 4:EXIT
Enter ur choice
3
elements are 23
elements are 45
1:PUSH 2:POP 3:DISPLAY 4:EXIT
Enter ur choice
2
Deleted element is 45
1:PUSH 2:POP 3:DISPLAY 4:EXIT
Enter ur choice
4
C:JDK1.3BIN>
Downloaded from www.pencilji.com
Downloaded from www.pencilji.com
2. Program to implement the following operations on a Queue
Add an element
Delete an element
Display 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<=pos-1;j++)
a[j]=a[j+1];
pos--;
n--;
}
else
if(choice.equals("3"))
{
if(n==0)
System.out.println("QUEUE EMPTY");
else
for(j=0;j<pos;j++)
System.out.println("Elements are:t" +a[j]);
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");
}
}
}
}
Downloaded from www.pencilji.com
Downloaded from www.pencilji.com
Output:
C:JDK1.3BIN>java Queue1
1.INSERTION
2.DELETION
3.DISPLAY
4.EXIT
Enter your choice
1
Enter the element
2
1.INSERTION
2.DELETION
3.DISPLAY
4.EXIT
Enter your choice
1
Enter the element
5
1.INSERTION
2.DELETION
3.DISPLAY
4.EXIT
Enter your choice
3
Elements are: 2
Elements are:5
1.INSERTION
2.DELETION
3.DISPLAY
4.EXIT
Enter your choice
2
1.INSERTION
2.DELETION
3.DISPLAY
4.EXIT
Enter your choice
3
Elements are: 5
1.INSERTION
2.DELETION
3.DISPLAY
4.EXIT
Enter your choice
4
Downloaded from www.pencilji.com
Downloaded from www.pencilji.com
3. Program to implement the following operations on a singly linked list
Create a List
Add a Node to the Front of the List
Add a Node to the Back of the List
Delete a Specified Node
Display 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<size;i++)
{
System.out.println("Enter the node item:");
input=br.readLine();
l1.add(input);
}
}
public void last()throws IOException
{
System.out.println("Enter the item to be inserted at the last:");
input=br.readLine();
l1.addLast(input);
}
public void begin()throws IOException
{
System.out.println("Enter the item to be inserted at the begining:");
input=br.readLine();
l1.addFirst(input);
}
public void delete()throws IOException
{
int pos;
System.out.println("Enter the position:");
input=br.readLine();
pos=Integer.parseInt(input);
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 Choice");
}
Downloaded from www.pencilji.com
Downloaded from www.pencilji.com
}
}
}
Output:
Downloaded from www.pencilji.com
Downloaded from www.pencilji.com
C:JDK1.3BIN>java Link14
1.CREATE LINKED LIST
2.ADD FIRST
3.ADD LAST
4.REMOVE
5.DISPLAY
6.EXIT
Enter your choice:
1
Enter the size:
2
Enter the node item:
4
Enter the node item:
-2
1.CREATE LINKED LIST
2.ADD FIRST
3.ADD LAST
4.REMOVE
5.DISPLAY
6.EXIT
Enter your choice:
2
Enter the item to be inserted at the beginning:
90
1.CREATE LINKED LIST
2.ADD FIRST
3.ADD LAST
4.REMOVE
5.DISPLAY
6.EXIT
Enter your choice:
5
Elements in the list:
[90, 4, -2]
1.CREATE LINKED LIST
2.ADD FIRST
3.ADD LAST
4.REMOVE
5.DISPLAY
6.EXIT
Enter your choice:
3
Enter the item to be inserted at the last:
10
Downloaded from www.pencilji.com
Downloaded from www.pencilji.com
1.CREATE LINKED LIST
2.ADD FIRST
3.ADD LAST
4.REMOVE
5.DISPLAY
6.EXIT
Enter your choice:
5
Elements in the list:
[90, 4, -2, 10]
1.CREATE LINKED LIST
2.ADD FIRST
3.ADD LAST
4.REMOVE
5.DISPLAY
6.EXIT
Enter your choice:
4
Enter the position:
2
1.CREATE LINKED LIST
2.ADD FIRST
3.ADD LAST
4.REMOVE
5.DISPLAY
6.EXIT
Enter your choice:
5
Elements in the list:
[90, 4, 10]
1.CREATE LINKED LIST
2.ADD FIRST
3 ADD LAST
4.REMOVE
5.DISPLAY
6.EXIT
Enter your choice:
6
C:JDK1.3BIN>
Downloaded from www.pencilji.com
Downloaded from www.pencilji.com
4. Program to implement a Producer and Consumer Problem using
Threads
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)
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<26)
{
q.put(i++);
}
}
}
class Consumer implements Runnable
{
Q q;Thread t;
Consumer (Q q)
{
this.q=q;
t=new Thread(this, "Consumer");
t.start();
}
public void run()
{
int i=0;
while(i < 26)
{
q.get();
}
}
}
class PC
{
public static void main(String args[])
{
Q q=new Q();
new Producer(q);
new Consumer(q);
System.out.println("press ctrl+c to exit");
}
}
Downloaded from www.pencilji.com
Downloaded from www.pencilji.com
Output:
C:JDK1.3BIN>javac PC.java
C:JDK1.3BIN>java PC
put 0
get0
put 1
get1
put 2
get2
put 3
get3
|
|
put 24
get24
put 25
get25
press ctrl+c to exit
C:JDK1.3BIN>
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<0)
{
x=300;
}
repaint();
Thread.sleep(100);
}
} catch(InterruptedException e1){}
}
public void paint(Graphics g)
{
for(int i=0;i<10;i++)
g.drawString(str,x,y);
}
}
AB.html
<applet code=AB width=300 height=100>
</applet>
Downloaded from www.pencilji.com
Downloaded from www.pencilji.com
Output:
C:JDK1.3BIN>javac AB.java
C:JDK1.3BIN>appletviewer AB.html
Applet Viewer:AB
Applet
WELCOME TO RNSIT
Applet started.
Downloaded from www.pencilji.com
Downloaded from www.pencilji.com
6. Develop a Java program for a Client and Server Program to do the
following:
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 machine
String 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);
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
Downloaded from www.pencilji.com
Downloaded from www.pencilji.com
{
//get the file name from the client
String 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);
}
}
}
}
Downloaded from www.pencilji.com
Downloaded from www.pencilji.com
Output:
C:JDK1.3BIN>javac Client.java
C:JDK1.3BIN>javac Server.java
C:JDK1.3BIN>java Server 80
/* In a new prompt*/
C:JDK1.3BIN>java Client localhost 80
Local host 80
ENTER THE FILE NAME>
Server.java /*File Serever.java is displayed */
/*In the server prompt*/
Client requested file: Server.java
Downloaded from www.pencilji.com
Downloaded from www.pencilji.com
7. Program to implement the Simple Client/Server Application using
RM
/*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);
}
}
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();
}
}
Downloaded from www.pencilji.com
Downloaded from www.pencilji.com
Output:
C:JDK1.3BIN>javac TimeIntf.java
C:JDK1.3BIN>javac TimeServer.java
C:JDK1.3BIN>javac TimeClient.java
C:JDK1.3BIN>rmic TimeServer
C:JDK1.3BIN>start rmiregistry
/*minimize the rmiregistry window & the prompt window*/
/*Open a new prompt window*/
C:JDK1.3BIN>java TimeClient
/*Using ALT+TAB switch to the other prompt window*/
C:JDK1.3BIN>java TimeServer
Java TimeSerever
date & time 02:19:09
Hello this is to demonstrate RMI
/*Open the client prompt using ALT+TAB */
date & time 02:19:12
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
browser
2. To generate a response & send it to the client browser.
Create a HTML document to do the following:
1. To accept employee name & telephone number
2. 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("<html>");
out.println("<body bgcolor=pink>");
out.println("Hello" +name);
out.println("<br><br>");
out.println("Your phone number is:" +phone);
out.println("</body></html>");
}
catch(Exception e){}
}
}
/*html Program*/
<html>
<body>
<form action="/examples/servlet/TestServlet" method="post">
Enter your name:<input type="text" name="name"><br><br>
Enter your Telphone no:<input type="text" name="phone"><br><br>
<input type="submit"value="submit">
<input type="reset" value="clear">
</form>
</body>
</html>
Downloaded from www.pencilji.com
Downloaded from www.pencilji.com
Output:
C:JDK1.3BIN>javac Test Servlet.java
/*Then a TestServlet.class file is created. Move this to the path below*/
C:Apache groupTomcat 4.1web appsWeb Inf  classes TestServlet.class
/*similarly move the prg11.html file to the path below*/
C:Apache groupTomcat4.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
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 & to
generate 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("<html>");
out.println("<body>");
out.println("<h1>The color you have choosen is :</h1> " +color);
out.println("</body></html>");
}
}
<!_HTML program_>
<html>
<body>
<form action="examples/servlet/pgm12" method="post">
<center>
<h3> Select any one color</h3>
<input type ="radio" name="r1" value="blue"> BLUE
<input type ="radio" name="r1" value="green"> Green
<input type ="radio" name="r1" value="red"> RED
<br><br>
<input type ="submit" value ="Submit">
</center>
</form>
</body>
</html>
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
Downloaded from www.pencilji.com
Downloaded from www.pencilji.com

More Related Content

DOC
Final JAVA Practical of BCA SEM-5.
PDF
DCN Practical
ODT
Java practical
PDF
Java practical(baca sem v)
PDF
Core java pract_sem iii
DOCX
Java practical
PDF
Java programs
PDF
Important java programs(collection+file)
Final JAVA Practical of BCA SEM-5.
DCN Practical
Java practical
Java practical(baca sem v)
Core java pract_sem iii
Java practical
Java programs
Important java programs(collection+file)

What's hot (18)

DOCX
Code red SUM
PDF
DOCX
Programs of java
PDF
DOCX
C# labprograms
PDF
C# Advanced L04-Threading
ODP
(1) c sharp introduction_basics_dot_net
DOCX
201913046 wahyu septiansyah network programing
PDF
Internet Technology (Practical Questions Paper) [CBSGS - 75:25 Pattern] {Mast...
DOCX
Java PRACTICAL file
PPTX
Next Generation Developer Testing: Parameterized Testing
DOCX
JavaExamples
PDF
delegates
DOCX
C# console programms
PPT
Thread
PPTX
Java practice programs for beginners
PDF
Integration Project Inspection 3
PDF
Server1
Code red SUM
Programs of java
C# labprograms
C# Advanced L04-Threading
(1) c sharp introduction_basics_dot_net
201913046 wahyu septiansyah network programing
Internet Technology (Practical Questions Paper) [CBSGS - 75:25 Pattern] {Mast...
Java PRACTICAL file
Next Generation Developer Testing: Parameterized Testing
JavaExamples
delegates
C# console programms
Thread
Java practice programs for beginners
Integration Project Inspection 3
Server1
Ad

Similar to 54240326 copy (20)

PDF
Lab02kdfshdfgajhdfgajhdfgajhdfgjhadgfasjhdgfjhasdgfjh.pdf
PDF
CS3381 OBJECT ORIENTED PROGRAMMINGLABS_1.pdf
DOCX
cs3381-object oriented programming-ab-manual
PDF
Create a new java class called ListNode. Implement ListNode as a gen.pdf
PPT
05-stack_queue.ppt
DOCX
object oriented programming lab manual .docx
PDF
Introduction data structure
PDF
STAGE 2 The Methods 65 points Implement all the methods t.pdf
PDF
Write a Java Class to Implement a Generic Linked ListYour list mus.pdf
PDF
02 stackqueue
PPT
1 list datastructures
PDF
Using NetBeansImplement a queue named QueueLL using a Linked List .pdf
PPTX
CS8391-Data Structures Unit 2
PPTX
DS- Stack ADT
PPT
Lecture9_StackQueuepresentationaaaaa.ppt
PPT
StackQueue.ppt
PPT
Lecture9_StackQueue.ppt
PPT
Lecture9_StackQueue operation on stacks .ppt
PPT
23 stacks-queues-deques
Lab02kdfshdfgajhdfgajhdfgajhdfgjhadgfasjhdgfjhasdgfjh.pdf
CS3381 OBJECT ORIENTED PROGRAMMINGLABS_1.pdf
cs3381-object oriented programming-ab-manual
Create a new java class called ListNode. Implement ListNode as a gen.pdf
05-stack_queue.ppt
object oriented programming lab manual .docx
Introduction data structure
STAGE 2 The Methods 65 points Implement all the methods t.pdf
Write a Java Class to Implement a Generic Linked ListYour list mus.pdf
02 stackqueue
1 list datastructures
Using NetBeansImplement a queue named QueueLL using a Linked List .pdf
CS8391-Data Structures Unit 2
DS- Stack ADT
Lecture9_StackQueuepresentationaaaaa.ppt
StackQueue.ppt
Lecture9_StackQueue.ppt
Lecture9_StackQueue operation on stacks .ppt
23 stacks-queues-deques
Ad

54240326 copy

  • 1. Downloaded from www.pencilji.com Downloaded from www.pencilji.com 1. Program for implementing a stack & to perform Push & Pop operations 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(top<max) { System.out.println("Enter the element of the stack"); stk[top++]=Integer.parseInt(input.readLine()); } else System.out.println("Stack overflow"); } catch(IOException e){} } public void delete() { if(top>0) 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<top;i++) System.out.println(" elements are"+stk[i]);
  • 2. 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); } } } }
  • 3. Downloaded from www.pencilji.com Downloaded from www.pencilji.com Output: C:cd jdk1.3bin>javac Stack.java C:JDK1.3BIN>java Stack Enter the size of the stack 2 1:PUSH 2:POP 3:DISPLAY 4:EXIT Enter ur choice 1 Enter the element of the stack 23 1:PUSH 2:POP 3:DISPLAY 4:EXIT Enter ur choice 1 45 1:PUSH 2:POP 3:DISPLAY 4:EXIT Enter ur choice 3 elements are 23 elements are 45 1:PUSH 2:POP 3:DISPLAY 4:EXIT Enter ur choice 2 Deleted element is 45 1:PUSH 2:POP 3:DISPLAY 4:EXIT Enter ur choice 4 C:JDK1.3BIN>
  • 4. Downloaded from www.pencilji.com Downloaded from www.pencilji.com 2. Program to implement the following operations on a Queue Add an element Delete an element Display 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<=pos-1;j++) a[j]=a[j+1]; pos--; n--; } else if(choice.equals("3")) { if(n==0) System.out.println("QUEUE EMPTY"); else for(j=0;j<pos;j++) System.out.println("Elements are:t" +a[j]);
  • 5. 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"); } } } }
  • 6. Downloaded from www.pencilji.com Downloaded from www.pencilji.com Output: C:JDK1.3BIN>java Queue1 1.INSERTION 2.DELETION 3.DISPLAY 4.EXIT Enter your choice 1 Enter the element 2 1.INSERTION 2.DELETION 3.DISPLAY 4.EXIT Enter your choice 1 Enter the element 5 1.INSERTION 2.DELETION 3.DISPLAY 4.EXIT Enter your choice 3 Elements are: 2 Elements are:5 1.INSERTION 2.DELETION 3.DISPLAY 4.EXIT Enter your choice 2 1.INSERTION 2.DELETION 3.DISPLAY 4.EXIT Enter your choice 3 Elements are: 5 1.INSERTION 2.DELETION 3.DISPLAY 4.EXIT Enter your choice 4
  • 7. Downloaded from www.pencilji.com Downloaded from www.pencilji.com 3. Program to implement the following operations on a singly linked list Create a List Add a Node to the Front of the List Add a Node to the Back of the List Delete a Specified Node Display 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<size;i++) { System.out.println("Enter the node item:"); input=br.readLine(); l1.add(input); } } public void last()throws IOException { System.out.println("Enter the item to be inserted at the last:"); input=br.readLine(); l1.addLast(input); } public void begin()throws IOException { System.out.println("Enter the item to be inserted at the begining:"); input=br.readLine(); l1.addFirst(input); } public void delete()throws IOException { int pos; System.out.println("Enter the position:"); input=br.readLine(); pos=Integer.parseInt(input);
  • 8. 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 Choice"); }
  • 9. Downloaded from www.pencilji.com Downloaded from www.pencilji.com } } } Output:
  • 10. Downloaded from www.pencilji.com Downloaded from www.pencilji.com C:JDK1.3BIN>java Link14 1.CREATE LINKED LIST 2.ADD FIRST 3.ADD LAST 4.REMOVE 5.DISPLAY 6.EXIT Enter your choice: 1 Enter the size: 2 Enter the node item: 4 Enter the node item: -2 1.CREATE LINKED LIST 2.ADD FIRST 3.ADD LAST 4.REMOVE 5.DISPLAY 6.EXIT Enter your choice: 2 Enter the item to be inserted at the beginning: 90 1.CREATE LINKED LIST 2.ADD FIRST 3.ADD LAST 4.REMOVE 5.DISPLAY 6.EXIT Enter your choice: 5 Elements in the list: [90, 4, -2] 1.CREATE LINKED LIST 2.ADD FIRST 3.ADD LAST 4.REMOVE 5.DISPLAY 6.EXIT Enter your choice: 3 Enter the item to be inserted at the last: 10
  • 11. Downloaded from www.pencilji.com Downloaded from www.pencilji.com 1.CREATE LINKED LIST 2.ADD FIRST 3.ADD LAST 4.REMOVE 5.DISPLAY 6.EXIT Enter your choice: 5 Elements in the list: [90, 4, -2, 10] 1.CREATE LINKED LIST 2.ADD FIRST 3.ADD LAST 4.REMOVE 5.DISPLAY 6.EXIT Enter your choice: 4 Enter the position: 2 1.CREATE LINKED LIST 2.ADD FIRST 3.ADD LAST 4.REMOVE 5.DISPLAY 6.EXIT Enter your choice: 5 Elements in the list: [90, 4, 10] 1.CREATE LINKED LIST 2.ADD FIRST 3 ADD LAST 4.REMOVE 5.DISPLAY 6.EXIT Enter your choice: 6 C:JDK1.3BIN>
  • 12. Downloaded from www.pencilji.com Downloaded from www.pencilji.com 4. Program to implement a Producer and Consumer Problem using Threads 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)
  • 13. 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<26) { q.put(i++); } } } class Consumer implements Runnable { Q q;Thread t; Consumer (Q q) { this.q=q; t=new Thread(this, "Consumer"); t.start(); } public void run() { int i=0; while(i < 26) { q.get(); } } } class PC { public static void main(String args[]) { Q q=new Q(); new Producer(q); new Consumer(q); System.out.println("press ctrl+c to exit"); } }
  • 14. Downloaded from www.pencilji.com Downloaded from www.pencilji.com Output: C:JDK1.3BIN>javac PC.java C:JDK1.3BIN>java PC put 0 get0 put 1 get1 put 2 get2 put 3 get3 | | put 24 get24 put 25 get25 press ctrl+c to exit C:JDK1.3BIN>
  • 15. 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<0) { x=300; } repaint(); Thread.sleep(100); } } catch(InterruptedException e1){} } public void paint(Graphics g) { for(int i=0;i<10;i++) g.drawString(str,x,y); } } AB.html <applet code=AB width=300 height=100> </applet>
  • 16. Downloaded from www.pencilji.com Downloaded from www.pencilji.com Output: C:JDK1.3BIN>javac AB.java C:JDK1.3BIN>appletviewer AB.html Applet Viewer:AB Applet WELCOME TO RNSIT Applet started.
  • 17. Downloaded from www.pencilji.com Downloaded from www.pencilji.com 6. Develop a Java program for a Client and Server Program to do the following: 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 machine String 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);
  • 18. 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
  • 19. Downloaded from www.pencilji.com Downloaded from www.pencilji.com { //get the file name from the client String 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); } } } }
  • 20. Downloaded from www.pencilji.com Downloaded from www.pencilji.com Output: C:JDK1.3BIN>javac Client.java C:JDK1.3BIN>javac Server.java C:JDK1.3BIN>java Server 80 /* In a new prompt*/ C:JDK1.3BIN>java Client localhost 80 Local host 80 ENTER THE FILE NAME> Server.java /*File Serever.java is displayed */ /*In the server prompt*/ Client requested file: Server.java
  • 21. Downloaded from www.pencilji.com Downloaded from www.pencilji.com 7. Program to implement the Simple Client/Server Application using RM /*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); } }
  • 22. 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(); } }
  • 23. Downloaded from www.pencilji.com Downloaded from www.pencilji.com Output: C:JDK1.3BIN>javac TimeIntf.java C:JDK1.3BIN>javac TimeServer.java C:JDK1.3BIN>javac TimeClient.java C:JDK1.3BIN>rmic TimeServer C:JDK1.3BIN>start rmiregistry /*minimize the rmiregistry window & the prompt window*/ /*Open a new prompt window*/ C:JDK1.3BIN>java TimeClient /*Using ALT+TAB switch to the other prompt window*/ C:JDK1.3BIN>java TimeServer Java TimeSerever date & time 02:19:09 Hello this is to demonstrate RMI /*Open the client prompt using ALT+TAB */ date & time 02:19:12
  • 24. 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 browser 2. To generate a response & send it to the client browser. Create a HTML document to do the following: 1. To accept employee name & telephone number 2. 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("<html>"); out.println("<body bgcolor=pink>"); out.println("Hello" +name); out.println("<br><br>"); out.println("Your phone number is:" +phone); out.println("</body></html>"); } catch(Exception e){} } } /*html Program*/ <html> <body> <form action="/examples/servlet/TestServlet" method="post"> Enter your name:<input type="text" name="name"><br><br> Enter your Telphone no:<input type="text" name="phone"><br><br> <input type="submit"value="submit"> <input type="reset" value="clear"> </form> </body> </html>
  • 25. Downloaded from www.pencilji.com Downloaded from www.pencilji.com Output: C:JDK1.3BIN>javac Test Servlet.java /*Then a TestServlet.class file is created. Move this to the path below*/ C:Apache groupTomcat 4.1web appsWeb Inf classes TestServlet.class /*similarly move the prg11.html file to the path below*/ C:Apache groupTomcat4.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
  • 26. 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 & to generate 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("<html>"); out.println("<body>"); out.println("<h1>The color you have choosen is :</h1> " +color); out.println("</body></html>"); } } <!_HTML program_> <html> <body> <form action="examples/servlet/pgm12" method="post"> <center> <h3> Select any one color</h3> <input type ="radio" name="r1" value="blue"> BLUE <input type ="radio" name="r1" value="green"> Green <input type ="radio" name="r1" value="red"> RED <br><br> <input type ="submit" value ="Submit"> </center> </form> </body> </html>
  • 27. 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