Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
50 views

JAVA File

The document contains programs in Java covering topics like Fibonacci series, arrays, matrices, inheritance, interfaces, packages and more. Each program is accompanied by its expected output. The programs are intended as examples for learning Java concepts.

Uploaded by

expertgirlgaming
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
50 views

JAVA File

The document contains programs in Java covering topics like Fibonacci series, arrays, matrices, inheritance, interfaces, packages and more. Each program is accompanied by its expected output. The programs are intended as examples for learning Java concepts.

Uploaded by

expertgirlgaming
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 22

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;
System.out.println( +a);
System.out.println( +b);
c=a+b;
while(c<50)
{
System.out.println( +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--)
System.out.print(" ");
for(int a=0; a<=i; a++)
System.out.print("*");
for(int a=1; a<=i; a++)
System.out.print("*");
System.out.print("\n");
}
}
}

OUTPUT:

WAP to overload a function

5
class Ovrld
{
void show()
{
System.out.println("There is no argument");
}
void show(int x)
{
System.out.println("There is one argument");
}
public static void main(String s[])
{
Ovrld o=new Ovrld();
o.show();
o.show(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++)
System.out.print(twoD[i][j]+" ");
System.out.println();
}
}
}

OUTPUT:

WAP to merge two arrays

7
class MergeArray
{
static void merge(int x[],int y[])
{
int z[]=new int[x.length+y.length];
for(int i=0;i<x.length;i++)
{
z[i]=x[i];
}
for (int i=x.length;i<(x.length+y.length);i++)
{
z[i]=y[i-x.length];
}
System.out.println("\n\nThe merged array is:");
for(int j=0;j<(x.length+y.length);j++)
{
System.out.print(z[j]+" ");
}
}
public static void main(String s[])
{
int a[]={57,34,65,45};
int b[]={43,28,50,13,3};
System.out.println("The elements of first array are:");
for(int i=0;i<a.length;i++)
{
System.out.print(a[i]+" ");
}
System.out.println("\n\nThe elements of second array are:");
for(int i=0;i<b.length;i++)
{
System.out.print(b[i]+" ");
}
merge(a,b);
}
}
OUTPUT:

WAP to multiply two matrices


8
import java.lang.*;
import java.io.*;
class mulmatrix
{
static int a[][], b[][], c[][], m, n, x, y;
static void mul()
{
DataInputStream dts=new DataInputStream(System.in);
try
{
System.out.println("Enter size of Array A:");
m = Integer.parseInt(dts.readLine());
n = Integer.parseInt(dts.readLine());
a = new int[m][n];
System.out.println("Enter elements of array A:");
System.out.println("enter first matrix order :");
for(int i=0; i<m; i++)
{
for(int j=0; j<n; j++)
{
a[i][j]=Integer.parseInt(dts.readLine());
}
}
System.out.println("Enter size of Array B:");
x = Integer.parseInt(dts.readLine());
y = Integer.parseInt(dts.readLine());
b = new int[x][y];
System.out.println("enter second matrix order :");
for(int i=0; i<x; i++)
{
for(int j=0; j<y; j++)
{
b[i][j] = Integer.parseInt(dts.readLine());
}
}
}
catch(Exception x1)
{
System.out.println("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++)
{
System.out.print(c[i][j]+" ");
}
System.out.println("\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)
System.out.println("-->");
System.out.println(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()
{
System.out.println("it is default constructor");
}
{
System.out.println("it is funny");
}
public void display()
{
System.out.println("a="+a);
}
public static void main(String arg[])
{
A x=new A();
x.display();
A y=new A(10);
y.display();
}
}

OUTPUT:

D: \ java \ java A.java


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 java.io.*;
class ArraySortDemo
{
public static void main(String ar[])throws Exception
{
int arr[] = new int[6];
System.out.println("Unsorted Array:");
DataInputStream in=new DataInputStream(System.in);
for(int i=0;i<6;i++)
{
arr[i]=Integer.parseInt(in.readLine());
}
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;
}
}
}
System.out.println("sorted Array:"+ arr[0]+" "+arr[1]+" "+arr[2]+" "
+arr[3]+" "+arr[4]+" "+arr[5]);
}
}

OUTPUT:

13
Write a program using Inheritance.
import java.io.*;
class A
{
int a,b;
void get_ab(int a,int b)
{
this.a = a;
this.b = b;
}
}
class B extends A
{
void display()
{
System.out.println("Value of a is : "+a);
System.out.println("Value of b is : "+b);
}
}
interface C
{
void add();
}
class D extends B implements C
{
public void add()
{
int k;
k = a + b;
System.out.println("Value of k is : "+k);
}
}
class Inheritance
{
public static void main(String args[]) throws IOException
{
DataInputStream obj = new DataInputStream(System.in);
D ob = new D();
ob.get_ab(10,20);
ob.display();
ob.add();
}
}
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;
System.out.println("Area of Rectangle = "+area.compute(10,20));
area = cir;
System.out.println("Area of Circle = "+area.compute(10,0));
}
}

