EX.
NO : 1
DATE :
STRING EXTRACTION
AIM:
To write a java application to extract a portion of a character sting and print the extraction.
ALGORITHM:
Step 1: Start the process.
Step 2: Get any input as string.
Step 3: Get the position of sub string.
Step 4: Using the substring function to extract a particular portion of the string.
Step 5: Display the extracted string.
Step 6: Stop the process.
1
CODING:
import [Link].*;
import [Link].*;
class mm1
public static void main(String args[])throws IOException
DataInputStream in=new DataInputStream([Link]);
String s1,s2;
int a,b;
[Link](" Enter the string");
s1=[Link]();
[Link](" Enter the substring position");
a=[Link]([Link]());
b=[Link]([Link]());
s2=[Link](a,b);
[Link](" Substring:"+s2);
2
OUTPUT:
D:\JDK1.4\bin>javac [Link]
D:\JDK1.4\bin>java mm1
Enter the string:
chakaravarthy
Enter the substring position:
Substring: ra
D:\JDK1.4\bin>
3
[Link]
DATE:
ARITHMETIC OPERATION USING MULTIPLE
INHERITANCE
AIM:
To write a java program to implement the concept of multiple inheritance using interface.
ALGORITHM:
STEP 1: Start the process.
STEP 2: Create the first class and implement in the second class.
STEP 3: Then create a add(), sub(), mul(), div() function by using object.
STEP 4: Display the result.
STEP 5: Stop the process.
4
CODING:
import [Link].*;
import [Link].*;
interface first
public void add();
public void sub();
interface second
public void mul();
public void div();
class arith implements first,second
int aa,bb;
arith(int a,int b)
aa=a;
bb=b;
public void add()
5
{
[Link]("add:"+(aa+bb));
public void sub()
[Link]("sub:"+(aa-bb));
public void mul()
[Link]("mul:"+(aa*bb));
public void div()
[Link]("div:"+(aa/bb));
}}
class mm2
public static void main(String args[])throws IOException
DataInputStream in=new DataInputStream([Link]);
int c,d;
[Link]("ARITH OPERATION");
[Link]("Enter the value of c");
6
c=[Link]([Link]());
[Link]("Enter the value of d");
d=[Link]([Link]());
arith r=new arith(c,d);
[Link]();
[Link]();
[Link]();
[Link]();
7
OUTPUT:
D:\JDK1.4\bin>javac [Link]
D:\JDK1.4\bin>mm2
ARITH OPERATION
Enter the value of c
Enter the value of d
add:7
sub:-3
mul:10
div:0
8
[Link]
DATE:
PAY-OUT OF-BOUNDS EXCEPTION
AIM:
To write a java program to create an exception called pay-out of bound and the exception.
ALGORITHM:
Step1: Start the process.
Step2: Create a user defined package pay out of bounds of exception to handle the pay out of bounds of
exception to handle the pay out of bound of employee.
Step 3: Import io, user define package and create class.
Step 4: Get name and basic salary of the employee.
Step 5: If basic salary<4000 then throws an user defined exception pay-out of exception and display the
error. Message salary “Basic salary is less then 4000”.
Step 6: Overview of the calculating not salary of the employee and display the employee details.
Step 7: Save the program.
Step 8: Stop the process.
9
CODING:
import [Link].*;
import p1.*;
import [Link].*;
public class mm3
String na=" ";
float bp,hra,da,pf,loan,ns;
public gp3(String n,float b)
na=n;
bp=b;
public void pay()
try
10
if(bp>4000)throw new pb();
da=(bp*5)/100;
hra=(bp*10)/100;
pf=(bp*3)/100;
loan=(bp*2)/100;
ns=(bp+da+hra)-(pf+loan);
catch(pb e)
[Link]("Error:payout of bounds exception");
public void display()
[Link]("NAME:"+na);
[Link]("BP:"+bp);
[Link]("DA:"+da);
[Link]("HRA:"+hra);
[Link]("PF:"+pf);
[Link]("NS:"+ns);
[Link]("LOAN:"+loan);
11
}
public static void main(String args[])throws IOException
String na=" ";
float bp=0;
DataInputStream d=new DataInputStream([Link]);
[Link]("\n\t\t Payout of bounds exception");
[Link]("\n\t\t **************************");
[Link]("Enter the name:");
na=[Link]();
[Link]("Enter the basic pay:");
bp=[Link]([Link]()).floatValue();
mm3 e=new mm3(na,bp);
[Link]();
if(bp>=4000)
[Link]();
12
OUTPUT:
D:\JDK1.4\bin>md p1
D:\JDK1.4\bin>cd p1
D:\JDK1.4\bin\p1>javac [Link]
D:\JDK1.4\ bin\p1>cd..
D:\JDK1.4\bin>javac [Link]
D:\JDK1.4\bin>java mm3
Payout of bounds exception
**************************
Enter the name:
MITHUN
Enter the basic pay:
4500
NAME: MITHUN
13
BP: 4500.0
DA: 225.0
HRA: 450.0
PF: 135.0
NS: 4950.0
LOAN: 90.0
D:\JDK1.4>bin>java mm3
[Link]
DATE:
MULTITHREADING
AIM:
To write a java program to implement the concept of multithreading with the use of any three
multiplication table and assign different priorities to them.
ALGORITHM:
STEP 1: Start the process.
STEP 2: Import language package.
STEP 3: Define three class extends, implements the run method in that class define for
the loop for creating multiplication.
STEP 4: In main method create object for each thread.
14
STEP 5: Using set priority() set the different thread classes.
STEP 6: Call the start method to run the thread.
STEP 7: Stop the process.
CODING:
import [Link].*;
class table extends Thread
public void run()
[Link]("1st table:");
for(int i=1;i<=5;i++)
[Link](""+i+"*1="+i);
}
15
class table1 extends Thread
public void run()
[Link]("2nd table:");
for(int j=1;j<=5;j++)
int sum;
sum=j*2;
[Link](""+j+"*2="+sum);
class table2 extends Thread
public void run()
[Link]("3rd table:");
for(int k=1;k<=5;k++)
int sum1;
sum1=k*3;
16
[Link](""+k+"*3="+sum1);
class mm4
public static void main(String args[])
table a=new table();
table1 b=new table1();
table2 c=new table2();
[Link](1);
[Link](5);
[Link](10);
[Link]();
[Link]();
[Link]();
17
OUTPUT:
D:\JDK1.4\bin>javac [Link]
D:\JDK1.4\bin>java mm4
1st table:
1*1=1
1*2=2
1*3=3
2nd table:
2*1=2
2*2=4
18
2*3=6
3rd table:
3*1=3
3*2=6
3*3=9
[Link]: 5
DATE:
SHAPES
AIM:
To write a java program to several shapes in the created windows.
ALGORITHM:
STEP 1: Start the process.
STEP 2: Import the applet and awt package.
19
STEP 3: Create a class and extends applet and implement action listener.
STEP 4: Now in the paint method create an object for graphical class.
STEP 5: Now draw different shapes using oval(), draw rect(), draw line(), draw arc()
function.
STEP 6: Stop the process.
CODING:
import [Link].*;
import [Link].*;
public class mm5 extends Applet
public void paint(Graphics g)
[Link]("round rectangle",100,291);
20
[Link]("line",20,185);
[Link]("oval",10,250);
[Link]("arc",50,220);
[Link](500,180,200,190);
[Link](100,300,850,0,105,50);
[Link](60,10,200,180);
[Link](120,220,80,550,10,105);
/*<applet code="[Link]" height=200 width=200>
</applet>*/
OUTPUT:
D:\jdk1.4>bin>javac [Link]
D:\JDK1.4>bin>appletviewer [Link]
21
[Link]
DATE:
MY DETAILS DEMO
22
AIM:
To write a java program to create a frame with for test fields name, street, city, pin -code with
suitable table.
ALGORITHM:
STEP 1: Start the process.
STEP 2: Define the java package such as awt, event.
STEP 3: Define a class which extend, the frame.
STEP 4: Create tablets, text fields, and button.
STEP 5: Assign the name for tables, text fields and button.
STEP 6: Add action listener and implement the action listener.
STEP 7: Add window listener extend window adapter.
STEP 8: Display the details on clicking the my details button.
STEP 9: Stop the process.
CODING:
import [Link].*;
import [Link].*;
public class mm6 extends Frame
23
{
Label L1=new Label("NAME");
Label L2=new Label("STREET");
Label L3=new Label("CITY");
Label L4=new Label("PIN-CODE");
TextField t1=new TextField(15);
TextField t2=new TextField(15);
TextField t3=new TextField(15);
TextField t4=new TextField(15);
Button m=new Button("MY DETAILS");
mm6()
setTitle("BIO-DATA");
add(L1);add(t1);
add(L2);add(t2);
add(L3);add(t3);
add(L4);add(t4);
add(m);
[Link](new BH());
setLayout(new FlowLayout());
addWindowListener(new WH());
class WH extends WindowAdapter
24
{
public void WindowClosing(WindowEvent e)
[Link](0);
class BH implements ActionListener
public void actionPerformed(ActionEvent e)
if([Link]().equals("MY DETAILS"))
[Link]("MITHUN");
[Link]("NEHRU STREET");
[Link]("ERODE");
[Link]("638001");
repaint();
public static void main(String args[])
mm6 m1=new mm6();
25
[Link](200,200,500,600);
[Link](true);
OUTPUT:
26
D:\JDK1.4>bin>javac [Link]
D:\JDK1.4>java
27
[Link]: 7
DATE:
MULTIPLE SELECTION LIST BOX
AIM:
To write a java program to demonstrate the multiple selection list box.
ALGORITHM:
STEP 1: Start the process.
STEP 2: Import the applet, awt, package.
STEP 3: Create a class that should extents from applets and implements action listener.
STEP 4: Declare the init(), function action performed() and also paint().
STEP 5: Use for loop get the list boy items and also display that continuously.
STEP 6: Write HTML coding and applet tag for specify output area.
STEP 7: Compile the coding we the java followed by class name.
STEP 8: The applet window will be open and display the item which is click by the
class.
STEP 9: Stop the process.
28
CODING:
import [Link].*;
import [Link].*;
import [Link].*;
import [Link].*;
public class mm7 extends Applet implements ActionListener
List a;
public void init()
a=new List(6,true);
[Link]("[Link]<CS>");
[Link]("IT");
[Link]("BCA");
[Link]("[Link](CA)");
[Link]("MCA");
[Link]("[Link](CA)");
add(a);
[Link](this);
public void actionPerformed(ActionEvent e)
repaint();
public void paint(Graphics g)
int ind[];
29
String q;
ind=[Link]();
q="MY QUALIFICATION=";
[Link](q,6,150);
for(int i=0;i<[Link];i++)
q+=[Link](ind[i]);
[Link](q,6,150);
/*<applet code="[Link]" height=200 width=200>
</applet>*/
30
OUTPUT:
D:\JDK1.4\javac [Link]
D:JDK1.4\appletviewer [Link]
31
[Link]: 8
DATE:
TEXT DEMO
AIM:
To write a java program to create a frame three text field name, age and qualification and a text field
for multiple line for address.
ALGORITHM:
STEP 1: Start the process.
STEP 2: Define the java package such as awt, event.
STEP 3: Define a class which extends the frame.
STEP 4: Create labels, text fields, text area and button.
STEP 5: Assign the frame for lable, text fields and button.
STEP 6: Add Action listener and implement and action listener.
STEP 7: Add window listener extends window adapter.
STEP 8: Display the details on clicking my detail button.
STEP 9: Stop the process.
32
CODING:
import [Link].*;
import [Link].*;
public class mm8 extends Frame
Label one=new Label("NAME");
Label two=new Label("AGE");
Label three=new Label("QUALIFICATION");
Label four=new Label("ADDRESS");
TextField t1=new TextField(15);
TextField t2=new TextField(15);
TextField t3=new TextField(15);
TextField t4=new TextField(15);
Button b=new Button("MY DETAILS");
String s1,s2;
mm8()
setTitle("INFORMATION");
add(one);add(t1);
add(two);add(t2);
add(three);add(t3);
add(four);add(t4);
add(b);
[Link](new BH());
setLayout(new FlowLayout());
addWindowListener(new WH());
33
}
class WH extends WindowAdapter
public void WindowClosing(WindowEvent e)
[Link](0);
class BH implements ActionListener
public void actionPerformed(ActionEvent e)
if([Link]().equals("MY DETAILS"))
[Link]("MITHUN");
[Link]("20");
[Link]("STUDENT");
[Link]("ERODE");
s1="your name"+[Link]();
s2="your address"+[Link]();
repaint();
public void paint(Graphics g)
[Link](s1,10,200);
[Link](s2,140,200);
34
}
public static void main(String args[])
mm8 m1=new mm8();
[Link](1,1,250,300);
[Link](true);
35
OUTPUT:
D:\JDK1.4 \javac [Link]
D:\JDK1.4\java mm8
36
[Link]
DATE:
MENU BAR AND PULLDOWN MENU
AIM:
To write a java program to square create menus and pull down menus.
ALGORITHM:
STEP 1: Start the process.
STEP 2: Import awt and event package.
STEP 3: Define class extends frame.
STEP 4: Create object for menu bar, menu item and add item to that menu.
\STEP 5: Add action listener and implement the action listener.
STEP 6: Using action performed method check the event then display the information on the window.
STEP 7: Compile the program.
STEP 8: Stop the process.
37
CODING:
import [Link].*;
import [Link].*;
public class mm9 extends Frame
MenuBar mb;
Menu color;
Menu foreground;
Menu background;
MenuItem blue;
MenuItem green;
MenuItem cyan;
MenuItem yellow;
Menu exit;
MenuItem quit;
String msg;
Font f;
mm9()
setTitle("MENU BAR AND PULLDOWN MENUS");
setBackground([Link]);
setLayout(new FlowLayout());
mb=new MenuBar();
setMenuBar(mb);
color=new Menu("Color");
38
foreground=new Menu("foreground");
background=new Menu("background");
blue=new MenuItem("BLUE");
green=new MenuItem("GREEN");
cyan=new MenuItem("CYAN");
yellow=new MenuItem("YELLOW");
exit=new Menu("EXIT");
quit=new MenuItem("QUIT");
[Link](color);
[Link](foreground);
[Link](background);
[Link](green);
[Link](blue);
[Link](cyan);
[Link](yellow);
[Link](exit);
[Link](quit);
[Link](new MH());
[Link](new MH());
[Link](new MH());
[Link](new MH());
[Link](new MH());
class MH implements ActionListener
public void actionPerformed(ActionEvent e)
if([Link]()=="BLUE")
39
{
setForeground([Link]);
msg="Blue color has been selected";
if([Link]()=="GREEN")
setForeground([Link]);
msg="Green color has been selected";
if([Link]()=="CYAN")
setForeground([Link]);
msg="Cyan color has been selected";
if([Link]()=="Yellow")
setForeground([Link]);
msg="Yellow has been selected";
if([Link]()=="Quit")
[Link](0);
repaint();
public void paint(Graphics g)
f=newFont("TimesNewRoman",[Link]+[Link],25);
40
[Link](f);
[Link](msg,150,200);
public static void main(String args[])
mm9f2=new mm9();
[Link](1,1,500,500);
[Link](true);
41
OUTPUT:
D:\JDK1.4 \javac [Link]
D:\JDK1.4\java mm9
42
[Link]
DATE:
MOUSE DEMO
AIM:
To write a java program to create frames which respond to the repose clicks, for each events with
such as mouse up, mouse down etd…
ALGORITHM:
STEP 1: Start the process.
STEP 2: Import awt and event package.
STEP 3: Create a class and extend applet and implements action listener, mouse enter, mouse
listener, mouse motion listener.
STEP 4: Declare the init function for initial process. Mouse clicked, mouse enter, mouse exit,
mouse moved, mouse dragged function.
STEP 5: Write the HTML coding for applet to display window.
STEP 6: Compile the program.
STEP 7: Run time applet program using applet viewer.
STEP 8: In applet window the mouse message will be displayed based on the mouse event.
STEP 9: Stop the process.
43
CODING:
import [Link].*;
import [Link].*;
import [Link].*;
public class mm10 extends Applet
String message=" ";
public void init()
addMouseListener(new MouseHandler());
addMouseMotionListener(new MouseMotionHandler());
public void paint(Graphics g)
[Link](message,50,50);
class MouseHandler implements MouseListener
public void mousePressed(MouseEvent e)
message="MOUSE PRESSED";
repaint();
public void mouseReleased(MouseEvent e)
44
{
message="MOUSE RELEASED";
repaint();
public void mouseClicked(MouseEvent e)
message="MOUSE CLICKED";
repaint();
public void mouseEntered(MouseEvent e)
message="MOUSE ENTERED";
repaint();
public void mouseExited(MouseEvent e)
message="MOUSE EXITED";
repaint();
class MouseMotionHandler implements MouseMotionListener
public void mouseMoved(MouseEvent e)
message="MOUSE MOVED";
repaint();
}
45
public void mouseDragged(MouseEvent e)
message="MOUSE DRAGGED";
repaint();
/*<applet code="[Link]" height=200 width=200>
</applet>*/
46
OUTPUT:
D:JDK1.4\javac [Link]
D:JDK1.4\appletviewer [Link]
47
[Link]
DATE:
DRAW SHAPES IN MOUSE CLICK POSITION
AIM:
To write a java program to draw circle, squash, ellise and rectangle at the mouse click positions.
ALGORITHM:
STEP 1: Start the process.
STEP 2: Import the applet awt package and also event class from awt package.
STEP 3: Create class canvas which is extends from applet and implement are mouse listener.
STEP 4: Declare the graphics canvas area.
STEP 5: In init() methods set the size of the graphics canvas area and also add it..
STEP 6: In mouse clicked() method set the size of or the graphics, canvas area and also a
the location for display the click, square, ellipse , rectangle by using for loop.
STEP 7: Create another class and graphics can extends from canvas class.
STEP 8: Stop the process.
48
CODING:
import [Link].*;
import [Link].*;
import [Link].*;
public class mm11 extends Applet
int x=100;
int y=100;
public void init()
addMouseListener(new MouseAdapter()
public void mouseClicked(MouseEvent e)
repaint();
x=[Link]();
y=[Link]();
});
public void paint(Graphics g)
[Link](x,y,100,100);
[Link](x+50,y,100,150);
49
[Link](x+100,y,100,100);
[Link](x,y+150,180,50);
/*<applet code="[Link]" height=400 width=400>
</applet>*/
50
OUTPUT:
D:JDK1.4\javac [Link]
D:JDK1.4\appletviewer [Link]
51
[Link]: 12
DATE:
APPENDING IN TEXT FILE
AIM:
To write a java program which open and existing file text to that file.
ALGORITHIM:
STEP 1: Start the process.
STEP 2: Import IO package.
STEP3: Define class and create object we create object random access file.
STEP4: Using this object we create new file in read write mode.
STEP 5: Using seek method to file out the location of pointer .It moves file pointer.
STEP 6: Write the content to the file using write byte.
STEP 7: Close the file.
STEP 8: Stop the process.
52
CODING:
import [Link].*;
class mm12
public static void main(String args[])
RandomAccessFile file1;
try
file1=new RandomAccessFile("[Link]","rw");
[Link]([Link]());
[Link]("PROGRAMMING LANGUAGE");
[Link]();
catch(IOException e)
[Link]("e");
53
OUTPUT:
D:\JDK1.4\bin> [Link]
D:\JDK1.4>java mm12
D:\MITHUN>type [Link]
JAVAPROGRAMMING LANGUAGE
D:\JDK1.4>
54