Java Lab Proograms r20 2 2
Java Lab Proograms r20 2 2
Java Lab Proograms r20 2 2
OUT-PUT:
Given quadratic equation:ax^2 + bx + c Enter
a:2
Enter b:3
Enter c:1
Roots are real and unequal First
root is:-0.5
Second root is:-1.0
OUT-PUT:
b)Bubble sort
AIM: To write a JAVA program to sort for an element in a given list of elements using bubble sort
SOURCE-CODE:
import java.util.Scanner; class
bubbledemo
{
public static void main(String args[])
{
int n, i,j, temp;
int a[ ]=new int[20];
Scanner s = new Scanner(System.in); System.out.println("Enter total
number of elements:");n = s.nextInt();
System.out.println("Enter elements:"); for (i
= 0; i < n; i++)
a[i] = s.nextInt();
for(i=0;i<n;i++)
{
for(j=0;j<n-1;j++)
{
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
System.out.println("The sorted elements are:");
for(i=0;i<n;i++)
System.out.print("\t"+a[i]);
}
}
OUT-PUT:
Enter total number of elements:10
Enter elements:
3257689140
The sorted elements are:
0123456789
c)Implementing StringBuffer
AIM: To write a JAVA program using StringBuffer to delete, remove character
SOURCE-CODE:
class stringbufferdemo
{
public static void main(String[] args)
{
StringBuffer sb1 = new StringBuffer("Hello World"); sb1.delete(0,6);
System.out.println(sb1);
StringBuffer sb2 = new StringBuffer("Some Content");
System.out.println(sb2);
sb2.delete(0, sb2.length()); System.out.println(sb2);
StringBuffer sb3 = new StringBuffer("Hello World");
sb3.deleteCharAt(0);
System.out.println(sb3);
}
}
OUT-PUT:
World
Some Content
Hello World
3. a) Implementing Class & Objects
AIM: To write a JAVA program to implement class mechanism. – Create a class, methods and invoke them
inside main method
SOURCE-CODE:
1. no return type and without parameter-list:
class A
{
int l=10,b=20;
void display()
{
System.out.println(l);
System.out.println(b);
}
}
class methoddemo
{
public static void main(String args[])
{
A a1=new A();
a1.display();
}
}
OUT-PUT:
10
20
2. no return type and with parameter-list:
class A
{
void display(int l,int b)
{
System.out.println(l);
System.out.println(b);
}
}
class methoddemo
{
public static void main(String args[])
{
A a1=new A();
a1.display(10,20);
}
}
6
OUT-PUT:
10
20
3. return type and without parameter-list
class A
{
int l=10,b=20;
int area()
{
return l*b;
}
}
class methoddemo
{
public static void main(String args[])
{
A a1=new A();
int r=a1.area();
System.out.println("The area is: "+r);
}
}
OUT-PUT:
The area is:200
4. return type and with parameter-list:
class A
{
int area(int l,int b)
{
return l*b;
}
}
class methoddemo
{
public static void main(String args[])
{
A a1=new A();
int r=a1.area(10,20);
System.out.println(“The area is:”+r);
}
}
OUT-PUT:
The area is:200
7
b)Method Overloading
AIM: To write a JAVA program implement method overloading
SOURCE-CODE:
class A
{
int l=10,b=20;int area()
{
return l*b;
}
int area(int l,int b)
{
return l*b;
}
}
class overmethoddemo
{
public static void main(String args[])
{
A a1=new A(); int r1=a1.area();
System.out.println("The area is: "+r1);int r2=a1.area(5,20); System.out.println("The area is: "+r2);
}
}
OUT-PUT:
The area is: 200The area is:
100
c)Implementing Constructor
AIM: To write a JAVAto implement constructor
SOURCE-CODEs:
A constructor with no parameters:
class A
{
int l,b;
A()
{ l=10; b=20;
}
int area()
{
return l*b;
}
}
class constructordemo
{
public static void main(String args[])
{
A a1=new A(); int r=a1.area();
System.out.println("The area is: "+r);
}
}
OUT-PUT:
The area is:200
A constructor with parameters
class A
{
8
int l,b;
A(int u,int v)
{
l=u; b=v;
}
int area()
{
return l*b;
}
}
class constructordemo
{
public static void main(String args[])
{
A a1=new A(10,20);
int r=a1.area(); System.out.println("The area is: "+r);
}
}
OUT-PUT:
The area is:200
d)Constructor Overloading
AIM: To write a JAVA program to implement constructor overloading
SOURCE-CODE:
class A
{
int l,b;
A()
{ l=10;b=20;
}
A(int u,int v)
{
l=u;b=v;
}
int area()
{
return l*b;
}
}
class overconstructdemo
{
public static void main(String args[])
{
A a1=new A(); int r1=a1.area();
System.out.println("The area is: "+r1);A a2=new A(30,40);
int r2=a2.area(); System.out.println("The area is: "+r2);
}
}
OUT-PUT:
9
4 a)Implementing Single Inheritance
AIM: To write a JAVA program to implement Single Inheritance
SOURCE-CODE:
class A
{ A()
{
System.out.println("Inside A's Constructor");
}
}
class B extends A
{ B()
{
System.out.println("Inside B's Constructor");
}
}
class singledemo
{
public static void main(String args[])
{
B b1=new B();
}
}
OUT-PUT:
Inside A's ConstructorInside B's Constructor
11
5 a) super keyword implementation
AIM: Write a JAVA program give example for “super” keyword
SOURCE-CODEs:
Using super to call super class constructor (Without parameters)
class A
{
int l,b;
A()
{ l=10;b=20;
}
}
class B extends A
{
int h;
B()
{
super();h=30;
}
int volume()
{
return l*b*h;
}
}
class superdemo
{
public static void main(String args[])
{
B b1=new B();
int r=b1.volume(); System.out.println("The vol. is: "+r);
}
}
OUT-PUT:
The vol. is:6000
Using super to call super class constructor (With parameters)
class A
{
int l,b;
A(int u,int v)
{
l=u;b=v;
}
}
class B extends A
{
int h;
B(int u,int v,int w)
{
super(u,v);h=w;
}
int volume()
{
return l*b*h;
12
}
}
class superdemo
{
public static void main(String args[])
{
B b1=new B(30,20,30);
int r=b1.volume(); System.out.println("The vol. is: "+r);
}
}
OUT-PUT:
The vol. is:18000
b)Implementing interface
AIM: To write a JAVA program to implement Interface.
SOURCE-CODEs:
(i) First form of interface implementation
interface A
{
void display();
}
class B implements A
{
public void display()
{
System.out.println("B's method");
}
}
class C extends B
{
public void callme()
{
System.out.println("C's method");
}
}
class interfacedemo
{
public static void main(String args[])
{
C c1=new C();c1.display();
c1.callme();
}
}
OUT-PUT:
B's methodC's method
13
(ii) Second form of interface implementation
interface D
{
void display();
}
interface E extends D
{
void show();
}
class A
{
void callme()
{
System.out.println("This is in callme method");
}
}
class B extends A implements E{
public void display()
{
System.out.println("This is in display method");
}
public void show()
{
System.out.println("This is in show method");
}
}
class C extends B
{
void call()
{
System.out.println("This is in call method");
}
}
class interfacedemo
{
public static void main(String args[])
{
C c1=new C();c1.display();
c1.show();
c1.callme();
c1.call();
}
}
OUT-PUT:
This is in display methodThis is in show
method This is in callme methodThis is in
call method
14
(iii) Third form of interface implementation
interface A
{
void display();
}
class B implements A
{
public void display()
{
System.out.println("This is in B's method");
}
}
class C implements A
{
public void display()
{
System.out.println("This is C's method");
}
}
class interfacedemo
{
public static void main(String args[])
{
B b1=new B(); C c1=new
C();b1.display(); c1.display();
}
}
OUT-PUT:
This is in B's methodThis is C's
method
(iv) Fourth form of interface implementation
interface A
{
void display();
}
interface B
{
void callme();
}
interface C extends A,B
{
void call();
}
class D implements C
15
{
public void display()
{
System.out.println("interface A");
}
public void callme()
{
System.out.println("interface B");
}
public void call()
{
System.out.println("interface C");
}
}
class interfacedemo
{
public static void main(String args[])
{
D d1=new D();d1.display();
d1.callme();
d1.call();
}
}
OUT-PUT:
interface A interface B interface
C
c) Runtime Polymorphism
AIM: To write a JAVA program that implements Runtime polymorphism
SOURCE-CODE:
class A
{
void display()
{
System.out.println("Inside A class");
}
}
class B extends A
{
void display()
{
System.out.println("Inside B class");
}
}
16
class C extends A
{
void display()
{
System.out.println("Inside C class");
}
}
class runtimedemo
{
public static void main(String args[])
{
A a1=new A();
B b1=new B();
C c1=new C(); A
ref;
ref=c1;
ref.display();
ref=b1;
ref.display();
ref=a1;
ref.display();
}
}
OUT-PUT:
Inside C class
Inside B class
Inside A class
6 a) Exception handling mechanism
AIM: To write a JAVA program that describes exception handling mechanism
SOURCE-CODE:
Usage of Exception Handling:
class trydemo
{
public static void main(String args[])
{
try
{
int a=10,b=0;int c=a/b;
System.out.println(c);
}
catch(ArithmeticException e)
{
System.out.println(e);
}
System.out.println("After the catch statement");
}
17
}
OUT-PUT:
java.lang.ArithmeticException: / by zeroAfter the catch statement
18
{
try
{
int a = 10, b = 0;int c =
a/b;
System.out.println (c);
}
catch(ArithmeticException e)
{
System.out.println (e);
}
}
}
OUT-PUT:
java.lang.ArithmeticException: / by zero
{
public static void main(String args[])
{
try
{
String a = null; System.out.println(a.charAt(0));
}
catch(NullPointerException e)
{
System.out.println(e);
}
}
}
OUT-PUT:
java.lang.NullPointerException
catch (FileNotFoundException e)
{
System.out.println(e);
}
}
}
OUT-PUT:
java.io.FileNotFoundException: E:\file.txt (The system cannot find the file specified)
(v) NumberFormat Exception
class numberformatdemo
{
public static void main(String args[])
{
20
try
{
int num = Integer.parseInt ("akki") ;
System.out.println(num);
}
catch(NumberFormatException e)
{
System.out.println(e);
}
}
}
OUT-PUT:
java.lang.NumberFormatException: For input string: "akki"
(vi) ArrayIndexOutOfBounds Exception
class arraybounddemo
{
public static void main(String args[])
{
try
{
int a[] = new int[5];a[6] =
9;
}
catch(ArrayIndexOutOfBoundsException e)
{
System.out.println (e);
}
}
}
OUT-PUT:
java.lang.ArrayIndexOutOfBoundsException: 6
4. creation of User Defined Exception
AIM: To write a JAVA program for creation of User Defined Exception
SOURCE-CODE:
class A extends Exception
{
A(String s1)
{
super(s1);
}
}
21
class owndemo
{
public static void main(String args[])
{
try
{
throw new A("demo ");
}
catch(Exception e)
{
System.out.println(e);
}
}
}
OUT-PUT:
A: demo
{
System.out.println(e);
}
}
}
class B implements Runnable
{
public void run()
{
try
{
for(int j=1;j<=10;j++)
{
Thread.sleep(2000);
System.out.println("hello");
}
}
catch(Exception e)
{
System.out.println(e);
}
}
}
class C implements Runnable
{
public void run()
{
try
{
for(int k=1;k<=10;k++)
{
Thread.sleep(3000);
System.out.println("welcome");
}
}
catch(Exception e)
{
System.out.println(e);
}
}
}
class runnabledemo
25
W WEST GODAVARI INSTITUTE OF SCIENCE & ENGINEERING
(Approved by AICTE, New Delhi and Affiliated to JNTU, Kakinada)
Department of Computer Science and Engineering
{
public static void main(String args[])
{
A a1=new A(); B
b1=new B(); C
c1=new C();
Thread t1=new Thread(a1);
Thread t2=new Thread(b1);
Thread t3=new Thread(c1);
t1.start();
t2.start();
t3.start();
}
}
OUT-PUT:
good morning
good morning
hello
good morning
welcome good
morninghello
good morninggood
morningwelcome
hello
goodmorning
goodmorning
hello
good morning
welcome
good morning
hello
welcomehello
hello
welcomehello
welcomehello
hello
welcome
welcome
welcome
welcome
6. Implementing isAlive() and join()
AIM: To write a program illustrating isAlive and join ()
26
W WEST GODAVARI INSTITUTE OF SCIENCE & ENGINEERING
(Approved by AICTE, New Delhi and Affiliated to JNTU, Kakinada)
Department of Computer Science and Engineering
SOURCE-CODE:
class A extends Thread
{
public void run()
{
try
{
for(int i=1;i<=10;i++)
{
sleep(1000);
System.out.println("good morning");
}
}
catch(Exception e)
{
System.out.println(e);
}
}
}
class B extends Thread
{
public void run()
{
try
{
for(int j=1;j<=10;j++)
{
sleep(2000); System.out.println("hello");
}
}
catch(Exception e)
{
System.out.println(e);
}
}
}
class C extends Thread
{
public void run()
27
W WEST GODAVARI INSTITUTE OF SCIENCE & ENGINEERING
(Approved by AICTE, New Delhi and Affiliated to JNTU, Kakinada)
Department of Computer Science and Engineering
{
try
{
for(int k=1;k<=10;k++)
{
sleep(3000);
System.out.println("welcome");
}
}
catch(Exception e)
{
System.out.println(e);
}
}
}
class isalivedemo
{
public static void main(String args[])
{
A a1=new A(); B
b1=new B(); C
c1=new C();
a1.start();
b1.start();
c1.start();
System.out.println(a1.isAlive());
System.out.println(b1.isAlive());
System.out.println(c1.isAlive());try
{
a1.join();
b1.join();
c1.join();
}
catch(InterruptedException e)
{
System.out.println(e);
}
System.out.println(a1.isAlive());
System.out.println(b1.isAlive());
System.out.println(c1.isAlive());
28
W WEST GODAVARI INSTITUTE OF SCIENCE & ENGINEERING
(Approved by AICTE, New Delhi and Affiliated to JNTU, Kakinada)
Department of Computer Science and Engineering
}
}
OUT-PUT:
true good morning
true hello
true welcome
good morning hello
good morning hello
hello welcome
good morning hello
welcome welcome
good morning hello
hello hello
good morning welcome
good morning welcome
welcome welcome
hello welcome
good morning false
good morning false
hello false
good morning
welcome
A a2=new A();
A a3=new A();
a1.setDaemon(true);
a1.start();
a2.start();
a3.start();
}
}
OUT-PUT:
daemon thread
work user thread
work user thread
work
8. Producer-Consumer problem
AIM: Write a JAVA program Producer Consumer Problem
SOURCE-CODE:
class A
{
int n;
boolean b=false;
synchronized int get()
{
if(!b)
try
{
wait();
}
catch(Exception e)
{
System.out.println(e);
}
System.out.println("Got:"+n);
b=false;
notify();
return
n;
}
synchronized void put(int n)
{
if(b)
try
30
W WEST GODAVARI INSTITUTE OF SCIENCE & ENGINEERING
(Approved by AICTE, New Delhi and Affiliated to JNTU, Kakinada)
Department of Computer Science and Engineering
{
wait();
31
W WEST GODAVARI INSTITUTE OF SCIENCE & ENGINEERING
(Approved by AICTE, New Delhi and Affiliated to JNTU, Kakinada)
Department of Computer Science and Engineering
}
catch(Exception e)
{
System.out.println(e);
}
this.n=n;
b=true;
System.out.println("Put:"+n);
notify();
}
}
class producer implements Runnable
{
A a1;
Thread t1;
producer(A a1)
{
this.a1=a1;
t1=new Thread(this);
t1.start();
}
public void run()
{
for(int i=1;i<=10;i++)
{
a1.put(i);
}
}
}
class consumer implements Runnable
{
A a1;
Thread t1;
consumer(A a1)
{
this.a1=a1;
t1=new Thread(this);
t1.start();
}
public void run()
{
for(int j=1;j<=10;j++)
32
W WEST GODAVARI INSTITUTE OF SCIENCE & ENGINEERING
(Approved by AICTE, New Delhi and Affiliated to JNTU, Kakinada)
Department of Computer Science and Engineering
{
a1.get();
}
}
}
class interdemo
{
public static void main(String args[])
{
A a1=new A();
producer p1=new producer(a1);
consumer c1=new
consumer(a1);
}
}
OUT-PUT:
Put:1
Got:1
Put:2
Got:2
Put:3
Got:3
Put:4
Got:4
Put:5
Got:5
Put:6
Got:6
Put:7
Got:7
Put:8
Got:8
Put:9
Got:9
Put:10
Got:10
34
W WEST GODAVARI INSTITUTE OF SCIENCE & ENGINEERING
(Approved by AICTE, New Delhi and Affiliated to JNTU, Kakinada)
Department of Computer Science and Engineering
}
}
}
OUT-PUT:
E:/java%20work/
Step-2:
In System Properties click Advanced and then click Environment Variables
35
W WEST GODAVARI INSTITUTE OF SCIENCE & ENGINEERING
(Approved by AICTE, New Delhi and Affiliated to JNTU, Kakinada)
Department of Computer Science and Engineering
Step-3:
In Environment Variables click New in System variables
It displays the following “New System Variable” dialog box.
Step-4:
Now type variable name as a path and then variable value as
c:\SOURCE-CODE Files\java\jdk1.5.0_10\bin;
Step-5:
Click OK
package mypack;
import mypack.box;
class packagedemo
{
public static void main(String args[])
{
box b1=new
box();
b1.display();
}
}
3. Now compile the above SOURCE-CODE in the current working directory d:\
javac packagedemo.java
37
W WEST GODAVARI INSTITUTE OF SCIENCE & ENGINEERING
(Approved by AICTE, New Delhi and Affiliated to JNTU, Kakinada)
Department of Computer Science and Engineering
4. Execute the above SOURCE-CODE in current working directory
38
W WEST GODAVARI INSTITUTE OF SCIENCE & ENGINEERING
(Approved by AICTE, New Delhi and Affiliated to JNTU, Kakinada)
Department of Computer Science and Engineering
java packagedemo
OUT-PUT:
10
20
}
OUT-PUT:
synchronized(this)
{
notify();
}
}
}
}
public void stop()
{
b=true;
}
public void run()
{
try
{
while(true)
{
Calendar
clndr=Calendar.getInstance();
h=clndr.get(Calendar.HOUR_OF_DAY);
if(h>12)h-=12;
m=clndr.get(Calendar.MINUTE); s=clndr.get(Calendar.SECOND);
SimpleDateFormat frmatter=new SimpleDateFormat("hh:mm:ss",
Locale.getDefault());
Date d=clndr.getTime(); str=frmatter.format(d);if(b)
{
synchronized (this)
{
while(b)
{
wait();
}
}
}
repaint();
thr.sleep(1000);
}
}
catch(Exception e)
{
System.out.println(e);
}
41
W WEST GODAVARI INSTITUTE OF SCIENCE & ENGINEERING
(Approved by AICTE, New Delhi and Affiliated to JNTU, Kakinada)
Department of Computer Science and Engineering
}
42
W WEST GODAVARI INSTITUTE OF SCIENCE & ENGINEERING
(Approved by AICTE, New Delhi and Affiliated to JNTU, Kakinada)
Department of Computer Science and Engineering
OUT-PUT:
AIM: To write a JAVA program to create different shapes and fill colors using Applet
SOURCE-CODE:
import java.awt.*; import
java.applet.*;
//<applet code="graphicsdemo" width="400"
height="400"></applet>public class graphicsdemo extends Applet
{
public void paint(Graphics g)
{
int x[]={10,220,220};
int y[]={400,400,520};
int n=3; g.drawLine(10,30,200,30);
g.setColor(Color.blue); g.drawRect(10,40,200,30); g.setColor(Color.red);
g.fillRect(10,80,200,30); g.setColor(Color.orange);
g.drawRoundRect(10,120,200,30,20,20); g.setColor(Color.green);
g.fillRoundRect(10,160,200,30,20,20); g.setColor(Color.blue);
g.drawOval(10,200,200,30); g.setColor(Color.black);
g.fillOval(10,240,40,40); g.setColor(Color.yellow);
g.drawArc(10,290,200,30,0,180); g.setColor(Color.yellow);
g.fillArc(10,330,200,30,0,180); g.setColor(Color.pink); g.fillPolygon(x,y,n);
}
}
OUT-PUT:
AIM: To write a JAVA program that display the x and y position of the cursor movement
usingMouse.
SOURCE-CODE:
import java.awt.*; import
java.awt.event.*;import
java.applet.*;
//<applet code="mouseevent" width=450
height=300></applet> public class mouseevent extends Applet
implements MouseListener, MouseMotionListener
{
String s1="
";int x,y;
public void init()
{
addMouseListener(this); addMouseMotionListener(this);
}
public void mouseClicked(MouseEvent me)
{ x=100
; y=100;
s1="Mouse clicked";
repaint();
}
public void mouseEntered(MouseEvent me)
{ x=100
; y=200;
s1="Mouse entered";
repaint();
}
public void mouseExited(MouseEvent me)
{ x=100
; y=300;
s1="Mouse exited";
repaint();
}
public void mousePressed(MouseEvent me)
{
x=me.getX();
y=me.getY();
s1="Mouse Pressed";
repaint();
}
public void mouseReleased(MouseEvent me)
45
W WEST GODAVARI INSTITUTE OF SCIENCE & ENGINEERING
(Approved by AICTE, New Delhi and Affiliated to JNTU, Kakinada)
Department of Computer Science and Engineering
{
x=me.getX();
y=me.getY(); s1="Mouse
Realeased";repaint();
}
public void mouseDragged(MouseEvent me)
{
x=me.getX();
y=me.getY();
s1="Mouse Dragged";
repaint();
}
public void mouseMoved(MouseEvent me)
{
x=me.getX();
y=me.getY();
s1="Mouse Moved";
repaint();
}
public void paint(Graphics g)
{
g.drawString(s1,x,y);
}
}
OUT-PUT:
46
W WEST GODAVARI INSTITUTE OF SCIENCE & ENGINEERING
(Approved by AICTE, New Delhi and Affiliated to JNTU, Kakinada)
Department of Computer Science and Engineering
AIM: To write a JAVA program that identifies key-up key-down event user entering textin a
Applet.
SOURCE-CODE:
import java.awt.*; import
java.awt.event.*;import
java.applet.*;
//<applet code="keyevent" width=450 height=300></applet>
public class keyevent extends Applet implements KeyListener
{
String s1="
";int x,y;
public void init()
{
addKeyListener(this);
requestFocus();
}
public void keyPressed(KeyEvent ke)
{ x=100
; y=200;
s1= "key pressed ";
repaint();
}
public void keyReleased(KeyEvent ke)
{ x=100
; y=400;
s1= "key Released ";
repaint();
}
public void keyTyped(KeyEvent ke)
{
s1=s1+ke.getKeyChar();
repaint();
}
public void paint(Graphics g)
{
g.drawString(s1,x,y);
}
}
OUT-PUT:
47
W WEST GODAVARI INSTITUTE OF SCIENCE & ENGINEERING
(Approved by AICTE, New Delhi and Affiliated to JNTU, Kakinada)
Department of Computer Science and Engineering
48