Ex.
No:01
Extraction of a String
Date:21.12.2014
AIM:
To write a Java program to extract a particular of character string and print the extract
string length.
ALGORITHM:
Step -1: Start the process.
Step -2: Create a class reading and in the main() method declare object for
DataInputStream.
Step -3: Declare String and integer data types to get string and values to extract from the
given string by the user.
Step -4: In the try block, use readline() method to get the string,length() to find the
length of user given string.
Step -5: Use substring() method to extract the portion of user given string specified by the
integer value.
Step -6: Display the respective result.
Step -7: Declare the corresponding catch block for the exception.
Step -8: Stop the process.
EXTRACTION OF A STRING
PROGRAM CODING:
import [Link];
class Reading
public static void main(String args[])
DataInputStream in=new DataInputStream([Link]);
String str1,str2;
int m,n,len;
try
[Link]("Enter The String:");
str1=[Link]();
len=[Link]();
[Link]("Enter The 2 Values To Extract Strings:");
n=[Link]([Link] ());
m=[Link]([Link] ());
str2=[Link](n,m);
[Link]("Extracted Portion of The String:"+str2);
[Link]("The Length of Extracted String:"+[Link]());
catch(Exception e)
{}
}
OUTPUT:
D:\Java>javac [Link]
D:\Java>java Reading
Enter the String:
renegade
Enter 2 values to extract the string:
Extracted portion of the string: egad
Length of the extracted string: 4
RESULT:
Thus the program has been executed successfully.
[Link]
Multiple Inheritance
Date:23.12.2014
AIM:
To write a Java program to implement the concept multiple inheritance using
interface.
ALGORITHM:
Step -1: Start the process.
Step -2: Create a class student to accept the roll number and to display.
Step -3: Create a class test, which extends the super class student to set and display the
marks.
Step -4: Create an interface Sports to assign sports score.
Step -5: Create a class result, which extends the super class test and implements interface
Sports.
Step -6: Create a method in order to inherit all the methods of super class and the interface.
Step -7: Under main() method create object for the last derived class in order to access all
the methods.
Step -8: Stop the process.
MULTIPLE INHERITANCE
PROGRAM CODING:
import [Link].*;
class Student
{
int rollnumber;
void getNumber(int n)
{
rollnumber=n;
}
void putNumber()
{
[Link]("ROLL NO:"+rollnumber);
}
}
class Test extends Student
{
float part1,part2;
void getMarks(float m1,float m2)
{
part1=m1;
part2=m2;
}
void putMarks()
{
[Link]("MARKS OBTAINED:");
[Link]("PART1:"+part1);
[Link]("PART2:"+part2);
}
}
interface Sports
{
float SportsWt=6.08f;
}
//void putWt()
class Results extends Test implements Sports
{
float total;
public void putWt()
{
[Link]("SPORTS:"+SportsWt);
}
void display()
{
total=part1+part2+SportsWt;
putNumber();
putMarks();
putWt();
[Link]("TOTAL SCORE:"+total);
}
}
class Hybrid
{
public static void main(String args[])
{
Results s1=new Results();
[Link](2207);
[Link](25.57f,33.08f);
[Link]();
}
}
OUTPUT:
D:\Java>javac [Link]
D:\Java>java Hybrid
ROLL NO:2207
MARKS OBTAINED:
PART1:25.57
PART2:33.08
SPORTS:6.08
TOTAL SCORE:64.73
RESULT:
Thus the program has been executed successfully.
[Link]
Creating an Exception
Date:23.12.2014
AIM:
To write a Java program to create an exception called PayoutofBound and throw the
exception.
ALGORITHM:
Step -1: Start the process.
Step -2: Create a class which extends the super class Exception.
Step -3: Declare the class employee and declare String and integer variables.
Step -4: Using the constructor assign values to the variables.
Step -5: Inside the salary() method range of salary is checked by if condition and it
throw the exception.
Step -6: Using main() method to declare object for the employee class and call the
respective method.
Step -7: Use try block to throw the exception.
Step -8: Stop the process.
CREATING AN EXCEPTION
PROGRAM CODING:
class PayoutofBoundException extends Exception
{
int pay;
PayoutofBoundException(int pay)
{
[Link]=pay;
}
}
public class employee
{
String name;
String Salary;
int pay;
employee(String n,int m)
{
name=n;
pay=m;
}
void findSalary() throws PayoutofBoundException
{
if(pay>10000)throw new PayoutofBoundException(pay);
if(pay<5000)
Salary="low";
else
Salary="high";
[Link]("Name:"+name+"\tSalary="+Salary+"\tPay:"+pay);
[Link](" ");
}
public static void main(String args[])
{
employee x,y,z;
x=new employee("Messi",3500);
y=new employee("Neymar",7000);
z=new employee("Suarez",5000);
try
{
[Link]();
[Link]();
[Link]();
}
catch(PayoutofBoundException e)
{
[Link]("Pay is greater than 10000");
}
}
}
OUTPUT:
D:\Java>javac [Link]
D:\Java>java employee
Name: Messi Salary=low Pay:3500
Name: Neymar Salary=high Pay:7000
Name :Suarez Salary=high Pay:5000
RESULT:
Thus the program has been executed successfully.
[Link]
Multi Threading
Date:30.12.2014
AIM:
To write a Java program to implement the concept of multi threading with the use
of any multiplication tables and design three different properties.
ALGORITHM:
Step -1: Start the program.
Step -2: Create three classes A, B and C such that each class extending the properties
of Thread class.
Step -3: By using for loop multiplication tables 1, 2 and 3 are formed under the classes A, B
and C respectively.
Step -4: Create the class with main() method
Step -5: Declare objects for the class A, B and C.
Step -6: By using the object set thread priority like max, min and start the thread by using
start() method.
Step -7: Stop the process.
MULTI THREADING
PROGRAM CODING:
import [Link].*;
class A extends Thread
{
public void run()
{
[Link]("\n1st table started:");
for(int i=1;i<=10;i++)
{
[Link](i+"*1="+i*1);
}
[Link]("Exit from 1st table.");
}
}
class B extends Thread
{
public void run()
{
[Link]("\n2nd table started:");
for(int j=1;j<=10;j++)
{
[Link](j+"*2="+j*2);
}
[Link]("Exit from 2nd table.");
}
}
class C extends Thread
{
public void run()
{
[Link]("\n3rd table started:");
for(int k=1;k<=10;k++)
{
[Link](k+"*3="+k*3);
}
[Link]("Exit from 3rd table.");
}
}
class Threadpriority
{
public static void main(String args[]) throws Exception
{
A Obj4=new A();
B Obj5=new B();
C Obj6=new C();
[Link](Thread.MAX_PRIORITY);
[Link](Thread.NORM_PRIORITY);
[Link](Thread.MIN_PRIORITY);
[Link]();
[Link]();
[Link]();
}
}
OUTPUT:
D:\Java>javac [Link]
D:\Java>java Threadpriority
1st table started:
1*1=1
2*1=2
3*1=3
4*1=4
5*1=5
6*1=6
7*1=7
8*1=8
9*1=9
10*1=10
Exit from 1st table.
2nd table started:
1*2=2
2*2=4
3*2=6
4*2=8
5*2=10
6*2=12
7*2=14
8*2=16
9*2=18
10*2=20
Exit from 2nd table.
3rd table started:
1*3=3
2*3=6
3*3=9
4*3=12
5*3=15
6*3=18
7*3=21
8*3=24
9*3=27
10*3=30
Exit from 3rd table.
RESULT:
Thus the program has been executed successfully.
[Link]
Displaying Several Shapes
Date:30.12.2015
AIM:
To write a Java program to drop several shapes in the created windows.
ALGORITHM:
Step -1: Start the program.
Step -2: Import packages like “awt” and “applet”.
Step -3: Create a class shapes by extending the Applet class.
Step -4: Declare the paint() method.
Step -5: Declare two array int x, int y and assign the values to it.
Step -6: Use polygon method and assign values of array into it.
Step -7: By using paint object, call drawLine(), drawRect(), drawOval() and assign
co-ordinate axis to all the shapes.
Step -8: Display the respective result.
Step -9: Stop the process.
DISPLAYING SEVERAL SHAPES
PROGRAM CODING:
import [Link].*;
import [Link].*;
/*<applet code="[Link]"Width=500 height=500></applet>*/
public class shapes extends Applet
{
public void paint(Graphics g)
{
int X[]={50,89,75};
int Y[]={300,400,450};
Polygon p= new Polygon(X,Y,3);
[Link](p);
setBackground([Link]);
[Link](50,50,100,50);
[Link](250,50,100,150);
[Link]([Link]);
[Link](250,50,100,150);
[Link]([Link]);
[Link](400,50,50,50);
[Link]([Link]);
[Link](500,50,50,50);
}
}
OUTPUT:
D:\Java>javac [Link]
D:\Java>appletviewer [Link]
RESULT:
Thus the program has been executed successfully.
[Link]
Creation of Frame
Date:06.01.2015
AIM:
To create a Java program to create a Frame with button is called “Mydetails”.
ALGORITHM:
Step -1: Start the process.
Step -2: Create a class my details by extending Frame class.
Step -3: Declare Labels, TextFields and Buttons and add the constructor using
mydetails.
Step -4: Create class WH and BH by extending class and using interface respecting.
Step -5: Create window for adding Label, getting text for displaying.
Step -6: Use main() method to create object for class and call the respective method.
Step -7: Stop the process.
CREATION OF FRAME
PROGRAM CODING:
import [Link].*;
import [Link].*;
public class Mydetails extends Frame
Label l1=new Label("Name");
Label l2=new Label("Street");
Label l3=new Label("City");
Label l4=new Label("Pincode");
TextField t1=new TextField(20);
TextField t2=new TextField(35);
TextField t3=new TextField(35);
TextField t4=new TextField(6);
Button Ok=new Button("Mydetails");
String msg;
Mydetails()
setTitle("Mydetails");
add(l1);add(t1);
add(l2);add(t2);
add(l3);add(t3);
add(l4);add(t4);
[Link](new BH());
add(Ok);
setLayout(new FlowLayout());
addWindowListener(new WH());
}
class WH extends WindowAdapter
public void windowclosing(WindowEvent e)
[Link](0);
class BH implements ActionListener
public void actionPerformed(ActionEvent e)
if([Link]().equals("ok"));
msg="Name: "+[Link]()+" "+"Street: "+[Link]()+" "+"\nCity: "+[Link]()+"
"+"\nPincode: "+[Link]();
repaint();
}}
public void paint(Graphics g)
[Link](msg,10,200);
public static void main(String args[])
Mydetails obj=new Mydetails();
[Link](1,1,150,300);
[Link](true);
}}
OUTPUT:
D:\Java>javac [Link]
D:\Java>java Mydetails
RESULT:
Thus the program has been executed successfully.
[Link]
Multi Selection List-Box
Date:24.01.2015
AIM:
To write a Java program to demonstrate multiple selection list-box.
ALGORITHM:
Step -1: Start the process.
Step -2: Create a class which extends applet sub default class and implement interface
ActionListener.
Step -3: Declare List, TextField, Button and String in init() method, assign values to the
text field and add.
Step -4: Add the number of items required for the list.
Step -5: Create a Button under the name “Show Selection” and add the button into
ActionListener.
Step -6: Under actionPerformed() method, use for loop to display the items that has
been selected from the list in the text area provided.
Step -7: Stop the process.
MULTI SELECTION LIST-BOX
PROGRAM CODING:
import [Link];
import [Link];
import [Link];
import [Link].*;
import [Link].*;
/*<applet code=[Link] width=400 height=400></applet>*/
public class multiselection extends Applet implements ActionListener
{
List list1;
TextField text1;
Button button1;
String selection[];
public void init()
{
text1= new TextField(40);
add(text1);
list1=new List(5,true);
[Link]("Android");
[Link]("iOS");
[Link]("Windows");
[Link]("Linux");
[Link]("Unix");
[Link]("item6");
[Link]("item7");
[Link]("item8");
[Link]("item9");
add(list1);
button1=new Button("Show Selection");
[Link](this);
add(button1);
}
public void actionPerformed(ActionEvent e)
{
String outString=new String("You Selected:");
if([Link]()==button1)
selection=[Link]();
for(int loopindex=0;loopindex<[Link];loopindex++)
{
outString+=","+selection[loopindex];
}
[Link](outString);
}
}
OUTPUT:
D:\Java>javac [Link]
D:\Java>appletviewer [Link]
RESULT:
Thus the program has been executed successfully.
[Link]
Frame with Multiple Line
Date:27.01.2015
AIM:
To write a Java program to create a frame with three text field for name, age,
qualification and address.
ALGORITHM:
Step -1: Start the process.
Step -2: Declare the Labels, TextFields, TextAreas and Buttons.
Step -3: In the constructor Frame(), add Label, TextField and Button, set title and layout
for the window.
Step -4: Create the class and method to get String variable and display them using
drawString().
Step -5: In the main(), create object for the class Frame and call the method setBound
and setVisible.
Step -6: Execute the process and display the result.
Step -7: Stop the process.
FRAME WITH MULTIPLE LINE
PROGRAM CODING:
import [Link].*;
import [Link].*;
class Frame1 extends Frame
{
Label l1=new Label("Name");
Label l2=new Label("Age");
Label l3=new Label("Qualification");
Label l4=new Label("Address");
TextField t1=new TextField(15);
TextField t2=new TextField(15);
TextField t3=new TextField(15);
TextArea t4;
Button OK=new Button("OK");
String msg,msg1,msg2,msg3;
Frame1()
{
setTitle("Text Area");
add(l1);add(t1);
add(l2);add(t2);
add(l3);add(t3);
t4=new TextArea(msg1,5,25);
add(l4);add(t4);
[Link](new BH());
add(OK);
setLayout(new FlowLayout());
addWindowListener(new WH());
}
class WH extends WindowAdapter
{
public void WindowClosing(WindowEvent e)
{
[Link](0);
}
}
class BH implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
if([Link]().equals("OK"));
msg=[Link]();
msg1=[Link]();
msg2=[Link]();
msg3=[Link]();
repaint();
}}
public void paint(Graphics g)
{
[Link]("Name---------------->"+msg,150,220);
[Link]("Age------------------->"+msg1,150,240);
[Link]("Qualification------->"+msg2,150,260);
[Link]("Address------------->"+msg3,150,280);
}
public static void main(String args[])
{
Frame1 c=new Frame1();
[Link](4,4,150,50);
[Link](true);
}}
OUTPUT:
D:\Java>javac [Link]
D:\Java>java Frame1
RESULT:
Thus the program has been executed successfully.
[Link]
Menu Box and Pull Down Menu
Date:03.01.2015
AIM:
To write a Java program to create menu bar and drop down menu.
ALGORITHM:
Step -1: Start the process.
Step -2: Create a class menu that extends the applet and use the interface ActionListener.
Step -3: Under the init() method, declare Button and Frame along with its size.
Step -4: Declare a class frame that extends the Frame class.
Step -5: Declare variable for Menu, MenuBar, Label and MenuItem.
Step -6: Declare constructor frame() and add all the variable of above said type.
Step -7: Use actionPerformed and windowClosing method and use if loop for setting the
text.
Step -8: Stop the process.
MENU BOX AND PULL DOWN MENU
PROGRAM CODING:
import [Link].*;
import [Link].*;
import [Link];
/*<applet code="[Link]"width=300 height=200></applet>*/
public class menu extends Applet implements ActionListener
Button b1;
frame menuWindow;
public void init()
b1=new Button("Display the menu Window");
add(b1);
[Link](this);
menuWindow=new frame("menu");
[Link](200,200);
public void actionPerformed(ActionEvent event)
if([Link]()==b1)
[Link](true);
}}}
class frame extends Frame implements ActionListener
Menu menu;
MenuBar menubar;
MenuItem menuitem1,menuitem2,menuitem3;
Label label;
frame(String title)
super(title);
label=new Label("Hello from java");
setLayout(new GridLayout(1,1));
add(label);
menubar=new MenuBar();
menu=new Menu("file");
menuitem1=new MenuItem("item1");
[Link](menuitem1);
[Link](this);
menuitem2=new MenuItem("item2");
[Link](menuitem2);
[Link](this);
menuitem3=new MenuItem("item3");
[Link](menuitem3);
[Link](this);
[Link](menu);
setMenuBar(menubar);
addWindowListener(new WindowAdapter()
public void WindowClosing(WindowEvent e)
setVisible(false);
});
public void actionPerformed(ActionEvent event)
if([Link]()==menuitem1)
[Link]("YOU CHOOSE ITEM 1");
if([Link]()==menuitem2)
[Link]("YOU CHOOSE ITEM 2");
if([Link]()==menuitem3)
[Link]("YOU CHOOSE ITEM 3");
}}
OUTPUT:
D:\Java>javac [Link]
D:\Java>appletviewer [Link]
RESULT:
Thus the program has been executed successfully.
[Link]
Mouse Event
Date:07.02.2015
AIM:
To write a Java program to create frame which respond to the mouse click for each
extends with move such as move up, down the corresponding message.
ALGORITHM:
Step -1: Start the process.
Step -2: Create a class me extends from Applet.
Step -3: Create method like mouseDown(), mouseUp(), mouseMoved() and mouseDrag() in
order click with mouse positions and movement.
Step -4: Declare and initialize data types String and integer to get the mouse position.
Step -5: Use drawstring() method to find the mouse movement and positions.
Step -6: Stop the process.
MOUSE EVENT
PROGRAM CODING:
import [Link].*;
import [Link].*;
import [Link].*;
import [Link].*;
/*<applet code="[Link]"width=800 height=511></applet>*/
public class me extends Applet
String m=" ";
int mx=0,my=0;
public boolean mouseDown(Event e,int x,int y)
mx=x;
my=y;
m="button pressed";
showStatus("Pressing mouse at position:"+x+","+y);
repaint();
return true;
public boolean mouseUp(Event e,int x,int y)
mx=x;
my=y;
m="button released";
showStatus("Mouse released at position:"+x+","+y);
repaint();
return true;
public boolean mouseMoved(Event e,int x,int y)
mx=x;
my=y;
m=”mouse moved”;
showStatus("Moving mouse at: "+x+","+y);
return true;
public boolean mouseDrag(Event e,int x,int y)
mx=x;
my=y;
m="button drag";
showStatus("Dragging the mouse at position: "+x+","+y);
repaint();
return true;
public void paint(Graphics g)
[Link](m,mx,my);
}
OUTPUT:
D:\Java>javac [Link]
D:\Java>appletviewer [Link]
RESULT:
Thus the program has been executed successfully.
[Link]
Drawing Shapes at Mouse Click Positions
Date:10.02.2015
AIM:
To write a Java program to draw a circle, square, ellipse and rectangle at the mouse
click.
ALGORITHM:
Step -1: Start the process.
Step -2: Create a class which extends Applet implements mouseListener.
Step -3: Use switch statement to draw several shapes like circle, square, ellipse, rectangle
Step -4: Use the different mouse events like clicked, entered and drag with respective
co-ordinate axis.
Step -5: Each corresponding shapes are down using mouse events like pressed, dragged,
released and exited.
Step -6: Stop the process.
DRAWING SHAPES AT MOUSE CLICK POSITIONS
PROGRAM CODING:
import [Link].*;
import [Link].*;
import [Link].*;
public class Drawshap extends Applet implements MouseListener
int X,Y,ch;
public void init()
addMouseListener(this);
ch=1;
public void paint(Graphics g)
switch(ch)
case 1:
[Link](X,Y,50,50);
break;
case 2:
[Link](X,Y,60,50);
break;
case 3:
[Link](X,Y,200,250);
break;
case 4:
[Link](X,Y,250,100);
break;
ch++;
if(ch==5)
ch=1;
showStatus(X+","+Y);
public void mouseClicked(MouseEvent me)
X=[Link]();
Y=[Link]();
repaint();
public void mouseEntered(MouseEvent me)
X=[Link]();
Y=[Link]();
repaint();
public void mouseDrag(MouseEvent me)
X=[Link]();
Y=[Link]();
repaint();
}
public void mouseExited(MouseEvent me)
public void mousePressed(MouseEvent me)
public void mouseDragged(MouseEvent me)
public void mouseReleased(MouseEvent me)
/*<applet code="[Link]"width=500 height=500></applet>*/
OUTPUT:
D:\Java>javac [Link]
D:\Java>appletviewer [Link]
RESULT:
Thus the program has been executed successfully.
[Link]
Appending Text to Existing File
Date:14.02.2015
AIM:
To write a Java program to open an existing file and append text to that file.
ALGORITHM:
Step -1: Start the process.
Step -2: Declare a class RandomAccess and declare the main method.
Step -3: Create an object for RandomAccess file as r file.
Step -4: Inside the try block, specify the name of the text file and open the file in read and
write mode.
Step -5: Using the length() function the length of the file and use seek() method to find the
end of the file.
Step -6: writeBytes() method is used to specify the text to copied in the text file.
Step -7: catch() block is used to print the exception.
Step -8: Stop the process.
APPENDING TEXT TO EXISTING FILE
PROGRAM CODING:
import [Link].*;
class RandomAccess
public static void main(String args[])
RandomAccessFile rFile;
try
rFile=new RandomAccessFile("[Link]","rw");
[Link]([Link]());
[Link]("Java is a Pure Object-Oriented Programming.");
catch(IOException ioe)
[Link](ioe);
}
OUTPUT:
D:\Java>javac [Link]
D:\Java>java [Link]
D:\Java>java [Link]
RESULT:
Thus the program has been executed successfully.