Categories
Java

Matrix Addition Using Array

The following java program is to add two matrices using array.It is also an example of two dimensional array.

import java.io.*;
class MatAdd
{
int m[][],r,c,i,j;
BufferedReader br;
void get(int p,int q)throws Exception
{
br=new BufferedReader(new InputStreamReader(System.in));
r=p;
c=q;
m=new int[r][c];
for(i=0;i<r;i++)
for(j=0;j<c;j++)
{
System.out.println("Enter element");
m[i][j]=Integer.parseInt(br.readLine());
}
}
void pro(MatAdd a,MatAdd b)
{
r=a.r;
c=a.c;
m=new int[r][c];
for(i=0;i<r;i++)
for(j=0;j<c;j++)
{
m[i][j]=a.m[i][j]+b.m[i][j];
}
}
void show()
{
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
System.out.print("\t"+m[i][j]);
System.out.println("");
}
}
}
class MatAddMain
{
public static void main(String arg[])throws Exception
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter r1,c1,r2,c2");
int r1=Integer.parseInt(br.readLine());
int c1=Integer.parseInt(br.readLine());
int r2=Integer.parseInt(br.readLine());
int c2=Integer.parseInt(br.readLine());
if(r1==r2 && c1==c2)
{
MatAdd a=new MatAdd();
MatAdd b=new MatAdd();
MatAdd c= new MatAdd();
a.get(r1,c1);
b.get(r2,c2);
c.pro(a,b);
c.show();
}else
System.out.println("Matrix addition not possible");
}
}
Categories
Java

Matrix Generation Using Array

The following java program generates matrix using two dimensional array.

import java.io.*;
class Matrix
{
int r,c;
int a[][];
BufferedReader br;
void get()throws Exception
{
System.out.println("Enter number of rows and columns");
br=new BufferedReader(new InputStreamReader(System.in));
r=Integer.parseInt(br.readLine());
c=Integer.parseInt(br.readLine());
a=new int[r][c];
}
void pro()throws Exception
{
for(int i=0;i<r;i++)
for(int j=0;j<c;j++)
{
System.out.println("Enter element");
a[i][j]=Integer.parseInt(br.readLine());
}
}
void show()
{
for(int i=0;i<r;i++)
{
for(int j=0;j<c;j++)
System.out.print("\t"+a[i][j]);
System.out.println(" ");
}
}
}
class MatrixMain
{
public static void main(String arg[])throws Exception
{
Matrix obj=new Matrix();
obj.get();
obj.pro();
obj.show();
}
}
Categories
Java

Search a number from an array in java

The following java program to search a number from a collection of numbers in an array.

import java.io.*;
class Search
{
int sno,n;
int a[];
BufferedReader br;
void get()throws Exception
{
System.out.println("Enter n");
br=new BufferedReader(new InputStreamReader(System.in));
n=Integer.parseInt(br.readLine());
a=new int[n];
for(int i=0;i<n;i++)
{
System.out.println("Enter "+(i+1)+" Element");
a[i]=Integer.parseInt(br.readLine());

}
}
void pro()throws Exception
{
System.out.println("Enter the numbers to be searched");
sno=Integer.parseInt(br.readLine());
int f=0;
for(int i=0;i<n;i++)
{
if(a[i]==sno)
{
f=1;
break;
}
}
if(f==1)
System.out.println(sno+" is found");
else
System.out.println(sno+" is not found");
}
}
class SearchMain
{
public static void main(String arg[])throws Exception
{
Search obj=new Search();
obj.get();
obj.pro();
}
}

Categories
Java

Find the biggest of ‘n’ numbers using array in java

The following java program finds the biggest number in a collection of array.

import java.io.*;
class BiggestOfArray
{
int n;
BufferedReader br;
int a[];
void get()throws Exception
{
System.out.println("Enter n");
br=new BufferedReader(new InputStreamReader(System.in));
n=Integer.parseInt(br.readLine());
a=new int[n];

}
void pro()throws Exception
{
for(int i=0;i<n;i++)
{
System.out.println("Enter"+(i+1)+"element");
a[i]=Integer.parseInt(br.readLine());
}
int big=a[0];
for(int i=0;i<n;i++)
{
if(a[i]>big)
big=a[i];
}
System.out.println("Biggest of all number is"+big);
}
}
class BiggestOfArrayMain
{
public static void main(String arg[])throws Exception
{
BiggestOfArray obj=new BiggestOfArray();
obj.get();
obj.pro();
}
}
Categories
Java

Sorting In Descending Order Using Array In Java

The following java program arranges a collection of numbers in descending order using array.

import java.io.*;
class Descending
{
int n,c;
int a[];
BufferedReader br;
void get()throws Exception
{
System.out.println("Enter n");
br=new BufferedReader(new InputStreamReader(System.in));
n=Integer.parseInt(br.readLine());
a=new int[n];
for(int i=0;i<n;i++)
{
System.out.println("Enter"+(i+1)+"element");
a[i]=Integer.parseInt(br.readLine());
}
}
void pro()throws Exception
{
int j;
for(int i=0;i<n-1;i++)
for(j=i+1;j<n;j++)
{
if(a[i]<a[j])
{
c=a[i];
a[i]=a[j];
a[j]=c;
}
}
}
void show()
{
for(int i=0;i<n;i++)
System.out.print(a[i]+"\t");
}
}
class DescendingMain
{
public static void main(String arg[])throws Exception
{
Descending obj=new Descending();
obj.get();
obj.pro();
obj.show();
}
}
Categories
Java

