Matrix addition,subtraction and Wrapper Classes.

In this article,Matrix addition,subtraction and Wrapper Classes are explained in detail with examples.
To perform these operations, the necessary and sufficient condition are both the matrices should be of same order because the operation is performed on corresponding elements from both the matrices.
Ex
//program related to Matrix addition,subtraction and Wrapper Classes-add,subtraction of a matrix.
class MatrixAddSub
{
public static void main(String args[])
{
int a[][]=new int[2][2];
int b[][]=new int[2][2];
int c[][]=new int[2][2];
int i,j;
System.out.println("Elements of A matrix: ");
a[0][0]=1;
a[0][1]=2;
a[1][0]=3;
a[1][1]=4;
System.out.println(a[0][0]+"\t"+a[0][1]);
System.out.println(a[1][0]+"\t"+a[1][1]);
System.out.println("Elements of B matrix: ");
b[0][0]=1;
b[0][1]=2;
b[1][0]=3;
b[1][1]=4;
System.out.println(b[0][0]+"\t"+b[0][1]);
System.out.println(b[1][0]+"\t"+b[1][1]);
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
c[i][j]=a[i][j]+b[i][j];
}
}
System.out.println("Resultant Addition Matrix is: ");
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
System.out.println(c[i][j] +"\t");
}
System.out.println();
}
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
c[i][j]=a[i][j]-b[i][j];
}
}
System.out.println("Resultant Subtraction Matrix is: ");
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
System.out.println(c[i][j] +"\t");
}
System.out.println();
}
}
}
Output
MatrixAddSub-output-Javaform
                            To get the code file use me.


Wrapper Classes.
This concept is a small reason to make java pure object-oriented programming language. For every primitive datatype in java, there is a class provided by java. Such class is known as "Wrapper class".
(From topic Matrix addition,subtraction and Wrapper Classes)
Wrapper Classes in Java-Javaform
Note
These classes objects are used to store one single immutable value of its corresponding primitive data-type value.
Immutable value means, the value which cannot be modified.
Ex
int i;
i=10;
Integer ii=new Integer(i);
i=10 int value as integer objects.
Here, integer object is string/wrapping /storing/packing a int value.
Note
This concept of wrapping primitive data type value in its corresponding wrapper class object is known as "Boxing".
Now,to access a value from wrapper class object we have predefined methods in its corresponding classes. (From topic Matrix addition,subtraction and Wrapper Classes)
byteValue()
shortValue()
intValue()
longValue()
floatValue()
doubleValue()
booleanValue()
charValue()
These methods are inherited to Byte,Short,Integer,Long,Float,Double,classes from "Number class".
Hence,"Number class" is super/base class for above 6 classes.
Number Class-Javaform
Wrapper class contains some other methods, which are popularly used in data conversion. They are,
valueOf(); //it converts primitive data type value in to its corresponding object.
parsexxx(); //String value to numeric value.This method converts string represented numeric value into its corresponding numeric value.   (From topic Matrix addition,subtraction and Wrapper Classes)
XXX--Stands for numeric datatype.
Ex
int x;
x=Integer.parseInt("2");
Ex
"2016"+10=>201610 //given statement is wrong as we are adding string and int.So convert string to int.
Integer.parseInt("2016")+10=>2026.
Unboxing
It is the process of accessing wrapper class object value into its corresponding primitive data type value. (From topic Matrix addition,subtraction and Wrapper Classes)
int i=10;
Integer ii=new Integer(i);--->boxing.
int x;
x=ii.int Value();--->un-boxing.
It is also known as un-wrapping/un-packing.
Note
From JDK 1.5,the above concept is done simple and easy. It is known as "Autoboxing" and "AutoUnboxing"
Ex
Integer ii=10; //no need to create object Autoboxing.
int x;
x=ii+20; //no need to use int value() method AutoUnboxing.

Ex
//program related to Matrix addition,subtraction and Wrapper Classes--wrapper classes.
class WrapperClass
{
public static void main(String args[])
{
 //wrapper class object var.declarations.
Boolean bb;
Character cc;
Byte by;
Short ss;
Integer ii;
Long ll;
Float ff;
Double dd;
//boxing using new operator.
bb=new Boolean(true);
cc=new Character('A');
byte b=10;
by=new Byte(b);
short s=100;
ss=new Short(s);
ii=new Integer(1000);
ll=new Long(10000);
ff=new Float(10.56f);
dd=new Double(20.56);
//un-boxing using methods
System.out.println(bb.booleanValue());
System.out.println(cc.charValue());
System.out.println(by.byteValue());
System.out.println(ss.shortValue());
System.out.println(ii.intValue());
System.out.println(ll.longValue());
System.out.println(ff.floatValue());
System.out.println(dd.doubleValue());
//autoboxing(directly storing values)
bb=false;
cc='z';
by=20;
ss=200;
ii=2000;
ll=20000l;
ff=100.56f;
dd=200.56;
//autoUn-boxing(directly storing values)
System.out.println(bb);
System.out.println(cc);
System.out.println(by);
System.out.println(ss);
System.out.println(ii);
System.out.println(ll);
System.out.println(ff);
System.out.println(dd);
}
}
Output
WrapperClass-Output-Javaform
                          To get the code file use me.

Continue to the next topic Object class and constructors in java.

Begin your career in Digital Marketing,What is digital marketing? Digital Marketing online course. It's an current evolving technology which can give support to establish your own startup through Digital Marketing.

Do check my new startup Surprise Planners in HyderabadLavish Surprises our services are surprise party planners in Hyderabadsurprise giftssurprise birthday party planners Hyderabad, Wedding anniversary surprises, surprise planners Hyderabad.

Hi Friends, Please comment down your views in the comment section below. Thank you...

No comments:

Connect with Us by Subscribing here for more Updates..!

* indicates required