Professional Documents
Culture Documents
1216BECE30042
1216BECE30042
1216BECE30042
1216BECE30042
Practical No: 17
Aim: Write a program to implement the concept
of interfaces
Source:
interface myinterface1
{
void method1(int var1);
}
interface myinterface2
{
void method2(int var2);
}
class myclass implements myinterface1,myinterface2
{
public void method1(int x)
{
System.out.println("method called by" +x);
}
public void method2(int y)
{
System.out.println("method called by"+y);
}
}
class interfacedemo
{
public static void main(String args[])
{
LDRP ITR, CE-IT
OUTPUT
method called by1
method called by2
Practical No: 18
Aim: Write
keyword
program
to
implement
Source:
interface myinterface{
void method(int var);
static final int a = 50;
}
class myclass implements myinterface{
public void method(int x) {
System.out.println("method is called");
}}
class finaldemo{
public static void main(String args[])
{
myinterface obj1 = new myclass();
obj1.a = obj1.a+10; //final can't be assigned value again
obj1.method(0);
}}
OUTPUT
Finaldemo.java:21 : error: cannot assign a value to final variable a
obj1.a = obj1.a+10;
^
1 error
final
Practical No: 19
Aim: Write a program in which a class extends
another class & implements an interface also.
Source:
interface myinterface
{
void call (int x);
}
class parentclass implements myinterface
{
public void call(int a)
{
System.out.println("INTERFACE IS CALLED");
}
parentclass()
{
System.out.println("Inheritance");
}
}
class child extends parentclass
{
child()
{
super();
}
}
class dualdemo
{
public static void main(String args[])
{
child obj1 = new child();
myinterface obj2 = new child();
obj2.call(0);
}
}
OUTPUT
Inheritance
Inheritance
INTERFACE IS CALLED
Practical No: 20
Aim:
Write
a
program
showing
the
implementation of how interfaces can be
extended.
Source:
interface myinterface1
{
void method1();
}
interface myinterface2 extends myinterface1
{
void method2();
}
class myclass implements myinterface2
{
public void method1()
{
System.out.println("method 1 is called");
}
public void method2()
{
System.out.println("method 2 is called");
}
}
class extinter
LDRP ITR, CE-IT
{
public static void main(String args[])
{
myinterface2 obj1 = new myclass();
obj1.method1();
obj1.method2();
}
}
OUTPUT
method 1 is called
method 2 is called
Practical No: 21
Aim: Create a package & import the methods of
that package in your class.
Source:
Code 1:
package classbulk;
public class bal
{
String name;
int bal;
public bal(String a, int x)
{
name=a;
bal = x;
}
public void display()
{
System.out.println(name + ": $" +bal);
}
}
Code 2:
import classbulk.*;
class testpack
{
public static void main(String args[])
{
LDRP ITR, CE-IT
OUTPUT
ldrp-itr: $50000
Practical No: 22
Aim: Write
concept of
Handling
Source:
class exdemo
{
public static void main(String args[])
{
try
{
for(int i=2;i>=0;i--)
{
System.out.println(10/i);
}
}
catch(ArithmeticException ex)
{
System.out.println("exception caught");
System.out.println(ex);
}
}
}
OUTPUT
5
10
Exception caught
Java.lang.ArithmeticException: / by zero
LDRP ITR, CE-IT
Practical No: 23
Aim: Write a program to implement Try with
multiple catches.
Source:
class exdemo1
{
public static void main(String args[])
{
int a[] = new int[9];
try
{
for(int i=10;i>=0;i--)
{
System.out.println(10/i);
a[i]=i;
}
}
catch(ArithmeticException ex)
{
System.out.println("exceptioncaughtArithmeticException");
System.out.println(ex);
}
catch(IndexOutOfBoundsException e)
{
System.out.println("exception caught IndexOutOfBoundsException");
System.out.println(e);
}
}}
OUTPUT
1
exception caught IndexOutOfBoundsException
Java.lang.ArrayIndexOutOfBoundsException: 10
Practical No: 28
Aim: Write a program
threads in Java.
to
create
Source:
Calss A extends Thread
{
public void run()
{
for(int i=1; i<=5; i++)
{
System.out.println("From Thread A : I ="+i);
}
System.out.println("Exit From A");
}
}
class B extends Thread
{
public void run()
{
for(int j=1; j<=5; j++)
{
System.out.println("From Thread B : J ="+j);
}
System.out.println("Exit From B");
}
}
class C extends Thread
{
public void run()
{
for(int k=1; k<=5; k++)
{
System.out.println("From Thread C : K ="+k);
}
System.out.println("Exit From C");
}
}
class multithread
LDRP ITR, CE-IT
multiple
{
public static void main(String a[])
{
A a1=new A();
a1.start();
B b1=new B();
b1.start();
C c1=new C();
c1.start();
}
}
OUTPUT
From Thread A : I =1
From Thread B : J =1
From Thread A : I =2
From Thread A : I =3
From Thread A : I =4
From Thread A : I =5
Exit From A
From Thread B : J =2
From Thread C : K =1
From Thread C : K =2
From Thread B : J =3
From Thread B : J =4
From Thread B : J =5
Exit From B
From Thread C : K =3
From Thread C : K =4
From Thread C : K =5
Exit From C
Practical No: 29
Aim: Write a program to implement various
LDRP ITR, CE-IT
if(k==1)
try
{
sleep(1000);
}
catch(Exception e){}
}
System.out.println("exit from C");
}
}
class thread method
{
public static void main(String args[])
{
A threadA=new A();
B threadB=new B();
C threadC=new C();
LDRP ITR, CE-IT
OUTPUT
start thread A
start thread B
start thread C
from thread B: j=1
from thread A: i=1
from thread C: k=1
from thread B: j=2
END OF MAIN THREAD
from thread B: j=3
from thread A: i=2
from thread A: i=3
from thread A: i=4
from thread A: i=5
exit from A
from thread C: k=2
from thread C: k=3
from thread C: k=4
from thread C: k=5
exit from C
Practical No: 30
Aim Write a program to implement thread
priorities.
Source:
LDRP ITR, CE-IT
{
public static void main(String args[])
{
Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
clicker hi = new clicker(Thread.NORM_PRIORITY + 2);
clicker mi = new clicker(Thread.NORM_PRIORITY);
clicker lo = new clicker(Thread.NORM_PRIORITY - 2);
lo.start();
mi.start();
hi.start();
try
{
Thread.sleep(500);
}
catch (InterruptedException e)
{
System.out.println("Main thread interrupted.");
}
lo.stop();
mi.stop();
hi.stop();
System.out.println("Low-priority thread: " + lo.click);
System.out.println("Mid-priority thread: " +mi.click);
System.out.println("High-priority thread: " + hi.click);
}
}
OUTPUT
Low-priority thread: 164202742
Mid-priority thread: -1739598247
High-priority thread: -1350618478
Practical No: 31
Aim Write a program to create thread using
Runnable interface.
LDRP ITR, CE-IT
Source:
class NewThread implements Runnable
{
Thread t;
NewThread()
{
t = new Thread(this, "Demo Thread");
t.start();
}
public void run()
{
try
{
for(int i = 5; i > 0; i--)
{
System.out.println("Child Thread: " + i);
Thread.sleep(500);
}
}
catch (InterruptedException e)
{
System.out.println("Child interrupted.");
}
System.out.println("Exiting child thread.");
}
}
LDRP ITR, CE-IT
class ThreadDemo
{
public static void main(String args[])
{
new NewThread();
try
{
for(int i = 5; i > 0; i--)
{
System.out.println("Main Thread: " + i);
Thread.sleep(1000);
}
}
catch (InterruptedException e)
{
System.out.println("Main thread interrupted."); }
System.out.println("Main thread exiting.");
}
}
OUTPUT
Main Thread :5
Child Thread :5
Child Thread :4
Main Thread :4
Child Thread :3
LDRP ITR, CE-IT
Child Thread :2
Main Thread :3
Child Thread :1
Exiting child thrad.
Main Thread :2
Main Thread :1
Exiting main thread.
Practical No: 32
Aim Write an applet program to print LDRPLDRP ITR, CE-IT
OUTPUT
Practical No: 33
Aim: Write an applet program to divide applet
LDRP ITR, CE-IT
OUTPUT
Practical No: 34
Aim: Create
applet.
an
animation
for
circle
Source:
import java.awt.*;
import java.applet.*;
/*
<applet code="MoveCircle" width=300 height=300>
</applet>
*/
public class MoveCircle extends Applet implements Runnable
{
Thread t;
int i=0,j=0,size=1;
public void init()
{
setBackground(Color.green);
setForeground(Color.black);
}
public void start()
{
t = new Thread(this);
t.start();
}
public void run()
{
int d=0;
while(d<size)
{
try
{
Thread.sleep(100);
}
catch(InterruptedException ex)
{
System.out.print("Problem");
}
d++;
LDRP ITR, CE-IT
using
repaint();
}
}
public void paint(Graphics g)
{
Dimension d = getSize();
int x = d.width;
//Getting Size of X-Dimension
int y = d.height; //Getting Size of Y-Dimension
size=x/2;
g.drawOval((x/2)-i/2,(y/2)-j/2,++i, ++j);
}
}
OUTPUT
Practical No: 35
LDRP ITR, CE-IT
catch(Exception e)
{
System.out.print(e);
}
repaint();
cnt++;
}
}
}
OUTPUT
Practical No: 36
Aim: Write an applet program to display image
and play audio.
LDRP ITR, CE-IT
Source:
import java.awt.*;
import java.applet.*;
/*
<applet code="DisplayImageplayaudio" width=1200 height=800>
</applet>
*/
public class DisplayImageplayaudio extends Applet
{
Image i;
AudioClip audioClip;
public void init()
{
i = getImage(getCodeBase(),"IMG_5055.JPG");
audioClip = getAudioClip(getCodeBase(), "Bey Yaar Tara
Vina - 320KBps [Gaana4u.Com].mp3");
}
public void start()
{
audioClip.play();
}
public void paint(Graphics g)
{
String str = "Displaying...& Playing Audio " + getCodeBase() +
"IMG_5055.JPG & Bey Yaar Tara Vina - 320KBps [Gaana4u.Com].mp3";
g.drawString(str,15,0);
g.drawImage(i, 20,20, this);
}
}
OUTPUT
LDRP ITR, CE-IT
Practical No: 37
Aim: Write an event program to make addition
of two values using TextField and Button
control.
LDRP ITR, CE-IT
Source:
import java.awt.*;
import java.applet.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
/*
<applet code="Addition" width=300 height=300>
</applet>
*/
{
}
public void actionPerformed(ActionEvent e)
{
int a, b, c;
a = Integer.parseInt(t1.getText()) ;
b = Integer.parseInt(t2.getText()) ;
c = a + b;
t3.setSize(200 , 20);
t3.setText("Ans is " + c);
}
}
OUTPUT
Practical No: 38
Aim: Write an event program which will draw
line according to mouse location. (Use
LDRP ITR, CE-IT
int x1,x2,y1,y2;
public void init()
{
x1=x2=y1=y2=0;
new DrawLineDemo(this);
}
public void paint(Graphics G)
{
G.drawLine(x1, y1, x2, y2);
update(G);
}
public void update(Graphics G)
{
G.drawLine(x1, y1, x2, y2);
}
}
OUTPUT
Practical No: 39
Aim: Write an event program which
demonstrate the use of Key Events
LDRP ITR, CE-IT
will
Source:
import java.awt.*;
import java.applet.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
/*
<applet code="Key" width=300 height=400>
</applet>
*/
public class Key extends Applet implements KeyListener
{
int X=20,Y=30;
String msg="KeyEvents--->";
public void init()
{
addKeyListener(this);
requestFocus();
setBackground(Color.green);
setForeground(Color.blue);
}
public void keyPressed(KeyEvent k)
{
showStatus("KeyDown");
int key=k.getKeyCode();
switch(key)
{
case KeyEvent.VK_UP:
showStatus("Move to Up");
break;
case KeyEvent.VK_DOWN:
showStatus("Move to Down");
break;
case KeyEvent.VK_LEFT:
showStatus("Move to Left");
break;
case KeyEvent.VK_RIGHT:
showStatus("Move to Right");
break;
}
repaint();
}
LDRP ITR, CE-IT
OUTPUT
Practical No: 40
Aim: Write an applet that contains three
buttons OK,CANCEL and HELP and one
LDRP ITR, CE-IT
{
if(e.getSource()==ok)
{
t.setForeground(Color.BLUE);
t.setBackground(Color.red);
Font f = new Font("ARIAL", 2, 10);
t.setFont(f);
t.setText("OK Pressed");
}
else if(e.getSource()==can)
{
t.setBackground(Color.green);
t.setText("Cancel Pressed");
}
else
{
t.setBackground(Color.yellow);
t.setText("Help Pressed");
}
}
}
OUTPUT
Practical No: 41
Aim: Write a program to scan the value from
user and write that data inside the file-1. Copy
content of file-1 into file-2 and at last display
LDRP ITR, CE-IT
OUTPUT
C:\JAVA>javac Practical41.java
C:\JAVA>java Practical41 1.txt 2.txt
By,Aman Lokandwala
1.txt
By,Aman Lokandwala
2.txt
By,Aman Lokandwala
Practical No: 42
Aim: Write a program that counts the no. of
words in a text file. The file name is passed as
LDRP ITR, CE-IT
{
System.out.println("Total No of Words="+(count+1));
}
else
{
System.out.println("Total No of Words="+count);
}
fin.close();
}
}
OUTPUT
3.txt
Created by Aman Lokhandwala, roll no 42.
4.txt
C:\JAVA>java Prac43 3.txt
Total No of Words=7
C:\JAVA>java Prac43 4.txt
Total No of Words=0