Page 344 - Computer science 868 Class 12
P. 344
Ans. (c)
(iv) The statement or expression at ?4? is: [ISC 2022]
(a) t (b) M[i+1][k]
(c) r (d) c
Ans. (a)
(v) The statement or expression at ?5? is: [ISC 2022]
(a) M[i+1][k] (b) M[k][j+1]
(c) M[j][k] (d) M[j+1][k]
Ans. (d)
6. Design a class MatRev to reverse each element of a matrix. [ISC 2019]
Example:
72 371 5 27 173 5
12 6 426 becomes 21 6 624
5 123 94 5 321 49
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 parameterised 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 java.io.*;
import java.util.*;
class MatRev{
private int arr[][];
private int m;
private int n;
MatRev(int mm, int nn) {
m=mm;
n = nn;
arr=new int[m][n];
}
public void fillArray( )throws IOException{
Scanner sc = new Scanner(System.in);
System.out.println("Enter matrix elements::");
for(int i = 0; i < m; i++) {
for(int j = 0; j < n; j++) {
arr[i][j] = sc.nextInt();
}
}
}
public int reverse(int x) {
int rev = 0;
for(int i = x; i != 0; i /= 10)
rev = rev * 10 + i % 10;
return rev;
}
public void revMat(MatRev p) {
for(int i = 0; i < m; i++) {
342342 Touchpad Computer Science-XII

