[Go to site: main page, start]

0% found this document useful (0 votes)
9 views3 pages

Matrix Element Reversal Class Design

The document describes the implementation of a Java class named MatRev that is designed to reverse each element of a matrix. It includes methods for filling the matrix, reversing individual elements, and displaying the matrix. The main function demonstrates how to create objects of the class and utilize its methods to perform the desired operations.

Uploaded by

krishna007733
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views3 pages

Matrix Element Reversal Class Design

The document describes the implementation of a Java class named MatRev that is designed to reverse each element of a matrix. It includes methods for filling the matrix, reversing individual elements, and displaying the matrix. The main function demonstrates how to create objects of the class and utilize its methods to perform the desired operations.

Uploaded by

krishna007733
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Design a class MatRev to reverse each element of a matrix.

Example:

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.
Ans.

import [Link].*;
import [Link].*;
class MatRev{
int arr[][];
int m;
int n;
MatRev(int mm, int nn)
{
m=mm;
n = nn;
arr=new int[m][n];
}
void fillArray( )
{
Scanner sc = new Scanner([Link]);
[Link]("Enter matrix elements::");
for(int i = 0; i < m; i++) {
for(int j = 0; j < n; j++) {
arr[i][j] = [Link]();
}
}
}
int reverse(int x)
{
int rev = 0;
while(x!=0)
{
rev = rev * 10 + x % 10;
x=x/10;
}
return rev;
}
public void revMat(MatRev p)
{
for(int i = 0; i < m; i++)
{
for(int j = 0; j < n; j++)
{
[Link][i][j] = reverse([Link][i][j]);
} }
}
public void show() {
for(int i = 0; i < m; i++) {
for(int j = 0; j < n; j++) {
[Link]. print(arr[i][j] + "\t");
}
[Link]();
}
}
public static void main()
{
Scanner sc = new Scanner([Link]);
[Link]("Enter number of rows::");
int x = [Link]();
[Link]("Enter number of columns::");
int y = [Link]();
MatRev obj1 = new MatRev(x, y);
MatRev obj2 = new MatRev(x, y);
[Link]();
[Link](obj1);
[Link]("Original Matrix is::");
[Link]();
[Link]("Matrix with reversed elements");
[Link]();
} }

You might also like