0% found this document useful (0 votes)
80 views75 pages

Softcopy of Javafile

This document provides an index of 20 programs related to object-oriented programming concepts in Java. The programs cover topics like string manipulation, arrays, inheritance, polymorphism, exceptions, threads, files and more. Each program is numbered and described briefly in the index. The document appears to be submitted by a student to their instructor for review and grading.

Uploaded by

Tushar Suneja
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
80 views75 pages

Softcopy of Javafile

This document provides an index of 20 programs related to object-oriented programming concepts in Java. The programs cover topics like string manipulation, arrays, inheritance, polymorphism, exceptions, threads, files and more. Each program is numbered and described briefly in the index. The document appears to be submitted by a student to their instructor for review and grading.

Uploaded by

Tushar Suneja
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 75

PRACTICAL FILE

OF

OBJECT
TECHNOLOGY

SUBMITTED To:-DR. GOPAL SINGH


SUBMITTED BY:-Shweta
ROLL NO. 19208
INDEX
Program no. Program name Page no Sign
1 Write a program to compare a string,reverse a string
and find out length of a string

2 Write a program to replace and append operation on a


string using string buffer class

3 Write a program to get substring of content of the


string buffer class

4 Write a program to find the GCD and LCM of three


number using command

5 Write a program for sorting an array using

a. bubble sort

b. insertion sort

6 Write a program for enter a martix by user and check


it is sparse matrix or not.

7 Write a program for two dimensional array

a. multiplication of matrix

b. transpose of matrix

8 Write a program for searching an element from an


array

a. linear search
b. binary search

9 Write a program for Employee information


(EID,Name,age,salary,job profile) using interface
and  multiple inheritance.

10 Write a program for student record in which one class


contains data for student record and another  has
student sports record using inheritance.

11 Write a program for exception handling and


implements any five inbuilt

12 Write a program for runtime polymorphism.

13 Write a program for handle exception using try and


multiple catch block with finally keyword.

14 Write a program for create a Thread from Thread


class and Runnable interface.

15 Write a program for multiple Thread class and use


setPriority method in it.

16 Write a program

a. Using Synchronization block

b. Using Synchronization method

17 Write a program to explain Deadlock in


multithreading.

18 Write a program to use Applet Program and it’s life


cycle.
19 Write a code to read data from keyboard and write it
to the file using FileOutputStream and read data from
file using FileInputStream and display it on monitor.

20 Write a program to create a text file using FileWriter


and write text into it and read data from text file
using FileReader an display it on the moniter.

21 Write a program to

a. draw line in Frame using drawLine() method

b. draw rectangle using drawRect() method and


fillRect() method.

1. Write a program to compare a string,reverse a string and find


out length of a string
class string1_mansi

public static void main(String args[])

String s1="mansi";

String s2="soni";

StringBuffer str=new StringBuffer("object-oriented");

int length=s1.length();

System.out.println("length of string is \n"+length);

str.reverse();

System.out.println("reverse string is \n"+str);

System.out.println("after comparing is \n"+s1.equals(s2));

Output:-
2. Write a program to replace and append operation on a string
using string buffer class
class String2_mansi

public static void main(String args[])

StringBuffer str=new StringBuffer("java is good");

str.append(" and object oriented");

System.out.println("now str is \n"+str);

str.replace(4,6," oops");

System.out.println("then we have \n"+str);

Output:

3. Write a program to get substring of content of the string buffer


class
class String3_mansi
{

public static void main(String args[])

StringBuffer str=new StringBuffer("life is better with friends");

StringBuffer s2=new StringBuffer(str.substring(8,14));

System.out.println("substring is \n"+s2);

Output:

4. Write a program to find the GCD and LCM of three number


using command
import java.util.*; {

class Gcdlcm_mansi public static void main(String args[])


{ if(x>=y)

int num1,num2,num3; return calc(x-y,y);

num1=Integer.parseInt(args[0]); else

num2=Integer.parseInt(args[1]); return calc(x,y-x);

num3=Integer.parseInt(args[2]); }

int gcd1,gcd,lcm; return(x);

gcd1=calc(num1,num2); }

gcd=calc(num3,gcd1); }

