Softcopy of Javafile
Softcopy of Javafile
OF
OBJECT
TECHNOLOGY
a. bubble sort
b. insertion sort
a. multiplication of matrix
b. transpose of matrix
a. linear search
b. binary search
16 Write a program
21 Write a program to
String s1="mansi";
String s2="soni";
int length=s1.length();
str.reverse();
Output:-
2. Write a program to replace and append operation on a string
using string buffer class
class String2_mansi
str.replace(4,6," oops");
Output:
System.out.println("substring is \n"+s2);
Output:
num1=Integer.parseInt(args[0]); else
num3=Integer.parseInt(args[2]); }
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);
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
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;
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);
for(int i=0;i<arr.length;i++)
System.out.println(arr[i]+"");
}
5.A Output:-
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;
int[] arr1={19,140,13,26,4,1,558,2};
for(int i:arr1)
{
System.out.println(i+" ");
System.out.println( );
insertionsort_mansi(arr1);
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;
int i,j,zero=0,count=0;
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
6. Output:-
7 Write a program for two dimensional array
a. Matrix multiplication
b. transpose
import java.util.Scanner;
class multiply
int i,j;
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]));
for(j=0;j<b[i].length;j++)
System.out.print(" "+c[i][j]);
System.out.println();
else
class transpo
int i,j;
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
int m,n,i,j,p,q;
n = in.nextInt();
for(i=0;i<m;i++)
for(j=0;j<n;j++)
a[i][j] = in.nextInt();
p = in.nextInt();
q = in.nextInt();
for(i=0;i<p;i++)
for(j=0;j<q;j++)
b[i][j] = in.nextInt();
obj.input(a,b);
obj1.transpose(a);
7.A output:-
import java.util.Scanner;
class Binarysearch
n = in.nextInt();
array[c] = in.nextInt();
search = in.nextInt();
first = 0;
last = n - 1;
first = middle + 1;
break;
else
last = middle - 1;
8.A Output:-
8. Write a program for searching an element from an array
b. linear search
class linearsearch_mansi
for(int i=0;i<arr.length;i++)
if(arr[i]==key)
return i;
return-1;
int[] a1={10,20,30,50,70,90};
int key=20;
8.B Output:-
9. Write a program for Employee information
(EID,Name,age,salary,job profile) using interface
and multiple inheritance.
interface A
interface B
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");
void sports()
System.out.println("game:cricket");
System.out.println("member no:5");
class Studentinheritance_Mansi
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
try
int a=30,b=0;
int c=a/b;
System.out.println("result="+c);
catch(ArithmeticException e)
try
{
String a=null; //null value
System.out.println(a.charAt(0));
catch(NullPointerException e)
try
System.out.println(c);
catch(StringIndexOutOfBoundsException e)
try
catch(FileNotFoundException e)
try
int num=Integer.parseInt("akki");
System.out.println(num);
catch(NumberFormatException e)
}
11. Output:-
13. Write a program for multilevel inheritance using class
class A
class B extends A
class C extends B
{
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
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
return a+b;
return a+b+c;
class Testoverloading_mansi
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
try
a[5]=30/0;
System.out.println(a[10]);
catch(ArithmeticException e)
catch(ArrayIndexOutOfBoundsException e)
}
catch(Exception e)
finally
}
17. Write a program for create a Thread from Thread class and
Runnable interface.
class A implements Runnable
for(int i=1;i<=10;i++)
System.out.println("\t threadA"+i);
System.out.println("end of thread");
class Runnabletest_mansi
A runnable=new A();
threadA.start();
}
18. Write a programfor multiple Thread class and use setPriority
method in it.
class A extends Thread
System.out.println("threadA started");
for(int i=1;i<=4;i++)
System.out.println("threadB started");
for(int j=1;j<=4;j++)
System.out.println("threadC started");
for(int k=1;k<=4;k++)
class Threadpriority_mansi
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();
}
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
}}
T1 t;
MyThread1(T1 t)
this.t=t;
t.printT1(9);
T1 t;
MyThread2(T1 t)
this.t=t;
t.printT1(100);
}
}
T1 obj=new T1();
t1.start();
t2.start();
}
19. Write a program
b. Using Synchronization method
class T
for(int i=1;i<=5;i++)
System.out.println(n*i);
try
{
Thread.sleep(400);
catch(Exception e)
System.out.println(e);
T t;
MyThread1(T t)
this.t=t;
t.printT(5);
}
class MyThread2 extends Thread
T t;
MyThread2(T t)
this.t=t;
t.printT(50);
T obj=new T();
t1.start();
t2.start();
}
Ouput:-
20. Write a program to explain Deadlock in multithreading.
class util
try
Thread.sleep(millis);
catch(InterruptedException e)
e.printStackTrace();
class Shared
System.out.println("test1-begin");
util.sleep(100);
s2.test2(this);
System.out.println("test1_end");
}
System.out.println("test2-begin");
util.sleep(100);
s1.test1(this);
System.out.println("test2_end");
this.s1=s1;
this.s2=s2;
@Override
{
s1.test1(s2);
this.s1=s1;
this.s2=s2;
@Override
s2.test2(s1);
t1.start();
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.*;
setBackground(Color.CYAN);
System.out.println("init()called");
System.out.println("start()called");
System.out.println("Paint()called");
}
public void stop()
System.out.println("stop()called");
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{
fos.write(65); //A
fos.close();
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
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
fw.write(104);
fw.write("ello world");
fw.flush();
fw.close();
catch(Exception e) { }
void show()
try
{
FileReader fr = new FileReader("file1.txt");
int i = fr.read();
while(i !=-1)
System.out.println((char) i);
i = fr.read();
catch(Exception e) { }
class rahul23
obj.input();
obj.show();
}
23.output:-
24. Write a program to
a. draw line in Frame using drawLine() method
import java.awt.*;
Frame frame;
setTitle("line");
setSize(300,350);
setVisible(true);
g.drawLine(60,90,150,200);
g.drawLine(110,120,250,270);
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.*;
g.setColor(Color.blue);
g.drawRect(50,80,150,100);
g.setColor(Color.magenta);
g.fillRect(230,80,150,100);
24B:output:=