Java Practical
Java Practical
Java Practical
class manoj
{
public static void main(String args[])
{
System.out.println("hello ");
System.out.println("welcome to java world ");
System.out.println("a very good morning ");
manoj obj=new manoj();
obj.add(2,7);
}
int result;
C:\Sun>cd appserver
C:\Sun\AppServer>cd jdk
C:\Sun\AppServer\jdk>cd bin
C:\Sun\AppServer\jdk\bin>javac manoj.java
C:\Sun\AppServer\jdk\bin>
PROGRAM 2
WRITE A PROGRAM TO FIND LARGEST OF THREE NUMBERS
class greater
{
public static void main(String args[])
{
int a=98,b=87,c=99;
if(a>b)
{
if(a>c)
{
System.out.println(" a is greater ");
}
else
{
System.out.println(" c is greater ");
}
}
else
{
if(b>c)
{
System.out.println(" b is greater ");
}
else
{
System.out.println(" c is greater ");
}
}
}
}
OUTPUT
C:\Sun\AppServer\jdk\bin>javac greater.java
C:\Sun\AppServer\jdk\bin>java greater
c is greater
C:\Sun\AppServer\jdk\bin>
PROGRAM 3
WRITE A PROGRAM TO PRINT FIBONACII SERIES USING LOOP
class fab
{
public static void main(String args[])
{
int i=1;
int j=1;
int k=0;
System.out.println("fibbonacci series");
while(i<=10)
{
System.out.println(+j);
j=j+k;
k=j-k;
i++;
}
}
}
OUTPUT
C:\Sun\AppServer\jdk\bin>javac fab.java
C:\Sun\AppServer\jdk\bin>java fab
fibbonacci series
1
1
2
3
5
8
13
21
34
55
C:\Sun\AppServer\jdk\bin>
PROGRAM 4
WRITE A PROGRAM USING CLASSES AND OBJECT IN JAVA
public data()
{
id="8043";
name="MANOJ";
age="22";
}
public void displaydata()
{
System.out.println("my id is="+id);
System.out.println("my name is="+name);
System.out.println("my age is="+age);
}
public static void main(String args[])
{
data obj=new data();
obj.displaydata();
}
}
OUTPUT
C:\Sun\AppServer\jdk\bin>javac data.java
C:\Sun\AppServer\jdk\bin>java data
my id is=8043
my name is=MANOJ
my age is=22
C:\Sun\AppServer\jdk\bin>
PROGRAM 5
WRITE A PROGRAM TO IMPLEMENT INHERITANCE
class room
{
int l;
int b;
room(int x, int y)
{
l=x;
b=y;
}
int area()
{
return(l*b);
}
}
class bedroom extends room
{
int h;
bedroom(int x,int y, int z)
{
super(x,y);
h=z;
}
int volume()
{
return(l*b*h);
}
}
class inheritance
{
public static void main(String args[])
{
bedroom room1=new bedroom(10,20,30);
int area1=room1.area();
int volume1=room1.volume();
System.out.println("area1="+area1);
System.out.println("volume1="+volume1);
}
}
OUTPUT
C:\Sun\AppServer\jdk\bin>javac inheritance.java
C:\Sun\AppServer\jdk\bin>java inheritance
area1=200
volume1=6000
C:\Sun\AppServer\jdk\bin>
PROGRAM 6
WRITE A PROGRAM TO IMPLEMENT MULTITHREADING
class A extends Thread
{
public void run()
{
for(int i=1;i<=5;i++)
{
System.out.println("from thread A :i= "+i);
}
}
}
class B extends Thread
{
public void run()
{
for(int i=1;i<=5;i++)
{
System.out.println("from thread B :i= "+i);
}
}
}
class C extends Thread
{
public void run()
{
for(int i=1;i<=5;i++)
{
System.out.println("from thread C :i= "+i);
}
}
}
class threadtest
{
public static void main(String args[])
{
new A().start();
new B().start();
new C().start();
}
}
OUTPUT
C:\Sun\AppServer\jdk\bin>javac threadtest.java
C:\Sun\AppServer\jdk\bin>java threadtest
from thread A :i= 1
from thread A :i= 2
from thread A :i= 3
from thread A :i= 4
from thread A :i= 5
from thread B :i= 1
from thread B :i= 2
from thread B :i= 3
from thread B :i= 4
from thread B :i= 5
from thread C :i= 1
from thread C :i= 2
from thread C :i= 3
from thread C :i= 4
from thread C :i= 5
C:\Sun\AppServer\jdk\bin>
PROGRAM 7
WRITE A PROGRAM TO IMPLEMENT METHOD OVERRIDING
class sup
{
int x;
sup(int x)
{
this.x=x;
}
void display()
{
System.out.println("x= "+x);
}
}
class overloading
{
public static void main(String args[])
{
sub s=new sub(10,20);
s.display();
}
}
OUTPUT
C:\Sun\AppServer\jdk\bin>javac overloading.java
C:\Sun\AppServer\jdk\bin>java overloading
x= 10
y= 20
C:\Sun\AppServer\jdk\bin>
PROGRAM 8
WRITE A PROGRAM TO IMPLEMENT METHOD OVERLOADING
class funcload
{
public static void main(String args[])
{
funcload obj=new funcload();
obj.add(15,24);
obj.add(2.3f,0.8f);
obj.add(56,76.76f);
}
int x,y;
float p,q,result;
void add(int a,int b)
{
x=a;
y=b;
result=x+y;
System.out.println("the result is:" + result);
}
void add(float a,float b)
{
p=a;
q=b;
result=p+q;
System.out.println("the result is:" + result);
}
void add(int a,float b)
{
x=a;
p=b;
result=x+p;
System.out.println("the result is:" + result);
}
}
OUTPUT
C:\Sun\AppServer\jdk\bin>javac funcload.java
C:\Sun\AppServer\jdk\bin>java funcload
the result is:39.0
the result is:3.1
the result is:132.76001
C:\Sun\AppServer\jdk\bin>
PROGRAM 9
WRITE A PROGRAM TO IMPLEMENT PACKAGE
Creation of package
package pack;
public class rev
{
public static void inita()
{
char str[]={'m','a','n','o','j'};
int i=0;
for(i=str.length-1;i>=0;i--)
{
System.out.print(str[i]);
}
}
}
Implementation of package
import pack.*;
class demo12
{
public static void main(String args[])
{
rev ob=new rev();
ob.inita();
}
}
OUTPUT
C:\monu>md pack
C:\ monu>cd pack
C:\ monu \pack>edit rev.java
C:\ monu \pack>javac rev.java
C:\ monu \pack>cd..
C:\ monu \edit demo12.java
C:\ monu >javac demo12.java
C:\ monu >java demo12
jonam
C:\ monu>
PROGRAM 10
WRITE A PROGRAM TO IMPLEMENT INTERFACE
interface area
{
final float p=3.14f;
float compute(float x,float y);
}
class rectangle implements area
{
public float compute(float x,float y)
{
return(x*y);
}
}
class circle implements area
{
public float compute(float x,float y)
{
return(p*x*x);
}
}
class inter
{
public static void main(String args[])
{
rectangle r=new rectangle();
circle c=new circle();
area a;
a=r;
System.out.println("\nArea of rectangle=
"+a.compute(10,20));
a=c;
System.out.println("\nArea of circle= "+a.compute(10,0));
}
}
OUTPUT
C:\Sun\AppServer\jdk\bin>javac inter.java
C:\Sun\AppServer\jdk\bin>java inter
C:\Sun\AppServer\jdk\bin>
PROGRAM 11
WRITE A PROGRAM TO IMPLEMENT APPLETS IN JAVA
import java.awt.*;
import java.applet.*;
//<applet code="ma" height=750 width=350> </applet>
public class ma extends Applet implements Runnable
{
int i;
Thread t;
public void init()
{
try
{
t= new Thread(this);
t.start();}
catch(Exception e){}
}
for(i=1;i<=20;i++){
try
{
repaint();
Thread.sleep(2000);}
catch(Exception e)
{}
}
}
public void paint(Graphics s)
{
try
{
s.drawString("The value of i is "+i,60,50);}
catch(Exception e){}
}
public static void main(String ar[])
{
ma ob;
}
}
OUTPUT
PROGRAM 12
WRITE A PROGRAM TO IMPLEMENT EXCEPTION HANDLING IN
JAVA
class handle
{
public static void main(String args[])
{
int a=10;
int b=5;
int c=5;
int x;
int y;
try
{
x=a/(b-c);
}
catch(ArithmeticException e)
{
System.out.println("Division by zero");
}
y=a/(b+c);
System.out.println("y="+y);
}
}
OUTPUT
C:\Sun\AppServer\jdk\bin>javac handle.java
C:\Sun\AppServer\jdk\bin>java handle
Division by zero
y=1
C:\Sun\AppServer\jdk\bin>