lcm=(num1*num2*num3)/gcd;

System.out.println("gcd is ="+gcd);

System.out.println("lcm is ="+lcm);

static int calc(int x,int y)

if((x==0)||(y==0))

return(0);

else

while(x!=y) 4. output
{
5. WRITE A PROGRAM FOR SORTING AN ARRAY USING
A. BUBBLE SORT
class Bubblesort_mansi

static void bubblesort_mansi(int[] arr)

int n=arr.length;

int temp=0;

for(int i=0;i<n;i++)

for(int j=1;j<(n-i);j++)

if(arr[j-1]>arr[j])

temp=arr[j-1];

arr[j-1]=arr[j];

arr[j]=temp;

public static void main(String arg[])

int arr[]={8,60,357,23,4,30,50};
System.out.println("array before bubble sort");

for(int i=0;i<arr.length;i++)

System.out.println(arr[i]+"");

System.out.println();

bubblesort_mansi(arr);

System.out.println("array after bubble sort");

for(int i=0;i<arr.length;i++)

System.out.println(arr[i]+"");

}
5.A Output:-

5. Write a program for sorting an array using


B. insertion sort
public class Insertionsort_mansi

public static void insertionsort_mansi(int array[])

int n=array.length;

for(int j=1;j<n;j++)

int key=array[j];

int i=j-1;

while((i>-1)&&(array [i]>key))

array[i+1]=array[i];

i--;

array[i+1]=key;

public static void main(String arg[])

int[] arr1={19,140,13,26,4,1,558,2};

System.out.println("before insertion sort");

for(int i:arr1)

