PRACTICLE FILE
A
PRACTICAL FILE
OBJECT TECHNOLOGY
(JAVA)
SUBMITTED TO: SUBMITTED BY:
Mr. SUSHIL KUMAR PRADEEP KLUMAR YADAV
[ASSISTANT PROFESSOR] MCA-3RD SEM
UNIVERSITY ROLL NO.-
2
INDEX
Sr. NO PROGRAMS PAGE NO. REMARKS
1. Write a program of Fibonacci Series 4
2. Write a program for creating a Piramid 5
3. WAP to overload a function 6
4. Write a program to demonstrate a two- 7
dimensional array
5. WAP to merge two arrays 8
6. WAP to multiply two matrices 9-10
7. Write a program of package definition by usage 11
8. Write a program for a Constructor in java 12
9. Program for sorting of an array 13
10. Write a program using Inheritance 14
11. Write a program for Interface Implementation 15
12. Write a program how to built your package in 16-17
java
13. Write a program in java illustrating the concept 18-19
of multithreading
14. Write a program to check whether a string is 20
palindrome or not
15. Write a program of Exception Handling 21
16. Write a program using Graphics 22
Write a program of Fibonacci Series
3
class Fibnocci
{
public static void main(String p[])
{
int a=0,b=1,c=0;
[Link]( +a);
[Link]( +b);
c=a+b;
while(c<50)
{
[Link]( +c);
a=b;
b=c;
c=a+b;
}
}
}
OUTPUT:
Write a program for creating a Piramid
4
class Piramid
{
public static void main(String[] args)throws Exception
{
for(int i=0; i<=9; i++)
{
for(int a=9; a>=i; a--)
[Link](" ");
for(int a=0; a<=i; a++)
[Link]("*");
for(int a=1; a<=i; a++)
[Link]("*");
[Link]("\n");
}
}
}
OUTPUT:
WAP to overload a function
5
class Ovrld
{
void show()
{
[Link]("There is no argument");
}
void show(int x)
{
[Link]("There is one argument");
}
public static void main(String s[])
{
Ovrld o=new Ovrld();
[Link]();
[Link](5);
}
}
OUTPUT:
Write a program to demonstrate a two- dimensional array.
6
public class TwoDArray
{
public static void main(String args[])
{
int twoD[][] = new int[4][5];
int i,j,k = 0;
for(i=0;i<4;i++)
for(j=0;j<5;j++)
{
twoD[i][j] = k;
k++;
}
for(i=0;i<4;i++)
{
for(j=0;j<5;j++)
[Link](twoD[i][j]+" ");
[Link]();
}
}
}
OUTPUT:
WAP to merge two arrays
7
class MergeArray
{
static void merge(int x[],int y[])
{
int z[]=new int[[Link]+[Link]];
for(int i=0;i<[Link];i++)
{
z[i]=x[i];
}
for (int i=[Link];i<([Link]+[Link]);i++)
{
z[i]=y[[Link]];
}
[Link]("\n\nThe merged array is:");
for(int j=0;j<([Link]+[Link]);j++)
{
[Link](z[j]+" ");
}
}
public static void main(String s[])
{
int a[]={57,34,65,45};
int b[]={43,28,50,13,3};
[Link]("The elements of first array are:");
for(int i=0;i<[Link];i++)
{
[Link](a[i]+" ");
}
[Link]("\n\nThe elements of second array are:");
for(int i=0;i<[Link];i++)
{
[Link](b[i]+" ");
}
merge(a,b);
}
}
OUTPUT:
WAP to multiply two matrices
8
import [Link].*;
import [Link].*;
class mulmatrix
{
static int a[][], b[][], c[][], m, n, x, y;
static void mul()
{
DataInputStream dts=new DataInputStream([Link]);
try
{
[Link]("Enter size of Array A:");
m = [Link]([Link]());
n = [Link]([Link]());
a = new int[m][n];
[Link]("Enter elements of array A:");
[Link]("enter first matrix order :");
for(int i=0; i<m; i++)
{
for(int j=0; j<n; j++)
{
a[i][j]=[Link]([Link]());
}
}
[Link]("Enter size of Array B:");
x = [Link]([Link]());
y = [Link]([Link]());
b = new int[x][y];
[Link]("enter second matrix order :");
for(int i=0; i<x; i++)
{
for(int j=0; j<y; j++)
{
b[i][j] = [Link]([Link]());
}
}
}
catch(Exception x1)
{
[Link]("error");
}
c = new int[m][y];
for(int i=0; i<m; i++)
{
for(int j=0; j<y; j++)
{
9
for(int k=0;k<y;k++)
{
c[i][j]=c[i][j]+a[i][k]*b[k][j];
}
}
}
for(int i=0; i<2; i++)
{
for(int j=0; j<2; j++)
{
[Link](c[i][j]+" ");
}
[Link]("\t");
}
}
public static void main(String arg[])
{
mul();
}
}
OUTPUT:
10
Write a program of package definition by usage
package MyPack;
class Balance
{
String name;
double bal;
Balance(String n, double b)
{
name = n;
bal = b;
}
void show()
{
if(bal<0)
[Link]("-->");
[Link](name+": $"+bal);
}
}
class AccountBalance
{
public static void main(String args[])
{
Balance current[] = new Balance[3];
current[0] = new Balance("K. J. Fielding",123.23);
current[1] = new Balance("Will Tell",157.02);
current[2] = new Balance("Tom Jackson",-12.33);
for(int i=0;i<3;i++)
current[i].show();
}
}
OUTPUT:
11
Write a program for a Constructor in java
class A
{
int a;
public A(int x)
{
a=x;
}
public A()
{
[Link]("it is default constructor");
}
{
[Link]("it is funny");
}
public void display()
{
[Link]("a="+a);
}
public static void main(String arg[])
{
A x=new A();
[Link]();
A y=new A(10);
[Link]();
}
}
OUTPUT:
D: \ java \ java [Link]
D: \ java \ javac A
It is funny
It is default constructor
a=0
it is funny
a=10
12
Program for sorting of an array.
import [Link].*;
class ArraySortDemo
{
public static void main(String ar[])throws Exception
{
int arr[] = new int[6];
[Link]("Unsorted Array:");
DataInputStream in=new DataInputStream([Link]);
for(int i=0;i<6;i++)
{
arr[i]=[Link]([Link]());
}
int temp;
for(int i=0;i<5;i++)
{
for(int j=0;j<5-i;j++)
{
if(arr[j] > arr[j+1])
{
temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
}
[Link]("sorted Array:"+ arr[0]+" "+arr[1]+" "+arr[2]+" "
+arr[3]+" "+arr[4]+" "+arr[5]);
}
}
OUTPUT:
13
Write a program using Inheritance.
import [Link].*;
class A
{
int a,b;
void get_ab(int a,int b)
{
this.a = a;
this.b = b;
}
}
class B extends A
{
void display()
{
[Link]("Value of a is : "+a);
[Link]("Value of b is : "+b);
}
}
interface C
{
void add();
}
class D extends B implements C
{
public void add()
{
int k;
k = a + b;
[Link]("Value of k is : "+k);
}
}
class Inheritance
{
public static void main(String args[]) throws IOException
{
DataInputStream obj = new DataInputStream([Link]);
D ob = new D();
ob.get_ab(10,20);
[Link]();
[Link]();
}
}
OUTPUT:
14
Write a program for Interface Implementation.
interface Area
{
final static float pi=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(pi*x*x);
}
}
class InterfaceTest
{
public static void main(String args[ ])
{
Rectangle rect = new Rectangle( );
Circle cir = new Circle( );
Area area;
area=rect;
[Link]("Area of Rectangle = "+[Link](10,20));
area = cir;
[Link]("Area of Circle = "+[Link](10,0));
}
}
OUTPUT:
15
Write a program how to built your package in java.
package package1;
public class ClassA
{
public void displayA()
{
[Link]("Class A");
}
public void sum(int a,int b)
{
int c=a+b;
[Link]("Sum is= "+c);
}
public void sub(int a,int b)
{
int c=a-b;
[Link]("Sub is= "+c);
}
}
package package2;
public class ClassB
{
public void displayB()
{
[Link]("Class B");
}
public void mul(int a,int b)
{
int c=a*b;
[Link]("Mul is= "+c);
}
public void div(int a,int b)
{
int c=a/b;
[Link]("Div is= "+c);
}
}
import [Link];
import [Link];
class TryPack extends ClassA
{
void dd()
{
displayA();
sum(20,10);
16
sub(20,10);
}
}
class PackageTest1
{
public static void main(String args[])
{
ClassB objectB=new ClassB();
[Link]();
[Link](10,5);
[Link](10,5);
TryPack objectT=new TryPack();
[Link]();
}
}
Output:
Microsoft Windows [Version 6.0.6002]
Copyright (c) 2006 Microsoft Corporation. All rights reserved.
C:\Users\dimple>cd\
C:\>cd neha
C:\neha>javac [Link]
C:\neha>java PackageTest1
Class B
Mul is= 50
Div is= 2
Class A
Sum is= 30
Sub is= 10
17
Write a program in java illustrating the concept of multithreading
class A extends Thread
{
public void run()
{
for(int i=1;i<=5;i++)
{
[Link]("\tFrom ThreadA :i="+i);
}
[Link]("Exit form A");
}
}
class B extends Thread
{
public void run()
{
for(int j=1;j<=5;j++)
{
[Link]("\tFrom ThreadB :j="+j);
}
[Link]("Exit form B");
}
}
class C extends Thread
{
public void run()
{
for(int k=1;k<=5;k++)
{
[Link]("\tFrom ThreadC :k="+k);
}
[Link]("Exit form C");
}
}
class ThreadTest
{
public static void main(String arg[])
{
new A().start();
new B().start();
new C().start();
}
}
18
Output:
Microsoft Windows [Version 6.0.6002]
Copyright (c) 2006 Microsoft Corporation. All rights reserved.
C:\Users\dimple>cd\
C:\>cd neha
C:\neha>javac [Link]
C:\neha>java ThreadTest
From ThreadA :i=1
From ThreadA :i=2
From ThreadB :j=1
From ThreadA :i=3
From ThreadB :j=2
From ThreadA :i=4
From ThreadB :j=3
From ThreadB :j=4
From ThreadB :j=5
From ThreadA :i=5
Exit form A
Exit form B
From ThreadC :k=1
From ThreadC :k=2
From ThreadC :k=3
From ThreadC :k=4
From ThreadC :k=5
Exit form C
19
Write a program to check whether a string is palindrome or not
import [Link].*;
class palindromes
{
public static void main(String args[])throws
IOException
{
BufferedReader br=new BufferedReader(new
InputStreamReader([Link]));
[Link]("enter the String");
String s=[Link]();
int n=[Link]();
int palin=1;
for(int i=0;i<(n/2);i++)
{
if([Link](i)!=[Link](n-i-1))
{
palin=0;
break;
}
}
if(palin==1)
[Link]("String is palindrome");
else
[Link]("String is not palindrome");
}}
OUTPUT:
Microsoft Windows [Version 6.0.6002]
Copyright (c) 2006 Microsoft Corporation. All rights reserved.
C:\Users\dimple>cd\
C:\neha>cd\
C:\>cd neha
C:\neha>javac [Link]
C:\neha>java palindromes
enter the String
deepa
String is not palindrome
C:\neha>java palindromes
enter the String
nitin
String is palindrome
20
Write a program of Exception Handling.
import [Link].*;
class Harmonic
{
public static void main(String args[])
{
try
{
DataInputStream dis = new DataInputStream([Link]);
try
{
[Link]("Enter value of n = ");
String str = [Link]();
int n = [Link](str);
float sum = 0.0f;
for(int i=1;i<=n;i++)
{
sum = sum + (float) 1/i;
}
[Link]("Sum of a harmonic series = "+sum);
}
catch(NumberFormatException e)
{
[Link]("Wrong input data");
}
}
catch(Exception e)
{}
}}
OUTPUT:
21
Write a program using Graphics.
import [Link].*;
import [Link].*;
import [Link];
public class myframe extends JFrame
{
JButton b1,b2;
JTextComponent t1;
JLabel l1;
public myframe()
{
l1=new JLabel();
b1=new JButton("Click");
b2=new JButton("<html><b><i>press</i></b></html>");
t1=new JTextField(" ");
}
public void launchFrame()
{
[Link]([Link]);
setLayout(new FlowLayout());
[Link]("Enter Name");
add(l1);
add(t1);
add(b1);
add(b2);
}
public static void main(String[] args)
{
myframe m1=new myframe();
[Link]();
[Link](true);
[Link](200,300);
}
}
OUTPUT:
22