1
2D Add
Write a program to add two one dimensional array
class ArrayAdd
private int a[][] – member array
private int size – to store size
public ArrayAdd() // default constructor
public ArrayAdd(int) // parameterised constructor
public void fillarray() throws IOException // function to accept thearray
public void disparray() // function to display the array
public void sum(ArrayAdd x,ArrayAdd y ) // function to add two arrays by passing two objects
public ArrayAdd sum(ArrayAdd y ) – add two arrays using passing object and calling object
Answer
import java.io.*;
public class ArrayAdd
private int a[][]; // data member
private int size; // size to store size of the array
public ArrayAdd() // default constructor
public ArrayAdd(int x) // parameterised constructor to assign size
size = x;
a = new int[size][size]; ////instantiate object
}
2
public void fillarray() throws IOException // function to input 2D array
InputStreamReader ISR = new InputStreamReader(System.in);
BufferedReader br=new BufferedReader(ISR);
String S;
for(int i=0;i<size;i++)
for(int j=0;j<size;j++)
System.out.println("Enter number ");
S=br.readLine();
a[i][j]=Integer.parseInt(S);
public void disparray() // function to display the array
int i,j;
for(i=0; i<size; i++)
for(j=0;j<size;j++)
System.out.print(a[i][j]+"\t");
System.out.println();
public void sum(ArrayAdd x,ArrayAdd y ) // function to add
3
int i,j;
for(i=0;i<size;i++)
for(j=0;j<size;j++)
a[i][j]= x.a[i][j] + y.a[i][j]; // add the content of x and y to calling object
public ArrayAdd sum(ArrayAdd y ) // function to add to array
int i,j;
ArrayAdd z = new ArrayAdd(size); // new object is created
for(i=0;i<size;i++)
for(j=0;j<size;j++)
z.a[i][j]= a[i][j] + y.a[i][j]; // calling object and passing object is added to the created object
return z; // returns the created object
//CLASS II contains the main programe to make use of the class ArrayAdd
import java.io.*;
public class AddOut
public void main() throws IOException
InputStreamReader ISR = new InputStreamReader(System.in);
BufferedReader br=new BufferedReader(ISR);
System.out.println("Enter no: of rows and columns for array1");
int n=Integer.parseInt(br.readLine()); // store the no of elements
4
ArrayAdd obj1=new ArrayAdd(n); // creates obj1
System.out.println("Enter array 1");
obj1.fillarray(); // input contents of array
System.out.println("Enter no: of rows and columns for array2");
n=Integer.parseInt(br.readLine()); //input size of array 2
ArrayAdd obj2=new ArrayAdd(n);// create obj2
System.out.println("Enter array 2");
obj2.fillarray(); // input object array 2
ArrayAdd obj3=new ArrayAdd(n); // create the third array
// obj3=obj2.sum(obj1); // function call
obj3.sum(obj1,obj2); // function call
obj1.disparray(); // display array1
System.out.println( );
obj2.disparray(); // display array2
System.out.println();
obj3.disparray(); // display array3
OUTPUT
Enter no of rows and columns for array1 3
Enter array 1
Enter number
1
Enter number
2
Enter number
3
Enter number
4
5
Enter number
5
Enter number
6
Enter number
7
Enter number
8
Enter number
9
Enter no of elements for array2 3
Enter number
9
Enter number
8
Enter number
7
Enter number
6
Enter number
5
Enter number
4
Enter number
3
Enter number
2
Enter number
1
1 2 3
4 5 6
7 8 9
9 8 7
6 5 4
3 2 1
10 10 10
10 10 10
10 10 10
6
BinarySearch
Write a program to search for an element an array using Binary Searching technique
import java.io.*;
public class BinSearch
private int a[]; // data member
private int size; // size to store size of the array
public BinSearch() // default constructor
public BinSearch(int x) // parameterised constructor to assign size
size=x;
a=new int[size];
public void fillArray() throws IOException // function to input array
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter the "+size+" numbers");
for(int i=0;i<size;i++)
a[i]=Integer.parseInt(br.readLine());
public void dispArray() // function to display the array
int i;
for(i=0;i<size;i++)
7
System.out.print(a[i]+"\t");
System.out.println();
public void sort()//function to sort the array using bubble sort
int i,j,temp;
for(i=0;i<size-1;i++)
for(j=0;j<size-1;j++)
if(a[j]>a[j+1])
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
public void search(int n)//function to search an element using binary search
boolean flag=false;
int L=0,U=size-1,M=(L+U)/2;
while(M>0)
if(a[M]==n)
8
flag=true;
break;
else if(a[M]<n)
L=M+1;
else
U=M-1;
M=(L+U)/2;
if(flag==false)
System.out.println("Unsuccesful Search");
else
System.out.println(n+" is found in the position:"+(M+1));
Class II to use the above defined class
import java.io.*;
class BinaryOut
public void main()throws IOException
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter the size of the array");
int s=Integer.parseInt(br.readLine());
9
BinSearch obj=new BinSearch(s);
obj.fillArray();
obj.sort();
System.out.println("Given array in ascending order");
obj.dispArray();
System.out.println("Enter the number to be searched");
int n=Integer.parseInt(br.readLine());
obj.search(n);
OUTPUT
Enter the size of the array
Enter the 6 numbers
23
12
10
42
35
Given array in ascending order
5 10 12 23 35 42
Enter the number to be searched
35
35 is found in the position:5
10
LinearSearch
Write a program to search for an element an array using Linear Searching technique
import java.io.*;
public class LinSearch
private int a[]; // data member
private int size; // size to store size of the array
public LinSearch() // default constructor
public LinSearch(int x) // parameterised constructor to assign size
size=x;
a=new int[size];
public void fillArray() throws IOException // function to input array
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter the "+size+" numbers");
for(int i=0;i<size;i++)
a[i]=Integer.parseInt(br.readLine());
public void dispArray() // function to display the array
int i;
for(i=0;i<size;i++)
11
System.out.print(a[i]+"\t");
System.out.println();
public void search(int n)//function to search an element using linear search
boolean flag=false;
int i;
for(i=0;i<size;i++)
if(a[i]==n)
flag=true;
break;
if(flag==false)
System.out.println("Unsuccesful Search");
else
System.out.println(n+" is found in the position:"+(i+1));
Class II to use the above defined class
import java.io.*;
class LinearOut
{
12
public void main()throws IOException
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter the size of the array");
int s=Integer.parseInt(br.readLine());
LinSearch obj=new LinSearch(s);
obj.fillArray();
System.out.println("Given Array:");
obj.dispArray();
System.out.println("Enter the number to be searched");
int n=Integer.parseInt(br.readLine());
obj.search(n);
OUTPUT
Enter the size of the array
Enter the 5 numbers
12
42
32
15
62
Given Array:
12 42 32 15 62
Enter the number to be searched
13
62
62 is found in the position:5