{
System.out.println(i+" ");

System.out.println( );

insertionsort_mansi(arr1);

System.out.println("after insertion sort");

for(int i:arr1)

System.out.println(i+" ");

5.B Output:-
6. Write a program for enter a martix by user and check it is sparse
matrix or not.
import java.util.Scanner;

public class Sparsematrix_mansi

public static void main(String args[])

int i,j,zero=0,count=0;

int array[][]=new int[10][10];

System.out.println("enter total rows and columns");

Scanner s=new Scanner(System.in);

int row=s.nextInt();

int column=s.nextInt();

System.out.println("enter matrix:");

for(i=0;i<row;i++)

for(j=0;j<column;j++)

array[i][j]=s.nextInt();

System.out.println(" ");

for(i=0;i<row;i++)

for(j=0;j<column;j++)

{
if(array[i][j]==0)

zero++;

else

count++;

if(zero>count)

System.out.println("matrix is sparse");

else

System.out.println("matrix is not sparse");

6. Output:-
7 Write a program for two dimensional array
a. Matrix multiplication
b. transpose
import java.util.Scanner;

class multiply

int i,j;

void input(int a[][],int b[][])

int c[][] = new int[a.length][b[i].length];

if(a[i].length==b.length)

for(i=0;i<a.length;i++)

for(j=0;j<b[i].length;j++)

c[i][j]=0;

for(int k=0;k<b.length;k++)

c[i][j]=c[i][j]+((a[i][k])*(b[k][j]));

System.out.println("matrix multiplication ==");


for(i=0;i<a.length;i++)

for(j=0;j<b[i].length;j++)

System.out.print(" "+c[i][j]);

System.out.println();

else

System.out.println("matrix multiplication is not possible ");

class transpo

int i,j;

void transpose(int a[][])

int d[][] = new int[a.length][a[i].length];

System.out.println("transpose of tha matrix = ");


for(i=0;i<a.length;i++)

for(j=0;j<a[i].length;j++)

d[i][j] = a[j][i];

for(i=0;i<a.length;i++)

for(j=0;j<a[i].length;j++)

System.out.print(" "+d[i][j]);

System.out.println();

class mansi_74

public static void main(String args[])

int m,n,i,j,p,q;

Scanner in = new Scanner(System.in);

System.out.println("enter the row and column of the first matrix =");


m = in.nextInt();

n = in.nextInt();

int a[][]= new int[m][n];

System.out.println("enter the elements of first matrix == ");

for(i=0;i<m;i++)

for(j=0;j<n;j++)

a[i][j] = in.nextInt();

System.out.println("enter the row and column of the second matrix =");

p = in.nextInt();

q = in.nextInt();

int b[][]=new int[p][q];

System.out.println("enter the elements of second matrix == ");

for(i=0;i<p;i++)

for(j=0;j<q;j++)

b[i][j] = in.nextInt();

multiply obj = new multiply();


transpo obj1 = new transpo();

obj.input(a,b);

obj1.transpose(a);

7.A output:-

8. Write a program for searching an element from an array


a. binary search

import java.util.Scanner;

class Binarysearch

public static void main(String args[])

int c, first, last, middle, n, search, array[];

Scanner in = new Scanner(System.in);

System.out.println("Enter number of elements");

n = in.nextInt();

array = new int[n];

System.out.println("Enter " + n + " integers");

for (c = 0; c < n; c++)

array[c] = in.nextInt();

System.out.println("Enter value to find");

search = in.nextInt();
first = 0;

last = n - 1;

middle = (first + last)/2;

while( first <= last )

if ( array[middle] < search )

first = middle + 1;

else if ( array[middle] == search )

System.out.println(search + " found at location " + (middle


+ 1) + ".");

break;

else

last = middle - 1;

middle = (first + last)/2;

if (first > last)

System.out.println(search + " isn't present in the list.\n");

8.A Output:-
8. Write a program for searching an element from an array
b. linear search
class linearsearch_mansi

public static int linearsearch(int[]arr,int key)

for(int i=0;i<arr.length;i++)

if(arr[i]==key)

return i;

return-1;

public static void main(String arg[])

int[] a1={10,20,30,50,70,90};

int key=20;

System.out.println(key+" is found at index:"+linearsearch(a1,key));

8.B Output:-
9. Write a program for Employee information
(EID,Name,age,salary,job profile) using interface
and  multiple inheritance.
interface A

public void display();

interface B

public void display1();

class C implements A,B

public void display()

System.out.println("\n name=Mansi\n Class=MCA (2nd year)\n


RollNo=18174\n");

public void display1()

System.out.println("interest in Sport=Badminton");

class Employee_mansi

{
public static void main(String args[])

C c1=new C();

c1.display();

c1.display1();

9.output:-
10. Write a program for student record in which one class contains
data for student record and another  has student sports record using
inheritance.
class record1

void data()

System.out.println("name:Mansi yadav");

System.out.println("class:mca3rd");

System.out.println("roll no:101");

System.out.println("marks:200000");

class record2 extends record1

void sports()

System.out.println("game:cricket");

System.out.println("member no:5");

class Studentinheritance_Mansi

public static void main(String arg[])


{

record2 d=new record2();

d.sports();

d.data();

10. Output:-
11. Write a program for exception handling and implements any
five inbuilt
import java.io.File;

import java.io.FileNotFoundException;

import java.io.FileReader;

class Exceptionhandling_mansi

public static void main(String arg[])

try

int a=30,b=0;

int c=a/b;

System.out.println("result="+c);

catch(ArithmeticException e)

System.out.println("can't divide a number by 0");

try

{
String a=null; //null value

System.out.println(a.charAt(0));

catch(NullPointerException e)

System.out.println("null pointer exception");

try

String a="this is like chipping";

char c=a.charAt(24); //accessing 25th element

System.out.println(c);

catch(StringIndexOutOfBoundsException e)

System.out.println("string index out of bound exception");

try

//follwing file does not exist

File file=new File(":E//file.text");

FileReader fr=new FileReader(file);


}

catch(FileNotFoundException e)

System.out.println("file does not exist");

try

//"akki" is not a number

int num=Integer.parseInt("akki");

System.out.println(num);

catch(NumberFormatException e)

System.out.println("number format exception");

}
11. Output:-
13. Write a program for multilevel inheritance using class
class A

public void display()

System.out.println("display the thing");

class B extends A

public void area()

System.out.println("shown the area");

class C extends B

public void volume()

System.out.println("show the volume");

public class Tester_mansi

{
public static void main(String arg[])

C c1=new C();

c1.display();

c1.area();

c1.volume();

}
14. Write a program for runtime polymorphism.
class A

void alphabet()

System.out.println("A is printed");

class B extends A

void alphabet()

System.out.println("B is printed");

class C extends B

void alphabet()

System.out.println("C is printed");

class D extends C

{
void alphabet()

System.out.println("D is printed");

class Polymorphism_mansi

public static void main(String arg[])

A s;

s=new B();

s.alphabet();

s=new C();

s.alphabet();

s=new D();

s.alphabet();

}
15. Write a program for method overloading.

class A

static int add(int a,int b)

return a+b;

static int add(int a,int b,int c)

return a+b+c;

class Testoverloading_mansi

public static void main(String arg[])

System.out.println(A.add(7,9));

System.out.println(A.add(5,3,8));

}
16. Write a program for handle exception using try and multiple
catch block with finally keyword.
public class multiplecatch_mansi

public static void main(String arg[])

try

int a[]=new int[5];

a[5]=30/0;

System.out.println(a[10]);

catch(ArithmeticException e)

System.out.println("arithmetic exception occurs");

catch(ArrayIndexOutOfBoundsException e)

System.out.println("array index out of bounds exception occur");

}
catch(Exception e)

System.out.println("parent exception occurs");

finally

System.out.println("rest of the code");

}
17. Write a program for create a Thread from Thread class and
Runnable interface.
class A implements Runnable

public void run()

for(int i=1;i<=10;i++)

System.out.println("\t threadA"+i);

System.out.println("end of thread");

class Runnabletest_mansi

public static void main(String arg[])

A runnable=new A();

Thread threadA=new Thread(runnable);

threadA.start();

System.out.println("end of main thread");

}
18. Write a programfor multiple Thread class and use setPriority
method in it.
class A extends Thread

