Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
4 views

Java Lab Program With Aim & Algorithm

The document outlines various Java applications, including string extraction, multiple inheritance using interfaces, exception creation, multithreading, drawing shapes, creating frames with text fields, and multiple selection list boxes. Each section includes an aim, algorithm, coding examples, and output results. The applications demonstrate fundamental Java programming concepts and GUI functionalities.

Uploaded by

gowthamiguna777
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Java Lab Program With Aim & Algorithm

The document outlines various Java applications, including string extraction, multiple inheritance using interfaces, exception creation, multithreading, drawing shapes, creating frames with text fields, and multiple selection list boxes. Each section includes an aim, algorithm, coding examples, and output results. The applications demonstrate fundamental Java programming concepts and GUI functionalities.

Uploaded by

gowthamiguna777
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 47

EX.

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:

Step 1 : Start the Process.


Step 2 : Declare the string and assign the value of the string.
Step 3 : Assigning the starting and ending position.
Step 4 : Creating the character array.
Step 5 : Get the value from the string starting and ending position using getChars() method.
Step 6 : Print the extracted string.

Step 7 : Stop the process.

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

Enter the string: Welcome to the java programming

Enter the starting position: 0

Enter the ending position: 9

Extracted string is Welcome to

3
EX.NO : 2
MULTIPLE INHERITANCE USING INTERFACE
DATE :

AIM:

To create a java application to implement the concept of multiple inheritance using


interface.

ALGORITHM:

Step 1 : Start the Process.


Step 2 : Create class Student.
Step 3 : Create class Test extends the class Student.
Step 4 : Create interface Sports.
Step 5 : Create class Result which extends class Test and implements interface Sports.
Step 6 : Create object reference for Result class
Step 7 : Pass roll number and marks as arguments.
Step 8 : Display the student details using interface concept.
Step 9 : Stop the process.

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

Total Score =63.5

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:

Step 1 : Start the Process.


Step 2 : Create two class variables (i) name and (ii) basic pay.
Step 3 : Write a method to display( ) to display the employee salary details.
Step 4 : Give values to class variables.
Step 5 : Check if the employee basic pay is less than or equal to 1000, then it throws user-
defined exception.
Step 6 : Else calculate the employee salary and execute the method display( ).
Step 7 : Stop the process.

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

CHECK PAY OUT OF BOUNDS EXCEPTION

EMPLOYEE PAY SLIP

********************

NAME : Shiva

BASIC SALARY 10000

MEDICAL ALLOWANCE 500

HOUSE RENT ALLOWANCE 1000

GROSS PAY 11500

PROVIDENT FUND 1000

LIC 2000

NET AMOUNT 12500

EMPLOYEE PAY SLIP

********************

Entered basic salary less than 1000

So this Employee pay slip will not be displayed

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:

Step 1 : Start the Process.


Step 2 : Declare three classes which extends Thread class.
Step 3 : Override run( ) method in each class for the Thread function.
Step 4 : Create an object reference for each class.
Step 5 : Set priority for each class.
Step 6 : Initiate each class using start( ) method.

Step 7 : Stop the process.

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:

To create a java application to draw several shapes in the created window.

ALGORITHM:

Step 1 : Start the Process.


Step 2 : Create the class which extends Applet.
Step 3 : Override the paint ( ) method.
Step 4 : Draw the shapes Line, Rectangle, Round Rectangle, Oval, Arc and Polygon.

Step 5 : Display the shapes.


Step 6 : Stop the process.

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:

Step 1 : Start the Process.

Step 2 : Create a Frame.


Step 3 : Add Labels, TextFields and Buttons into the Frame using add( ) method.
Step 4 : Override the method actionPerformed( ).

Step 5 : Set the value to Labels and actions to Button.


Step 6 : Display the Labels, TextFields and Buttons using show( ) method.
Step 7 : Stop the process.

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:

To create a java application to demonstrate the Multiple Selection Listbox.

ALGORITHM:

Step 1 : Start the Process.


Step 2 : Declare class which extends Applet and implements ActionListener.
Step 3 : Add Listbox.
Step 4 : Override init( ) method.

Step 5 : Override actionPerformed( ) method.


Step 6 : Using paint( ) method to display the selected content of the list by clicking.
Step 7 : Stop the process.

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

Note: P7.java uses or overrides a deprecated API.

Note: Recompile with -deprecation for details.

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:

Step 1 : Start the Process.


Step 2 : Create a Frame.
Step 3 : Add Button, TextField and TextArea into the Frame using add( ) method.
Step 4 : Use show( ) method to display the Button, TextField and TextArea.
Step 5 : Override actionPerformed( ) method.
Step 6 : Stop the process.

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 1 : Start the Process.


Step 2 : Create the class which extends Frame and implements ActionListener.
Step 3 : Create the Frame.
Step 4 : Add MenuBar, Menu, MenuItems and Button into the Frame using add( ) method.

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:

Step 1 : Start the Process.


Step 2 : Create the class which extends Frame and implements MouseListener and
ActionListener.
Step 3 : Create the Frame.
Step 4 : Add Label and Button into the Frame using add( ) method.
Step 5 : Display the Label and Button using show( ) method.
Step 6 : Override the method actionPerformed( ).
Step 7 : Stop the process.

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:

Step 1 : Start the Process.


Step 2 : Create the class which extends Applet.
Step 3 : Override the init( ) method.
Step 4 : Override the paint( ) method.
Step 5 : Draw and Display the Shapes.
Step 6 : Stop the process.

41
CODING:

/*<applet code = "P11" width = 800 height = 800></applet>*/


import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class P11 extends Applet
{
int X = 100;
int Y = 100;
{
addMouseListener(new MouseAdapter( )
{
public void mouseClicked(MouseEvent e)
{
repaint( );
X = e.getX( );
Y = e.getY( );
}
});
}
public void paint(Graphics g)
{
g.drawRect(X,Y,100,100);
g.drawRect(X+50,Y,100,150);
g.drawOval(X+100,Y,100,100);
g.drawOval(X,Y+100,100,100);
}
}

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:

Step 1 : Start the Process.


Step 2 : Create the class for access the Random Access File.
Step 3 : Using object reference to open an existing File in readwrite mode.
Step 4 : Write any statement in the field at this end.

Step 5 : Close the file.


Step 6 : Display the result.
Step 7 : Stop the process.

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

You might also like