OUTPUT:

15
Write a program how to built your package in java.
package package1;
public class ClassA
{
public void displayA()
{
System.out.println("Class A");
}
public void sum(int a,int b)
{
int c=a+b;
System.out.println("Sum is= "+c);
}
public void sub(int a,int b)
{
int c=a-b;
System.out.println("Sub is= "+c);
}
}
package package2;
public class ClassB
{
public void displayB()
{
System.out.println("Class B");
}
public void mul(int a,int b)
{
int c=a*b;
System.out.println("Mul is= "+c);
}
public void div(int a,int b)
{
int c=a/b;
System.out.println("Div is= "+c);
}
}
import package1.ClassA;
import package2.ClassB;
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();
objectB.displayB();
objectB.mul(10,5);
objectB.div(10,5);
TryPack objectT=new TryPack();
objectT.dd();
}
}

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 PackageTest1.java
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++)
{
System.out.println("\tFrom ThreadA :i="+i);
}
System.out.println("Exit form A");
}
}
class B extends Thread
{
public void run()
{
for(int j=1;j<=5;j++)
{
System.out.println("\tFrom ThreadB :j="+j);
}
System.out.println("Exit form B");
}
}
class C extends Thread
{
public void run()
{
for(int k=1;k<=5;k++)
{
System.out.println("\tFrom ThreadC :k="+k);
}
System.out.println("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 ThreadTest.java
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 java.io.*;
class palindromes
{
public static void main(String args[])throws
IOException
{
BufferedReader br=new BufferedReader(new
InputStreamReader(System.in));
System.out.println("enter the String");
String s=br.readLine();
int n=s.length();
int palin=1;

for(int i=0;i<(n/2);i++)
{
if(s.charAt(i)!=s.charAt(n-i-1))
{
palin=0;
break;
}
}
if(palin==1)
System.out.println("String is palindrome");
else
System.out.println("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 palindromes.java
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 java.io.*;
class Harmonic
{
public static void main(String args[])
{
try
{
DataInputStream dis = new DataInputStream(System.in);
try
{
System.out.println("Enter value of n = ");
String str = dis.readLine();
int n = Integer.parseInt(str);
float sum = 0.0f;
for(int i=1;i<=n;i++)
{
sum = sum + (float) 1/i;
}
System.out.println("Sum of a harmonic series = "+sum);
}
catch(NumberFormatException e)
{
System.out.println("Wrong input data");
}
}
catch(Exception e)
{}
}}

OUTPUT:

21
Write a program using Graphics.
import java.awt.*;
import javax.swing.*;
import javax.swing.text.JTextComponent;
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()
{
b1.setBackground(Color.RED);
setLayout(new FlowLayout());
l1.setText("Enter Name");
add(l1);
add(t1);
add(b1);
add(b2);
}
public static void main(String[] args)
{
myframe m1=new myframe();
m1.launchFrame();
m1.setVisible(true);
m1.setSize(200,300);
}
}

OUTPUT:

22

You might also like