public void run()

System.out.println("threadA started");

for(int i=1;i<=4;i++)

System.out.println("\t from thread A:i="+i);

System.out.println("terminate from A");

class B extends Thread

public void run()

System.out.println("threadB started");

for(int j=1;j<=4;j++)

System.out.println("\t from thread B:j="+j);

System.out.println("exit from B");


}

class C extends Thread

public void run()

System.out.println("threadC started");

for(int k=1;k<=4;k++)

System.out.println("\t from thread C:k="+k);

System.out.println("exit from C");

class Threadpriority_mansi

public static void main(String arg[])

A threadA=new A();

B threadB=new B();

C threadC=new C();

threadC.setPriority(Thread.MAX_PRIORITY);

threadB.setPriority(threadA.getPriority()+1);
threadA.setPriority(Thread.MIN_PRIORITY);

System.out.println("start threada");

threadA.start();

System.out.println("start threadb");

threadB.start();

System.out.println("start threadc");

threadC.start();

System.out.println("end of main thread");

}
19. Write a program
a. Using Synchronization block
class T1

void printT1(int n)

synchronized(this)

for(int i=1;i<=5;i++)

System.out.println(n*i);

try

Thread.sleep(400);

catch(Exception e)

System.out.println(e);

}//end of method

}}

class MyThread1 extends Thread


{

T1 t;

MyThread1(T1 t)

this.t=t;

public void run()

t.printT1(9);

class MyThread2 extends Thread

T1 t;

MyThread2(T1 t)

this.t=t;

public void run()

t.printT1(100);

}
}

public class Synchronizationblock_mansi

public static void main(String arg[])

T1 obj=new T1();

MyThread1 t1=new MyThread1(obj);

MyThread2 t2=new MyThread2(obj);

t1.start();

t2.start();

}
19. Write a program
b. Using Synchronization method
class T

synchronized void printT(int n)

for(int i=1;i<=5;i++)

System.out.println(n*i);

try
{

Thread.sleep(400);

catch(Exception e)

System.out.println(e);

class MyThread1 extends Thread

T t;

MyThread1(T t)

this.t=t;

public void run()

t.printT(5);

}
class MyThread2 extends Thread

T t;

MyThread2(T t)

this.t=t;

public void run()

t.printT(50);

public class Testmethod1_mansi

public static void main(String arg[])

T obj=new T();

MyThread1 t1=new MyThread1(obj);

MyThread2 t2=new MyThread2(obj);

t1.start();

