Java Lab Program With Aim & Algorithm
Java Lab Program With Aim & Algorithm
NO : 1
STRING EXTRACTION
DATE :
AIM:
To create a java application to extract a portion of a character string and print the
extracted string.
ALGORITHM:
1
CODING:
import java.io.*;
class P1
{
public static void main(String args[ ]) throws Exception
{
DataInputStream dis = new DataInputStream(System.in);
String S;
int start, end;
System.out.println(“Enter the string:”);
S=dis.readLine();
System.out.println(“Enter the starting position:”);
start=Integer.parseInt(dis.readLine());
System.out.println(“Enter the ending position:”);
end=Integer.parseInt(dis.readLine());
char buf[] = new char[end-start];
S.getChars(start,end,buf,0);
System.out.println(“Extracted String is);
System.out.println(buf);
}
}
2
OUTPUT:
C:\jdk1.3\bin>javac P1.java
C:\jdk1.3\bin>java P1
3
EX.NO : 2
MULTIPLE INHERITANCE USING INTERFACE
DATE :
AIM:
ALGORITHM:
4
CODING:
import java.io.*;
class Student
{
int rollnumber;
void getnumber(int n)
{
rollnumber = n;
}
void putnumber( )
{
System.out.println("Roll No:" +rollnumber);
}
}
class Test extends Student
{
float part1,part2;
void getmarks(float m1, float m2)
{
part1 = m1;
part2 = m2;
}
void putmarks( )
{
System.out.println("\t\t MARKS OBTAINED");
System.out.println("\t\t ******************");
System.out.println("\n");
System.out.println("\t Mark 1 = " +part1);
System.out.println("\n");
System.out.println("\t Mark 2 = " +part2);
System.out.println("\n");
}
}
interface Sports
{
float sportswt = 7.0F;
void putwt( );
}
class Result extends Test implements Sports
{
float total;
public void putwt( )
{
System.out.println("Sportswt = " +sportswt);
}
5
void display( )
{
total = part1+part2+sportswt;
putnumber( );
putmarks( );
putwt( );
System.out.println("Total Score =" +total);
}
}
class P2
{
public static void main(String args[ ])
{
Result s1 = new Result( );
s1.getnumber(1234);
s1.getmarks(23.5F, 33.0F);
s1.display( );
}
}
6
OUTPUT:
C:\jdk1.3\bin>javac P2.java
C:\jdk1.3\bin>java P2
Roll No:1234
MARKS OBTAINED
******************
Mark 1 = 23.5
Mark 2 = 33.0
Sportswt = 7.0
7
EX.NO : 3
AN EXCEPTION CREATION
DATE :
AIM:
To create a java application for create an exception called payout-of-bounds and throw
the exception.
ALGORITHM:
8
CODING:
import java.io.*;
class P3
{
String name;
int bp,hra,ma,pf,lic,gp,np;
P3(String n, int b)
{
name = n;
bp = b;
}
void display( ) throws Pay
{
System.out.println("\t\t EMPLOYEE PAY SLIP");
System.out.println("\t\t ********************");
if(bp<=1000)
throw new Pay(bp);
ma = bp*5/100;
hra = bp*10/100;
gp = bp+hra+ma;
pf = bp*10/100;
lic = bp*20/100;
np = gp-(pf+lic);
System.out.println("\t NAME : " +name);
System.out.println("\t BASIC SALARY : " +bp);
System.out.println("\t MEDICAL ALLOWANCE : " +ma);
System.out.println("\t HOUSE RENT ALLOWANCE : " +hra);
System.out.println("\t GROSS PAY : " +gp);
System.out.println("\t PROVIDENT FUND : " +pf);
System.out.println("\t LIC : " +lic);
System.out.println("\t NET AMOUNT : " +np);
System.out.println(" ");
}
public static void main(String args[ ])
{
P3 x,y;
System.out.println("\n\t\t CHECK PAY OUT OF BOUNDS EXCEPTION");
x = new P3("Shiva", 10000);
y = new P3("Shakthi", 900);
try
{
x.display( );
y.display( );
}
catch(Pay b)
9
{
}
}
}
class Pay extends Exception
{
int basic;
Pay(int a)
{
basic = a;
System.out.println("\t Entered basic salary is "+basic);
System.out.println("\t Basic salary is less than 1000, so this Employee pay slip will not be
displayed");
}
}
10
OUTPUT:
C:\jdk1.3\bin>javac P3.java
C:\jdk1.3\bin>java P3
********************
NAME : Shiva
LIC 2000
********************
11
EX.NO : 4
MULTITHREADING
DATE :
AIM:
To create a java application to implement the concept of Multithreading with the use of
any three multiplication tables and assign three different priorities to thread.
ALGORITHM:
12
CODING:
import java.lang.*;
class X extends Thread
{
public void run( )
{
for(int i=1;i<=5;i++)
{
System.out.println(i+"*"+5+"="+i*5);
}
}
}
class Y extends Thread
{
public void run( )
{
for(int j=1;j<=5;j++)
{
System.out.println(j+"*"+7+"="+j*7);
}
}
}
class Z extends Thread
{
public void run( )
{
for(int k=1;k<=5;k++)
{
System.out.println(k+"*"+10+"="+k*10);
}
}
}
class P4
{
public static void main(String args[ ])
{
System.out.println("\n\t MULTITHREADING");
System.out.println("\n\t ******************");
X p=new X( );
Y q=new Y( );
Z r=new Z();
p.setPriority(Thread.MAX_PRIORITY);
q.setPriority(Thread.NORM_PRIORITY);
r.setPriority(Thread.MIN_PRIORITY);
p.start( );
13
q.start( );
r.start( );
}
}
14
OUTPUT:
C:\jdk1.3\bin>javac P4.java
C:\jdk1.3\bin>java P4
MULTITHREADING
******************
1*5=5
2*5=10
3*5=15
4*5=20
1*10=10
1*7=7
5*5=25
2*10=20
2*7=14
3*10=30
3*7=21
4*10=40
4*7=28
5*10=50
5*7=35
15
EX.NO : 5
SEVERAL SHAPES
DATE :
AIM:
ALGORITHM:
16
CODING:
import java.applet.*;
import java.awt.*;
/*<applet code = "P5" width = 500 height = 500></applet>*/
public class P5 extends Applet
{
public void paint(Graphics g)
{
setBackground(Color.green);
g.drawLine(1,1,40,100);
g.drawRect(10,240,40,30);
g.drawRoundRect(5,280,80,50,10,10);
g.drawOval(40,330,200,120);
g.drawArc(90,280,80,40,180,180);
int X[ ] = {30,20,30,200,30};
int Y[ ] = {30,30,200,200,30};
int n = 4;
g.drawPolygon(X,Y,n);
}
}
17
OUTPUT:
C:\jdk1.3\bin>javac P5.java
C:\jdk1.3\bin>appletviewer P5.java
18
EX.NO : 6
FRAME CREATION WITH FOUR TEXT FIELDS
DATE :
AIM:
To create a java application to create a Frame with four Text fields as Name, Street, City,
Pincode and also add buttons called “My Detail” and “Exit”. When the button is clicked its
corresponding action will be done.
ALGORITHM:
19
CODING:
import java.awt.*;
import java.awt.event.*;
class P6 extends Frame implements ActionListener
{
Button b1,b2;
Label l,l1,l2,l3;
TextField t1,t2,t3,t4;
P6( )
{
setBounds(50,80,400,400);
setLayout(null);
l = new Label("Name");
l1 = new Label("Street");
l2 = new Label("City");
l3 = new Label("Pincode");
l.setBounds(50,50,80,20);
l1.setBounds(50,75,80,20);
l2.setBounds(50,100,80,20);
l3.setBounds(50,125,80,20);
add(l);
add(l1);
add(l2);
add(l3);
b1 = new Button("My Details");
b1.setBounds(110,170,80,20);
b1.addActionListener(this);
add(b1);
b2 = new Button("exit");
b2.setBounds(110,250,80,20);
b2.addActionListener(this);
add(b2);
t1 = new TextField( );
t2 = new TextField( );
t3 = new TextField( );
t4 = new TextField( );
t1.setBounds(200,50,100,20);
t2.setBounds(200,75,100,20);
t3.setBounds(200,100,100,20);
t4.setBounds(200,125,100,20);
add(t1);
add(t2);
add(t3);
add(t4);
show( );
20
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource( )==b1)
{
t1.setText("Raja");
t2.setText("Perundurai");
t3.setText("Erode");
t4.setText("638052");
}
else
if(e.getSource( )==b2)
{
dispose( );
System.exit(0);
}
}
public static void main(String args[ ])
{
P6 obj = new P6( );
}
}
21
OUTPUT:
C:\jdk1.3\bin>javac P6.java
C:\jdk1.3\bin>java P6
22
23
EX.NO : 7
MULTIPLE SELECTION LISTBOX
DATE :
AIM:
ALGORITHM:
24
CODING:
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
/*<applet code="P7" height=500 width=500></applet>*/
public class P7 extends Applet implements ActionListener
{
List hobby;
public void init( )
{
hobby = new List(6,true);
hobby.addItem("Sports");
hobby.addItem("Reading Books");
hobby.addItem("Listening Music");
hobby.addItem("Drawing");
hobby.addItem(" ");
hobby.addActionListener(this);
add(hobby);
}
public void actionPerformed(ActionEvent e)
{
repaint( );
}
public void paint(Graphics g)
{
String msg = " ";
int idx[];
idx = hobby.getSelectedIndexes( );
for(int i=0;i<idx.length;i++)
{
msg+=hobby.getItem(idx[i]);
}
g.drawString("SELECTED HOBBIES ARE:", 20,20);
g.drawString(msg,170,200);
}
}
25
OUTPUT:
C:\jdk1.3\bin>javac P7.java
C:\jdk1.3\bin>appletviewer P7.java
26
EX.NO : 8
FRAME CREATION WITH THREE TEXT FIELDS
DATE :
AIM:
To create a java application to create a Frame with three TextField for Name, Age and
Qualification and TextArea for multiple lines for Address.
ALGORITHM:
27
CODING:
import java.awt.*;
import java.awt.event.*;
class P8 extends Frame implements ActionListener
{
Label l1,l2,l3,l4;
Button b1;
TextField t1,t2,t3;
TextArea t4;
P8( )
{
setBounds(50,80,400,400);
setLayout(null);
l1 = new Label("Name");
l2 = new Label("Age");
l3 = new Label("Qualification");
l4 = new Label("Address");
l1.setBounds(50,50,80,20);
l2.setBounds(50,75,80,20);
l3.setBounds(50,100,80,20);
l4.setBounds(50,125,80,20);
add(l1);
add(l2);
add(l3);
add(l4);
b1 = new Button("Exit");
b1.setBounds(110,170,80,20);
b1.addActionListener(this);
add(b1);
t1=new TextField("KUMAR");
t2=new TextField("16");
t3=new TextField("BCA");
t4=new TextArea("Pallipalayam",4,20);
t1.setBounds(200,50,80,20);
t2.setBounds(200,75,80,20);
t3.setBounds(200,100,80,20);
t4.setBounds(200,125,80,20);
add(t1);
add(t2);
add(t3);
add(t4);
show( );
}
public void actionPerformed(ActionEvent e)
{
28
if(e.getSource( )==b1)
{
dispose( );
System.exit(0);
}
}
public static void main(String args[ ])
{
P8 obj = new P8( );
}
}
29
OUTPUT:
C:\jdk1.3\bin>javac P8.java
C:\jdk1.3\bin>java P8
30
EX.NO : 9
MENUBARS AND PULLDOWN MENUS
DATE :
AIM:
To create the java application to create Menu bar and Pull-down menus.
ALGORITHM:
Step 5 : Display the MenuBar, Menu, MenuItems and Button into the Frame using show( )
method.
Step 6 : Override the actionPerformed( ) method.
Step 7 : Stop the process.
31
CODING:
import java.io.*;
import java.awt.*;
import java.awt.event.*;
class P9 extends Frame implements ActionListener
{
MenuBar mb;
Menu m1,m2;
MenuItem i1,i2,i3,i4,i5,i6,i7,i8,i9,i10;
Button b;
P9( )
{
setBounds(50,50,750,500);
setLayout(null);
b=new Button("Exit");
b.addActionListener(this);
b.setBounds(110,170,80,20);
add(b);
m1=new Menu("File");
m2=new Menu("Edit");
i1=new MenuItem("New");
i2=new MenuItem("Open");
i3=new MenuItem("Save");
i4=new MenuItem("Save As");
i5=new MenuItem("Close");
i6=new MenuItem("Exit");
i7=new MenuItem("Cut");
i8=new MenuItem("Copy");
i9=new MenuItem("Paste");
i10=new MenuItem("Clear");
i1.addActionListener(this);
i2.addActionListener(this);
i3.addActionListener(this);
i4.addActionListener(this);
i5.addActionListener(this);
i6.addActionListener(this);
i7.addActionListener(this);
i8.addActionListener(this);
i9.addActionListener(this);
i10.addActionListener(this);
mb=new MenuBar( );
setMenuBar(mb);
m1.add(m1);
m2.add(m2);
mb.add(m1);
32
mb.add(m2);
m1.add(i1);
m1.add(i2);
m1.add(i3);
m1.add(i4);
m1.add(i5);
m1.add(i6);
m2.add(i7);
m2.add(i8);
m2.add(i9);
m2.add(i10);
show( );
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource( )==b)
{
dispose( );
System.exit(0);
}
}
public static void main(String args[ ])
{
P9 obj = new P9( );
}
}
33
OUTPUT:
C:\jdk1.3\bin>javac P9.java
C:\jdk1.3\bin>java P9
34
35
EX.NO : 10
FRAME CREATION FOR MOUSE EVENTS
DATE :
AIM:
To create the java application to create Frame which respond to the mouse events and
display the corresponding message when the mouse event is occur.
ALGORITHM:
36
CODING:
import java.awt.*;
import java.awt.event.*;
class P10 extends Frame implements MouseListener,ActionListener
{
Button b;
Label l;
P10( )
{
l = new Label( );
setBounds(100,100,550,550);
setLayout(null);
l.setBounds(100,100,120,50);
addMouseListener(this);
add(l);
b=new Button("Exit");
b.setBounds(200,250,80,20);
b.addActionListener(this);
add(b);
show( );
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource( )==b)
{
dispose( );
System.exit(0);
}
}
public void mousePressed(MouseEvent e)
{
l.setText("Mouse Pressed");
}
public void mouseClicked(MouseEvent e)
{
l.setText("Mouse Clicked");
}
public void mouseEntered(MouseEvent e)
{
l.setText("Mouse Entered");
}
public void mouseExited(MouseEvent e)
{
l.setText("Mouse Exited");
}
37
public void mouseReleased(MouseEvent e)
{
l.setText("Mouse Released");
}
public static void main(String args[ ])
{
P10 obj = new P10( );
}
}
38
OUTPUT:
C:\jdk1.3\bin>javac P10.java
C:\jdk1.3\bin>java P10
39
40
EX.NO : 11
DRAGGING SHAPES
DATE :
AIM:
To create the java application to draw Circle, Square, Ellipse and Rectangle at the mouse
click positions.
ALGORITHM:
41
CODING:
42
OUTPUT:
C:\jdk1.3\bin>javac P11.java
C:\jdk1.3\bin>appletviewer P11.java
43
44
EX.NO : 12
APPEND TEXT TO EXISTING FILE
DATE :
AIM:
To create the java application to open an existing File and Append Text to that File.
ALGORITHM:
45
CODING:
import java.io.*;
class P12
{
public static void main(String args[ ])
{
RandomAccessFile rFile;
try
{
rFile = new RandomAccessFile("quote.txt","rw");
rFile.seek(rFile.length( ));
rFile.writeBytes("Write in your HEART that every day is the best day in every year");
rFile.close( );
}
catch(IOException ioe)
{
System.out.println(ioe);
}
}
}
46
OUTPUT:
C:\jdk1.3\bin>javac P12.java
C:\jdk1.3\bin>java P12
C:\jdk1.3\bin>type quote.txt
Write in your HEART that every day is the best day in every year.
47