1216BECE30042

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 47

Object Oriented Programing With Java

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

Object Oriented Programing With Java


1216BECE30042

myinterface1 obj1 = new myclass();


myinterface2 obj2 = new myclass();
obj1.method1(1);
obj2.method2(2);
}
}

OUTPUT
method called by1
method called by2

LDRP ITR, CE-IT

Object Oriented Programing With Java


1216BECE30042

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

LDRP ITR, CE-IT

final

Object Oriented Programing With Java


1216BECE30042

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();
}
}

LDRP ITR, CE-IT

Object Oriented Programing With Java


1216BECE30042

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

LDRP ITR, CE-IT

Object Oriented Programing With Java


1216BECE30042

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

Object Oriented Programing With Java


1216BECE30042

{
public static void main(String args[])
{
myinterface2 obj1 = new myclass();
obj1.method1();
obj1.method2();
}
}

OUTPUT
method 1 is called
method 2 is called

LDRP ITR, CE-IT

Object Oriented Programing With Java


1216BECE30042

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

Object Oriented Programing With Java


1216BECE30042

bal obj1 = new bal("ldrp-itr",50000);


obj1.display();
}
}

OUTPUT
ldrp-itr: $50000

LDRP ITR, CE-IT

Object Oriented Programing With Java


1216BECE30042

Practical No: 22
Aim: Write
concept of
Handling

a program to implement the


Try-Catch block in Exception

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

Object Oriented Programing With Java


1216BECE30042

LDRP ITR, CE-IT

Object Oriented Programing With Java


1216BECE30042

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);
}
}}

LDRP ITR, CE-IT

Object Oriented Programing With Java


1216BECE30042

OUTPUT
1
exception caught IndexOutOfBoundsException
Java.lang.ArrayIndexOutOfBoundsException: 10

LDRP ITR, CE-IT

Object Oriented Programing With Java


1216BECE30042

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

Object Oriented Programing With Java


1216BECE30042