t2.start();

}
Ouput:-
20. Write a program to explain Deadlock in multithreading.
class util

static void sleep(long millis)

try

Thread.sleep(millis);

catch(InterruptedException e)

e.printStackTrace();

class Shared

synchronized void test1(Shared s2)

System.out.println("test1-begin");

util.sleep(100);

s2.test2(this);

System.out.println("test1_end");
}

synchronized void test2(Shared s1)

System.out.println("test2-begin");

util.sleep(100);

s1.test1(this);

System.out.println("test2_end");

class Thread1 extends Thread

private Shared s1;

private Shared s2;

public Thread1(Shared s1,Shared s2)

this.s1=s1;

this.s2=s2;

@Override

public void run()

{
s1.test1(s2);

class Thread2 extends Thread

private Shared s1;

private Shared s2;

public Thread2(Shared s1,Shared s2)

this.s1=s1;

this.s2=s2;

@Override

public void run()

s2.test2(s1);

public class deadlock_mansi

public static void main(String arg[])


{

Shared s1=new Shared();

Shared s2=new Shared();

Thread1 t1=new Thread1(s1,s2);

t1.start();

Thread2 t2=new Thread2(s1,s2);

t2.start();

util.sleep(200);

Output:-
21. Write a program to use Applet Program and it’s life cycle.
import java.applet.Applet;

import java.awt.Graphics;

import java.awt.*;

/*<applet code="appletcycle_jyoti.class" width="350" height="150"></applet>*/

public class appletcycle _jyoti extends Applet

public void init()

setBackground(Color.CYAN);

System.out.println("init()called");

public void start()

System.out.println("start()called");

public void paint(Graphics g)

System.out.println("Paint()called");

}
public void stop()

System.out.println("stop()called");

public void destroy()

System.out.println("destroy()called");

21:output:-
22. Write a code to read data from keyboard and write it to the file
using FileOutputStream and read data from file using
FileInputStream and display it on monitor.
import java.io.*;

class A

void input()

try{

FileOutputStream fos = new FileOutputStream("c:\\io\\abc.txt");

fos.write(65); //A

fos.close();

FileOutputStream fos1 = new FileOutputStream("c:\\io\\abc.txt",true);

byte buf[] = {66,67,68,69,70};

fos1.write(buf);

fos1.close();

catch(Exception e)

{}

void show()

try{
FileInputStream fis =new FileInputStream("h:\\io\\abc.txt");

int k;

while((k=fis.read())!=-1)

System.out.println((char) k);

fis.close();

catch(Exception e)

{}

class mansi22

public static void main(String args[])throws IOException

A obj = new A();

obj.input();

obj.show();

}
22:output:-
23. Write a program to create a text file using FileWriter and write
text into it and read data from text file using FileReader an display
it on the moniter.*/
Coding :
import java.io.*;

class a

void input()

try

FileWriter fw = new FileWriter(&quot;file1.txt&quot;,false);

fw.write(104);

fw.write(&quot;ello world&quot;);

fw.flush();

fw.close();

catch(Exception e) { }

void show()

try

{
FileReader fr = new FileReader(&quot;file1.txt&quot;);

int i = fr.read();

while(i !=-1)

System.out.println((char) i);

i = fr.read();

catch(Exception e) { }

class rahul23

public static void main(String[] args)throws IOException

a obj = new a();

obj.input();

obj.show();

}
23.output:-
24. Write a program to
a. draw line in Frame using drawLine() method
import java.awt.*;

public class drawingline_jyoti extends Frame

Frame frame;

setTitle("line");

setSize(300,350);

setVisible(true);

public void paint(Graphics g)

g.drawLine(60,90,150,200);

g.drawLine(110,120,250,270);

public static void main(String arg[])

new drawingline_jyoti();

24A:output:=
24. Write a program to
b. draw rectangle using drawRect() method and fillRect() method.
import java.awt.*;

import java.applet.*;

public class rect_jyoti extends Applet

public void paint(Graphics g)

g.setColor(Color.blue);

g.drawRect(50,80,150,100);

g.setColor(Color.magenta);

g.fillRect(230,80,150,100);

24B:output:=

You might also like