Design a class MatRev to reverse each element of a matrix. Some of the members of the class are given below:
Class name : MatRev
Data members/instance variables:
arr[ ][ ] : to store integer elements
m : to store the number of rows
n : to store the number of columns
Member functions/methods:
MatRev(int mm, int nn) :parameterised constructor to initialise the data members m = mm and n = nn
void fillarray( ) : to enter elements in the array
int reverse(int x) : returns the reverse of the number x
void revMat( MatRev P) : reverses each element of the array of the parameterized object and stores it in the array of the current object
void show( ) : displays the array elements in matrix form
Define the class MatRev giving details of the constructor( ), void fillarray( ), int reverse(int), void revMat(MatRev) and void show( ). Define the main( ) function to create objects and call the functions accordingly to enable the task.
import java.util.*;
public class MatRev
{
int arr[ ][ ],m,n;
static Scanner x=new Scanner(System.in);
MatRev(int nn,int mm)
{
m=mm;
n=nn;
arr=new int[m][n];
}
void fillarray()
{
for(int i=0;i<m;i++)
for(int j=0;j<n;j++)
arr[i][j]=x.nextInt();
}
int reverse(int x)
{
int s=0,c=0;
while(x!=0){
c=x%10;
s=s*10 + c;
x=x/10;
}
return s;
}
void revMat(MatRev P)
{
for(int i=0;i<m;i++)
for(int j=0;j<n;j++)
arr[i][j]=reverse(P.arr[i][j]);
}
void show()
{
for(int i=0;i<m;i++){
System.out.println();
for(int j=0;j<m;j++)
System.out.print(arr[i][j] + "\t");
}
}
static void main()
{
MatRev obj = new MatRev(3,4);
MatRev obj1=new MatRev(3,4);
obj.fillarray();
System.out.print("\n Original Array");
obj.show();
obj1.revMat(obj);
System.out.print("\n Reverse array");
obj1.show();
}
}