0% found this document useful (0 votes)
181 views14 pages

VTU Java Lab Manual for IV Sem

Prog1a.java class overload public overload(int a) System.out.println("this is a one parameter constructor with integer a="+a);. Prog1b.java class outer int a=12; private int b=12.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
181 views14 pages

VTU Java Lab Manual for IV Sem

Prog1a.java class overload public overload(int a) System.out.println("this is a one parameter constructor with integer a="+a);. Prog1b.java class outer int a=12; private int b=12.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd

IV SEM JAVA LAB MANUAL (MCA, VTU)

1a) [Link] [Link](23134.5343f);


class overload { }
public overload() { }
[Link]("this is a zero Output:
parameter constructor"); this is a zero parameter constructor
} this is a one parameter constructor
public overload(int a) { with integer a=12
[Link]("this is a one this is a one parameter constructor
parameter constructor with integer with Floating b=1.2546546E7
a="+a); this is a one parameter constructor
} with Floating b=1256.2343
public overload(float b) { this is a zero parameter method
[Link]("this is a one this is a one parameter method with
parameter constructor with Floating integer a=231
b="+b); this is a one parameter method with
} floating b=1.2546546E7
public void methodoverload() { this is a one parameter method with
[Link]("this is a zero floating b=23134.535
parameter method");
} 1b) [Link]
public void methodoverload(int a) { class outer
[Link]("this is a one {
parameter method with integer int a=12;
a="+a); private int b=21;
} protected int c=442;
public void methodoverload( float b) public int d=1111;
{ class inner
[Link]("this is a one {
parameter method with floating int p=150;
b="+b); private int q=250;
} protected int r=380;
} public int s=450;
public class prog1a void inmeth()
{ {
public static void main(String arg[]) [Link]("outer class default
{ member in inner class a="+a);
overload o1=new overload(); [Link]("outer class private
overload o2=new overload(12); member in inner class b="+b);
overload o3=new [Link]("outer class
overload(12546546l); protected member in inner class
overload o4=new c="+c);
overload(1256.23421f); [Link]("outer class public
[Link](); member in inner class d="+d);
[Link](231); }
[Link](12546546l); } // end of inner class
IV SEM JAVA LAB MANUAL (MCA, VTU)

void outmeth() 2a) [Link]


{ class A
inner i=new inner(); {
[Link](); A()
[Link](); {
[Link]("inner class default [Link]("This is a
member in outer class p="+i.p); constructor inside class A");
[Link]("inner class private }
member in outer class q="+i.q); int var=10;
[Link]("inner class void meth()
protected member in outer class {
r="+i.r); [Link]("this is class A
[Link]("inner class public variable: "+var);
member in outer class s="+i.s); }
} } // end of class A
} class B extends A
public class prog1b {
{ int var=20;
public static void main(String arg[]) B(int a)
{ {
outer o=new outer(); var=a;
[Link](); [Link]("This is a
} constructor inside class B");
} }
Output: void meth()
outer class default member in inner {
class a=12 [Link]("this is class B
outer class private member in inner variable:"+var);
class b=21 [Link]("This is class A
outer class protected member in inner variable: Inside B class:"+[Link]);
class c=442 [Link]();
outer class public member in inner }
class d=1111 } //end of class B
inner class default member in outer class C extends B
class p=150 {
inner class private member in outer C(float f)
class q=250 {
inner class protected member in outer super(150);
class r=380
inner class public member in outer [Link]("Inside C
class s=450 constructor:"+f);
}
void meth()
{
[Link]();
IV SEM JAVA LAB MANUAL (MCA, VTU)

} throw new newexception();


} }
class prog2a }
{ }
public static void main(String class prog2b
arg[]) {
{ public static void main(String arg[])
C c=new C(125.45f); {
[Link](); try
} {
} throwexception t=new
Output: throwexception();
This is a constructor inside class A [Link]();
This is a constructor inside class B }
Inside C constructor:125.45 catch(newexception ne)
this is class B variable:150 {
This is class A variable: Inside B [Link]("ERROR:"+ne);
class:10 }
this is class A variable: 10 finally
{
2b) [Link] [Link]("THIS IS EXECUTED
class newexception extends Exception FINALLY");
{ }
public String toString() } // end of main()
{ }
return("this is my Output:
exception"); ERROR: DIVIDED BY ZERO ERROR
} ERROR:this is my exception
} THIS IS EXECUTED FINALLY
class throwexception
{ 3a) [Link]
int a,b=21; interface myinterface
void meth() throws {
newexception int a=125;
{ float b=399.5f;
try double meth();
{ }
interface newinterface extends
[Link](b/a); myinterface
} {
void meth2();
catch(ArithmeticException ae) }
{ class interfaceclass implements
[Link]("ERROR: DIVIDED newinterface
BY ZERO ERROR"); {
IV SEM JAVA LAB MANUAL (MCA, VTU)

public double meth() [Link]("THIS THREAD


{ PRIORITY IS : "+getPriority());
return(a*b); start();
} }
public void meth2() public void run()
{ {
[Link]("THE ADDITION OF try
THE GIVEN NUMBER : "+(a+b)); {
} for(int i=1;i<=5;i++)
} {
class prog3a [Link]("CHILD:
{ "+getName()+" " +i);
public static void main(String sleep(500);
arg[]) }
{ }
interfaceclass i=new catch(InterruptedException ie)
interfaceclass(); {
i.meth2();

[Link]("PRODUCT [Link]("EXCEPTION : "+ie);


OF THE GIVEN NUMBER : "+[Link]()); }
} }
} }
Output: class prog3b
THE ADDITION OF THE GIVEN NUMBER {
: 524.5 public static void main(String
PRODUCT OF THE GIVEN NUMBER : arg[])
49937.5 {
childthread t1=new
3b) [Link] childthread("Thread-1");
class childthread extends Thread childthread t2=new
{ childthread("Thread-2");
childthread(String a) childthread t3=new
{ childthread("Thread-3");
super(a); Thread
[Link]("THREAD t=[Link]();
NAME IS : "+getName()); [Link]("MAIN
if(getName().equals("Thread-1")) THREAD: "+t);
setPriority(6); try
if(getName().equals("Thread-2")) {
setPriority(8); for(int i=1;i<=5;i+
if(getName().equals("Thread-3")) +)
setPriority(7); {

[Link]([Link]()
+" " +i);
IV SEM JAVA LAB MANUAL (MCA, VTU)

public class BanApplet extends Applet


[Link](1000); implements Runnable
} {
} Thread t=null;
String m;
catch(InterruptedException e) boolean f=false;
{ public void start()
{
[Link]("Exception: t=new Thread(this);
"+e); m=" BIKOO CHANGMAI
} ";
} [Link]();
} }
Output: public void run()
THREAD NAME IS : Thread-1 {
THIS THREAD PRIORITY IS : 6 for(;;)
THREAD NAME IS : Thread-2 {
CHILD: Thread-1 1 try
THIS THREAD PRIORITY IS : 8 {
THREAD NAME IS : Thread-3 repaint();
CHILD: Thread-2 1 [Link](100);
THIS THREAD PRIORITY IS : 7 char ch;
CHILD: Thread-3 1 ch=[Link](0);
MAIN THREAD: Thread[main,5,main]
main 1 m=[Link](1,[Link]());
CHILD: Thread-1 2 m+=ch;
CHILD: Thread-2 2 if(f)
CHILD: Thread-3 2 break;
CHILD: Thread-1 3 }
CHILD: Thread-2 3 catch(InterruptedException
main 2 ie)
CHILD: Thread-3 3 {
CHILD: Thread-1 4 [Link]("Error: "+ie);
CHILD: Thread-2 4 }
CHILD: Thread-3 4 }
CHILD: Thread-1 5 }
CHILD: Thread-2 5 public void stop()
main 3 {
CHILD: Thread-3 5 f=true;
main 4 }
main 5 public void paint(Graphics g)
{
4a) [Link] [Link](m,150,200);
import [Link].*; }
import [Link].*; } //end of class
IV SEM JAVA LAB MANUAL (MCA, VTU)

4b) [Link] try


import [Link].*; {
import [Link].*; [Link]("[Link]
public class parameterApplet extends cDriver");
Applet con=[Link](url
{ );
String name,num; stat=[Link]();
public void start() }
{ catch(ClassNotFoundException c)
name=getParameter("name"); {
num=getParameter("num"); [Link]("class not found");
} [Link](0);
public void paint(Graphics g) }
{ catch(SQLException e)
try {
{ [Link](e);
[Link](name,150,100); [Link](0);
[Link](num,150,120); }
} //menu frame
catch(NumberFormatException ne) menu=new JFrame("Menu");
{ JButton add=new JButton("ADD");
[Link](ne); JButton query=new JButton("Query");
} JButton exit=new JButton("Exit");
} //insert frame
} insert=new JFrame("Insert");
JLabel u=new JLabel("USN");
5) [Link] JLabel n=new JLabel("Name");
import [Link].*; JLabel s=new JLabel("Sem");
import [Link].*; JLabel sub1=new JLabel("SUB1");
import [Link].*; JLabel sub2=new JLabel("SUB2");
import [Link].*; JLabel sub3=new JLabel("SUB3");
public class db usn=new JTextField(15);
{ name=new JTextField(15);
Connection con; sem=new JTextField(15);
Statement stat; s1=new JTextField(15);
ResultSet result; s2=new JTextField(15);
JTextField usn,name, s3=new JTextField(15);
sem,s1,s2,s3,qsem,p1,p2; JButton iok=new JButton("OK");
JFrame menu,insert,fquery; //query frame
JTable tab; fquery=new JFrame("Query");
JScrollPane sp; JLabel semister=new
JFrame table; JLabel("Semister");
public db() JLabel per=new JLabel("Percentage");
{ qsem=new JTextField(15);
String url="jdbc:odbc:mca"; p1=new JTextField(15);
p2=new JTextField(15);
IV SEM JAVA LAB MANUAL (MCA, VTU)

JButton qok=new JButton("OK"); {


//adding objects to menu frame public void
[Link](300,80); actionPerformed(ActionEvent e)
[Link](new FlowLayout()); {
[Link](JFram [Link](true);
e.EXIT_ON_CLOSE); }
[Link](add); });
[Link](query); [Link](new
[Link](exit); ActionListener()
//adding objects to insert frame {
[Link](230,220); public void
[Link](new FlowLayout()); actionPerformed(ActionEvent e)
[Link](u); {
[Link](usn); try
[Link](n); {
[Link](name); [Link]();
[Link](s); [Link]();
[Link](sem); }
[Link](sub1); catch(SQLException sq)
[Link](s1); {
[Link](sub2); [Link](sq);
[Link](s2); }[Link](0);
[Link](sub3); }
[Link](s3); });
[Link](iok); [Link](new
//adding objects to query frame ActionListener()
[Link](270,120); {
[Link](new FlowLayout()); public void
[Link](semister); actionPerformed(ActionEvent e)
[Link](qsem); {
[Link](per); int tot,a,b,c;
[Link](p1); float per;
[Link](p2); String q="",res;
[Link](qok); try
[Link](true); {
[Link](new a=[Link]([Link]());
ActionListener() b=[Link]([Link]());
{ c=[Link]([Link]());
public void tot=a+b+c;
actionPerformed(ActionEvent e) per=(float)tot/3;
{ res=(per<40)?"Fail":(per<50)?"Pass":
[Link](true); (per<60)?"Second":
} (per<70)?"First":"Distinction";
}); q="INSERT INTO student
[Link](new VALUES("+[Link]()
ActionListener() +",'"+[Link]()
IV SEM JAVA LAB MANUAL (MCA, VTU)

+"','"+[Link]() ))+" AND per BETWEEN


+"',"+a+","+b+","+c+","+tot+","+per+",'" "+[Link]([Link]())+"
+res+"')"; AND "+[Link]([Link]());
[Link](q); result=[Link](q);
[Link](""); flag=[Link]();
[Link](""); if(!flag)
[Link](""); {
[Link](""); d[0][0]="no data found";
[Link](""); }
[Link](""); else
} {
catch(NumberFormatException ne) do
{ {
[Link]("Error in input"); d[i]
} [0]=[Link]([Link](1));
catch(SQLException se) d[i][1]=[Link](2);
{ d[i]
[Link](q); [2]=[Link]([Link](3));
[Link]("Error in d[i]
Query"+se); [3]=[Link]([Link](4));
} d[i]
[Link](false); [4]=[Link]([Link](5));
} d[i]
}); [5]=[Link]([Link](6));
[Link](new d[i]
ActionListener() [6]=[Link]([Link](7));
{ d[i]
public void [7]=[Link]([Link](8));
actionPerformed(ActionEvent e)
{ d[i]
[Link](false); [8]=[Link](9);
table=new JFrame("Student i++;
Information"); [Link](d[i][1]);
[Link](800,200); }while([Link]());
Object data[][]; }
String d[][]=new String[50][9],q=""; data= (Object[][]) d;
String head[] = tab=new JTable(data,head);
{"USN","NAME","SEMISTER","SUB1","S sp=new JScrollPane(tab);
UB2","SUB3","TOTAL","PERCENTAGE", [Link](sp);
"RESULT"}; [Link](true);
int i=0; [Link]("");
boolean flag=false; [Link]("");
try [Link]("");
{ }
q="SELECT * FROM student WHERE catch(SQLException se)
sem="+[Link]([Link]( {
IV SEM JAVA LAB MANUAL (MCA, VTU)

[Link](q); [Link]("[Link]");
[Link]("Error in sin=[Link]();
query"+se); dis=new DataInputStream(sin);
} str=[Link]();
catch(NumberFormatException ne) while(![Link]("end"))
{ {

[Link]("Error "+ne); [Link](str);


} str=[Link]();
} }
}); [Link]();
} [Link]();
public static void main(String }catch(Exception e)
arg[]) {
{ [Link]("Client Error " +e);
[Link](new }
Runnable() }
{ }
public void run() [Link]
{ import [Link].*;
new db(); import [Link].*;
} public class server
}); {
} public static void main(String arg[]){
} ServerSocket ss=null;
Socket s=null;
6) [Link] String str;
import [Link].*; InputStream sin;
import [Link].*; FileReader fis;
public class client BufferedReader br;
{ DataInputStream dis;
public static void main(String arg[]) OutputStream sout;
{ DataOutputStream sdos;
int c; try
Socket s; {
InputStream sin; ss=new ServerSocket(8080);
DataInputStream dis; [Link]("server started");
OutputStream sout; }
DataOutputStream sdos; catch(Exception e)
String str; {
try [Link]("server error");
{ }
s=new Socket("localhost",8080); while(true)
sout=[Link](); {
sdos=new DataOutputStream(sout); try
IV SEM JAVA LAB MANUAL (MCA, VTU)

{ {
s=[Link](); static String data[]={"Remote " ,
sin=[Link](); "Method " , "Invocation " , "is " , "Greate
dis=new DataInputStream(sin); "};
String strf=new String([Link]()); public ServerRMI() throws
fis=new FileReader(strf); RemoteException
br=new BufferedReader(fis); {
sout=[Link](); super();
sdos=new DataOutputStream(sout); }
while((str=[Link]())!=null) public int getLength() throws
{ RemoteException
[Link](str); {
} return [Link];
[Link]("end"); }
[Link](); public String getData(int a)
[Link](); throws RemoteException
} {
catch(Exception e) return data[a];
{ }
[Link]("server connection public static void main(String
error:"+e); arg[])
[Link](0); {
}
} try
} {
} ServerRMI sr=new ServerRMI();
Output:
[Link]("ServerRMI",sr);
7)[Link]
import [Link].*; [Link]("I am
import [Link].*; Registered");
public interface RemoteInterface }
extends Remote catch(Exception e)
{ {
public int getLength() throws
RemoteException; [Link]("Server
public String getData(int a) Error "+e);
throws RemoteException; }
} }
[Link] }
import [Link].*; [Link]
import [Link].*; import [Link].*;
public class ServerRMI extends import [Link].*;
UnicastRemoteObject implements class ClientRMI
RemoteInterface {
IV SEM JAVA LAB MANUAL (MCA, VTU)

public static void main(String [Link]("<head>");


arg[]) [Link]("<title>User Name &
{ Password</title>");
try [Link]("</head>");
{ [Link]("<body>");
RemoteInterface [Link]("<h1>Welcome
ri=(RemoteInterface)[Link](" "+uname+"</h1>");
ServerRMI"); [Link]("<h1>Your Password is
int n=[Link](); "+psd+"</h1>");
for(int i=0;i<n;i++) [Link]("</body>");
[Link]([Link](i)); [Link]("</html>");
} [Link]();
catch(Exception e) }catch(Exception e)
{ {
PrintWriter pw =
[Link]("Clieng Error [Link]();
"+e); [Link]("Servlet Error");
} [Link]();
} }
} }
}
8) [Link] [Link]
import [Link].*; <!DOCTYPE HTML PUBLIC
import [Link].*; "-//W3C//DTD HTML 4.01
Transitional//EN">
public class DynamicServlet extends <html>
GenericServlet <head>
{ <title></title>
public void </head>
service(ServletRequest request, <body>
ServletResponse response) throws <form method="POST"
ServletException, IOException action="[Link]
{ es/servlet/DynamicServlet">
try <p>&nbsp;&nbsp;&nbsp; Enter your
{ Username and Password</p>
[Link]("text/ <p>&nbsp;</p>
html"); <p>&nbsp;&nbsp;&nbsp;&nbsp;
PrintWriter pw = &nbsp;&nbsp;&nbsp;
[Link](); Username&nbsp;&nbsp;&nbsp;
String uname= <input type="text"
[Link]("username"); name="username" size="20"></p>
String psd=
[Link]("password"); <p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp
[Link]("<html>"); ;&nbsp;&nbsp;
[Link]("<body>"); Password&nbsp;&nbsp;&nbsp;
IV SEM JAVA LAB MANUAL (MCA, VTU)

<input type="password" [Link]("</h3></pre>");


name="password" size="20"></p> }
<p>&nbsp;</p> [Link]();
[Link]();
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp [Link]();
;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&n }catch(Exception e)
bsp;&nbsp;&nbsp;&nbsp;&nbsp; {
<input type="submit" PrintWriter pw =
value="Submit" name="B1"><input [Link]();
type="reset" value="Reset" [Link]("Servlet Error");
name="B2"></p> [Link]();
</form> }
}
</body> }
</html> [Link]

9) [Link] <!DOCTYPE HTML PUBLIC


import [Link].*; "-//W3C//DTD HTML 4.01
import [Link].*; Transitional//EN">
import [Link].*; <html>
<head>
public class DownloadServlet extends <title></title>
GenericServlet </head>
{ <body>
public void <h1><a
service(ServletRequest request, href="[Link]
ServletResponse response) throws /servlet/DownloadServlet">Dynamic
ServletException, IOException Servlet</a></h1>
{ </body>
try </html>
{
FileReader fis = new 10b) [Link]
FileReader("D:\\ksp\\servlet\\Downl import [Link].*;
[Link]"); import [Link].*;
BufferedReader br=new import [Link].*;
BufferedReader(fis);
[Link]("text/ public class GetServlet extends
html"); HttpServlet
PrintWriter pw = {
[Link](); public void
String str; doGet(HttpServletRequest request,
while((str=[Link]())!=null) HttpServletResponse response)
{ throws ServletException, IOException
[Link]("<pre><h3>"); {
[Link](str);
IV SEM JAVA LAB MANUAL (MCA, VTU)

String </head>
name=[Link]("T2"); <body>
<form method="POST"
action="[Link]
[Link]("text/html") es/servlet/PostServlet">
; <p>&nbsp;</p>
PrintWriter <p><b><font size="4> This is for
pw=[Link](); Post Method</font></b></p>
[Link]("<h1>Your <p> Enter Your Name <input
name is </h1>"+name); type="text" name="T1"
[Link](); size="20"></p>
} <p> <input type="submit"
} value="Enter" name="B1"><input
type="reset" value="Clear"
[Link] name="B2"></p>
import [Link].*; </form>
import [Link].*; <form method="GET"
import [Link].*; action="[Link]
es/servlet/GetServlet">
public class PostServlet extends <p>&nbsp;</p>
HttpServlet <p> <b><font size="4">This is for
{ Get Method</font></b></p>
public void <p> Enter Your Name <input
doPost(HttpServletRequest request, type="text" name="T2"
HttpServletResponse response) size="20"></p>
throws ServletException, IOException <p><input type="submit"
{ value="Enter" name="B3"><input
String name= type="reset" value="Clear"
[Link]("T1"); name="B4"></p>
[Link]("text/ </form> </body> </html>
html");
PrintWriter pw=[Link](); 11)
[Link]("Your name is "+name); [Link]
[Link](); import [Link].*;
} import [Link].*;
} import [Link].*;
public class RedirectServlet extends
[Link] HttpServlet
<!DOCTYPE HTML PUBLIC {
"-//W3C//DTD HTML 4.01 public void
Transitional//EN"> service(HttpServletRequest request,
HttpServletResponse response)
<html> throws ServletException, IOException
<head> {
<title></title>
IV SEM JAVA LAB MANUAL (MCA, VTU)

13a)
[Link]("text/html") [Link]
; <html>
PrintWriter pw = <head><title> PRINTING 10 VEN AND
[Link](); ODD NUMBER</title></head>
[Link]("[Link] <body bgcolor="cyan">
st:8080/examples/servlets/DynamicS <font size="4">
[Link]"); <%
} int i;
} %>
<b><h1>FIRST 10 EVEN NUMBER
12) ARE:</h1></b>
[Link] <%
import [Link].*; for(i=0;i<20;i+=2)
import [Link].*; {
import [Link].*; %>
import [Link].*; <p><%=i%></p>
<%
public class SessionServlet extends ;}
HttpServlet %>
{ <b><h1>FIRST 10 ODD NUMBER
public void ARE:</h1></b>
doGet(HttpServletRequest request, <%
HttpServletResponse response) for(i=1;i<20;i+=2)
throws ServletException, IOException {
{ %>
HttpSession ses= <p><%=i%></p>
[Link](true); <%
[Link]("text/ ;}
html"); %>
PrintWriter </body>
pw=[Link](); </html>
[Link]("<b>");
Date
d=(Date)[Link]("date");
if(d!=null)
[Link]("Last access:
"+d+"<br>");
d=new Date();
[Link]("date",d);
[Link]("Current date :
"+d);
}
}

You might also like