Java Lab Manual
Java Lab Manual
Java Lab Manual
AIM:
ALGORITHM:
PROGRAM:
class Box
double width;
double height;
double depth;
classBoxDemo
Double vol;
mybox.width = 10;
mybox.height = 20;
mybox.depth = 15;
1
System.out.println("Volume is " + vol);
OUTPUT:
RESULT:
Thus, the required java program has been typed, executed and verified successfully.
2
“EXCEPTION HANDLING”
AIM:
ALGORITHM:
PROGRAM:
class Exc2
int d, a;
try
d = 0;
a = 42 / d;
catch (ArithmeticException e)
System.out.println("Division by zero.");
3
}
OUTPUT:
RESULT:
Thus, the required java program has been typed, executed and verified successfully.
4
“SINGLE INHERITANCE”
AIM:
ALGORITHM:
PROGRAM:
class employee
privateinteno;
eno = no;
ename = name;
5
{
privateintdno;
dno = no;
dname = name;
d.setemp(100,"aaaa");
d.setdept(20,"Sales");
d.putemp();
d.putdept();
6
OUTPUT:
RESULT:
Thus, the required java program has been typed, executed and verified successfully.
7
“MULTIPLE INHERITANCE – USING INTERFACE”
AIM:
ALGORITHM:
PROGRAM:
importjava.lang.*;
import java.io.*;
interface Exam
voidpercent_cal();
class Student
String name;
int roll_no,mark1,mark2;
name=n;
roll_no=r;
mark1=m1;
mark2=m2;
8
}
void display()
super(n,r,m1,m2);
int total=(mark1+mark2);
float percent=total*100/200;
void display()
super.display();
9
}
class Multiple
R.display();
R.percent_cal();
OUTPUT:
RESULT:
Thus, the required java program has been typed, executed and verified successfully.
10
“MULTI-LEVEL INHERITANCE”
AIM:
ALGORITHM:
PROGRAM:
importjava.lang.*;
import java.io.*;
class Account
String cust_name;
intacc_no;
Account(String a, int b)
cust_name=a;
acc_no=b;
void display()
11
}
intmin_bal,saving_bal;
super(a,b);
min_bal=c;
saving_bal=d;
void display()
super.display();
super(a,b,c,d);
deposits=e;
12
withdrawals=f;
void display()
super.display();
class Multilevel
A.display();
13
OUTPUT:
RESULT:
Thus, the required java program has been typed, executed and verified successfully.
14
“HIERARCHICAL INHERITANCE”
AIM:
ALGORITHM:
PROGRAM:
int x=10,y=20;
void display()
System.out.println("Value of X= "+x);
System.out.println("Value of Y= "+y);
void add()
15
System.out.println("X+Y= "+(x+y));
voidmul()
System.out.println("X*Y= "+(x*y));
classHier
16
OUTPUT:
RESULT:
Thus, the required java program has been typed, executed and verified successfully.
17
“HYBRID INHERITANCE”
AIM:
ALGORITHM:
PROGRAM:
class a
inti,j;
void show()
System.out.println("value of i is"+i);
System.out.println("value of j is"+j);
class b extends a
void sum()
int k=i+j;
System.out.println("value of k is "+k);
18
}
class c extends a
void fact()
{ int j=1;
for(int a=1;a<=i;a++)
j=j*a;
System.out.println(j);
aobj=new a();
obj.i=10;
obj.j=20;
obj.show();
b obj1=new b();
obj1.i=100;
obj1.j=200;
obj1.sum();
19
c obj2=new c();
obj2.i=5;
obj2.fact();
OUTPUT:
RESULT:
Thus, the required java program has been typed, executed and verified successfully.
20
“PACKAGES”
AIM:
ALGORITHM:
PROGRAM:
package p1;
class c1
obj.m1();
21
OUTPUT:
RESULT:
Thus, the required java program has been typed, executed and verified successfully.
22
“INTERFACES”
AIM:
ALGORITHM:
PROGRAM:
Interface program:
interface Callback {
void callback(intparam);
Implementation program:
classTestIface {
23
c.callback(42);
OUTPUT:
RESULT:
Thus, the required java program has been typed, executed and verified successfully.
24
“EVENT HANDLING”
AIM:
ALGORITHM:
PROGRAM:
Java program:
importjava.awt.*;
importjava.awt.event.*;
importjava.applet.*;
implementsKeyListener
int X=20,Y=30;
String msg="KeyEvents--->";
addKeyListener(this);
requestFocus();
setBackground(Color.green);
setForeground(Color.blue);
25
}
showStatus("KeyDown");
int key=k.getKeyCode();
switch(key)
caseKeyEvent.VK_UP:
showStatus("Move to Up");
break;
caseKeyEvent.VK_DOWN:
showStatus("Move to Down");
break;
caseKeyEvent.VK_LEFT:
showStatus("Move to Left");
break;
caseKeyEvent.VK_RIGHT:
showStatus("Move to Right");
break;
repaint();
showStatus("Key Up");
26
}
msg+=k.getKeyChar();
repaint();
g.drawString(msg,X,Y);
Html code:
<html>
</applet>
</html>
27
OUTPUT:
RESULT:
Thus, the required java program has been typed, executed and verified successfully.
28
“FILE HANDLING”
AIM:
ALGORITHM:
PROGRAM:
import java.io.*;
class Test
try
FileOutputStreamfout=new FileOutputStream("abc.txt");
byte b[]=s.getBytes();
fout.write(b);
fout.close();
System.out.println("success...");
}catch(Exception e)
29
System.out.println(e);
OUTPUT:
RESULT:
Thus, the required java program has been typed, executed and verified successfully.
30
“THREAD HANDLING”
AIM:
ALGORITHM:
PROGRAM:
classthreadeg
Thread t = Thread.currentThread();
t.setName(“My Thread”);
try {
System.out.println(n);
Thread.sleep(1000);
} catch (InterruptedException e) {
31
System.out.println(“Main thread interrupted”);
OUTPUT:
RESULT:
Thus, the required java program has been typed, executed and verified successfully.
32
“JAVA SWINGS”
AIM:
ALGORITHM:
PROGRAM:
importjava.awt.*;
importjava.awt.event.*;
importjavax.swing.*;
classEventDemo {
JLabeljlab;
EventDemo() {
jfrm.setLayout(new FlowLayout());
jfrm.setSize(220, 90);
jfrm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
33
JButtonjbtnAlpha = new JButton("Alpha");
jbtnAlpha.addActionListener(new ActionListener() {
});
jbtnBeta.addActionListener(new ActionListener() {
});
jfrm.add(jbtnAlpha);
jfrm.add(jbtnBeta);
jfrm.add(jlab);
jfrm.setVisible(true);
34
SwingUtilities.invokeLater(new Runnable() {
newEventDemo();
});
OUTPUT:
RESULT:
Thus, the required java program has been typed, executed and verified successfully.
35
“APPLET”
AIM:
ALGORITHM:
PROGRAM:
Java program:
importjava.awt.*;
importjava.applet.*;
for(int i=0;i<=250;i++)
g.setColor(c1);
g.drawRect(250+i,250+i,100+i,100+i);
g.drawOval(100+i,100+i,50+i,50+i);
g.drawLine(50+i,20+i,10+i,10+i);
36
}
Html code:
<html>
</applet>
</html>
OUTPUT:
37
RESULT:
Thus, the required java program has been typed, executed and verified successfully.
38
“RMI”
AIM:
ALGORITHM:
PROGRAM:
Interface program:
importjava.rmi.*;
39
Implementation program:
importjava.rmi.*;
importjava.rmi.server.*;
importjava.math.*;
publiccalimp()throws RemoteException
{}
return a+b;
return a-b;
return a*b;
return a/b;
40
{
return 1/a;
return a/100;
return Math.sqrt(a);
return -a;
Client program:
import java.rmi.*;
import java.awt.*;
import java.awt.event.*;
staticTextField tf;
41
Double a,b,c;
String o;
String url="rmi://localhost/calculator";
in=(calint)Naming.lookup(url);
String str[]={"backspace","c","ce"};
cmd=new Button[3];
for(int i=0;i<str.length;i++)
cmd[i]=new Button(str[i]);
cmd[i].addActionListener(this);
p1.add(cmd[i]);
p2.setLayout(new GridLayout(0,5));
no=new Button[10];
for(int i=0;i<=9;i++)
no[i]=new Button(""+i);
no[i].addActionListener(this);
42
}
op=new Button[10];
String str1[]={"+","-","*","/",".","=","1/x","%","+/-","sqrt"};
for(int i=0;i<str1.length;i++)
op[i]=new Button(str1[i]);
op[i].addActionListener(this);
p2.add(no[0]);p2.add(no[1]);p2.add(no[2]);p2.add(no[3]);p2.add(no[4]);
p2.add(no[5]);p2.add(no[6]);p2.add(no[7]);p2.add(no[8]);p2.add(no[9]);
p2.add(op[0]);p2.add(op[1]);p2.add(op[2]);p2.add(op[3]);p2.add(op[4]);
p2.add(op[5]);p2.add(op[6]);p2.add(op[7]);p2.add(op[8]);p2.add(op[9]);
p3.setLayout(new BorderLayout());
p3.add(p1,BorderLayout.NORTH);
p3.add(p2,BorderLayout.CENTER);
tf=new TextField();
setLayout(new BorderLayout());
add(tf,BorderLayout.NORTH);
add(p3,BorderLayout.CENTER);
addWindowListener(new WindowAdapter()
System.exit(0);
43
}
});
cc.addbutton();
cc.setSize(200,200);
cc.setVisible(true);
Button bt=(Button)ae.getSource();
String str=bt.getLabel();
try
if(str=="backspace")
tf.setText(tf.getText().substring(0,tf.getText().length()-1));
else if(str=="c"||str=="ce")
tf.setText("");
44
else if(str=="+"||str=="-"||str=="*"||str=="/")
a=Double.parseDouble(tf.getText());
tf.setText("");
o=str;
else if(str=="=")
b=Double.parseDouble(tf.getText());
if(str=="+")
c=in.add(a,b);
else if(str=="-")
c=in.sub(a,b);
else if(str=="*")
c=in.mul(a,b);
else
c=in.divide(a,b);
tf.setText(Double.toString(c));
else if(str=="1/x")
a=Double.parseDouble(tf.getText());
c=in.div(a);
tf.setText(Double.toString(c));
45
else if(str=="%")
a=Double.parseDouble(tf.getText());
c=in.per(a);
tf.setText(Double.toString(c));
else if(str=="+/-")
a=Double.parseDouble(tf.getText());
c=in.sign(a);
tf.setText(Double.toString(c));
else if(str=="sqrt")
a=Double.parseDouble(tf.getText());
c=in.sqroot(a);
tf.setText(Double.toString(c));
else
tf.setText(tf.getText()+str);
catch(Exception e)
{}
46
}
Server program:
importjava.rmi.*;
Naming.rebind("calculator",in);
System.out.println("server running");
OUTPUT:
47
RESULT:
Thus, the required java program has been typed, executed and verified successfully.
48
“JDBC”
AIM:
ALGORITHM:
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
PROGRAM:
importjava.sql.*;
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
49
System.out.println("Connected");
50
String sql10="INSERT INTO olap1empdepthistory SELECT
EmployeeID,DepartmentID,ShiftID,StartDate,EndDate FROM
HumanResources.EmployeeDepartmentHistory";
stmt.executeUpdate(sql10);
//System.out.println("table created");
catch(Exception ex) {
System.exit(1);
OUTPUT:
RESULT:
Thus, the required java program has been typed, executed and verified successfully.
51