{
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

Object Oriented Programing With Java


1216BECE30042

method in Thread like yield(), sleep(), stop()


etc.
Source:
class A extends Thread
{
public void run()
{
for(int i=1;i<=5;i++)
{
if(i==1) yield();
System.out.println("\t 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("\t from thread B: j=" +j);
if(j==3) stop();
}
System.out.println("exit from B");
}
}
LDRP ITR, CE-IT

Object Oriented Programing With Java


1216BECE30042

class C extends Thread


{
public void run()
{
for(int
k=1;k<=5;k++)
{
System.out.println("\t from thread C: k=" +k);

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

Object Oriented Programing With Java


1216BECE30042

System.out.println("start thread A");


threadA.start();
System.out.println("start thread B");
threadB.start();
System.out.println("start thread C");
threadC.start();
System.out.println("END OF MAIN THREAD");
}
}

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

Object Oriented Programing With Java


1216BECE30042

class clicker implements Runnable


{
int click = 0;
Thread t;
private volatile boolean running = true;
public clicker(int p)
{
t = new Thread(this);
t.setPriority(p);
}
public void run()
{
while (running)
{
click++;
}
}
public void stop()
{
running = false;
}
public void start()
{
t.start();
}
}
class Priority
LDRP ITR, CE-IT

Object Oriented Programing With Java


1216BECE30042

{
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);
}
}

LDRP ITR, CE-IT

Object Oriented Programing With Java


1216BECE30042

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

Object Oriented Programing With Java


1216BECE30042

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

Object Oriented Programing With Java


1216BECE30042

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

Object Oriented Programing With Java


1216BECE30042

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

Object Oriented Programing With Java


1216BECE30042

ITR at the center of the applet. Print Address


of LDRP in the status bar.
Sourcecode:
import java.awt.*;
import java.applet.*;
/*
<applet code="FirstApplet" width=300 height=300>
</applet>
*/
public class FirstApplet extends Applet
{
public void init()
{
setBackground(Color.green);
setForeground(Color.black);
}
public void start()
{
}
public void paint(Graphics g)
{
int x = getWidth();
int y = getHeight();
g.drawString("LDRP-ITR",x/2-22,y/2-22);
}
}

OUTPUT

LDRP ITR, CE-IT

Object Oriented Programing With Java


1216BECE30042

Practical No: 33
Aim: Write an applet program to divide applet
LDRP ITR, CE-IT

Object Oriented Programing With Java


1216BECE30042

into four equal parts (Vertically) and fill with


different colors.
Source:
/*
<applet code="FillRect" width=400 height=400>
</applet>
*/
import java.awt.*;
import java.applet.*;
public class FillRect extends Applet
{
public void paint(Graphics g)
{
Color col[] = {Color.BLUE, Color.RED, Color.LIGHT_GRAY,
Color.ORANGE};
Dimension d = getSize();
for(int i=0;i<4;i++)
{
g.setColor(col[i]);
int inc = d.height/ 4;
g.fillRect(i*inc, 0, inc,d.width);
}
}
}

OUTPUT

LDRP ITR, CE-IT

Object Oriented Programing With Java


1216BECE30042

LDRP ITR, CE-IT

Object Oriented Programing With Java


1216BECE30042

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

Object Oriented Programing With Java


1216BECE30042

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

Object Oriented Programing With Java


1216BECE30042

Aim: Divide applet into eight different arcs and


fill those arcs using eight different colors. Color
of the arc changes after every second.
Source:
/*
<applet code="FillArcThread" width=300 height=300>
</applet>
*/
import java.awt.*;
import java.applet.*;
public class FillArcThread extends Applet implements Runnable
{
int cnt=0;
public void paint(Graphics g)
{
Color c[] = {Color.BLUE, Color.RED, Color.DARK_GRAY,
Color.GREEN, Color.MAGENTA, Color.ORANGE, Color.PINK,
Color.YELLOW};
Dimension d = getSize();
for(int i=0;i<8;i++)
{
g.setColor(c[(cnt+i)%8]);
g.fillArc(0, 0, d.width, d.height, i*45, 45);
}
}
Thread t;
public void init()
{
t = new Thread(this);
t.start();
}
public void run()
{
while(true)
{
try
{
Thread.sleep(1000);
}
LDRP ITR, CE-IT

Object Oriented Programing With Java


1216BECE30042

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

Object Oriented Programing With Java


1216BECE30042

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

Object Oriented Programing With Java


1216BECE30042

Practical No: 37
Aim: Write an event program to make addition
of two values using TextField and Button
control.
LDRP ITR, CE-IT

Object Oriented Programing With Java


1216BECE30042

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 class Addition extends Applet implements ActionListener


{
Button b1;
TextField t1, t2, t3;
public void init()
{
b1 = new Button("Add");
t1 = new TextField();
t2 = new TextField();
t3 = new TextField("Ans");
add(b1);
add(t1);
add(t2);
add(t3);
b1.addActionListener(this);
}
public void start()
LDRP ITR, CE-IT

Object Oriented Programing With Java


1216BECE30042

{
}
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

Object Oriented Programing With Java


1216BECE30042

MousePressed and MouseReleased event)


Source:
/*
<applet code="DrawLineMouse" width=300 height=300>
</applet>
*/
import java.awt.*;
import java.applet.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
class DrawLineDemo extends MouseAdapter implements MouseMotionListener
{
DrawLineMouse m;
public DrawLineDemo(DrawLineMouse m)
{
this.m =m;
m.addMouseListener(this);
m.addMouseMotionListener(this);
}
public void mouseMoved(MouseEvent e)
{
m.showStatus("X : " + e.getX() + " Y : " + e.getY());
m.repaint();
}
public void mousePressed(MouseEvent e)
{
m.x1 = e.getX();
m.y1 = e.getY();
}
public void mouseReleased(MouseEvent e)
{
m.x2 = e.getX();
m.y2 = e.getY();
m.repaint();
}
}
public class DrawLineMouse extends Applet
{
LDRP ITR, CE-IT

Object Oriented Programing With Java


1216BECE30042

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

Object Oriented Programing With Java


1216BECE30042

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

Object Oriented Programing With Java


1216BECE30042

public void keyReleased(KeyEvent k)


{
showStatus("Key Up");
}
public void keyTyped(KeyEvent k)
{
msg+=k.getKeyChar();
repaint();
}
public void paint(Graphics g)
{
g.drawString(msg,X,Y);
}
}

OUTPUT

Practical No: 40
Aim: Write an applet that contains three
buttons OK,CANCEL and HELP and one
LDRP ITR, CE-IT

Object Oriented Programing With Java


1216BECE30042

TextField. if OK is pressed shown on the status


bar-OK is pressed and the TextField should
turn red. When CANCEL is pressed-shown on
the status bar - CANCEL is pressed and
TextField should turn green. When HELP is
pressed shown on the status bar-HELP is
pressed and the text field should turn yellow.
Source:
import java.awt.*;
import java.applet.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
/*
<applet code="ActionPerformDemo" width=300 height=300>
</applet>
*/
public class ActionPerformDemo extends Applet implements ActionListener
{
Button ok, can, help;
TextField t;
public void init()
{
ok = new Button("OK");
can = new Button("Cancel");
help = new Button("Help");
t = new TextField("Text Field");
add(t);
add(ok);
add(can);
add(help);
ok.addActionListener(this);
can.addActionListener(this);
help.addActionListener(this);
}
public void actionPerformed(ActionEvent e)
LDRP ITR, CE-IT

Object Oriented Programing With Java


1216BECE30042

{
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

Object Oriented Programing With Java


1216BECE30042

content of file-2 on console.


Source:
import java.io.*;
class Practical41
{
public static void main(String ag[]) throws IOException
{
BufferedReader br=new BufferedReader(new
InputStreamReader(System.in));
String str;
OutputStream f1=new FileOutputStream(ag[0]);
do
{
str=br.readLine();
byte buf[]=str.getBytes();
f1.write(buf);
}while(!str.equals("q"));
f1.close();
int i;
FileInputStream fin;
FileOutputStream fout;
try
{
try
{
fin = new FileInputStream(ag[0]);
}
catch(FileNotFoundException e)
{
System.out.println("Input File Not Found");
return;
}
try
{
fout = new FileOutputStream(ag[1]);
}
catch(FileNotFoundException e)
{
LDRP ITR, CE-IT

Object Oriented Programing With Java


1216BECE30042

System.out.println("Error Opening Output File");


return;
}}
catch(ArrayIndexOutOfBoundsException e)
{
System.out.println("Copy File From To");
return;
}
try
{do
{
i = fin.read();
if(i != -1)
fout.write(i);
} while(i != -1); }
catch(IOException e)
{System.out.println("File Error");}
fin.close();
fout.close();
}}

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

Object Oriented Programing With Java


1216BECE30042

a command line argument. The program should


check whether the file exists or not. The words
in the file are separated by white space
characters.
Source:
import java.io.*;
class Prac43
{
public static void main(String args[]) throws IOException
{
int i;
int count=0;
FileInputStream fin;
try{
try
{
fin = new FileInputStream(args[0]);
}
catch(FileNotFoundException e)
{
System.out.println("Input File Not Found");
return;
}
} catch(ArrayIndexOutOfBoundsException e)
{
return;
}
try
{
do
{
i = fin.read();
if(i==32)
++count;
}while(i!=-1);
}
catch(IOException e)
{
System.out.println("File Error");
}
if(count>0)
LDRP ITR, CE-IT

Object Oriented Programing With Java


1216BECE30042

{
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

LDRP ITR, CE-IT

Object Oriented Programing With Java


1216BECE30042

LDRP ITR, CE-IT

You might also like