Sorting In Ascending Order Using Array In Java

The following java program arranges a collection of numbers in ascending order using array.

import java.io.*;
class Ascending
{
int n;
int c;
BufferedReader br;
int a[];
void get()throws Exception
{
System.out.println("Enter n");
br=new BufferedReader(new InputStreamReader(System.in));
n=Integer.parseInt(br.readLine());
a=new int[n];
for(int i=0;i<n;i++)
{
System.out.println("Enter "+(i+1)+" Element");
a[i]=Integer.parseInt(br.readLine());
}
}
void pro()throws Exception
{
int j;
for(int i=0;i<n-1;i++)
for( j=i+1;j<n;j++)
{
if(a[i]>a[j])
{
c=a[i];
a[i]=a[j];
a[j]=c;
}
}
}
void show()
{
for(int i=0;i<n;i++)
System.out.print(a[i]+"\t");
}
}
class AscendingMain
{
public static void main(String arg[])throws Exception
{
Ascending obj=new Ascending();
obj.get();
obj.pro();
obj.show();
}
}
Categories
Java

Print ‘N’ Numbers in Reverse Order Using Array

The following java program gets ‘n’ numbers from user and prints the same in reverse order using array.

import java.io.*;
class PrintNReverse
{
int n;
int a[];
BufferedReader br;
void get()throws Exception
{
	System.out.println("Enter n");
	 br=new BufferedReader(new InputStreamReader(System.in));
	n=Integer.parseInt(br.readLine());
	a=new int[n];	
}
void pro()throws Exception
{
	for(int i=0;i<n;i++)
	{
		System.out.println("Enter"+(i+1)+"element");
		a[i]=Integer.parseInt(br.readLine());		
	}
}
void show()
{
	for(int i=n-1;i>=0;i--)
		System.out.println(a[i]);
	
}
}
class PrintNReverseMain
{
	public static void main(String arg[])throws Exception
	{
		PrintNReverse obj=new PrintNReverse();
		obj.get();
		obj.pro();
		obj.show();
	}
}
Categories
Java

Array Manipulation

Array is a finite collection of similar datatype arranged in a adjacent manner.

Array Syntax

Method – 1

datatype arrayname[]=new datatype[size];

 Method – 2

datatype arrayname[];
arrayname=new datatype[size];

Method – 3

datatype arrayname[]= {value1,vlaue2,…};

The following java program gets n numbers as inputs and prints the n numbers as output using array.

import java.io.*;
class ArrayExample
{
int n;
int a[];
BufferedReader br;
void get()throws Exception
{
System.out.println("Enter n");
br=new BufferedReader(new InputStreamReader(System.in));
n=Integer.parseInt(br.readLine());
a=new int[n];
}
void pro()throws Exception
{
for(int i=0;i<n;i++)
{
System.out.println("Enter "+(i+1)+" element");
a[i]=Integer.parseInt(br.readLine());
}
}
void show()
{
for(int i=0;i<n;i++)
System.out.println(a[i]);
}
}
class ArrayExampleMain
{
public static void main(String arg[])throws Exception
{
ArrayExample obj=new ArrayExample();
obj.get();
obj.pro();
obj.show();
}
}
Categories
Java

Static Properties and Methods

Static Properties

1.It will be created commonly only once for all objects.

2.It will be initialized only once when the first object is created , further initialization is not permitted.

3.It is visible within the class but lifetime is throughout the program.

Static Methods

1.It can access only static members.

2.It can be invoked using class name.

In the following java program,static properties and methods are used.

import java.io.*;
class StaticExample
{
int a,b;
static int c;
void get()throws Exception
{
System.out.println("Enter 3 numbers");
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
a=Integer.parseInt(br.readLine());
b=Integer.parseInt(br.readLine());
c=Integer.parseInt(br.readLine());
}
void show()
{
System.out.println(a+"\t"+b+"\t"+c);
}
static void disp()
{
System.out.println(c);
}
}
class StaticExampleMain
{
public static void main(String arg[])throws Exception
{
StaticExample s1=new StaticExample();
StaticExample s2=new StaticExample();
StaticExample s3=new StaticExample();
s1.get();
s2.get();
s3.get();
s1.show();
s2.show();
s3.show();
StaticExample.disp();
}
}
Categories
Java

Usage of ‘this’ keyword in java

It always holds the reference of the current object.It can be used in the following situations

1.this can be used to refer current class instance variable.

2.this can be used to invoke current class method (implicitly).

3.this() can be used to invoke current class constructor.

4.this can be passed as an argument in the method call.

5.this can be passed as an argument in the constructor call.

6.this can be used to return the current class instance from the method.

when the names of the formal arguments and names of the properties of current object are same,to refer the properties of the current object ‘this’ operator is used.
The following java program explains the above mentioned point.

class Exam
{
int a,b;
void set(int a,int b)
{
this.a=a;
this.b=b;
}
void show()
{
System.out.println(a+"\t"+b);
}
}
class ExamMain
{
public static void main(String arg[])throws Exception
{
Exam e=new Exam();
e.set(10,20);
e.show();
}
}